Before you start
Fit an autoregressive model AR(p) by least squares and use it to forecast.
An AR(p) model predicts each value from the p values before it:
x_t = c + phi_1 * x_{t-1} + phi_2 * x_{t-2} + ... + phi_p * x_{t-p}
Fitting it is ordinary linear regression. For every t from p to n - 1,
make one row [1, x_{t-1}, ..., x_{t-p}] with target x_t. Solve for
(c, phi_1, ..., phi_p) by least squares, for example with np.linalg.lstsq.
To forecast steps values, predict the next value, append it to the series,
and repeat. Later forecasts use earlier forecasts as inputs.
def ar_forecast(x, p, steps):
Input
ar_forecast(np.array([1.0, 2.0, 4.0, 8.0, 16.0, 32.0]), p=1, steps=2)
Output
array([ 64., 128.])
Reasoning
Every value is twice the one before, so least squares finds c = 0 and
phi_1 = 2 with zero error. The next value is 2 * 32 = 64, and the one
after it is 2 * 64 = 128, built from the first forecast.
len(x) > 2 * p, so there are more rows than unknowns.(steps,).Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell