InstrumentalVariableRegression#

class causalpy.pymc_models.InstrumentalVariableRegression[source]#

Custom PyMC model for instrumental linear regression

Example

>>> import causalpy as cp
>>> import numpy as np
>>> from causalpy.pymc_models import InstrumentalVariableRegression
>>> N = 10
>>> e1 = np.random.normal(0, 3, N)
>>> e2 = np.random.normal(0, 1, N)
>>> Z = np.random.uniform(0, 1, N)
>>> ## Ensure the endogeneity of the the treatment variable
>>> X = -1 + 4 * Z + e2 + 2 * e1
>>> y = 2 + 3 * X + 3 * e1
>>> t = X.reshape(10,1)
>>> y = y.reshape(10,1)
>>> Z = np.asarray([[1, Z[i]] for i in range(0,10)])
>>> X = np.asarray([[1, X[i]] for i in range(0,10)])
>>> COORDS = {'instruments': ['Intercept', 'Z'], 'covariates': ['Intercept', 'X']}
>>> sample_kwargs = {
...     "tune": 5,
...     "draws": 10,
...     "chains": 2,
...     "cores": 2,
...     "target_accept": 0.95,
...     "progressbar": False,
... }
>>> iv_reg = InstrumentalVariableRegression(sample_kwargs=sample_kwargs)
>>> iv_reg.fit(X, Z,y, t, COORDS, {
...                  "mus": [[-2,4], [0.5, 3]],
...                  "sigmas": [1, 1],
...                  "eta": 2,
...                  "lkj_sd": 1,
...              }, None)
Inference data...

Methods

InstrumentalVariableRegression.__init__([...])

InstrumentalVariableRegression.add_coord(name)

Registers a dimension coordinate with the model.

InstrumentalVariableRegression.add_coords(...)

Vectorized version of Model.add_coord.

InstrumentalVariableRegression.add_named_variable(var)

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

InstrumentalVariableRegression.build_model(X, ...)

Specify model with treatment regression and focal regression data and priors

InstrumentalVariableRegression.calculate_cumulative_impact(impact)

InstrumentalVariableRegression.calculate_impact(...)

InstrumentalVariableRegression.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).

InstrumentalVariableRegression.compile_d2logp([...])

Compiled log probability density hessian function.

InstrumentalVariableRegression.compile_dlogp([...])

Compiled log probability density gradient function.

InstrumentalVariableRegression.compile_fn(outs, *)

Compiles an PyTensor function

InstrumentalVariableRegression.compile_logp([...])

Compiled log probability density function.

InstrumentalVariableRegression.create_value_var(...)

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

InstrumentalVariableRegression.d2logp([...])

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

InstrumentalVariableRegression.debug([...])

Debug model function at point.

InstrumentalVariableRegression.dlogp([vars, ...])

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

InstrumentalVariableRegression.eval_rv_shapes()

Evaluates shapes of untransformed AND transformed free variables.

InstrumentalVariableRegression.fit(X, Z, y, ...)

Draw samples from posterior distribution and potentially from the prior and posterior predictive distributions.

InstrumentalVariableRegression.initial_point([...])

Computes the initial point of the model.

InstrumentalVariableRegression.logp([vars, ...])

Elemwise log-probability of the model.

InstrumentalVariableRegression.logp_dlogp_function([...])

Compile an PyTensor function that computes logp and gradient.

InstrumentalVariableRegression.make_obs_var(...)

Create a TensorVariable for an observed random variable.

InstrumentalVariableRegression.name_for(name)

Checks if name has prefix and adds if needed

InstrumentalVariableRegression.name_of(name)

Checks if name has prefix and deletes if needed

InstrumentalVariableRegression.point_logps([...])

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

InstrumentalVariableRegression.predict(X)

Predict data given input data X

InstrumentalVariableRegression.print_coefficients(labels)

InstrumentalVariableRegression.profile(outs, *)

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

InstrumentalVariableRegression.register_data_var(data)

Register a data variable with the model.

InstrumentalVariableRegression.register_rv(...)

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

InstrumentalVariableRegression.replace_rvs_by_values(...)

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

InstrumentalVariableRegression.sample_predictive_distribution([...])

Function to sample the Multivariate Normal posterior predictive Likelihood term in the IV class.

InstrumentalVariableRegression.score(X, y)

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

InstrumentalVariableRegression.set_data(...)

Changes the values of a data variable in the model.

InstrumentalVariableRegression.set_dim(name, ...)

Update a mutable dimension.

InstrumentalVariableRegression.set_initval(...)

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

InstrumentalVariableRegression.shape_from_dims(dims)

InstrumentalVariableRegression.to_graphviz(*)

Produce a graphviz Digraph from a PyMC model.

InstrumentalVariableRegression.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)#
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)