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 1 commit
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
9 changes: 9 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,9 @@ def logcdf(value, mu, lam, alpha):
msg="mu > 0, lam > 0, alpha >= 0",
)

def icdf(mu, lam):
return stats.invgauss(mu=mu, scale=lam)


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")