Skip to content

Commit

Permalink
Adding logcdf method to inverse gamma distribution (#3944)
Browse files Browse the repository at this point in the history
* adding logcdf method to inverse gamma distribution

* formatting fixes

* more formatting fixes

* mark xfail for float32

* updating release notes to master

* adding PR to release notes

Co-authored-by: Eelke Spaak <eelke.spaak@gmail.com>
  • Loading branch information
dfm and Spaak authored Dec 2, 2020
1 parent 7ff2f49 commit 988ab9d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
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

0 comments on commit 988ab9d

Please sign in to comment.