use crate::standard_distributions::standard_exponential;
use crate::{create_state, Frechet};
impl Frechet {
pub fn new(_seed: u32) -> Self {
Self {
xyzuv: create_state(_seed),
shape_inv: 1_f64,
scale: 1_f64,
}
}
pub fn sample(&mut self) -> f64 {
loop {
let z = standard_exponential(&mut self.xyzuv);
if z > 0_f64 {
return z.powf(-self.shape_inv) * self.scale;
}
}
}
pub fn try_set_params(&mut self, shape: f64, scale: f64) -> Result<(f64, f64), &str> {
if shape <= 0_f64 || scale <= 0_f64 {
Err("形状母数あるいは尺度母数が0以下です。確率変数のパラメータは前回の設定を維持します。")
} else {
self.shape_inv = shape.powi(-1_i32);
self.scale = scale;
Ok((shape, scale))
}
}
}
impl std::fmt::Display for Frechet {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
writeln!(f, "構造体の型: {}", std::any::type_name::<Self>())?;
writeln!(f, "形状母数: {}", self.shape_inv.powi(-1_i32))?;
writeln!(f, "尺度母数: {}", self.scale)?;
Ok(())
}
}