Sequential Prediction via Nixtla’s AutoARIMA

I want to use Nixtlas’s StatsForecast to make predictions one step at a time, so that when a new observation is recorded – a new prediction is generated based on said observation.

Here is my naive implementation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import pandas as pd
import numpy as np
import os
from statsmodels.tsa.arima_process import arma_generate_sample
from statsforecast import StatsForecast
from statsforecast.models import AutoARIMA
os.environ['NIXTLA_ID_AS_COL'] = '1'
n = 100
## Generate synthetic AR(3) data.
ar3 = arma_generate_sample([1, 0.5, 0.5, 0.5], [1], n)
df = pd.DataFrame(data=ar3, columns=['y']).assign(unique_id=0)
df['ds'] = pd.date_range('01-01-2020', periods=ar3.size, freq='D')
## Instantiate a StatsForecast object
sf = StatsForecast(
models = [AutoARIMA(season_length = 1)],
freq = 'D'
)
res = []
## Generate sequential predictions
for k in range(n//2, n):
## In this cycle we pretend we have k observations
train = df.iloc[:k]
## Generate a prediction from k observations
pred = sf.forecast(df=train, h=1).loc[0, 'AutoARIMA']
## Collect data
time = train.ds.max() + pd.Timedelta('1D')
truth = df[df.ds == time].y.values[0]
res.append(dict(pred=pred, time=time, truth=truth))
res = pd.DataFrame(res).set_index('time')
res.plot()
</code>
<code>import pandas as pd import numpy as np import os from statsmodels.tsa.arima_process import arma_generate_sample from statsforecast import StatsForecast from statsforecast.models import AutoARIMA os.environ['NIXTLA_ID_AS_COL'] = '1' n = 100 ## Generate synthetic AR(3) data. ar3 = arma_generate_sample([1, 0.5, 0.5, 0.5], [1], n) df = pd.DataFrame(data=ar3, columns=['y']).assign(unique_id=0) df['ds'] = pd.date_range('01-01-2020', periods=ar3.size, freq='D') ## Instantiate a StatsForecast object sf = StatsForecast( models = [AutoARIMA(season_length = 1)], freq = 'D' ) res = [] ## Generate sequential predictions for k in range(n//2, n): ## In this cycle we pretend we have k observations train = df.iloc[:k] ## Generate a prediction from k observations pred = sf.forecast(df=train, h=1).loc[0, 'AutoARIMA'] ## Collect data time = train.ds.max() + pd.Timedelta('1D') truth = df[df.ds == time].y.values[0] res.append(dict(pred=pred, time=time, truth=truth)) res = pd.DataFrame(res).set_index('time') res.plot() </code>
import pandas as pd
import numpy as np
import os

from statsmodels.tsa.arima_process import arma_generate_sample
from statsforecast import StatsForecast
from statsforecast.models import AutoARIMA

os.environ['NIXTLA_ID_AS_COL'] = '1'


n = 100

## Generate synthetic AR(3) data. 
ar3 = arma_generate_sample([1, 0.5, 0.5, 0.5], [1], n)
df = pd.DataFrame(data=ar3, columns=['y']).assign(unique_id=0)
df['ds'] = pd.date_range('01-01-2020', periods=ar3.size, freq='D')

## Instantiate a StatsForecast object
sf = StatsForecast(
    models = [AutoARIMA(season_length = 1)],
    freq = 'D'
)

res = []
## Generate sequential predictions
for k in range(n//2, n):

    ## In this cycle we pretend we have k observations 
    train = df.iloc[:k]
    
    ## Generate a prediction from k observations 
    pred = sf.forecast(df=train, h=1).loc[0, 'AutoARIMA']


    ## Collect data
    time = train.ds.max() + pd.Timedelta('1D')
    truth = df[df.ds == time].y.values[0]    
    res.append(dict(pred=pred, time=time, truth=truth))
    
res = pd.DataFrame(res).set_index('time')
res.plot()

Problem: in my implementation, the StatsForecast object is repeatedly optimizing the order of the ARIMA model, which is time consuming.

I would like to be able to:

  1. Train AutoARIMA once, freeze the underlying ARIMA model and use that to make a prediction every time a new observation is collected (i.e. every cycle).
  2. Find optimal ARIMA order once, fit model coefficients every time a new observation is collected, then make prediction.

I can get the order via

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from statsforecast.arima import arima_string
arima_string(sf.fit(train).fitted_[0,0].model_)
</code>
<code>from statsforecast.arima import arima_string arima_string(sf.fit(train).fitted_[0,0].model_) </code>
from statsforecast.arima import arima_string
arima_string(sf.fit(train).fitted_[0,0].model_)

Then input the parameters into a statsmodels.tsa.arima.model.ARIMA object. This seems like quite a convluted approach quite cumbersome. Even then, I will likely make mistakes translating the order from Nixtla to statsmodels. There has to be a better way.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật