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