LinearChangePointDetection#
- class causalpy.pymc_models.LinearChangePointDetection[source]#
Custom PyMC model to estimate one ChangePoint in time series.
This model implements three types of changepoints: level shift, trend change, and impulse response. While the underlying mathematical framework could theoretically be applied to other changepoint detection problems, it has been specifically designed and tested for use in interrupted time series causal inference settings.
In words: - beta represents the regression coefficients for the baseline signal μ. - tau is the changepoint time, and w is a sigmoid function to smooth the transition. - level and trend model a linear shift after the changepoint. - A and lambda define an impulse response at the changepoint. - mu_in combines the level, trend, and impulse contributions. - mu_ts is the total mean, including baseline and intervention. - sigma is the observation noise. - Finally, y is drawn from a Normal distribution with mean mu_ts and standard deviation sigma.
\[\begin{split}\beta &\sim \mathrm{Normal}(0, 5) \\ \mu &= \beta \cdot X\\ \\ \tau &\sim \mathrm{Uniform}(\text{lower_bound}, \text{upper_bound}) \\ w &= sigmoid(t-\tau) \\ \\ \text{level} &\sim \mathrm{Normal}(0, 5) \\ \text{trend} &\sim \mathrm{Normal}(0, 0.5) \\ A &\sim \mathrm{Normal}(0, 5) \\ \lambda &\sim \mathrm{HalfNormal}(0, 5) \\ \text{impulse} &= A \cdot exp(-\lambda \cdot |t-\tau|) \\ \mu_{in} &= \text{level} + \text{trend} \cdot (t-\tau) + \text{impulse}\\ \\ \sigma &\sim \mathrm{HalfNormal}(0, 1) \\ \mu_{ts} &= \mu + \mu_{in} \\ \\ y &\sim \mathrm{Normal}(\mu_{ts}, \sigma)\end{split}\]- Example
>>> import causalpy as cp >>> import numpy as np >>> from patsy import build_design_matrices, dmatrices >>> from causalpy.pymc_models import LinearChangePointDetection >>> data = cp.load_data("its") >>> formula="y ~ 1 + t + C(month)" >>> y, X = dmatrices(formula, data) >>> outcome_variable_name = y.design_info.column_names[0] >>> labels = X.design_info.column_names >>> _y, _X = np.asarray(y), np.asarray(X) >>> _X = xr.DataArray( ... _X, ... dims=["obs_ind", "coeffs"], ... coords={ ... "obs_ind": data.index, ... "coeffs": labels, ... }, ... ) >>> _y = xr.DataArray( ... _y, ... dims=["obs_ind", "treated_units"], ... coords={ ... "obs_ind": data.index, ... "treated_units": ["unit_0"] ... }, ... ) >>> COORDS = { ... "coeffs": labels, ... "obs_ind": np.arange(X.shape[0]), ... "treated_units": ["unit_0"], ... } >>> model = LinearChangePointDetection(cp_effect_type="level", sample_kwargs={"draws" : 10, "tune":10, "progressbar":False}) >>> model.set_time_range(None, data) >>> model.fit(X=_X, y=_y, coords=COORDS) Inference ...
Methods
LinearChangePointDetection.__init__(...[, ...])Initializes the InterventionTimeEstimator model.
LinearChangePointDetection.add_coord(name[, ...])Register a dimension coordinate with the model.
LinearChangePointDetection.add_coords(coords, *)Vectorized version of
Model.add_coord.Add a random graph variable to the named variables of the model.
LinearChangePointDetection.build_model(X, y, ...)Defines the PyMC model
LinearChangePointDetection.calculate_cumulative_impact(impact)Check that the logp is defined and finite at the starting point.
Compiled log probability density hessian function.
Compiled log probability density gradient function.
Compiles a PyTensor function.
Compiled log probability density function.
Clone the model.
Create a
TensorVariablethat will be used as the random variable's "value" in log-likelihood graphs.LinearChangePointDetection.d2logp([vars, ...])Hessian of the models log-probability w.r.t.
LinearChangePointDetection.debug([point, ...])Debug model function at point.
LinearChangePointDetection.dlogp([vars, ...])Gradient of the models log-probability w.r.t.
Evaluate shapes of untransformed AND transformed free variables.
LinearChangePointDetection.fit(X, y[, coords])Draw samples from posterior, prior predictive, and posterior predictive distributions, placing them in the model's idata attribute.
Compute the initial point of the model.
LinearChangePointDetection.logp([vars, ...])Elemwise log-probability of the model.
Compile a PyTensor function that computes logp and gradient.
Create a TensorVariable for an observed random variable.
Check if name has prefix and adds if needed.
Check if name has prefix and deletes if needed.
Compute the log probability of point for all random variables in the model.
Predict data given input data X
Generate priors dynamically based on the input data.
LinearChangePointDetection.profile(outs, *)Compile and profile a PyTensor function which returns
outsand 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.
Score the Bayesian \(R^2\) given inputs
Xand outputsy.LinearChangePointDetection.set_data(name, values)Change the values of a data variable in the model.
LinearChangePointDetection.set_dim(name, ...)Update a mutable dimension.
Set an initial value (strategy) for a random variable.
Set time_range.
LinearChangePointDetection.to_graphviz(*[, ...])Produce a graphviz Digraph from a PyMC model.
Attributes
basic_RVsList of random variables the model is defined in terms of.
continuous_value_varsAll the continuous value variables in the model.
coordsCoordinate values for model dimensions.
datalogpPyTensor scalar of log-probability of the observed variables and potential terms.
default_priorsdim_lengthsThe symbolic lengths of dimensions in the model.
discrete_value_varsAll the discrete value variables in the model.
isrootobservedlogpPyTensor scalar of log-probability of the observed variables.
parentpotentiallogpPyTensor scalar of log-probability of the Potential terms.
prefixrootunobserved_RVsList of all random variables, including deterministic ones.
unobserved_value_varsList of all random variables (including untransformed projections), as well as deterministics used as inputs and outputs of the model's log-likelihood graph.
value_varsList of unobserved random variables used as inputs to the model's log-likelihood (which excludes deterministics).
varlogpPyTensor scalar of log-probability of the unobserved random variables (excluding deterministic).
varlogp_nojacPyTensor scalar of log-probability of the unobserved random variables (excluding deterministic) without jacobian term.
- __init__(cp_effect_type, cp_effect_param=None, sample_kwargs=None)[source]#
Initializes the InterventionTimeEstimator model.
- Parameters:
cp_effect_type (
str|list[str]) –Optional dictionary that specifies prior parameters for the intervention effects. Expected keys are:
”level”: [mu, sigma]
”trend”: [mu, sigma]
”impulse”: [mu, sigma1, sigma2]
If a key is missing, the corresponding effect is ignored. If the associated list is incomplete, default values will be used.
sample_kwargs – Optional dictionary of arguments passed to pm.sample().
- classmethod __new__(*args, **kwargs)#