Rustの統計分布の関数を使う~statrs版とprobability版の競演~
Rustの統計分布の関数を使ってみる、続編。
すでに11月6日のtest02で、正規分布の累積密度の逆関数を使ってみていた。このときはライブラリとして"statrs"を用いた。
今回は、Beta分布の分布関数 (CDF) を、"statrs"に加えて"probability"ライブラリを用いて算出してみる。以下はそれぞれのライブラリのオフィシャルっぽいページ。
statrs
https://docs.rs/statrs/latest/statrs/
https://crates.io/crates/statrs
probabililty
https://docs.rs/probability/latest/probability/
https://crates.io/crates/probability
ではやってみる。
環境はWindows 10。
自分的プロジェクト名はtest04。
ソースなどはこちら (Github)
Rustソース
Rustのソースファイル中の、CDFを出すところは以下のような感じ。beta_cdf_statrs_crate()はstatrs版、beta_cdf_prob_crate()はprobability版。
main.rs
fn beta_cdf_statrs_crate( alpha: f64, beta: f64, z: f64) -> f64 { use statrs::distribution::Beta; use statrs::distribution::ContinuousCDF; let dist = Beta::new( alpha, beta).unwrap(); let p_hat = dist.cdf( z); return p_hat; } fn beta_cdf_prob_crate( alpha: f64, beta: f64, z: f64) -> f64 { use probability::distribution::Beta; use probability::distribution::Distribution; let dist = Beta::new( alpha, beta, 0.0, 1.0); let p_hat = dist.distribution( z); return p_hat; }
Cargo.tomlファイル
Cargo.tomlの[dependencies]の部分は次のような感じにする。
Cargo.toml
[dependencies] statrs = "0.16" probability = "0.20.1"
結果
実行結果として出力されたものの一部は次のような感じ。
Test for CDF() of Beta(alpha,beta) p_hat = CDF(z) for various z values Test two crates; "1" means "statrs" crate, "2" means "probability" crate alpha beta z p_hat_1 p_hat_2 0.5 0.5 0.01 0.06376856085851985 0.06376856085851985 0.5 0.5 0.02 0.09033447060173311 0.09033447060173312 0.5 0.5 0.03 0.11082468660445927 0.1108246866044594 0.5 0.5 0.04 0.1281884336979499 0.1281884336979499 0.5 0.5 0.05 0.1435662931287062 0.1435662931287063
整形した表で示すとこんな感じ。
alpha | beta | z | p_hat_1 | p_hat_2 |
---|---|---|---|---|
0.5 | 0.5 | 0.01 | 0.06376856085851985 | 0.06376856085851985 |
0.5 | 0.5 | 0.02 | 0.09033447060173311 | 0.09033447060173312 |
0.5 | 0.5 | 0.03 | 0.11082468660445927 | 0.1108246866044594 |
0.5 | 0.5 | 0.04 | 0.1281884336979499 | 0.1281884336979499 |
0.5 | 0.5 | 0.05 | 0.1435662931287062 | 0.1435662931287063 |
要するに、"statrs"と"probability"の結果はほとんど同じ。差は最大で2.9976E-15でした。
めでたしめでたし。。
とはいかなかったのです!!
その真相は次回にでも。。(焦らし)