pymc.logp#
- pymc.logp(rv,value,warn_rvs=True,**kwargs)[source]#
Create a graph for the log-probability of a random variable.
- Parameters:
- rv
TensorVariable - valuetensor_like
Should be the same type (shape and dtype) as the rv.
- warn_rvsbool, default
True Warn if RVs were found in the logp graph.This can happen when a variable has other other random variables as inputs.In that case, those random variables should be replaced by their respective values.pymc.logprob.conditional_logp can also be used as an alternative.
- rv
- Returns:
- logp
TensorVariable
- logp
- Raises:
RuntimeErrorIf the logp cannot be derived.
Examples
Create a compiled function that evaluates the logp of a variable
importpymcaspmimportpytensor.tensorasptmu=pt.scalar("mu")rv=pm.Normal.dist(mu,1.0)value=pt.scalar("value")rv_logp=pm.logp(rv,value)# Use .eval() for debuggingprint(rv_logp.eval({value:0.9,mu:0.0}))# -1.32393853# Compile a function for repeated evaluationsrv_logp_fn=pm.compile_pymc([value,mu],rv_logp)print(rv_logp_fn(value=0.9,mu=0.0))# -1.32393853
Derive the graph for a transformation of a RandomVariable
importpymcaspmimportpytensor.tensorasptmu=pt.scalar("mu")rv=pm.Normal.dist(mu,1.0)exp_rv=pt.exp(rv)value=pt.scalar("value")exp_rv_logp=pm.logp(exp_rv,value)# Use .eval() for debuggingprint(exp_rv_logp.eval({value:0.9,mu:0.0}))# -0.81912844# Compile a function for repeated evaluationsexp_rv_logp_fn=pm.compile_pymc([value,mu],exp_rv_logp)print(exp_rv_logp_fn(value=0.9,mu=0.0))# -0.81912844
Define a CustomDist logp
importpymcaspmimportpytensor.tensorasptdefnormal_logp(value,mu,sigma):returnpm.logp(pm.Normal.dist(mu,sigma),value)withpm.Model()asmodel:mu=pm.Normal("mu")sigma=pm.HalfNormal("sigma")pm.CustomDist("x",mu,sigma,logp=normal_logp)
