Distributions
Strafe provides a number of statistical distributions from R
's base, and these distributions can be used to perform comparisons, calculations, and tests. Internally strafe uses these distributions for virtually all its calculations. Each distribution accepts numerics for its arguments (f64
, i32
, usize
, etc.) and checks that these arguments are valid via the Constraint
types. For example, the NormalBuilder
accepts a mean that is Real
(any number that is not a NAN
), and a standard deviation that is Positive
(any number that is greater than 0 and not NAN
). You can find these constraint types in the strafe-type
crate.
Below is an example of using the normal distribution to find the upper and lower 75th quantile, and 11 densities between -5 and 5.
use strafe::distribution::{Distribution, NormalBuilder}; use strafe::types::FloatConstraint; fn main() { let norm = NormalBuilder::new() .with_mean(0) .with_standard_deviation(1) .build(); let lower = norm.quantile(0.95, false); let upper = norm.quantile(0.95, true); println!("{lower} {upper}"); let densities = (-5..=5) .map(|x| norm.density(x as f64 / 5.0).unwrap()) .collect::<Vec<_>>(); println!("{densities:#.7?}"); }
-1.6448536269514715 1.6448536269514715
[
0.2419707,
0.2896916,
0.3332246,
0.3682701,
0.3910427,
0.3989423,
0.3910427,
0.3682701,
0.3332246,
0.2896916,
0.2419707,
]