中心極限定理#
// 依存関係のインストール
:dep image = "0.23"
:dep evcxr_image = "1.1"
// プロット用ライブラリ
:dep plotters = { version = "^0.3.5", default_features = false, features = ["evcxr", "all_series", "all_elements"] }
// 乱数
:dep rand = { version = "0.8.5" }
// ライブラリ
:dep myml = { path = "../myml/" }
// インポート
use evcxr_image::ImageDisplay;
use image::{GenericImage, imageops::FilterType};
use plotters::prelude::*;
use rand::prelude::*;
use myml::{utility::linspace, normal::normal};
サンプリング#
/// ランダムな変数`n`個を`rep`回サンプリングする
fn sample_means(n: usize, rep: usize) -> Vec<f64> {
(0..rep)
.map(|_| {
// 平均
let mean = (0..n)
.map(|_| random::<f64>())
.sum::<f64>() / n as f64;
mean
})
.collect()
}
const REP: usize = 10000;
\(N = 1\) のとき
let means = sample_means(1, REP);
evcxr_figure((640, 480), |root| {
root.fill(&WHITE)?;
let mut chart = ChartBuilder::on(&root)
.caption("N = 1", ("Sans", 20).into_font())
.x_label_area_size(50)
.y_label_area_size(50)
.build_cartesian_2d(0..10, 0.0..40.0)?;
chart.configure_mesh()
.draw()?;
let hist = Histogram::vertical(&chart)
.style(BLUE.filled())
.margin(10)
.data(means.iter().map(|&x| ((x * 10.0) as i32, 0.01)));
chart.draw_series(hist)?;
Ok(())
})
\(N = 2\) のとき
let means = sample_means(2, REP);
evcxr_figure((640, 480), |root| {
root.fill(&WHITE)?;
let mut chart = ChartBuilder::on(&root)
.caption("N = 2", ("Sans", 20).into_font())
.x_label_area_size(50)
.y_label_area_size(50)
.build_cartesian_2d(0..10, 0.0..40.0)?;
chart.configure_mesh()
.draw()?;
let hist = Histogram::vertical(&chart)
.style(BLUE.filled())
.margin(10)
.data(means.iter().map(|&x| ((x * 10.0) as i32, 0.01)));
chart.draw_series(hist)?;
Ok(())
})
\(N = 4\) のとき
let means = sample_means(4, REP);
evcxr_figure((640, 480), |root| {
root.fill(&WHITE)?;
let mut chart = ChartBuilder::on(&root)
.caption("N = 4", ("Sans", 20).into_font())
.x_label_area_size(50)
.y_label_area_size(50)
.build_cartesian_2d(0..10, 0.0..40.0)?;
chart.configure_mesh()
.draw()?;
let hist = Histogram::vertical(&chart)
.style(BLUE.filled())
.margin(10)
.data(means.iter().map(|&x| ((x * 10.0) as i32, 0.01)));
chart.draw_series(hist)?;
Ok(())
})
\(N = 10\) のとき
let means = sample_means(10, REP);
evcxr_figure((640, 480), |root| {
root.fill(&WHITE)?;
let mut chart = ChartBuilder::on(&root)
.caption("N = 10", ("Sans", 20).into_font())
.x_label_area_size(50)
.y_label_area_size(50)
.build_cartesian_2d(0..20, 0.0..40.0)?;
chart.configure_mesh()
.draw()?;
let hist = Histogram::vertical(&chart)
.style(BLUE.filled())
.margin(5)
.data(means.iter().map(|&x| ((x * 20.0) as i32, 0.01)));
chart.draw_series(hist)?;
Ok(())
})