Statistics/Normal distribution: Difference between revisions

Content added Content deleted
(Added name of procedures from module "random".)
(Rust Implementation)
Line 2,808: Line 2,808:
=
=


</pre>
=={{header|Rust}}==
{{libheader|math}}
{{libheader|rand}}
{{libheader|rand_distr}}
<lang rust>//! Rust rosetta example for normal distribution
use math::{histogram::Histogram, traits::ToIterator};
use rand;
use rand_distr::{Distribution, Normal};

/// Returns the mean of the provided samples
///
/// ## Arguments
/// * data -- reference to float32 array
fn mean(data: &[f32]) -> Option<f32> {
let sum: f32 = data.iter().sum();
Some(sum / data.len() as f32)
}

/// Returns standard deviation of the provided samples
///
/// ## Arguments
/// * data -- reference to float32 array
fn standard_deviation(data: &[f32]) -> Option<f32> {
let mean = mean(data).expect("invalid mean");
let sum = data.iter().fold(0.0, |acc, &x| acc + (x - mean).powi(2));
Some((sum / data.len() as f32).sqrt())
}

/// Prints a histogram in the shell
///
/// ## Arguments
/// * data -- reference to float32 array
/// * maxwidth -- the maxwidth of the histogram in # of characters
/// * bincount -- number of bins in the histogram
/// * ch -- character used to plot the graph
fn print_histogram(data: &[f32], maxwidth: usize, bincount: usize, ch: char) {
let min_val = data.iter().cloned().fold(f32::NAN, f32::min);
let max_val = data.iter().cloned().fold(f32::NAN, f32::max);
let histogram = Histogram::new(Some(&data.to_vec()), bincount, min_val, max_val).unwrap();
let max_bin_value = histogram.get_counters().iter().max().unwrap();
println!();
for x in histogram.to_iter() {
let (bin_min, bin_max, freq) = x;
let bar_width = (((freq as f64) / (*max_bin_value as f64)) * (maxwidth as f64)) as u32;
let bar_as_string = (1..bar_width).fold(String::new(), |b, _| b + &ch.to_string());
println!(
"({:>6},{:>6}) |{} {:.2}%",
format!("{:.2}", bin_min),
format!("{:.2}", bin_max),
bar_as_string,
(freq as f64) * 100.0 / (data.len() as f64)
);
}
println!();
}

/// Runs the demo to generate normal distribution of three different sample sizes
fn main() {
let expected_mean: f32 = 0.0;
let expected_std_deviation: f32 = 4.0;
let normal = Normal::new(expected_mean, expected_std_deviation).unwrap();

let mut rng = rand::thread_rng();
for &number_of_samples in &[1000, 10_000, 1_000_000] {
let data: Vec<f32> = normal
.sample_iter(&mut rng)
.take(number_of_samples)
.collect();
println!("Statistics for sample size {}:", number_of_samples);
println!("\tMean: {:?}", mean(&data).expect("invalid mean"));
println!(
"\tStandard deviation: {:?}",
standard_deviation(&data).expect("invalid standard deviation")
);
print_histogram(&data, 80, 40, '-');
}
}</lang>
{{out}}
<pre style="height: 120ex; overflow: scroll">
Statistics for sample size 1000:
Mean: -0.11356559
Standard deviation: 4.0244555

(-13.81,-13.12) | 0.10%
(-13.12,-12.44) | 0.00%
(-12.44,-11.75) | 0.10%
(-11.75,-11.06) | 0.20%
(-11.06,-10.38) |- 0.30%
(-10.38, -9.69) | 0.10%
( -9.69, -9.01) |--- 0.50%
( -9.01, -8.32) |---- 0.60%
( -8.32, -7.64) |------ 0.80%
( -7.64, -6.95) |-------------- 1.60%
( -6.95, -6.27) |----------------- 1.90%
( -6.27, -5.58) |------------------------ 2.60%
( -5.58, -4.90) |----------------------- 2.50%
( -4.90, -4.21) |---------------------------------------- 4.20%
( -4.21, -3.53) |------------------------------------- 3.90%
( -3.53, -2.84) |------------------------------------------------- 5.10%
( -2.84, -2.15) |---------------------------------------------- 4.80%
( -2.15, -1.47) |------------------------------------------------------------------------ 7.40%
( -1.47, -0.78) |---------------------------------------------------------- 6.00%
( -0.78, -0.10) |----------------------------------------------------------------------- 7.30%
( -0.10, 0.59) |------------------------------------------------------------------------------- 8.10%
( 0.59, 1.27) |----------------------------------------------------------------------- 7.30%
( 1.27, 1.96) |------------------------------------------------- 5.10%
( 1.96, 2.64) |------------------------------------------------------------ 6.20%
( 2.64, 3.33) |----------------------------------------- 4.30%
( 3.33, 4.01) |----------------------------- 3.10%
( 4.01, 4.70) |------------------------------------- 3.90%
( 4.70, 5.39) |-------------------------- 2.80%
( 5.39, 6.07) |---------------------- 2.40%
( 6.07, 6.76) |---------------- 1.80%
( 6.76, 7.44) |---------------- 1.80%
( 7.44, 8.13) |--------- 1.10%
( 8.13, 8.81) |---------- 1.20%
( 8.81, 9.50) | 0.20%
( 9.50, 10.18) | 0.00%
( 10.18, 10.87) | 0.10%
( 10.87, 11.55) |- 0.30%
( 11.55, 12.24) | 0.10%
( 12.24, 12.92) | 0.10%
( 12.92, 13.61) | 0.10%

Statistics for sample size 10000:
Mean: 0.02012564
Standard deviation: 3.9697735

(-15.80,-14.99) | 0.02%
(-14.99,-14.18) | 0.04%
(-14.18,-13.37) | 0.04%
(-13.37,-12.56) | 0.04%
(-12.56,-11.75) | 0.09%
(-11.75,-10.94) | 0.08%
(-10.94,-10.13) |- 0.25%
(-10.13, -9.32) |--- 0.42%
( -9.32, -8.51) |----- 0.67%
( -8.51, -7.70) |--------- 1.10%
( -7.70, -6.89) |------------- 1.48%
( -6.89, -6.08) |------------------ 1.98%
( -6.08, -5.27) |-------------------------- 2.82%
( -5.27, -4.45) |------------------------------------ 3.80%
( -4.45, -3.64) |--------------------------------------------- 4.66%
( -3.64, -2.83) |------------------------------------------------------- 5.72%
( -2.83, -2.02) |------------------------------------------------------------------ 6.85%
( -2.02, -1.21) |---------------------------------------------------------------------------- 7.80%
( -1.21, -0.40) |---------------------------------------------------------------------------- 7.79%
( -0.40, 0.41) |------------------------------------------------------------------------------- 8.09%
( 0.41, 1.22) |----------------------------------------------------------------------------- 7.89%
( 1.22, 2.03) |--------------------------------------------------------------------------- 7.76%
( 2.03, 2.84) |-------------------------------------------------------------------- 7.06%
( 2.84, 3.65) |------------------------------------------------------- 5.74%
( 3.65, 4.46) |-------------------------------------------- 4.64%
( 4.46, 5.27) |-------------------------------------- 4.00%
( 5.27, 6.08) |---------------------------- 3.03%
( 6.08, 6.89) |------------------- 2.07%
( 6.89, 7.71) |-------------- 1.54%
( 7.71, 8.52) |-------- 0.97%
( 8.52, 9.33) |----- 0.61%
( 9.33, 10.14) |-- 0.36%
( 10.14, 10.95) |- 0.25%
( 10.95, 11.76) | 0.19%
( 11.76, 12.57) | 0.08%
( 12.57, 13.38) | 0.02%
( 13.38, 14.19) | 0.01%
( 14.19, 15.00) | 0.03%
( 15.00, 15.81) | 0.00%
( 15.81, 16.62) | 0.01%

Statistics for sample size 1000000:
Mean: -0.004743685
Standard deviation: 4.0006065

(-20.00,-19.02) | 0.00%
(-19.02,-18.04) | 0.00%
(-18.04,-17.06) | 0.00%
(-17.06,-16.07) | 0.00%
(-16.07,-15.09) | 0.00%
(-15.09,-14.11) | 0.01%
(-14.11,-13.13) | 0.03%
(-13.13,-12.15) | 0.06%
(-12.15,-11.16) | 0.14%
(-11.16,-10.18) |- 0.28%
(-10.18, -9.20) |--- 0.53%
( -9.20, -8.22) |------ 0.95%
( -8.22, -7.24) |----------- 1.51%
( -7.24, -6.25) |------------------ 2.40%
( -6.25, -5.27) |--------------------------- 3.48%
( -5.27, -4.29) |-------------------------------------- 4.82%
( -4.29, -3.31) |-------------------------------------------------- 6.27%
( -3.31, -2.32) |------------------------------------------------------------- 7.62%
( -2.32, -1.34) |----------------------------------------------------------------------- 8.77%
( -1.34, -0.36) |----------------------------------------------------------------------------- 9.58%
( -0.36, 0.62) |------------------------------------------------------------------------------- 9.74%
( 0.62, 1.60) |---------------------------------------------------------------------------- 9.39%
( 1.60, 2.59) |-------------------------------------------------------------------- 8.49%
( 2.59, 3.57) |---------------------------------------------------------- 7.30%
( 3.57, 4.55) |----------------------------------------------- 5.86%
( 4.55, 5.53) |----------------------------------- 4.45%
( 5.53, 6.51) |------------------------ 3.16%
( 6.51, 7.50) |---------------- 2.09%
( 7.50, 8.48) |--------- 1.34%
( 8.48, 9.46) |----- 0.81%
( 9.46, 10.44) |-- 0.46%
( 10.44, 11.42) | 0.23%
( 11.42, 12.41) | 0.11%
( 12.41, 13.39) | 0.06%
( 13.39, 14.37) | 0.02%
( 14.37, 15.35) | 0.01%
( 15.35, 16.34) | 0.00%
( 16.34, 17.32) | 0.00%
( 17.32, 18.30) | 0.00%
( 18.30, 19.28) | 0.00%
</pre>
</pre>