Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/distribution/beta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,29 @@ impl ContinuousCDF<f64, f64> for Beta {
beta::inv_beta_reg(self.shape_a, self.shape_b, x)
}
}

/// Calculates the inverse cumulative distribution function for the beta
/// distribution at `x`.
///
/// # Returns an error instead of a panic
///
/// If x is not in `[0, 1]`.
///
/// # Formula
///
/// ```text
/// I^{-1}_x(α, β)
/// ```
///
/// where `α` is shapeA, `β` is shapeB, and `I_x` is the inverse of the
/// regularized lower incomplete beta function.
fn try_inverse_cdf(&self, x: f64) -> Result<f64, Box<dyn std::error::Error>> {
if !(0.0..=1.0).contains(&x) {
Err("x must be in [0, 1]".into())
} else {
Ok(beta::inv_beta_reg(self.shape_a, self.shape_b, x))
}
}
}

impl Min<f64> for Beta {
Expand Down
2 changes: 1 addition & 1 deletion src/distribution/inverse_gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl Continuous<f64, f64> for InverseGamma {
mod tests {
use super::*;
use crate::distribution::internal::density_util;
use crate::distribution::internal::testing_boiler;


testing_boiler!(shape: f64, rate: f64; InverseGamma; InverseGammaError);

Expand Down
2 changes: 0 additions & 2 deletions src/distribution/laplace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,6 @@ mod tests {
use super::*;
use crate::prec;

use crate::distribution::internal::testing_boiler;

testing_boiler!(location: f64, scale: f64; Laplace; LaplaceError);

// A wrapper for the `assert_relative_eq!` macro from the approx crate.
Expand Down
1 change: 0 additions & 1 deletion src/distribution/log_normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ impl Continuous<f64, f64> for LogNormal {
mod tests {
use super::*;
use crate::distribution::internal::density_util;
use crate::distribution::internal::testing_boiler;

testing_boiler!(location: f64, scale: f64; LogNormal; LogNormalError);

Expand Down
12 changes: 12 additions & 0 deletions src/distribution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ pub trait ContinuousCDF<K: Float, T: Float>: Min<K> + Max<K> {
}
(high + low) / two
}

/// Due to issues with rounding and floating-point accuracy the default
/// implementation may be ill-behaved.
/// Specialized inverse cdfs should be used whenever possible.
/// Performs a binary search on the domain of `cdf` to obtain an approximation
/// of `F^-1(p) := inf { x | F(x) >= p }`. Needless to say, performance may
/// may be lacking.
#[doc(alias = "quantile function")]
#[doc(alias = "quantile")]
fn try_inverse_cdf(&self, p: T) -> Result<K, Box<dyn std::error::Error>> {
Ok(self.inverse_cdf(p))
}
}

/// The `DiscreteCDF` trait is used to specify an interface for univariate
Expand Down
3 changes: 2 additions & 1 deletion src/distribution/multivariate_normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,12 @@ where
///
/// # Panics
/// If both the `cov` and `cholesky` arguments are `None`; at least one must be `Some(_)`.
type MeanCovarainceCholeskyReturn<D> = (OVector<f64, D>, OMatrix<f64, D, D>, Cholesky<f64, D>);
fn normalize_constructor_arguments<D>(
mean: OVector<f64, D>,
covariance: Option<OMatrix<f64, D, D>>,
cholesky: Option<Cholesky<f64, D>>,
) -> Result<(OVector<f64, D>, OMatrix<f64, D, D>, Cholesky<f64, D>), MultivariateNormalError>
) -> Result<MeanCovarainceCholeskyReturn<D>, MultivariateNormalError>
where
D: DimMin<D, Output = D>,
nalgebra::DefaultAllocator: nalgebra::allocator::Allocator<D>
Expand Down
1 change: 0 additions & 1 deletion src/distribution/negative_binomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ impl Discrete<u64, f64> for NegativeBinomial {
mod tests {
use super::*;
use crate::distribution::internal::density_util;
use crate::distribution::internal::testing_boiler;

testing_boiler!(r: f64, p: f64; NegativeBinomial; NegativeBinomialError);

Expand Down
1 change: 0 additions & 1 deletion src/distribution/poisson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ pub fn sample_unchecked<R: ::rand::Rng + ?Sized>(rng: &mut R, lambda: f64) -> f6
mod tests {
use super::*;
use crate::distribution::internal::density_util;
use crate::distribution::internal::testing_boiler;
testing_boiler!(lambda: f64; Poisson; PoissonError);

#[test]
Expand Down
1 change: 0 additions & 1 deletion src/distribution/triangular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,6 @@ fn sample_unchecked<R: ::rand::Rng + ?Sized>(rng: &mut R, min: f64, max: f64, mo
mod tests {
use super::*;
use crate::distribution::internal::density_util;
use crate::distribution::internal::testing_boiler;

testing_boiler!(min: f64, max: f64, mode: f64; Triangular; TriangularError);

Expand Down
1 change: 0 additions & 1 deletion src/distribution/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ mod tests {
use super::*;
use crate::prec;
use crate::distribution::internal::density_util;
use crate::distribution::internal::testing_boiler;

testing_boiler!(min: f64, max: f64; Uniform; UniformError);

Expand Down
1 change: 0 additions & 1 deletion src/distribution/weibull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ impl Continuous<f64, f64> for Weibull {
mod tests {
use super::*;
use crate::distribution::internal::density_util;
use crate::distribution::internal::testing_boiler;

testing_boiler!(shape: f64, scale: f64; Weibull; WeibullError);

Expand Down
5 changes: 1 addition & 4 deletions src/function/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
///
/// Returns 0 for a 0 length coefficient slice
pub fn polynomial(z: f64, coeff: &[f64]) -> f64 {
coeff
.into_iter()
.rev()
.fold(0_f64, |acc, val| acc * z + val)
coeff.iter().rev().fold(0_f64, |acc, val| acc * z + val)
}

#[rustfmt::skip]
Expand Down
Loading