You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/source/guides/Probability_Distributions.rst
+24-13
Original file line number
Diff line number
Diff line change
@@ -29,21 +29,32 @@ A variable requires at least a ``name`` argument, and zero or more model paramet
29
29
30
30
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.
31
31
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:
33
38
34
39
::
35
40
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)
37
49
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
+
::
41
53
42
-
def logp(self, value):
43
-
...
44
-
return total_log_prob
54
+
def custom_logp(value, mu):
55
+
return -0.5 * (value - mu)**2
45
56
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.
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.
62
73
63
74
For the exponential survival function, this is:
64
75
@@ -67,7 +78,7 @@ For the exponential survival function, this is:
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.
73
84
@@ -98,10 +109,10 @@ This allows for probabilities to be calculated and random numbers to be drawn.
0 commit comments