Skip to content
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

Adding logcdf method to inverse gamma distribution #3944

Merged
merged 7 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ This new version of `Theano-PyMC` comes with an experimental JAX backend which,
- Test model logp before starting any MCMC chains (see [#4116](https://github.com/pymc-devs/pymc3/issues/4116))
- Fix bug in `model.check_test_point` that caused the `test_point` argument to be ignored. (see [PR #4211](https://github.com/pymc-devs/pymc3/pull/4211#issuecomment-727142721))
- Refactored MvNormal.random method with better handling of sample, batch and event shapes. [#4207](https://github.com/pymc-devs/pymc3/pull/4207)
- The `InverseGamma` distribution now implements a `logcdf`. [#3944](https://github.com/pymc-devs/pymc3/pull/3944)

### Documentation
- Added a new notebook demonstrating how to incorporate sampling from a conjugate Dirichlet-multinomial posterior density in conjunction with other step methods (see [#4199](https://github.com/pymc-devs/pymc3/pull/4199)).
Expand Down
24 changes: 24 additions & 0 deletions pymc3/distributions/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -2634,6 +2634,30 @@ def logp(self, value):
def _distr_parameters_for_repr(self):
return ["alpha", "beta"]

def logcdf(self, value):
"""
Compute the log of the cumulative distribution function for Inverse Gamma distribution
at the specified value.

Parameters
----------
value: numeric
Value(s) for which log CDF is calculated. If the log CDF for multiple
values are desired the values must be provided in a numpy array or theano tensor.

Returns
-------
TensorVariable
"""
alpha = self.alpha
beta = self.beta
return bound(
tt.log(tt.gammaincc(alpha, beta / value)),
value >= 0,
alpha > 0,
beta > 0,
)


class ChiSquared(Gamma):
r"""
Expand Down
10 changes: 10 additions & 0 deletions pymc3/tests/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,13 +913,23 @@ def test_fun(value, mu, sigma):
lambda value, alpha, beta: sp.gamma.logcdf(value, alpha, scale=1.0 / beta),
)

@pytest.mark.xfail(
condition=(theano.config.floatX == "float32"),
reason="Fails on float32 due to numerical issues",
)
def test_inverse_gamma(self):
self.pymc3_matches_scipy(
InverseGamma,
Rplus,
{"alpha": Rplus, "beta": Rplus},
lambda value, alpha, beta: sp.invgamma.logpdf(value, alpha, scale=beta),
)
self.check_logcdf(
InverseGamma,
Rplus,
{"alpha": Rplus, "beta": Rplus},
lambda value, alpha, beta: sp.invgamma.logcdf(value, alpha, scale=beta),
)

@pytest.mark.xfail(
condition=(theano.config.floatX == "float32"),
Expand Down