Skip to content

Commit 8822346

Browse files
Added icdf for Rice and skewnormal distributions
1 parent 2c355f1 commit 8822346

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

pymc/distributions/continuous.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ def polyagamma_cdf(*args, **kwargs):
7676
from scipy import stats
7777
from scipy.interpolate import InterpolatedUnivariateSpline
7878
from scipy.special import expit
79+
from scipy.stats import norm
80+
from scipy.optimize import newton
81+
from scipy.special import ive, iv
7982

8083
from pymc.distributions import transforms
8184
from pymc.distributions.dist_math import (
@@ -2993,6 +2996,23 @@ def logp(value, mu, sigma, alpha):
29932996
tau > 0,
29942997
msg="tau > 0",
29952998
)
2999+
def icdf(prob, mu, sigma, alpha, max_iter=100, tol=1e-8):
3000+
def cdf_difference(x):
3001+
return norm.cdf(x) - 2 * norm.cdf(alpha * x) - prob
3002+
3003+
x0 = norm.ppf(prob)
3004+
x = x0
3005+
3006+
for _ in range(max_iter):
3007+
diff = cdf_difference(x)
3008+
3009+
if np.abs(diff) < tol:
3010+
return mu + sigma * x
3011+
3012+
derivative = norm.pdf(x) - 2 * alpha * norm.pdf(alpha * x)
3013+
x -= derivative
3014+
3015+
return mu + sigma * x
29963016

29973017

29983018
class Triangular(BoundedContinuous):
@@ -3348,6 +3368,17 @@ def logp(value, b, sigma):
33483368
b >= 0,
33493369
msg="sigma >= 0, b >= 0",
33503370
)
3371+
def icdf(prob, nu, sigma, x0=1.0, max_iter=100, tol=1e-8):
3372+
def cdf(x):
3373+
return (1 + x / sigma**2) * np.exp(-x**2 / (2 * sigma**2)) * iv(0, x * nu / sigma**2) - prob
3374+
3375+
def cdf_derivative(x):
3376+
return ((1 - x**2 / sigma**2) * np.exp(-x**2 / (2 * sigma**2)) * iv(0, x * nu / sigma**2)
3377+
+ (x / sigma**2) * np.exp(-x**2 / (2 * sigma**2)) * ive(0, x * nu / sigma**2)) * nu / sigma**2
3378+
3379+
approx_icdf = newton(cdf, x0, fprime=cdf_derivative, tol=tol, maxiter=max_iter)
3380+
3381+
return approx_icdf
33513382

33523383

33533384
class Logistic(Continuous):

0 commit comments

Comments
 (0)