BayesianBasisExpansionTimeSeries#
- class causalpy.pymc_models.BayesianBasisExpansionTimeSeries[source]#
Bayesian Structural Time Series Model.
This model allows for the inclusion of trend, seasonality (via Fourier series), and optional exogenous regressors.
\[\begin{split}\text{trend} &\sim \text{LinearTrend}(...) \\ \text{seasonality} &\sim \text{YearlyFourier}(...) \\ \beta &\sim \mathrm{Normal}(0, \sigma_{\beta}) \quad \text{(if X is provided)} \\ \sigma &\sim \mathrm{HalfNormal}(\sigma_{err}) \\ \mu &= \text{trend_component} + \text{seasonality_component} + X \cdot \beta \quad \text{(if X is provided)} \\ y &\sim \mathrm{Normal}(\mu, \sigma)\end{split}\]- Parameters:
n_order (int, optional) – The number of Fourier components for the yearly seasonality. Defaults to 3. Only used if seasonality_component is None.
n_changepoints_trend (int, optional) – The number of changepoints for the linear trend component. Defaults to 10. Only used if trend_component is None.
prior_sigma (float, optional) – Prior standard deviation for the observation noise. Defaults to 5.
trend_component (Optional[Any], optional) – A custom trend component model. If None, the default pymc-marketing LinearTrend component is used. Must have an apply(time_data) method that returns a PyMC tensor.
seasonality_component (Optional[Any], optional) – A custom seasonality component model. If None, the default pymc-marketing YearlyFourier component is used. Must have an apply(time_data) method that returns a PyMC tensor.
sample_kwargs (dict, optional) – A dictionary of kwargs that get unpacked and passed to the
pymc.sample()
function. Defaults to an empty dictionary.
Methods
Register a dimension coordinate with the model.
Vectorized version of
Model.add_coord
.Add a random graph variable to the named variables of the model.
Defines the PyMC model.
BayesianBasisExpansionTimeSeries.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
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.
Gradient of the models log-probability w.r.t.
Evaluate shapes of untransformed AND transformed free variables.
BayesianBasisExpansionTimeSeries.fit
(X, y, ...)Draw samples from posterior, prior predictive, and posterior predictive distributions, placing them in the model's idata attribute. :type X:
Optional
[ndarray
] :param X: NumPy array of exogenous regressors. Can be None or an array with 0 columns if no exogenous variables. :type X: np.ndarray or None :type y:ndarray
:param y: The target variable. :type y: np.ndarray :type coords:Dict
[str
,Any
] :param coords: Coordinates dictionary. Must contain "datetime_index" (pd.DatetimeIndex). If X is provided and has columns, coords must also contain "coeffs" (List[str]). :type coords: dict.Compute the initial point of the model.
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 X and coords for prediction period.
Compile and profile a 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.
BayesianBasisExpansionTimeSeries.score
(X, y, ...)Score the Bayesian R2.
Change the values of a data variable in the model.
Update a mutable dimension.
Set an initial value (strategy) for a random variable.
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.
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__(n_order=3, n_changepoints_trend=10, prior_sigma=5, trend_component=None, seasonality_component=None, sample_kwargs=None)[source]#
- classmethod __new__(*args, **kwargs)#