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

BayesianBasisExpansionTimeSeries.__init__([...])

BayesianBasisExpansionTimeSeries.add_coord(name)

Register a dimension coordinate with the model.

BayesianBasisExpansionTimeSeries.add_coords(...)

Vectorized version of Model.add_coord.

BayesianBasisExpansionTimeSeries.add_named_variable(var)

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

BayesianBasisExpansionTimeSeries.build_model(X, ...)

Defines the PyMC model.

BayesianBasisExpansionTimeSeries.calculate_cumulative_impact(impact)

BayesianBasisExpansionTimeSeries.calculate_impact(...)

BayesianBasisExpansionTimeSeries.check_start_vals(...)

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

BayesianBasisExpansionTimeSeries.compile_d2logp([...])

Compiled log probability density hessian function.

BayesianBasisExpansionTimeSeries.compile_dlogp([...])

Compiled log probability density gradient function.

BayesianBasisExpansionTimeSeries.compile_fn(outs, *)

Compiles a PyTensor function.

BayesianBasisExpansionTimeSeries.compile_logp([...])

Compiled log probability density function.

BayesianBasisExpansionTimeSeries.copy()

Clone the model.

BayesianBasisExpansionTimeSeries.create_value_var(...)

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

BayesianBasisExpansionTimeSeries.d2logp([...])

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

BayesianBasisExpansionTimeSeries.debug([...])

Debug model function at point.

BayesianBasisExpansionTimeSeries.dlogp([...])

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

BayesianBasisExpansionTimeSeries.eval_rv_shapes()

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.

BayesianBasisExpansionTimeSeries.get_context([...])

BayesianBasisExpansionTimeSeries.initial_point([...])

Compute the initial point of the model.

BayesianBasisExpansionTimeSeries.logp([...])

Elemwise log-probability of the model.

BayesianBasisExpansionTimeSeries.logp_dlogp_function([...])

Compile a PyTensor function that computes logp and gradient.

BayesianBasisExpansionTimeSeries.make_obs_var(...)

Create a TensorVariable for an observed random variable.

BayesianBasisExpansionTimeSeries.name_for(name)

Check if name has prefix and adds if needed.

BayesianBasisExpansionTimeSeries.name_of(name)

Check if name has prefix and deletes if needed.

BayesianBasisExpansionTimeSeries.point_logps([...])

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

BayesianBasisExpansionTimeSeries.predict(X, ...)

Predict data given input X and coords for prediction period.

BayesianBasisExpansionTimeSeries.print_coefficients(labels)

BayesianBasisExpansionTimeSeries.profile(outs, *)

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

BayesianBasisExpansionTimeSeries.register_data_var(data)

Register a data variable with the model.

BayesianBasisExpansionTimeSeries.register_rv(...)

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

BayesianBasisExpansionTimeSeries.replace_rvs_by_values(...)

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

BayesianBasisExpansionTimeSeries.score(X, y, ...)

Score the Bayesian R2.

BayesianBasisExpansionTimeSeries.set_data(...)

Change the values of a data variable in the model.

BayesianBasisExpansionTimeSeries.set_dim(...)

Update a mutable dimension.

BayesianBasisExpansionTimeSeries.set_initval(...)

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

BayesianBasisExpansionTimeSeries.shape_from_dims(dims)

BayesianBasisExpansionTimeSeries.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.

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]#
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.

  • n_order (int)

  • n_changepoints_trend (int)

  • prior_sigma (float)

  • trend_component (Any | None)

  • seasonality_component (Any | None)

classmethod __new__(*args, **kwargs)#