Skip to content

Commit 89561ec

Browse files
authored
Merge branch 'main' into data-as-observed
2 parents 77f7384 + ce5f2a2 commit 89561ec

25 files changed

+670
-483
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ repos:
4949
- --exclude=versioneer.py
5050
- --last-year-present
5151
- repo: https://github.com/astral-sh/ruff-pre-commit
52-
rev: v0.9.2
52+
rev: v0.9.6
5353
hooks:
5454
- id: ruff
5555
args: [--fix, --show-fixes]

conda-envs/environment-dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies:
1313
- numpy>=1.25.0
1414
- pandas>=0.24.0
1515
- pip
16-
- pytensor>=2.26.2,<2.27
16+
- pytensor>=2.26.2,<2.28
1717
- python-graphviz
1818
- networkx
1919
- scipy>=1.4.1

conda-envs/environment-docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dependencies:
1111
- numpy>=1.25.0
1212
- pandas>=0.24.0
1313
- pip
14-
- pytensor>=2.26.2,<2.27
14+
- pytensor>=2.26.2,<2.28
1515
- python-graphviz
1616
- rich>=13.7.1
1717
- scipy>=1.4.1

conda-envs/environment-jax.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ dependencies:
2121
- numpyro>=0.8.0
2222
- pandas>=0.24.0
2323
- pip
24-
- pytensor>=2.26.2,<2.27
24+
- pytensor>=2.26.2,<2.28
2525
- python-graphviz
2626
- networkx
2727
- rich>=13.7.1

conda-envs/environment-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ dependencies:
1414
- numpy>=1.25.0
1515
- pandas>=0.24.0
1616
- pip
17-
- pytensor>=2.26.2,<2.27
17+
- pytensor>=2.26.2,<2.28
1818
- python-graphviz
1919
- networkx
2020
- rich>=13.7.1

conda-envs/windows-environment-dev.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies:
1313
- numpy>=1.25.0
1414
- pandas>=0.24.0
1515
- pip
16-
- pytensor>=2.26.2,<2.27
16+
- pytensor>=2.26.2,<2.28
1717
- python-graphviz
1818
- networkx
1919
- rich>=13.7.1

conda-envs/windows-environment-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dependencies:
1616
- numpy>=1.25.0
1717
- pandas>=0.24.0
1818
- pip
19-
- pytensor>=2.26.2,<2.27
19+
- pytensor>=2.26.2,<2.28
2020
- python-graphviz
2121
- networkx
2222
- rich>=13.7.1

docs/source/api/data.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ Data
1010
MutableData
1111
get_data
1212
Data
13-
GeneratorAdapter
1413
Minibatch

docs/source/guides/Probability_Distributions.rst

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,32 @@ A variable requires at least a ``name`` argument, and zero or more model paramet
2929

3030
Probability distributions are all subclasses of ``Distribution``, which in turn has two major subclasses: ``Discrete`` and ``Continuous``. In terms of data types, a ``Continuous`` random variable is given whichever floating point type is defined by ``pytensor.config.floatX``, while ``Discrete`` variables are given ``int16`` types when ``pytensor.config.floatX`` is ``float32``, and ``int64`` otherwise.
3131

32-
All distributions in ``pm.distributions`` will have two important methods: ``random()`` and ``logp()`` with the following signatures:
32+
All distributions in ``pm.distributions`` are associated with two key functions:
33+
34+
1. ``logp(dist, value)`` - Calculates log-probability at given value
35+
2. ``draw(dist, size=...)`` - Generates random samples
36+
37+
For example, with a normal distribution:
3338

3439
::
3540

36-
class SomeDistribution(Continuous):
41+
with pm.Model():
42+
x = pm.Normal('x', mu=0, sigma=1)
43+
44+
# Calculate log-probability
45+
log_prob = pm.logp(x, 0.5)
46+
47+
# Generate samples
48+
samples = pm.draw(x, size=100)
3749

38-
def random(self, point=None, size=None):
39-
...
40-
return random_samples
50+
Custom distributions using ``CustomDist`` should provide logp via the ``dist`` parameter:
51+
52+
::
4153

42-
def logp(self, value):
43-
...
44-
return total_log_prob
54+
def custom_logp(value, mu):
55+
return -0.5 * (value - mu)**2
4556

46-
PyMC expects the ``logp()`` method to return a log-probability evaluated at the passed ``value`` argument. This method is used internally by all of the inference methods to calculate the model log-probability that is used for fitting models. The ``random()`` method is used to simulate values from the variable, and is used internally for posterior predictive checks.
57+
custom_dist = pm.CustomDist('custom', dist=custom_logp, mu=0)
4758

4859

4960
Custom distributions
@@ -58,7 +69,7 @@ An exponential survival function, where :math:`c=0` denotes failure (or non-surv
5869
f(c, t) = \left\{ \begin{array}{l} \exp(-\lambda t), \text{if c=1} \\
5970
\lambda \exp(-\lambda t), \text{if c=0} \end{array} \right.
6071
61-
Such a function can be implemented as a PyMC distribution by writing a function that specifies the log-probability, then passing that function as a keyword argument to the ``DensityDist`` function, which creates an instance of a PyMC distribution with the custom function as its log-probability.
72+
Such a function can be implemented as a PyMC distribution by writing a function that specifies the log-probability, then passing that function as a keyword argument to the ``CustomDist`` function, which creates an instance of a PyMC distribution with the custom function as its log-probability.
6273

6374
For the exponential survival function, this is:
6475

@@ -67,7 +78,7 @@ For the exponential survival function, this is:
6778
def logp(value, t, lam):
6879
return (value * log(lam) - lam * t).sum()
6980

70-
exp_surv = pm.DensityDist('exp_surv', t, lam, logp=logp, observed=failure)
81+
exp_surv = pm.CustomDist('exp_surv', dist=logp, t=t, lam=lam, observed=failure)
7182

7283
Similarly, if a random number generator is required, a function returning random numbers corresponding to the probability distribution can be passed as the ``random`` argument.
7384

@@ -98,10 +109,10 @@ This allows for probabilities to be calculated and random numbers to be drawn.
98109

99110
::
100111

101-
>>> y.logp(4).eval()
112+
>>> pm.logp(y, 4).eval()
102113
array(-1.5843639373779297, dtype=float32)
103114

104-
>>> y.random(size=3)
115+
>>> pm.draw(y, size=3)
105116
array([5, 4, 3])
106117

107118

pymc/backends/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
RunType: TypeAlias = Run
8888
HAS_MCB = True
8989
except ImportError:
90-
TraceOrBackend = BaseTrace # type: ignore[misc]
90+
TraceOrBackend = BaseTrace # type: ignore[assignment, misc]
9191
RunType = type(None) # type: ignore[assignment, misc]
9292

9393

0 commit comments

Comments
 (0)