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: pymc/model.py
+50-33Lines changed: 50 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -2199,15 +2199,14 @@ def Deterministic(name, var, model=None, dims=None):
2199
2199
returnvar
2200
2200
2201
2201
2202
-
defPotential(name, var, model=None, dims=None):
2203
-
"""
2204
-
Add an arbitrary factor potential to the model likelihood
2205
-
2206
-
The Potential function is used to add arbitrary factors (such as constraints or other likelihood components) to adjust the probability density of the model.
"""Add an arbitrary term to the model log-probability.
2207
2204
2208
2205
Warnings
2209
2206
--------
2210
-
Potential functions only influence logp-based sampling. Therefore, they are applicable for sampling with ``pm.sample`` but not ``pm.sample_prior_predictive`` or ``pm.sample_posterior_predictive``.
2207
+
Potential terms only influence probability-based sampling, such as ``pm.sample``, but not forward sampling like
2208
+
``pm.sample_prior_predictive`` or ``pm.sample_posterior_predictive``. A warning is raised when doing forward
2209
+
sampling with models containing Potential terms.
2211
2210
2212
2211
Parameters
2213
2212
----------
@@ -2228,62 +2227,80 @@ def Potential(name, var, model=None, dims=None):
2228
2227
2229
2228
Examples
2230
2229
--------
2231
-
Have a look at the following example:
2232
-
2233
-
In this example, we define a constraint on ``x`` to be greater or equal to 0 via the ``pm.Potential`` function.
2234
-
We pass ``pm.math.log(pm.math.switch(constraint, 1, 0))`` as second argument which will return an expression depending on if the constraint is met or not and which will be added to the likelihood of the model.
2235
-
The probablity density that this model produces agrees strongly with the constraint that ``x`` should be greater than or equal to 0. All the cases who do not satisfy the constraint are strictly not considered.
2230
+
In this example, we define a constraint on ``x`` to be greater or equal to 0.
2231
+
The statement ``pm.math.log(pm.math.switch(constraint, 0, 1))`` adds either 0 or -inf to the model logp,
2232
+
depending on whether the constraint is met. During sampling, any proposals where ``x`` is negative will be rejected.
However, if we use ``pm.math.log(pm.math.switch(constraint, 1.0, 0.5))`` the potential again penalizes the likelihood when constraint is not met but with some deviations allowed.
2246
-
Here, Potential function is used to pass a soft constraint.
2247
-
A soft constraint is a constraint that is only partially satisfied.
2248
-
The effect of this is that the posterior probability for the parameters decreases as they move away from the constraint, but does not become exactly zero.
2249
-
This allows the sampler to generate values that violate the constraint, but with lower probability.
2244
+
2245
+
Instead, with a soft constraint like ``pm.math.log(pm.math.switch(constraint, 1, 0.5))``,
2246
+
the sampler will be less likely, but not forbidden, from accepting negative values for `x`.
In this example, Potential is used to obtain an arbitrary prior.
2260
-
This prior distribution refers to the prior knowledge that the values of ``max_items`` are likely to be small rather than being large.
2261
-
The prior probability of ``max_items`` is defined using a Potential object with the log of the inverse of ``max_items`` as its value.
2262
-
This means that larger values of ``max_items`` have a lower prior probability density, while smaller values of ``max_items`` have a higher prior probability density.
2263
-
When the model is sampled, the posterior distribution of ``max_items`` given the observed value of ``n_items`` will be influenced by the power-law prior defined in the Potential object
2258
+
A Potential term can depend on multiple variables.
2259
+
In the following example, the ``soft_sum_constraint`` potential encourages ``x`` and ``y`` to have a small sum.
2260
+
The more the sum deviates from zero, the more negative the penalty value of ``(-((x + y)**2))``.
In the next example, the ``soft_sum_constraint`` potential encourages ``x`` and ``y`` to have a small sum, effectively adding a soft constraint on the relationship between the two variables.
2275
-
This can be useful in cases where you want to ensure that the sum of multiple variables stays within a certain range, without enforcing an exact value.
2276
-
In this case, the larger the deviation, larger will be the negative value (-((x + y)**2)) which the MCMC sampler will attempt to minimize.
2277
-
However, the sampler might generate values for some small deviations but with lower probability hence this is a soft constraint.
2286
+
A Potential can be used to define a specific likelihood term.
2287
+
In the following example, a normal likelihood term is added to fixed data.
2288
+
The same result would be obtained by using an observed `Normal` variable.
The potential value is incorporated into the model log-probability, so it should be -inf (or very negative) when a constraint is violated, so that those draws are rejected. 0 won't have any effect and positive values will make the proposals more likely to be accepted.
0 commit comments