I'm wanting to create my own graph like this one that criterion created, but based on my own list of numbers.
I'm using the plotters library ( which is amazing, thank you @381 ), but I don't know how to do the math to get the density value used for the y axis. Does anybody know of an easy crate or else just a code snippet that would give me that density value for a slice of f64s?
Not sure what you mean by "density" value? Do you mean the frequency of a value in a given set? Since these are f64s, you can have rounding errors, hence I cast to integers so that it additionally satisfies Eq:
use std::collections::HashMap;
const SCALE: f64 = 1.0;
fn main() {
let given_slice: &[f64] = &[1., 2., 3.];
let mut hashmap = HashMap::<i128, usize>::new();
given_slice
.iter()
.for_each(
|x| {
let rounded = (x * SCALE) as i128;
*hashmap
.entry(rounded)
.or_insert(0)
+= 1;
}
);
}
If you mean something more like calculating the values between the ticks on the Y-axis, then something like this would work:
const NUM_TICKS: f64 = 10.0;
fn main() {
let given_slice: &[f64] = &[1., 2., 3.];
let mut max = f64::MIN;
let mut min = f64::MAX;
given_slice
.iter()
.copied()
.for_each(|x| {
if x > max {
max = x;
}
if x < min {
min = x;
}
});
let size = max - min;
let distance_ticks = size / NUM_TICKS;
println!("{}", distance_ticks);
}
If I completely misinterpreted your question, please let me know!
I mean what it calls "Density" on the Y axis in that graph. It's also called PDF ( I can't remember what that stands for ). I just found this statrs::distribution::Normal struct that should calculate it for me I think ( assuming I give it the standard distribution which I also have to figure out how to calculate ). But there are a bunch of different distributions and I don't know which one to use so I just guessed Normal seemed pretty, "normal" .
I'm looking to render that shaded blue area which somehow represents the probability that a value will fall within that range. Does that make any sense? I have no background in statistics.
Unfortunately I'm not knowledgeable in statistics beyond basic number processing, so I can't help you choose a good distribution or an accurate one. Sorry for the let down.
That in turn uses some functionality from criterion::stats::univariate::kde. Ultimately this is performing kernel density estimation using a gaussian kernel, which is standard enough that you may be able to find a crate to do it for you. (Unfortunately the stats and plot modules are private so you can't just import from criterion.)