SyncAI.news, a Varaisys broadcasting
3 Statsmodels Tricks for Time Series Analysis & Forecasting
MM

Matthew Mayo

· 1 min read

EngineeringKDnuggets

3 Statsmodels Tricks for Time Series Analysis & Forecasting

A fitted statsmodels model computes a more than just the array of numbers most code pulls out of it. The point forecast is the smallest task it can perform. Every trick runs from a case of asking the results object for something it has already worked out, rather than having to rebuild that thing by hand. One dataset, one model, three methods people routinely reimplement. All three are run against the same monthly series and the same fitted model, so the only thing that changes between them is which method gets called on the object fit handed back.

Everything below was checked against statsmodels 0.15.0.

Start by installing statsmodels:

pip install statsmodels

Trick 1: Asking for the Interval, Not Just the Number

res.forecast(12) gives you twelve numbers. res.get_forecast(12) gives you a PredictionResults object instead, which happens to also carry the uncertainty the model already estimated. There's predicted_mean for the points, conf_int for the bounds, and summary_frame() for both at once. The intervals aren't even extra work; they are a result of the same computation, and the shorter method simply throws them away:

import statsmodels.api as sm
from statsmodels.tsa.arima.model import ARIMA

co2 = sm.datasets.co2.load_pandas().data["co2"]
co2 = co2.resample("MS").mean().ffill()
train, recent = co2[:-12], co2[-12:]

res = ARIMA(train, order=(1, 1, 1), seasonal_order=(1, 1, 1, 12)).fit()

print(res.get_forecast(12).summary_frame().head())

Output:

co2               mean   mean_se  mean_ci_lower  mean_ci_upper
2001-01-01  370.523929  0.322722     369.891406     371.156452
2001-02-01  371.253673  0.388214     370.492787     372.014559
2001-03-01  372.200726  0.429518     371.358887     373.042566
2001-04-01  373.468351  0.463501     372.559905     374.376797
2001-05-01  373.856957  0.494157     372.888427     374.825487

Trick 2: Adding New Data Without Refitting

updated = res.append(recent, refit=False)

print(updated.get_forecast(6).summary_frame().head())

Output:

Output:

Original source

This story was published by KDnuggets and written by Matthew Mayo. SyncAI.news shows a preview; the complete article is on the publisher's site.

Read the full story on kdnuggets.com

Similar News