Skip to content

Implement icdf for Univariate distribution #6528

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 11 commits into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -343,6 +343,9 @@ def logcdf(value, lower, upper):
msg="lower <= upper",
)

def icdf(value, lower, upper):
return lower + (upper - lower) * value


@_default_transform.register(Uniform)
def uniform_default_transform(op, rv):
Expand Down Expand Up @@ -835,6 +838,9 @@ def logcdf(value, loc, sigma):
msg="sigma > 0",
)

def icdf(value, lower, upper, loc, sigma):
return stats.truncnorm.ppf(q=value, a=lower, b=upper, loc=loc, scale=sigma)


class WaldRV(RandomVariable):
name = "wald"
Expand Down Expand Up @@ -1013,6 +1019,42 @@ def logcdf(value, mu, lam, alpha):
msg="mu > 0, lam > 0, alpha >= 0",
)

def icdf(value, mu, lam, alpha=0):
"""Calculate the inverse cumulative distribution function (icdf) of the Wald distribution.

Args
----
value (float): Probability value between 0 and 1.
mu (float): Mean of the distribution.
lam (float): Scale of the distribution.

Returns
-------
float: The value x such that P(W <= x) = p, where W is the Wald distribution with given mu and lam.

Raises
------
ValueError: If value is not between 0 and 1.
ValueError: If lam is not positive.
"""
# Compute standard deviation and location parameter
std = at.sqrt(lam)
loc = alpha + mu

# Compute inverse standard normal CDF
z = at.sqrt(2) * at.erfinv(2 * value - 1)

# Compute Wald ICDF
x = loc + std * z / (1 - 0.5 * std * z)

return check_parameters(
x,
0 <= value <= 1,
lam > 0,
alpha >= 0,
msg="0<=val<=1, lam > 0, alpha >= 0",
)


class BetaClippedRV(BetaRV):
@classmethod
Expand Down
31 changes: 29 additions & 2 deletions pymc/tests/distributions/test_continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@

import pymc as pm

from pymc.distributions.continuous import Normal, get_tau_sigma, interpolated
from pymc.distributions.continuous import (
Normal,
TruncatedNormal,
Uniform,
Wald,
get_tau_sigma,
interpolated,
)
from pymc.distributions.dist_math import clipped_beta_rvs
from pymc.logprob.abstract import logcdf
from pymc.logprob.joint_logprob import logp
Expand Down Expand Up @@ -2281,5 +2288,25 @@ def test_normal_icdf(self, dist_params, obs, size):
dist_params = dict(zip(dist_params_at, dist_params))

x = Normal.dist(*dist_params_at, size=size_at)

scipy_logprob_tester(x, obs, dist_params, test_fn=st.norm.ppf, test="icdf")

def test_uniform_icdf(self, dist_params, obs, size):
dist_params_at, obs_at, size_at = create_pytensor_params(dist_params, obs, size)
dist_params = dict(zip(dist_params_at, dist_params))

x = Uniform.dist(*dist_params_at, size=size_at)
scipy_logprob_tester(x, obs, dist_params, test_fn=st.uniform.ppf, test="icdf")

def test_truncated_normal_icdf(self, dist_params, obs, size):
dist_params_at, obs_at, size_at = create_pytensor_params(dist_params, obs, size)
dist_params = dict(zip(dist_params_at, dist_params))

x = TruncatedNormal.dist(*dist_params_at, size=size_at)
scipy_logprob_tester(x, obs, dist_params, test_fn=st.truncnorm.ppf, test="icdf")

def test_wald_icdf(self, *dist_params, obs, size):
dist_params_at, obs_at, size_at = create_pytensor_params(dist_params, obs, size)
dist_params = dict(zip(dist_params_at, dist_params))

x = Wald.dist(*dist_params_at, size=size_at)
scipy_logprob_tester(x, obs, dist_params, test_fn=st.wald.ppf, test="icdf")