MultivarLinearReg#

class causalpy.pymc_models.MultivarLinearReg[source]#

PyMC linear regression for multivariate outcomes (3D y) with covariance.

For use with InterruptedTimeSeries when multiple outcomes are specified (list of formulas). Accepts y with dims [“obs_ind”, “treated_units”, “outcomes”] and X with dims [“obs_ind”, “coeffs”]. Each (unit, outcome) has its own coefficient vector beta. Residuals across outcomes share a covariance matrix (LKJ prior), so you can estimate and interpret correlations between outcomes.

\[\begin{split}\beta &\sim \mathrm{Normal}(0, 50) \\ \Sigma &\sim \mathrm{LKJCholeskyCov}(\eta, n\_outcomes) \\ \mu_{i,u,o} &= X_i \cdot \beta_{u,o} \\ y_{i,u,\cdot} &\sim \mathrm{MvNormal}(\mu_{i,u,\cdot}, \Sigma)\end{split}\]

Methods

MultivarLinearReg.__init__([sample_kwargs, ...])

MultivarLinearReg.add_coord(name[, values, ...])

Register a dimension coordinate with the model.

MultivarLinearReg.add_coords(coords, *[, ...])

Vectorized version of Model.add_coord.

MultivarLinearReg.add_named_variable(var[, dims])

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

MultivarLinearReg.build_model(X, y, coords)

Define the PyMC model: regression + LKJ covariance between outcomes.

MultivarLinearReg.calculate_cumulative_impact(impact)

MultivarLinearReg.calculate_impact(y_true, ...)

Calculate the causal impact as the difference between observed and predicted values.

MultivarLinearReg.check_start_vals(start, ...)

Check that the logp is defined and finite at the starting point.

MultivarLinearReg.compile_d2logp([vars, ...])

Compiled log probability density hessian function.

MultivarLinearReg.compile_dlogp([vars, jacobian])

Compiled log probability density gradient function.

MultivarLinearReg.compile_fn(outs, *[, ...])

Compiles a PyTensor function.

MultivarLinearReg.compile_logp([vars, ...])

Compiled log probability density function.

MultivarLinearReg.copy()

Clone the model.

MultivarLinearReg.create_value_var(rv_var, ...)

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

MultivarLinearReg.d2logp([vars, jacobian, ...])

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

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

Debug model function at point.

MultivarLinearReg.dlogp([vars, jacobian])

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

MultivarLinearReg.eval_rv_shapes()

Evaluate shapes of untransformed AND transformed free variables.

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

Draw samples from posterior, prior predictive, and posterior predictive distributions.

MultivarLinearReg.get_context([...])

MultivarLinearReg.initial_point([random_seed])

Compute the initial point of the model.

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

Elemwise log-probability of the model.

MultivarLinearReg.logp_dlogp_function([...])

Compile a PyTensor function that computes logp and gradient.

MultivarLinearReg.make_obs_var(rv_var, data, ...)

Create a TensorVariable for an observed random variable.

MultivarLinearReg.name_for(name)

Check if name has prefix and adds if needed.

MultivarLinearReg.name_of(name)

Check if name has prefix and deletes if needed.

MultivarLinearReg.point_logps([point, ...])

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

MultivarLinearReg.predict(X[, coords, ...])

Predict data given input data X.

MultivarLinearReg.print_coefficients(labels)

Print regression coefficients per outcome; residual covariance is in posterior 'cov'.

MultivarLinearReg.priors_from_data(X, y)

Generate priors dynamically based on the input data.

MultivarLinearReg.profile(outs, *[, n, ...])

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

MultivarLinearReg.register_data_var(data[, dims])

Register a data variable with the model.

MultivarLinearReg.register_rv(rv_var, name, *)

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

MultivarLinearReg.replace_rvs_by_values(...)

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

MultivarLinearReg.score(X, y[, coords])

R² per (treated_unit, outcome) for multivariate y.

MultivarLinearReg.set_data(name, values[, ...])

Change the values of a data variable in the model.

MultivarLinearReg.set_dim(name, new_length)

Update a mutable dimension.

MultivarLinearReg.set_initval(rv_var, initval)

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

MultivarLinearReg.shape_from_dims(dims)

MultivarLinearReg.to_graphviz(*[, ...])

Produce a graphviz Digraph from a PyMC model.

Attributes

basic_RVs

List of random variables the model is defined in terms of.

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.

default_priors

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

supports_multivariate

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, priors=None)#
Parameters:
  • sample_kwargs (dict, optional) – Dictionary of kwargs that get unpacked and passed to the pymc.sample() function. Defaults to an empty dictionary if None.

  • priors (dict, optional) – Dictionary of priors for the model. Defaults to None, in which case default priors are used.

Return type:

None

classmethod __new__(*args, **kwargs)#