Skip to content

Commit

Permalink
Add ICDF function to pareto distribution
Browse files Browse the repository at this point in the history
Adds ICDF (quantile) function for the Pareto distribution. Source https://en.wikipedia.org/wiki/Pareto_distribution

Issue #6612
  • Loading branch information
james-2001 authored and ricardoV94 committed May 11, 2023
1 parent 10eab32 commit 9b72c2e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
20 changes: 12 additions & 8 deletions pymc/distributions/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -1491,16 +1491,10 @@ def logcdf(value, mu, b):

def icdf(value, mu, b):
res = pt.switch(
pt.le(value, 0.5),
mu + b * np.log(2 * value),
mu - b * np.log(2 - 2 * value)
pt.le(value, 0.5), mu + b * np.log(2 * value), mu - b * np.log(2 - 2 * value)
)
res = check_icdf_value(res, value)
return check_icdf_parameters(
res,
b > 0,
msg="b > 0"
)
return check_icdf_parameters(res, b > 0, msg="b > 0")


class AsymmetricLaplaceRV(RandomVariable):
Expand Down Expand Up @@ -1952,6 +1946,16 @@ def logcdf(value, alpha, m):
msg="alpha > 0, m > 0",
)

def icdf(value, alpha, m):
res = m * pt.pow(1 - value, -1 / alpha)
res = check_icdf_value(res, value)
return check_icdf_parameters(
res,
alpha > 0,
m > 0,
msg="alpha > 0, m > 0",
)


@_default_transform.register(Pareto)
def pareto_default_transform(op, rv):
Expand Down
11 changes: 6 additions & 5 deletions tests/distributions/test_continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,7 @@ def test_laplace(self):
{"mu": R, "b": Rplus},
lambda value, mu, b: st.laplace.logcdf(value, mu, b),
)
check_icdf(
pm.Laplace,
{"mu": R, "b": Rplus},
lambda q, mu, b: st.laplace.ppf(q, mu, b)
)
check_icdf(pm.Laplace, {"mu": R, "b": Rplus}, lambda q, mu, b: st.laplace.ppf(q, mu, b))

def test_laplace_asymmetric(self):
check_logp(
Expand Down Expand Up @@ -647,6 +643,11 @@ def test_pareto(self):
{"alpha": Rplusbig, "m": Rplusbig},
lambda value, alpha, m: st.pareto.logcdf(value, alpha, scale=m),
)
check_icdf(
pm.Pareto,
{"alpha": Rplusbig, "m": Rplusbig},
lambda q, alpha, m: st.pareto.ppf(q, alpha, scale=m),
)

@pytest.mark.skipif(
condition=(pytensor.config.floatX == "float32"),
Expand Down

0 comments on commit 9b72c2e

Please sign in to comment.