Skip to content

Add user-friendly logp and logcdf methods #4833

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 3 commits into from
Jul 6, 2021
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
22 changes: 7 additions & 15 deletions docs/source/developer_guide_implementing_distribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,33 +188,25 @@ Some notes:
1. As mentioned above, `v4` works in a very {functional}`Functional_Programming` way, and all the information that is needed in the `logp` and `logcdf` methods is expected to be "carried" via the `RandomVariable` inputs. You may pass numerical arguments that are not strictly needed for the `rng_fn` method but are used in the `logp` and `logcdf` methods. Just keep in mind whether this affects the correct shape inference behavior of the `RandomVariable`. If specialized non-numeric information is needed you might need to define your custom`_logp` and `_logcdf` {dispatch}`Dispatching` functions, but this should be done as a last resort.
1. The `logcdf` method is not a requirement, but it's a nice plus!

For a quick check that things are working you can try to create the new distribution in a model context:
For a quick check that things are working you can try the following:

```python

import pymc3 as pm
from pymc3.distributions.logp import logpt

with pm.Model() as model:
# pm.blah = pm.Uniform in this example
blah = pm.Blah('blah', [0, 0], [1, 2])
# pm.blah = pm.Uniform in this example
blah = pm.Blah.dist([0, 0], [1, 2])

# Test that the returned blah_op is still working fine
blah.eval()
# array([0.62778803, 1.95165513])

# logpt will replace the blah RandomVariable with the corresponding logp
# expression, which takes as input the `blah_value` `TensorVariable` and
# `blah.owner.inputs`. We pass `transformed=False` to return the original
# `logp` expression just for simplicity of the example.
blah_value = model.rvs_to_values[blah]
blah_logp = logpt(blah, {blah: blah_value}, transformed=False)
blah_logp.eval({blah_value: [1.5, 1.5]})
# Test the logp
pm.logp(blah, [1.5, 1.5]).eval()
# array([ -inf, -0.69314718])

# It will instead introduce the `logcdf` expression if we pass `cdf=True`
blah_logcdf = logpt(blah, {blah: blah_value}, cdf=True)
blah_logcdf.eval({blah_value: [1.5, 1.5]})
# Test the logcdf
pm.logcdf(blah, [1.5, 1.5]).eval()
# array([ 0. , -0.28768207])
```

Expand Down
4 changes: 3 additions & 1 deletion pymc3/distributions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from pymc3.distributions.logp import ( # isort:skip
from pymc3.distributions.logprob import ( # isort:skip
_logcdf,
_logp,
logcdf,
logp,
logp_transform,
logpt,
logpt_sum,
Expand Down Expand Up @@ -189,6 +190,7 @@
"BART",
"CAR",
"logpt",
"logp",
"_logp",
"logp_transform",
"logcdf",
Expand Down
2 changes: 1 addition & 1 deletion pymc3/distributions/discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
normal_lcdf,
)
from pymc3.distributions.distribution import Discrete
from pymc3.distributions.logp import _logcdf, _logp
from pymc3.distributions.logprob import _logcdf, _logp
from pymc3.math import log1mexp, logaddexp, logsumexp, sigmoid

__all__ = [
Expand Down
19 changes: 17 additions & 2 deletions pymc3/distributions/logp.py → pymc3/distributions/logprob.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,24 @@ def subtensor_logp(op, var, rvs_to_values, indexed_rv_var, *indices, **kwargs):
return logp_var


def logcdf(*args, **kwargs):
def logp(var, rv_values, **kwargs):
"""Create a log-probability graph."""

# Attach the value_var to the tag of var when it does not have one
if not hasattr(var.tag, "value_var"):
if isinstance(rv_values, Mapping):
value_var = rv_values[var]
else:
value_var = rv_values
var.tag.value_var = at.as_tensor_variable(value_var, dtype=var.dtype)

return logpt(var, rv_values, **kwargs)


def logcdf(var, rv_values, **kwargs):
"""Create a log-CDF graph."""
return logpt(*args, cdf=True, **kwargs)

return logp(var, rv_values, cdf=True, **kwargs)


@singledispatch
Expand Down
Loading