pymc.logcdf#

pymc.logcdf(rv, value, warn_rvs=None, **kwargs)[source]#

Create a graph for the log-CDF of a random variable.

Parameters
rvTensorVariable
valuetensor_like

Should be the same type (shape and dtype) as the rv.

warn_rvsbool, default True

Warn if RVs were found in the logcdf graph. This can happen when a variable has other random variables as inputs. In that case, those random variables should be replaced by their respective values.

Returns
logpTensorVariable
Raises
RuntimeError

If the logcdf cannot be derived.

Examples

Create a compiled function that evaluates the logcdf of a variable

import pymc as pm
import pytensor.tensor as pt

mu = pt.scalar("mu")
rv = pm.Normal.dist(mu, 1.0)

value = pt.scalar("value")
rv_logcdf = pm.logcdf(rv, value)

# Use .eval() for debugging
print(rv_logcdf.eval({value: 0.9, mu: 0.0}))  # -0.2034146

# Compile a function for repeated evaluations
rv_logcdf_fn = pm.compile_pymc([value, mu], rv_logcdf)
print(rv_logcdf_fn(value=0.9, mu=0.0))  # -0.2034146

Derive the graph for a transformation of a RandomVariable

import pymc as pm
import pytensor.tensor as pt

mu = pt.scalar("mu")
rv = pm.Normal.dist(mu, 1.0)
exp_rv = pt.exp(rv)

value = pt.scalar("value")
exp_rv_logcdf = pm.logcdf(exp_rv, value)

# Use .eval() for debugging
print(exp_rv_logcdf.eval({value: 0.9, mu: 0.0}))  # -0.78078813

# Compile a function for repeated evaluations
exp_rv_logcdf_fn = pm.compile_pymc([value, mu], exp_rv_logcdf)
print(exp_rv_logcdf_fn(value=0.9, mu=0.0))  # -0.78078813

Define a CustomDist logcdf

import pymc as pm
import pytensor.tensor as pt

def normal_logcdf(value, mu, sigma):
    return pm.logp(pm.Normal.dist(mu, sigma), value)

with pm.Model() as model:
    mu = pm.Normal("mu")
    sigma = pm.HalfNormal("sigma")
    pm.CustomDist("x", mu, sigma, logcdf=normal_logcdf)