Skip to content

Commit fc1aceb

Browse files
Add backports module and backport logp syntax (#5083)
* Add backports module and backport logp syntax Closes #5052 Co-authored-by: Ravin Kumar <ravinsdrive@gmail.com>
1 parent 916c068 commit fc1aceb

File tree

6 files changed

+83
-1
lines changed

6 files changed

+83
-1
lines changed

.github/workflows/pytest.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
floatx: [float32, float64]
1414
test-subset:
1515
- |
16+
--ignore=pymc3/tests/test_backports.py
1617
--ignore=pymc3/tests/test_dist_math.py
1718
--ignore=pymc3/tests/test_distribution_defaults.py
1819
--ignore=pymc3/tests/test_distributions.py
@@ -42,6 +43,7 @@ jobs:
4243
pymc3/tests/test_shared.py
4344
pymc3/tests/test_smc.py
4445
- |
46+
pymc3/tests/test_backports.py
4547
pymc3/tests/test_examples.py
4648
pymc3/tests/test_mixture.py
4749
pymc3/tests/test_ode.py

RELEASE-NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release Notes
22

3+
## PyMC 3.11.5 (TBD)
4+
### Backports
5+
+ 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)).
6+
37
## PyMC3 3.11.4 (20 August 2021)
48

59
### New Features

docs/source/developer_guide.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ explicit about the conversion. For example:
158158
with pm.Model() as model:
159159
z = pm.Normal('z', mu=0., sigma=5.) # ==> pymc3.model.FreeRV, or theano.tensor with logp
160160
x = pm.Normal('x', mu=z, sigma=1., observed=5.) # ==> pymc3.model.ObservedRV, also has logp properties
161-
x.logp({'z': 2.5}) # ==> -4.0439386
161+
pm.logp(x, {'z': 2.5}) # ==> -4.0439386
162162
model.logp({'z': 2.5}) # ==> -6.6973152
163163
164164
**TFP**

pymc3/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def _hotfix_theano_printing():
8282
from pymc3 import gp, ode, sampling
8383
from pymc3.backends import load_trace, save_trace
8484
from pymc3.backends.tracetab import *
85+
from pymc3.backports import logp
8586
from pymc3.blocking import *
8687
from pymc3.data import *
8788
from pymc3.distributions import *

pymc3/backports.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2021 The PyMC Developers
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typing import Union
16+
17+
import numpy as np
18+
19+
from theano.tensor import TensorVariable
20+
21+
from pymc3.distributions.distribution import Distribution
22+
from pymc3.model import Factor
23+
24+
25+
def logp(
26+
rv: Union[Factor, Distribution], value: Union[TensorVariable, np.ndarray]
27+
) -> Union[TensorVariable, np.ndarray]:
28+
"""
29+
Calculate log-probability of a distribution at specified value.
30+
31+
This function is a limited functionality backported version of PyMC >=4.0 like capabilities.
32+
33+
Parameters
34+
----------
35+
value : numeric
36+
Value(s) for which log-probability is calculated. If the log-probabilities for multiple
37+
values are desired the values must be provided in a numpy array or theano tensor
38+
39+
Returns
40+
-------
41+
logp : TensorVariable or np.ndarray
42+
"""
43+
return rv.logp(value)

pymc3/tests/test_backports.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2021 The PyMC Developers
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import numpy as np
16+
17+
import pymc3 as pm
18+
19+
20+
class TestLogpSyntax:
21+
def test_equivalence(self):
22+
with pm.Model():
23+
rv = pm.Normal("n")
24+
input = {"n": 2}
25+
np.testing.assert_array_equal(rv.logp(input), pm.logp(rv, input))
26+
27+
def test_equivalence_dist(self):
28+
rv = pm.Normal.dist()
29+
assert rv.logp(2).eval() == pm.logp(rv, 2).eval()
30+
np.testing.assert_array_equal(
31+
rv.logp(np.arange(3)).eval(), pm.logp(rv, np.arange(3)).eval()
32+
)

0 commit comments

Comments
 (0)