正規分布#
// 依存関係のインストール
: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 ndarray = { version = "0.15.6" }
// インポート
use evcxr_image::ImageDisplay;
use image::{GenericImage, imageops::FilterType};
use plotters::prelude::*;
use ndarray::prelude::*;
// ライブラリ
:dep myml = { path = "../myml/" }
use myml::{utility::linspace, normal::normal};
use std::f64::consts::PI;
正規分布のプロット#
let x = linspace(-5.0, 5.0, 100);
let y = normal(&x, 0.0, 1.0);
let points = x.into_iter().zip(y.into_iter()).collect::<Vec<_>>();
evcxr_figure((600, 480), |root| {
root.fill(&WHITE)?;
let mut chart = ChartBuilder::on(&root)
.caption("normal", ("Sans", 20).into_font())
.x_label_area_size(40)
.y_label_area_size(40)
.build_cartesian_2d(-5.0..5.0, 0.0..0.45)?;
chart.configure_mesh()
.draw()?;
chart.draw_series(
LineSeries::new(
points.iter().cloned(),
&BLUE
)
)?;
Ok(())
})
パラメータの役割#
\(\mu\) を移動させてみる
let x = linspace(-10.0, 10.0, 200);
let y0 = normal(&x, -3.0, 1.0);
let y1 = normal(&x, 0.0, 1.0);
let y2 = normal(&x, 5.0, 1.0);
let points0 = x.iter().copied().zip(y0.iter().copied()).collect::<Vec<_>>();
let points1 = x.iter().copied().zip(y1.iter().copied()).collect::<Vec<_>>();
let points2 = x.iter().copied().zip(y2.iter().copied()).collect::<Vec<_>>();
evcxr_figure((600, 480), |root| {
root.fill(&WHITE)?;
let mut chart = ChartBuilder::on(&root)
.caption("normal", ("Sans", 20).into_font())
.x_label_area_size(40)
.y_label_area_size(40)
.build_cartesian_2d(-10.0..10.0, 0.0..0.45)?;
chart.configure_mesh()
.draw()?;
chart.draw_series(
LineSeries::new(
points0.iter().cloned(),
&BLUE
)
)?;
chart.draw_series(
LineSeries::new(
points1.iter().cloned(),
&RED
)
)?;
chart.draw_series(
LineSeries::new(
points2.iter().cloned(),
&GREEN
)
)?;
Ok(())
})
\(\sigma\) を移動させてみる
let x = linspace(-10.0, 10.0, 200);
let y0 = normal(&x, 0.0, 0.5);
let y1 = normal(&x, 0.0, 1.0);
let y2 = normal(&x, 0.0, 2.0);
let points0 = x.iter().copied().zip(y0.iter().copied()).collect::<Vec<_>>();
let points1 = x.iter().copied().zip(y1.iter().copied()).collect::<Vec<_>>();
let points2 = x.iter().copied().zip(y2.iter().copied()).collect::<Vec<_>>();
evcxr_figure((600, 480), |root| {
root.fill(&WHITE)?;
let mut chart = ChartBuilder::on(&root)
.caption("normal", ("Sans", 20).into_font())
.x_label_area_size(40)
.y_label_area_size(40)
.build_cartesian_2d(-10.0..10.0, 0.0..0.8)?;
chart.configure_mesh()
.draw()?;
chart.draw_series(
LineSeries::new(
points0.iter().cloned(),
&BLUE
)
)?;
chart.draw_series(
LineSeries::new(
points1.iter().cloned(),
&RED
)
)?;
chart.draw_series(
LineSeries::new(
points2.iter().cloned(),
&GREEN
)
)?;
Ok(())
})