Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some more inverse CDF functions #260

Merged
merged 7 commits into from
Aug 14, 2024
Merged
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
41 changes: 41 additions & 0 deletions src/distribution/beta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@
beta::beta_reg(self.shape_b, self.shape_a, 1.0 - x)
}
}

/// Calculates the inverse cumulative distribution function for the beta
/// distribution
/// at `x`
///
/// # Formula
///
/// ```text
/// I^{-1}_x(α, β)
/// ```
///
/// where `α` is shapeA, `β` is shapeB, and `I_x` is the inverse of the
/// regularized lower incomplete beta function
fn inverse_cdf(&self, x: f64) -> f64 {
if !(0.0..=1.0).contains(&x) {
panic!("x must be in [0, 1]");

Check warning on line 182 in src/distribution/beta.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/beta.rs#L182

Added line #L182 was not covered by tests
} else {
beta::inv_beta_reg(self.shape_a, self.shape_b, x)
}
}
}

impl Min<f64> for Beta {
Expand Down Expand Up @@ -657,6 +677,27 @@
}
}

#[test]
fn test_inverse_cdf() {
// let inverse_cdf = |arg: f64| move |x: Beta| x.inverse_cdf(arg);
let func = |arg: f64| move |x: Beta| x.inverse_cdf(x.cdf(arg));
let test = [
((1.0, 1.0), 0.0, 0.0),
((1.0, 1.0), 0.5, 0.5),
((1.0, 1.0), 1.0, 1.0),
((9.0, 1.0), 0.0, 0.0),
((9.0, 1.0), 0.001953125, 0.001953125),
((9.0, 1.0), 0.5, 0.5),
((9.0, 1.0), 1.0, 1.0),
((5.0, 100.0), 0.0, 0.0),
((5.0, 100.0), 0.01, 0.01),
((5.0, 100.0), 1.0, 1.0),
];
for ((a, b), x, expect) in test {
test_case(a, b, expect, func(x));
};
}

#[test]
fn test_cdf_input_lt_0() {
let cdf = |arg: f64| move |x: Beta| x.cdf(arg);
Expand Down
43 changes: 43 additions & 0 deletions src/distribution/cauchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@
fn sf(&self, x: f64) -> f64 {
(1.0 / f64::consts::PI) * ((self.location - x) / self.scale).atan() + 0.5
}

/// Calculates the inverse cumulative distribution function for the
/// cauchy distribution at `x`
///
/// # Formula
///
/// ```text
/// x_0 + γ tan((x - 0.5) π)
/// ```
///
/// where `x_0` is the location and `γ` is the scale
fn inverse_cdf(&self, x: f64) -> f64 {
if !(0.0..=1.0).contains(&x) {
panic!("x must be in [0, 1]");

Check warning on line 137 in src/distribution/cauchy.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/cauchy.rs#L137

Added line #L137 was not covered by tests
} else {
self.location + self.scale * (f64::consts::PI * (x - 0.5)).tan()
}
}
}

impl Min<f64> for Cauchy {
Expand Down Expand Up @@ -466,6 +484,31 @@
test_case(f64::INFINITY, 1.0, 1.0, sf(5.0));
}

#[test]
fn test_inverse_cdf() {
let func = |arg: f64| move |x: Cauchy| x.inverse_cdf(x.cdf(arg));
test_almost(0.0, 0.1, -5.0, 1e-10, func(-5.0));
test_almost(0.0, 0.1, -1.0, 1e-14, func(-1.0));
test_case(0.0, 0.1, 0.0, func(0.0));
test_almost(0.0, 0.1, 1.0, 1e-14, func(1.0));
test_almost(0.0, 0.1, 5.0, 1e-10, func(5.0));
test_almost(0.0, 1.0, -5.0, 1e-14, func(-5.0));
test_almost(0.0, 1.0, -1.0, 1e-15, func(-1.0));
test_case(0.0, 1.0, 0.0, func(0.0));
test_almost(0.0, 1.0, 1.0, 1e-15, func(1.0));
test_almost(0.0, 1.0, 5.0, 1e-14, func(5.0));
test_almost(0.0, 10.0, -5.0, 1e-14, func(-5.0));
test_almost(0.0, 10.0, -1.0, 1e-14, func(-1.0));
test_case(0.0, 10.0, 0.0, func(0.0));
test_almost(0.0, 10.0, 1.0, 1e-14, func(1.0));
test_almost(0.0, 10.0, 5.0, 1e-14, func(5.0));
test_case(-5.0, 100.0, -5.0, func(-5.0));
test_almost(-5.0, 100.0, -1.0, 1e-10, func(-1.0));
test_almost(-5.0, 100.0, 0.0, 1e-14, func(0.0));
test_almost(-5.0, 100.0, 1.0, 1e-14, func(1.0));
test_almost(-5.0, 100.0, 5.0, 1e-10, func(5.0));
}

#[test]
fn test_continuous() {
test::check_continuous_distribution(&try_create(-1.2, 3.4), -1500.0, 1500.0);
Expand Down
15 changes: 15 additions & 0 deletions src/distribution/chi_squared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@
fn sf(&self, x: f64) -> f64 {
self.g.sf(x)
}

/// Calculates the inverse cumulative distribution function for the
/// chi-squared distribution at `x`
///
/// # Formula
///
/// ```text
/// γ^{-1}(k / 2, x * Γ(k / 2) / 2)
/// ```
///
/// where `k` is the degrees of freedom, `Γ` is the gamma function,
/// and `γ` is the lower incomplete gamma function
fn inverse_cdf(&self, p: f64) -> f64 {
self.g.inverse_cdf(p)
}

Check warning on line 155 in src/distribution/chi_squared.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/chi_squared.rs#L153-L155

Added lines #L153 - L155 were not covered by tests
}

impl Min<f64> for ChiSquared {
Expand Down
15 changes: 15 additions & 0 deletions src/distribution/erlang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,21 @@
fn sf(&self, x: f64) -> f64 {
self.g.sf(x)
}

/// Calculates the inverse cumulative distribution function for the erlang
/// distribution at `x`
///
/// # Formula
///
/// ```text
/// γ^{-1}(k, (k - 1)! x) / λ
/// ```
///
/// where `k` is the shape, `λ` is the rate, and `γ` is the upper
/// incomplete gamma function
fn inverse_cdf(&self, p: f64) -> f64 {
self.g.inverse_cdf(p)
}

Check warning on line 139 in src/distribution/erlang.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/erlang.rs#L137-L139

Added lines #L137 - L139 were not covered by tests
}

impl Min<f64> for Erlang {
Expand Down
39 changes: 39 additions & 0 deletions src/distribution/fisher_snedecor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@
)
}
}

/// Calculates the inverse cumulative distribution function for the
/// fisher-snedecor distribution at `x`
///
/// # Formula
///
/// ```text
/// z = I^{-1}_(x)(d1 / 2, d2 / 2)
/// d2 / (d1 (1 / z - 1))
/// ```
///
/// where `d1` is the first degree of freedom, `d2` is
/// the second degree of freedom, and `I` is the regularized incomplete
/// beta function
fn inverse_cdf(&self, x: f64) -> f64 {
if !(0.0..=1.0).contains(&x) {
panic!("x must be in [0, 1]");

Check warning on line 174 in src/distribution/fisher_snedecor.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/fisher_snedecor.rs#L174

Added line #L174 was not covered by tests
} else {
let z = beta::inv_beta_reg(self.freedom_1 / 2.0, self.freedom_2 / 2.0, x);
self.freedom_2 / (self.freedom_1 * (1.0 / z - 1.0))
}
}
}

impl Min<f64> for FisherSnedecor {
Expand Down Expand Up @@ -586,6 +608,23 @@
test_almost(10.0, 1.0, 0.65910686769794, 1e-12, sf(1.0));
}

#[test]
fn test_inverse_cdf() {
let func = |arg: f64| move |x: FisherSnedecor| x.inverse_cdf(x.cdf(arg));
test_almost(0.1, 0.1, 0.1, 1e-12, func(0.1));
test_almost(1.0, 0.1, 0.1, 1e-12, func(0.1));
test_almost(10.0, 0.1, 0.1, 1e-12, func(0.1));
test_almost(0.1, 1.0, 0.1, 1e-12, func(0.1));
test_almost(1.0, 1.0, 0.1, 1e-12, func(0.1));
test_almost(10.0, 1.0, 0.1, 1e-12, func(0.1));
test_almost(0.1, 0.1, 1.0, 1e-13, func(1.0));
test_almost(1.0, 0.1, 1.0, 1e-12, func(1.0));
test_almost(10.0, 0.1, 1.0, 1e-12, func(1.0));
test_almost(0.1, 1.0, 1.0, 1e-12, func(1.0));
test_almost(1.0, 1.0, 1.0, 1e-12, func(1.0));
test_almost(10.0, 1.0, 1.0, 1e-12, func(1.0));
}

#[test]
fn test_sf_lower_bound() {
let sf = |arg: f64| move |x: FisherSnedecor| x.sf(arg);
Expand Down
31 changes: 30 additions & 1 deletion src/distribution/pareto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@
(self.scale / x).powf(self.shape)
}
}

/// Calculates the inverse cumulative distribution function for the Pareto
/// distribution at `x`
///
/// # Formula
///
/// ```text
/// x_m / (1 - x)^(1 / α)
/// ```
///
/// where `x_m` is the scale and `α` is the shape
fn inverse_cdf(&self, p: f64) -> f64 {
if !(0.0..=1.0).contains(&p) {
panic!("x must be in [0, 1]");

Check warning on line 157 in src/distribution/pareto.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/pareto.rs#L157

Added line #L157 was not covered by tests
} else {
self.scale * (1.0 - p).powf(-1.0 / self.shape)
}
}
}

impl Min<f64> for Pareto {
Expand Down Expand Up @@ -524,12 +542,23 @@
test_case(1.0, 1.0, 1.0, sf(1.0));
test_case(5.0, 5.0, 1.0, sf(2.0));
test_almost(7.0, 7.0, 0.08235429999999999, 1e-14, sf(10.0));
test_almost(10.0, 10.0, 0.16150558288984573, 1e14, sf(12.0));
test_almost(10.0, 10.0, 0.16150558288984573, 1e-14, sf(12.0));
test_case(5.0, 1.0, 0.5, sf(10.0));
test_almost(3.0, 10.0, 0.0009765625, 1e-14, sf(6.0));
test_case(1.0, 1.0, 0.0, sf(f64::INFINITY));
}

#[test]
fn test_inverse_cdf() {
let func = |arg: f64| move |x: Pareto| x.inverse_cdf(x.cdf(arg));
test_case(0.1, 0.1, 0.1, func(0.1));
test_case(1.0, 1.0, 1.0, func(1.0));
test_case(7.0, 7.0, 10.0, func(10.0));
test_case(10.0, 10.0, 12.0, func(12.0));
test_case(5.0, 1.0, 10.0, func(10.0));
test_case(3.0, 10.0, 6.0, func(6.0));
}

#[test]
fn test_continuous() {
test::check_continuous_distribution(&try_create(1.0, 10.0), 1.0, 10.0);
Expand Down
44 changes: 43 additions & 1 deletion src/distribution/triangular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
/// } if min < x <= mode {
/// (x - min)^2 / ((max - min) * (mode - min))
/// } else if mode < x < max {
/// 1 - (max - min)^2 / ((max - min) * (max - mode))
/// 1 - (max - x)^2 / ((max - min) * (max - mode))
/// } else {
/// 1
/// }
Expand Down Expand Up @@ -134,6 +134,34 @@
0.0
}
}

/// Calculates the inverse cumulative distribution function for the triangular
/// distribution
/// at `x`
///
/// # Formula
///
/// ```text
/// if x < (mode - min) / (max - min) {
/// min + ((max - min) * (mode - min) * x)^(1 / 2)
/// } else {
/// max - ((max - min) * (max - mode) * (1 - x))^(1 / 2)
/// }
/// ```
fn inverse_cdf(&self, p: f64) -> f64 {
let a = self.min;
let b = self.max;
let c = self.mode;
if !(0.0..=1.0).contains(&p) {
panic!("x must be in [0, 1]");

Check warning on line 156 in src/distribution/triangular.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/triangular.rs#L156

Added line #L156 was not covered by tests
}

if p < (c - a) / (b - a) {
a + ((c - a) * (b - a) * p).sqrt()
} else {
b - ((b - a) * (b - c) * (1.0 - p)).sqrt()
}
}
}

impl Min<f64> for Triangular {
Expand Down Expand Up @@ -538,6 +566,20 @@
test_case(0.0, 3.0, 1.5, 0.0, sf(5.0));
}

#[test]
fn test_inverse_cdf() {
let func = |arg: f64| move |x: Triangular| x.inverse_cdf(x.cdf(arg));
test_almost(0.0, 1.0, 0.5, 0.25, 1e-15, func(0.25));
test_almost(0.0, 1.0, 0.5, 0.5, 1e-15, func(0.5));
test_almost(0.0, 1.0, 0.5, 0.75, 1e-15, func(0.75));
test_almost(-5.0, 8.0, -3.5, -4.0, 1e-15, func(-4.0));
test_almost(-5.0, 8.0, -3.5, -3.5, 1e-15, func(-3.5));
test_almost(-5.0, 8.0, -3.5, 4.0, 1e-15, func(4.0));
test_almost(-5.0, -3.0, -4.0, -4.5, 1e-15, func(-4.5));
test_almost(-5.0, -3.0, -4.0, -4.0, 1e-15, func(-4.0));
test_almost(-5.0, -3.0, -4.0, -3.5, 1e-15, func(-3.5));
}

#[test]
fn test_continuous() {
test::check_continuous_distribution(&try_create(-5.0, 5.0, 0.0), -5.0, 5.0);
Expand Down
33 changes: 33 additions & 0 deletions src/distribution/weibull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@
(-x.powf(self.shape) * self.scale_pow_shape_inv).exp()
}
}

/// Calculates the inverse cumulative distribution function for the weibull
/// distribution at `x`
///
/// # Formula
///
/// ```text
/// λ (-ln(1 - x))^(1 / k)
/// ```
///
/// where `k` is the shape and `λ` is the scale
fn inverse_cdf(&self, p: f64) -> f64 {
if !(0.0..=1.0).contains(&p) {
panic!("x must be in [0, 1]");

Check warning on line 155 in src/distribution/weibull.rs

View check run for this annotation

Codecov / codecov/patch

src/distribution/weibull.rs#L155

Added line #L155 was not covered by tests
}

(-((-p).ln_1p() / self.scale_pow_shape_inv)).powf(1.0 / self.shape)
}
}

impl Min<f64> for Weibull {
Expand Down Expand Up @@ -526,6 +544,21 @@
test_case(10.0, 1.0, 0.0, sf(10.0));
}

#[test]
fn test_inverse_cdf() {
let func = |arg: f64| move |x: Weibull| x.inverse_cdf(x.cdf(arg));
test_case(1.0, 0.1, 0.0, func(0.0));
test_almost(1.0, 0.1, 1.0, 1e-13, func(1.0));
test_case(1.0, 1.0, 0.0, func(0.0));
test_case(1.0, 1.0, 1.0, func(1.0));
test_almost(1.0, 1.0, 10.0, 1e-10, func(10.0));
test_case(10.0, 10.0, 0.0, func(0.0));
test_almost(10.0, 10.0, 1.0, 1e-5, func(1.0));
test_almost(10.0, 10.0, 10.0, 1e-10, func(10.0));
test_case(10.0, 1.0, 0.0, func(0.0));
test_case(10.0, 1.0, 1.0, func(1.0));
}

#[test]
fn test_continuous() {
test::check_continuous_distribution(&try_create(1.0, 0.2), 0.0, 10.0);
Expand Down