pymc.model.core.Deterministic#
- pymc.model.core.Deterministic(name, var, model=None, dims=None)[source]#
Create a named deterministic variable.
Wrapping an expression in
Deterministicis optional: ordinary symbolic expressions already participate in the model. Use it to store an intermediate result in the inference data, or to register an expression as a named target forpymc.do()orpymc.observe().Deterministic nodes do not add randomness to the model; their values are determined by their inputs.
- Parameters:
- name
str Name of the deterministic variable to be registered in the model.
- vartensor_like
Expression for the calculation of the variable.
- model
Model, optional The model object to which the Deterministic variable is added. If
Noneis provided, the current model in the context stack is used.- dims
strortupleofstr, optional Dimension names for the variable.
- name
- Returns:
- vartensor_like
The registered, named variable wrapped in Deterministic.
Notes
Even though adding a Deterministic node forces PyMC to compute this expression, which could have been optimized away otherwise, this doesn’t come with a performance cost. Indeed, Deterministic nodes are computed outside the main computation graph, which can be optimized as though there was no Deterministic nodes. Whereas the optimized graph can be evaluated thousands of times during a NUTS step, the Deterministic quantities are just computed once at the end of the step, with the final values of the other random variables.
Examples
Indeed, PyMC allows for arbitrary combinations of random variables, for example in the case of a logistic regression
with pm.Model(): alpha = pm.Normal("alpha", 0, 1) intercept = pm.Normal("intercept", 0, 1) p = pm.math.invlogit(alpha * x + intercept) outcome = pm.Bernoulli("outcome", p, observed=outcomes)
Here
pis a local Python name for an expression, not a named model variable. To record its value in the sampling trace, register it withDeterministic:with pm.Model(): alpha = pm.Normal("alpha", 0, 1) intercept = pm.Normal("intercept", 0, 1) p = pm.Deterministic("p", pm.math.invlogit(alpha * x + intercept)) outcome = pm.Bernoulli("outcome", p, observed=outcomes)
These two models are strictly equivalent from a mathematical point of view. However, in the first case, the inference data will only contain values for the variables
alpha,interceptandoutcome. In the second, it will also contain sampled values ofpfor each of the observed points.