#statistics: A set of statistical functions for rust
33 messages ยท Page 1 of 1 (latest)
```rs
pub fn mean(histogram: &[usize]) -> f64 {
let mut total = 0f64;
let mut mean = 0f64;
histogram.iter().enumerate().for_each(|(i, e)| {
// accumulate mean
mean += (i * e) as f64;
// accumulate total
total += *e as f64;
});
if total == 0.0 {
0.0
} else {
mean / total
}
}
clippy isnt smart enough here
youu could rewrite it as ```rs
let (mean, total) = histogram.iter().fold((0.0, 0.0), |(total, mean), &(i, e)| {
(mean + (i * e) as f64, total + e as f64)
});
taking &[usize] is also dubious
would be better to take I: Into<f64> + Mul<I>
seems iterators can solve lots of problems in a better way.
missing enumerate above
yes
I think I've learned
let (min, max, total) = values.iter().enumerate().fold(
(values.len() as f64, 0.0, 0.0),
|(min, max, total), (i, &e)| {
if e != 0 {
(
if (i as f64) < min { i as f64 } else { min },
if (i as f64) > max { i as f64 } else { max },
total + e as f64,
)
} else {
(min, max, total)
}
},
);
now I have lots of code to refactor ๐
im also not sure if you want it to return f64::INFINITY
when it gets an empty slice
though the alternative would be an Option<f64> which is pretty bad
well, depends on your needs
I will need do some integration tests and see how all of this modules will work together. Imฬ pretty sure I'll need to change some things. For example range.
Range can be index ranges and value ranges. Indexes are usize. Will need to use generics
but the main functionality is done
I had finnished now a Histogram module
is that not ```rs
i.min(min),
i.max(max),
no... because im dealing with the indexes of the array
the output tuple are indexes
let histogram = [0, 0, 1, 3, 6, 8, 11, 0, 0, 0];
let hist = Histogram::new(&histogram);
assert!(statistics::delta(2.00, hist.min()));
assert!(statistics::delta(6.00, hist.max()));
assert!(statistics::delta(29.00, hist.total()));
only total must be a f64, because it is a value.... min and max are indexes in histogram
I need analyse the bins of the accumulated probabilites
float indexes sound like a bad idea
yep, will need to review the api
look how I get a range around the mean by the given percentage:
let histogram = [0, 0, 1, 3, 6, 8, 11, 0, 0, 0];
let hist = Histogram::new(&histogram);
let range = hist.range(0.5);
assert_eq!(Range::new(4.000, 6.000), range);
again, Range must be indexes
but in another scenario, it will be normal value ranges
well, its done.... with this three modules, I can create a fuzzy toolset and even a simple neural network toolset
but first I will do some refactors in api to make it more stable.