Backtest._forecast#

Backtest._forecast(result, seed)[source]#

One origin’s forecast as (n_draws, H, k) draws or (H, k) points.

Applies the forecasting convention and normalizes what comes back. The predict hook is called when given; otherwise a result satisfying PredictiveResult is asked for forecast_paths(H, seed=seed) and any other for forecast(H). The raw array is then read by shape, in this order: a (H, k, 3) low/mean/high summary, the forecast() of the sampled families, keeps its middle column; an (H,) vector for a single series becomes (H, 1); an (H, k) array is a point forecast; an (S, H) array for a single series becomes (S, H, 1); and an (S, H, k) array is a set of draws.

Parameters:
  • result (object) – What the fitting function returned at this origin.

  • seed (int | None) – Passed to forecast_paths or the predict hook, so that each origin draws its own predictive shocks.

Return type:

NDArray[double]

Returns:

The normalized forecast; three-dimensional for draws, two-dimensional for points.

Raises:
  • SpecificationError – If no hook was given and the result offers neither forecast_paths nor a callable forecast.

  • DimensionError – If the forecast has none of the recognized shapes.

Example

>>> import numpy as np
>>> from cultivars.multivariate.reduced_form.vector_autoregression import VAR
>>> from cultivars.multivariate.large_dim.bayesian import BVAR
>>> y = np.random.default_rng(0).standard_normal((130, 2))
>>> harness = Backtest(y, lambda w: w, horizons=2, start=100)
>>> harness._forecast(VAR(y, order=1).fit(), None).shape
(2, 2)
>>> harness._forecast(BVAR(y, order=1).fit(n_draws=20, seed=0), 0).shape
(20, 2, 2)
>>> harness._forecast(object(), None)
Traceback (most recent call last):
    ...
cultivars.exceptions.SpecificationError: object offers neither forecast_paths() ...