Skip to content

Add backports module and backport logp syntax #5083

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 2 commits into from
Oct 17, 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
2 changes: 2 additions & 0 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
floatx: [float32, float64]
test-subset:
- |
--ignore=pymc3/tests/test_backports.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious why do we need to ignore it here? is it due to CI time?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first job in this matrix is the fall-back which runs all test files that aren't explicitly mentioned by the others.
It's complemented by the check_all_tests_are_covered.py pre-commit script checking that there's also no test file that's run by another job but not ignored here.

Pro-tip: On main you can run python scripts/check_all_tests_are_covered.py locally and it shows you which OS/floatX combination each test file is running in.

--ignore=pymc3/tests/test_dist_math.py
--ignore=pymc3/tests/test_distribution_defaults.py
--ignore=pymc3/tests/test_distributions.py
Expand Down Expand Up @@ -42,6 +43,7 @@ jobs:
pymc3/tests/test_shared.py
pymc3/tests/test_smc.py
- |
pymc3/tests/test_backports.py
pymc3/tests/test_examples.py
pymc3/tests/test_mixture.py
pymc3/tests/test_ode.py
Expand Down
4 changes: 4 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## PyMC 3.11.5 (TBD)
### Backports
+ The `pm.logp(rv, x)` syntax is now available and recommended to make your model code `v4`-ready. Note that this backport is just an alias and much less capable than what's available with `pymc >=4` (see [#5083](https://github.com/pymc-devs/pymc/pulls/5083)).

## PyMC3 3.11.4 (20 August 2021)

### New Features
Expand Down
2 changes: 1 addition & 1 deletion docs/source/developer_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ explicit about the conversion. For example:
with pm.Model() as model:
z = pm.Normal('z', mu=0., sigma=5.) # ==> pymc3.model.FreeRV, or theano.tensor with logp
x = pm.Normal('x', mu=z, sigma=1., observed=5.) # ==> pymc3.model.ObservedRV, also has logp properties
x.logp({'z': 2.5}) # ==> -4.0439386
pm.logp(x, {'z': 2.5}) # ==> -4.0439386
model.logp({'z': 2.5}) # ==> -6.6973152

**TFP**
Expand Down
1 change: 1 addition & 0 deletions pymc3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def _hotfix_theano_printing():
from pymc3 import gp, ode, sampling
from pymc3.backends import load_trace, save_trace
from pymc3.backends.tracetab import *
from pymc3.backports import logp
from pymc3.blocking import *
from pymc3.data import *
from pymc3.distributions import *
Expand Down
43 changes: 43 additions & 0 deletions pymc3/backports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2021 The PyMC Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Union

import numpy as np

from theano.tensor import TensorVariable

from pymc3.distributions.distribution import Distribution
from pymc3.model import Factor


def logp(
rv: Union[Factor, Distribution], value: Union[TensorVariable, np.ndarray]
) -> Union[TensorVariable, np.ndarray]:
"""
Calculate log-probability of a distribution at specified value.

This function is a limited functionality backported version of PyMC >=4.0 like capabilities.

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

Returns
-------
logp : TensorVariable or np.ndarray
"""
return rv.logp(value)
32 changes: 32 additions & 0 deletions pymc3/tests/test_backports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2021 The PyMC Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np

import pymc3 as pm


class TestLogpSyntax:
def test_equivalence(self):
with pm.Model():
rv = pm.Normal("n")
input = {"n": 2}
np.testing.assert_array_equal(rv.logp(input), pm.logp(rv, input))

def test_equivalence_dist(self):
rv = pm.Normal.dist()
assert rv.logp(2).eval() == pm.logp(rv, 2).eval()
np.testing.assert_array_equal(
rv.logp(np.arange(3)).eval(), pm.logp(rv, np.arange(3)).eval()
)