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
Registers a dimension coordinate with the model.
Vectorized version of
Model.add_coord
.Add a random graph variable to the named variables of the model.
Specify model with treatment regression and focal regression data and priors
InstrumentalVariableRegression.calculate_cumulative_impact
(impact)Check that the starting values for MCMC do not cause the relevant log probability to evaluate to something invalid (e.g. Inf or NaN).
Compiled log probability density hessian function.
Compiled log probability density gradient function.
Compiles an PyTensor function
Compiled log probability density function.
Create a
TensorVariable
that will be used as the random variable's "value" in log-likelihood graphs.Hessian of the models log-probability w.r.t.
Debug model function at point.
InstrumentalVariableRegression.dlogp
([vars, ...])Gradient of the models log-probability w.r.t.
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.
Computes the initial point of the model.
InstrumentalVariableRegression.logp
([vars, ...])Elemwise log-probability of the model.
Compile an PyTensor function that computes logp and gradient.
Create a TensorVariable for an observed random variable.
Checks if name has prefix and adds if needed
Checks if name has prefix and deletes if needed
Computes the log probability of point for all random variables in the model.
Predict data given input data X
Compiles and profiles an PyTensor function which returns
outs
and takes values of model vars as a dict as an argument.Register a data variable with the model.
Register an (un)observed random variable with the model.
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.
Score the Bayesian \(R^2\) given inputs
X
and outputsy
.Changes the values of a data variable in the model.
InstrumentalVariableRegression.set_dim
(name, ...)Update a mutable dimension.
Sets an initial value (strategy) for a random variable.
Produce a graphviz Digraph from a PyMC model.
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.