Skip to content

Commit 6e9da82

Browse files
rpgoldmanColCarroll
authored andcommitted
Support mu and sd parameterization for inverse gamma. (#3227)
* Support mu and sd parameterization for inverse gamma. * Fix bad newline. Issue detected by autopep8. * Fixed new test per @ColCarroll. Marked the test of the alternative parameterization as an expected failure. Also put in Colin's rewrite to make the test more elegant.
1 parent 4e33b32 commit 6e9da82

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

pymc3/distributions/continuous.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2409,10 +2409,16 @@ class InverseGamma(PositiveContinuous):
24092409
Shape parameter (alpha > 0).
24102410
beta : float
24112411
Scale parameter (beta > 0).
2412+
mu : float
2413+
Alternative shape parameter (mu > 0).
2414+
sd : float
2415+
Alternative scale parameter (sd > 0).
24122416
"""
24132417

2414-
def __init__(self, alpha, beta=1, *args, **kwargs):
2418+
def __init__(self, alpha=None, beta=None, mu=None, sd=None, *args, **kwargs):
24152419
super(InverseGamma, self).__init__(*args, defaults=('mode',), **kwargs)
2420+
2421+
alpha, beta = InverseGamma._get_alpha_beta(alpha, beta, mu, sd)
24162422
self.alpha = alpha = tt.as_tensor_variable(alpha)
24172423
self.beta = beta = tt.as_tensor_variable(beta)
24182424

@@ -2432,6 +2438,23 @@ def _calculate_mean(self):
24322438
m[self.alpha <= 1] = np.inf
24332439
return m
24342440

2441+
@staticmethod
2442+
def _get_alpha_beta(alpha, beta, mu, sd):
2443+
if (alpha is not None):
2444+
if (beta is not None):
2445+
pass
2446+
else:
2447+
beta = 1
2448+
elif (mu is not None) and (sd is not None):
2449+
alpha = (2 * sd**2 + mu**2)/sd**2
2450+
beta = mu * (mu**2 + sd**2) / sd**2
2451+
else:
2452+
raise ValueError('Incompatible parameterization. Either use '
2453+
'alpha and (optionally) beta, or mu and sd to specify '
2454+
'distribution.')
2455+
2456+
return alpha, beta
2457+
24352458
def random(self, point=None, size=None):
24362459
"""
24372460
Draw random values from InverseGamma distribution.

pymc3/tests/test_distributions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,15 @@ def test_inverse_gamma(self):
702702
InverseGamma, Rplus, {'alpha': Rplus, 'beta': Rplus},
703703
lambda value, alpha, beta: sp.invgamma.logpdf(value, alpha, scale=beta))
704704

705+
@pytest.mark.xfail(condition=(theano.config.floatX == "float32"),
706+
reason="Fails on float32 due to scaling issues")
707+
def test_inverse_gamma_alt_params(self):
708+
def test_fun(value, mu, sd):
709+
alpha, beta = InverseGamma._get_alpha_beta(None, None, mu, sd)
710+
return sp.invgamma.logpdf(value, alpha, scale=beta)
711+
self.pymc3_matches_scipy(
712+
InverseGamma, Rplus, {'mu': Rplus, 'sd': Rplus}, test_fun)
713+
705714
def test_pareto(self):
706715
self.pymc3_matches_scipy(Pareto, Rplus, {'alpha': Rplusbig, 'm': Rplusbig},
707716
lambda value, alpha, m: sp.pareto.logpdf(value, alpha, scale=m))

0 commit comments

Comments
 (0)