PyMCModel#

class causalpy.pymc_models.PyMCModel[source]#

A wraper class for PyMC models. This provides a scikit-learn like interface with methods like fit, predict, and score. It also provides other methods which are useful for causal inference.

Example

>>> import causalpy as cp
>>> import numpy as np
>>> import pymc as pm
>>> from causalpy.pymc_models import PyMCModel
>>> class MyToyModel(PyMCModel):
...     def build_model(self, X, y, coords):
...         with self:
...             X_ = pm.Data(name="X", value=X)
...             y_ = pm.Data(name="y", value=y)
...             beta = pm.Normal("beta", mu=0, sigma=1, shape=X_.shape[1])
...             sigma = pm.HalfNormal("sigma", sigma=1)
...             mu = pm.Deterministic("mu", pm.math.dot(X_, beta))
...             pm.Normal("y_hat", mu=mu, sigma=sigma, observed=y_)
>>> rng = np.random.default_rng(seed=42)
>>> X = rng.normal(loc=0, scale=1, size=(20, 2))
>>> y = rng.normal(loc=0, scale=1, size=(20,))
>>> model = MyToyModel(
...             sample_kwargs={
...                 "chains": 2,
...                 "draws": 2000,
...                 "progressbar": False,
...                 "random_seed": rng,
...             }
... )
>>> model.fit(X, y)
Inference data...
>>> X_new = rng.normal(loc=0, scale=1, size=(20,2))
>>> model.predict(X_new)
Inference data...
>>> model.score(X, y)
r2        0.390344
r2_std    0.081135
dtype: float64

Methods

PyMCModel.__init__([sample_kwargs])

PyMCModel.add_coord(name[, values, mutable, ...])

Registers a dimension coordinate with the model.

PyMCModel.add_coords(coords, *[, lengths])

Vectorized version of Model.add_coord.

PyMCModel.add_named_variable(var[, dims])

Add a random graph variable to the named variables of the model.

PyMCModel.build_model(X, y, coords)

Build the model, must be implemented by subclass.

PyMCModel.calculate_cumulative_impact(impact)

PyMCModel.calculate_impact(y_true, y_pred)

PyMCModel.check_start_vals(start)

Check that the starting values for MCMC do not cause the relevant log probability to evaluate to something invalid (e.g. Inf or NaN).

PyMCModel.compile_d2logp([vars, jacobian, ...])

Compiled log probability density hessian function.

PyMCModel.compile_dlogp([vars, jacobian])

Compiled log probability density gradient function.

PyMCModel.compile_fn(outs, *[, inputs, ...])

Compiles an PyTensor function

PyMCModel.compile_logp([vars, jacobian, sum])

Compiled log probability density function.

PyMCModel.create_value_var(rv_var, *, ...[, ...])

Create a TensorVariable that will be used as the random variable's "value" in log-likelihood graphs.

PyMCModel.d2logp([vars, jacobian, negate_output])

Hessian of the models log-probability w.r.t.

PyMCModel.debug([point, fn, verbose])

Debug model function at point.

PyMCModel.dlogp([vars, jacobian])

Gradient of the models log-probability w.r.t.

PyMCModel.eval_rv_shapes()

Evaluates shapes of untransformed AND transformed free variables.

PyMCModel.fit(X, y[, coords])

Draw samples from posterior, prior predictive, and posterior predictive distributions, placing them in the model's idata attribute.

PyMCModel.initial_point([random_seed])

Computes the initial point of the model.

PyMCModel.logp([vars, jacobian, sum])

Elemwise log-probability of the model.

PyMCModel.logp_dlogp_function([grad_vars, ...])

Compile an PyTensor function that computes logp and gradient.

PyMCModel.make_obs_var(rv_var, data, dims, ...)

Create a TensorVariable for an observed random variable.

PyMCModel.name_for(name)

Checks if name has prefix and adds if needed

PyMCModel.name_of(name)

Checks if name has prefix and deletes if needed

PyMCModel.point_logps([point, round_vals])

Computes the log probability of point for all random variables in the model.

PyMCModel.predict(X)

Predict data given input data X

PyMCModel.print_coefficients(labels[, round_to])

PyMCModel.profile(outs, *[, n, point, profile])

Compiles and profiles an PyTensor function which returns outs and takes values of model vars as a dict as an argument.

PyMCModel.register_data_var(data[, dims])

Register a data variable with the model.

PyMCModel.register_rv(rv_var, name, *[, ...])

Register an (un)observed random variable with the model.

PyMCModel.replace_rvs_by_values(graphs, **kwargs)

Clone and replace random variables in graphs with their value variables.

PyMCModel.score(X, y)

Score the Bayesian \(R^2\) given inputs X and outputs y.

PyMCModel.set_data(name, values[, coords])

Changes the values of a data variable in the model.

PyMCModel.set_dim(name, new_length[, ...])

Update a mutable dimension.

PyMCModel.set_initval(rv_var, initval)

Sets an initial value (strategy) for a random variable.

PyMCModel.shape_from_dims(dims)

PyMCModel.to_graphviz(*[, var_names, ...])

Produce a graphviz Digraph from a PyMC model.

PyMCModel.update_start_vals(a, b)

Update point a with b, without overwriting existing keys.

Attributes

basic_RVs

List of random variables the model is defined in terms of (which excludes deterministics).

continuous_value_vars

All the continuous value variables in the model

coords

Coordinate values for model dimensions.

datalogp

PyTensor scalar of log-probability of the observed variables and potential terms

dim_lengths

The symbolic lengths of dimensions in the model.

discrete_value_vars

All the discrete value variables in the model

isroot

observedlogp

PyTensor scalar of log-probability of the observed variables

parent

potentiallogp

PyTensor scalar of log-probability of the Potential terms

prefix

root

unobserved_RVs

List of all random variables, including deterministic ones.

unobserved_value_vars

List of all random variables (including untransformed projections), as well as deterministics used as inputs and outputs of the model's log-likelihood graph

value_vars

List of unobserved random variables used as inputs to the model's log-likelihood (which excludes deterministics).

varlogp

PyTensor scalar of log-probability of the unobserved random variables (excluding deterministic).

varlogp_nojac

PyTensor scalar of log-probability of the unobserved random variables (excluding deterministic) without jacobian term.

__init__(sample_kwargs=None)[source]#
Parameters:

sample_kwargs (Optional[Dict[str, Any]]) – A dictionary of kwargs that get unpacked and passed to the pymc.sample() function. Defaults to an empty dictionary.

static __new__(cls, *args, model=UNSET, **kwargs)#
Parameters:

model (Literal[UNSET] | None | ~pymc.model.core.Model)