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 icdf functions for Beta, Gamma, Chisquared and StudentT distributions #6845

Merged
merged 2 commits into from
Mar 25, 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
51 changes: 43 additions & 8 deletions pymc/distributions/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from pytensor.raise_op import Assert
from pytensor.tensor import gammaln
from pytensor.tensor.extra_ops import broadcast_shape
from pytensor.tensor.math import tanh
from pytensor.tensor.math import betaincinv, gammaincinv, tanh
from pytensor.tensor.random.basic import (
BetaRV,
_gamma,
Expand Down Expand Up @@ -1227,6 +1227,16 @@ def logcdf(value, alpha, beta):
msg="alpha > 0, beta > 0",
)

def icdf(value, alpha, beta):
res = betaincinv(alpha, beta, value)
res = check_icdf_value(res, value)
return check_icdf_parameters(
res,
alpha > 0,
beta > 0,
msg="alpha > 0, beta > 0",
)


class KumaraswamyRV(RandomVariable):
name = "kumaraswamy"
Expand Down Expand Up @@ -1872,6 +1882,21 @@ def logcdf(value, nu, mu, sigma):
msg="nu > 0, sigma > 0",
)

def icdf(value, nu, mu, sigma):
res = pt.switch(
pt.lt(value, 0.5),
-pt.sqrt(nu) * pt.sqrt((1.0 / betaincinv(nu * 0.5, 0.5, 2.0 * value)) - 1.0),
pt.sqrt(nu) * pt.sqrt((1.0 / betaincinv(nu * 0.5, 0.5, 2.0 * (1 - value))) - 1.0),
)
res = mu + res * sigma
res = check_icdf_value(res, value)
return check_icdf_parameters(
res,
nu > 0,
sigma > 0,
msg="nu > 0, sigma > 0",
)


class Pareto(BoundedContinuous):
r"""
Expand Down Expand Up @@ -2063,7 +2088,7 @@ def logcdf(value, alpha, beta):
def icdf(value, alpha, beta):
res = alpha + beta * pt.tan(np.pi * (value - 0.5))
res = check_icdf_value(res, value)
return check_parameters(
return check_icdf_parameters(
res,
beta > 0,
msg="beta > 0",
Expand Down Expand Up @@ -2147,7 +2172,7 @@ def logcdf(value, loc, beta):
def icdf(value, loc, beta):
res = loc + beta * pt.tan(np.pi * (value) / 2.0)
res = check_icdf_value(res, value)
return check_parameters(
return check_icdf_parameters(
res,
beta > 0,
msg="beta > 0",
Expand Down Expand Up @@ -2272,6 +2297,16 @@ def logcdf(value, alpha, scale):
)
return check_parameters(res, 0 < alpha, 0 < beta, msg="alpha > 0, beta > 0")

def icdf(value, alpha, scale):
res = scale * gammaincinv(alpha, value)
res = check_icdf_value(res, value)
return check_icdf_parameters(
res,
alpha > 0,
scale > 0,
msg="alpha > 0, beta > 0",
)


class InverseGamma(PositiveContinuous):
r"""
Expand Down Expand Up @@ -2556,7 +2591,7 @@ def logp(value, alpha, beta):
def icdf(value, alpha, beta):
res = beta * (-pt.log(1 - value)) ** (1 / alpha)
res = check_icdf_value(res, value)
return check_parameters(
return check_icdf_parameters(
res,
alpha > 0,
beta > 0,
Expand Down Expand Up @@ -3116,7 +3151,7 @@ def icdf(value, lower, c, upper):
upper - np.sqrt((upper - lower) * (upper - c) * (1 - value)),
)
res = check_icdf_value(res, value)
return check_parameters(
return check_icdf_parameters(
res,
lower <= c,
c <= upper,
Expand Down Expand Up @@ -3219,7 +3254,7 @@ def logcdf(value, mu, beta):
def icdf(value, mu, beta):
res = mu - beta * pt.log(-pt.log(value))
res = check_icdf_value(res, value)
return check_parameters(
return check_icdf_parameters(
res,
beta > 0,
msg="beta > 0",
Expand Down Expand Up @@ -3437,7 +3472,7 @@ def logcdf(value, mu, s):
def icdf(value, mu, s):
res = mu + s * pt.log(value / (1 - value))
res = check_icdf_value(res, value)
return check_parameters(
return check_icdf_parameters(
res,
s > 0,
msg="s > 0",
Expand Down Expand Up @@ -3787,7 +3822,7 @@ def logcdf(value, mu, sigma):
def icdf(value, mu, sigma):
res = sigma * -pt.log(2.0 * pt.erfcinv(value) ** 2) + mu
res = check_icdf_value(res, value)
return check_parameters(
return check_icdf_parameters(
res,
sigma > 0,
msg="sigma > 0",
Expand Down
21 changes: 21 additions & 0 deletions tests/distributions/test_continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ def test_beta_logcdf(self):
lambda value, alpha, beta: st.beta.logcdf(value, alpha, beta),
)

def test_beta_icdf(self):
check_icdf(
pm.Beta,
{"alpha": Rplus, "beta": Rplus},
lambda q, alpha, beta: st.beta.ppf(q, alpha, beta),
)

def test_kumaraswamy(self):
# Scipy does not have a built-in Kumaraswamy
def scipy_log_pdf(value, a, b):
Expand Down Expand Up @@ -557,6 +564,13 @@ def test_studentt_logcdf(self):
lambda value, nu, mu, sigma: st.t.logcdf(value, nu, mu, sigma),
)

def test_studentt_icdf(self):
check_icdf(
pm.StudentT,
{"nu": Rplusbig, "mu": R, "sigma": Rplusbig},
lambda q, nu, mu, sigma: st.t.ppf(q, nu, mu, sigma),
)

def test_cauchy(self):
check_logp(
pm.Cauchy,
Expand Down Expand Up @@ -623,6 +637,13 @@ def test_gamma_logcdf(self):
lambda value, alpha, beta: st.gamma.logcdf(value, alpha, scale=1.0 / beta),
)

def test_gamma_icdf(self):
check_icdf(
pm.Gamma,
{"alpha": Rplusbig, "beta": Rplusbig},
lambda q, alpha, beta: st.gamma.ppf(q, alpha, scale=1.0 / beta),
)

def test_inverse_gamma_logp(self):
check_logp(
pm.InverseGamma,
Expand Down
Loading