mod d;
mod p;
mod q;
mod r;
use strafe_type::{LogProbability64, Positive64, Probability64, Real64};
pub(crate) use self::{d::*, p::*, q::*, r::*};
use crate::traits::{Distribution, RNG};
#[cfg_attr(feature = "doc_outputs", cfg_attr(all(), doc = embed_doc_image::embed_image!("density", "src/distribution/lnorm/doctest_out/density.svg")))]
#[cfg_attr(feature = "doc_outputs", cfg_attr(all(), doc = "![Density][density]"))]
#[cfg_attr(feature = "doc_outputs", cfg_attr(all(), doc = include_str!("doctest_out/dens.md")))]
pub struct LogNormal {
mean_log: Real64,
standard_deviation_log: Positive64,
}
impl Distribution for LogNormal {
fn density<R: Into<Real64>>(&self, x: R) -> Real64 {
dlnorm(x, self.mean_log, self.standard_deviation_log, false)
}
fn log_density<R: Into<Real64>>(&self, x: R) -> Real64 {
dlnorm(x, self.mean_log, self.standard_deviation_log, true)
}
fn probability<R: Into<Real64>>(&self, q: R, lower_tail: bool) -> Probability64 {
plnorm(q, self.mean_log, self.standard_deviation_log, lower_tail)
}
fn log_probability<R: Into<Real64>>(&self, q: R, lower_tail: bool) -> LogProbability64 {
log_plnorm(q, self.mean_log, self.standard_deviation_log, lower_tail)
}
fn quantile<P: Into<Probability64>>(&self, p: P, lower_tail: bool) -> Real64 {
qlnorm(p, self.mean_log, self.standard_deviation_log, lower_tail)
}
fn log_quantile<LP: Into<LogProbability64>>(&self, p: LP, lower_tail: bool) -> Real64 {
log_qlnorm(p, self.mean_log, self.standard_deviation_log, lower_tail)
}
fn random_sample<R: RNG>(&self, rng: &mut R) -> Real64 {
rlnorm(self.mean_log, self.standard_deviation_log, rng)
}
}
pub struct LogNormalBuilder {
mean_log: Option<Real64>,
standard_deviation_log: Option<Positive64>,
}
impl LogNormalBuilder {
pub fn new() -> Self {
Self {
mean_log: None,
standard_deviation_log: None,
}
}
pub fn with_mean_log<R: Into<Real64>>(&mut self, mean_log: R) -> &mut Self {
self.mean_log = Some(mean_log.into());
self
}
pub fn with_standard_deviation_log<P: Into<Positive64>>(
&mut self,
standard_deviation_log: P,
) -> &mut Self {
self.standard_deviation_log = Some(standard_deviation_log.into());
self
}
pub fn build(&self) -> LogNormal {
let mean_log = self.mean_log.unwrap_or(0.0.into());
let standard_deviation_log = self.standard_deviation_log.unwrap_or(1.0.into());
LogNormal {
mean_log,
standard_deviation_log,
}
}
}
#[cfg(test)]
mod tests;
#[cfg(all(test, feature = "enable_proptest"))]
mod proptests;
#[cfg(all(test, feature = "enable_covtest"))]
mod covtests;