Skip to content

Add icdf functions for Moyal, Gumbel, Triangular and Weibull distributions #6802

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

Merged
merged 1 commit into from
Jun 29, 2023
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
42 changes: 42 additions & 0 deletions pymc/distributions/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,16 @@ def logp(value, alpha, beta):
msg="alpha > 0, beta > 0",
)

def icdf(value, alpha, beta):
res = beta * (-pt.log(1 - value)) ** (1 / alpha)
res = check_icdf_value(res, value)
return check_parameters(
res,
alpha > 0,
beta > 0,
msg="alpha > 0, beta > 0",
)


class HalfStudentTRV(RandomVariable):
name = "halfstudentt"
Expand Down Expand Up @@ -3089,6 +3099,20 @@ def logcdf(value, lower, c, upper):
msg="lower <= c <= upper",
)

def icdf(value, lower, c, upper):
res = pt.switch(
pt.lt(value, ((c - lower) / (upper - lower))),
lower + np.sqrt((upper - lower) * (c - lower) * value),
upper - np.sqrt((upper - lower) * (upper - c) * (1 - value)),
)
res = check_icdf_value(res, value)
return check_parameters(
res,
lower <= c,
c <= upper,
msg="lower <= c <= upper",
)


@_default_transform.register(Triangular)
def triangular_default_transform(op, rv):
Expand Down Expand Up @@ -3177,6 +3201,15 @@ def logcdf(value, mu, beta):
msg="beta > 0",
)

def icdf(value, mu, beta):
res = mu - beta * pt.log(-pt.log(value))
res = check_icdf_value(res, value)
return check_parameters(
res,
beta > 0,
msg="beta > 0",
)


class RiceRV(RandomVariable):
name = "rice"
Expand Down Expand Up @@ -3733,6 +3766,15 @@ def logcdf(value, mu, sigma):
msg="sigma > 0",
)

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(
res,
sigma > 0,
msg="sigma > 0",
)


class PolyaGammaRV(RandomVariable):
"""Polya-Gamma random variable."""
Expand Down
25 changes: 25 additions & 0 deletions tests/distributions/test_continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ def test_triangular(self):
lambda value, c, lower, upper: st.triang.logcdf(value, c - lower, lower, upper - lower),
skip_paramdomain_outside_edge_test=True,
)
check_icdf(
pm.Triangular,
{"lower": -Rplusunif, "c": Runif, "upper": Rplusunif},
lambda q, c, lower, upper: st.triang.ppf(q, c - lower, lower, upper - lower),
skip_paramdomain_outside_edge_test=True,
)

# Custom logp/logcdf check for values outside of domain
valid_dist = pm.Triangular.dist(lower=0, upper=1, c=0.9, size=2)
Expand Down Expand Up @@ -704,6 +710,13 @@ def test_weibull_logcdf(self):
lambda value, alpha, beta: st.exponweib.logcdf(value, 1, alpha, scale=beta),
)

def test_weibull_icdf(self):
check_icdf(
pm.Weibull,
{"alpha": Rplusbig, "beta": Rplusbig},
lambda q, alpha, beta: st.exponweib.ppf(q, 1, alpha, scale=beta),
)

def test_half_studentt(self):
# this is only testing for nu=1 (halfcauchy)
check_logp(
Expand Down Expand Up @@ -780,6 +793,11 @@ def test_gumbel(self):
{"mu": R, "beta": Rplusbig},
lambda value, mu, beta: st.gumbel_r.logcdf(value, loc=mu, scale=beta),
)
check_icdf(
pm.Gumbel,
{"mu": R, "beta": Rplusbig},
lambda q, mu, beta: st.gumbel_r.ppf(q, loc=mu, scale=beta),
)

def test_logistic(self):
check_logp(
Expand Down Expand Up @@ -863,6 +881,13 @@ def test_moyal_logcdf(self):
if pytensor.config.floatX == "float32":
raise Exception("Flaky test: It passed this time, but XPASS is not allowed.")

def test_moyal_icdf(self):
check_icdf(
pm.Moyal,
{"mu": R, "sigma": Rplusbig},
lambda q, mu, sigma: floatX(st.moyal.ppf(q, mu, sigma)),
)

def test_interpolated(self):
for mu in R.vals:
for sigma in Rplus.vals:
Expand Down