Search code examples
pythontime-seriesstatsmodelsarimapmdarima

Predict using fit pmdarima ARIMA model


I can fit a SARIMA model to some data using pmdarima.

import pmdarima as pm
from pmdarima.model_selection import train_test_split
import numpy as np
import matplotlib.pyplot as plt

# Load/split
y = pm.datasets.load_wineind()
train, test = train_test_split(y, train_size=150)

# Fit
model = pm.auto_arima(train, seasonal=True, m=12)

I can make forecasts from this data, and I can even see the in-sample forecasts from which I can compute the residuals.

N = test.shape[0]  # predict N steps into the future
forecasts = model.predict(N)
in_sample_forecasts = model.predict_in_sample()

But SARIMA is just a mathematical model (as far as I know). So I expect to be able to use the fitted model parameters to forecast on some other series entirely. Can I do this?

For example:

# Some other series entirely
some_other_series = train + np.random.randint(0, 5000, len(train))
# The following method does not exist but illustrates the desired functionality
forecasts = model.predict_for(some_other_series, N)

Solution

  • I have found a solution for this. The trick is to run another fit but get the optimizer under the hood to basically perform a no-op on the already fit parameters. I found that method='nm' actually obeyed maxiter=0, while others did not. Below is code for the pmdarima model but same idea would work for a SARIMAX model in statsmodels.

    from copy import deepcopy
    # Some other series entirely
    some_other_series = train + np.random.randint(0, 5000, len(train))
    # Deep copy original model for later comparison
    new_model = deepcopy(model)
    new_model.method = 'nm'
    new_model.fit(some_other_series, maxiter=0, start_params=new_model.params())
    new_model.params()
    new_model.predict(12)
    # Note that the params have stayed the same and predictions are different
    model.params()
    model.predict(12)