ARResult.simulate#

ARResult.simulate(n=200, *, seed=None, burn=_SIMULATION_BURN)[source]#

A fresh sample path at the fitted parameters.

burn + n periods of the zero-mean autoregression are generated from a zero start with Gaussian innovations of variance sigma2 and the first burn discarded. The constant and trend are regressors inside the recursion, \(y_t = c + b t + \sum_i \phi_i y_{t-i} + \varepsilon_t\), so the mean path they imply is what is added,

\[A + B t,\qquad B = \frac{b}{1 - \sum_i \phi_i},\qquad A = \frac{c - B \sum_i i\,\phi_i}{1 - \sum_i \phi_i},\]

with the time index running 1 .. n.

Parameters:
  • n (int) – Observations kept, at least 1.

  • seed (int | Generator | None) – Seed or generator.

  • burn (int) – Periods discarded from the start, non-negative.

Return type:

NDArray[double]

Returns:

An array of shape (n,).

Raises:

SpecificationError – If the counts are not usable, or the autoregressive coefficients sum to one so that no mean path exists.

Example

The simulated mean and variance are the fitted model’s unconditional moments:

>>> rng = np.random.default_rng(0)
>>> y = np.zeros(500)
>>> for t in range(1, 500):
...     y[t] = 1.0 + 0.5 * y[t - 1] + rng.standard_normal()
>>> res = AR(y, order=1, trend="c").fit()
>>> path = res.simulate(20000, seed=0)
>>> mean = res.const / (1 - res.ar_params[0])
>>> variance = res.sigma2 / (1 - res.ar_params[0] ** 2)
>>> bool(abs(path.mean() - mean) < 0.05), bool(abs(path.var() - variance) < 0.05)
(True, True)
>>> res.simulate(0)
Traceback (most recent call last):
    ...
cultivars.exceptions.SpecificationError: n must be at least 1; got 0.