When analyzing certain power data, I was asked to "predict power demand using AR, MA, ARIMA, SARIMA models." Since it was the first prediction method in Python for the first time, I decided to brute force the parameters.
The parameters are order = (p, d, q) and seasonal_order = (p, d, q), and we decided to adopt the parameter with the smallest aic.
I messed with what I found on stackoverflow and incorporated it. When I picked up the source, s was 12. Was it monthly data? This time, since the power data for one week is predicted from the training data for one month, s is 24, which is the smallest aic. Why···
import numpy as np
import pandas as pd
import statsmodels.api as sm
import statsmodels
import time
p = d = q = range(0,3)
import itertools
pdq = list(itertools.product(p, d, q))
seasonal_pdq = [(x[0], x[1], x[2], 12) for x in list(itertools.product(p, d, q))]
y=TargetData
minaic = 999999
minparam = 0
starttime = time.time()
for param in pdq:
for param_seasonal in seasonal_pdq:
try:
print('Start fitting')
mod = sm.tsa.statespace.SARIMAX(y,
order=param,
seasonal_order=param_seasonal,
enforce_stationarity=False,
enforce_invertibility=False)
results = mod.fit()
print('ARIMA{}x{}12 - AIC:{}'.format(param, param_seasonal, results.aic))
if minaic > results.aic:
minaic = results.aic
minparam = param
minparams = param_seasonal
print('aic minimum{} - aic minimumのp,d,q{} - aic minimumのp,d,q,s{}'.format(minaic, minparam,minparams))
except:
continue
duration = time.time() -starttime
print('p,d,End of estimation of q\n Measurement time:{}'.format(duration))
f = open('results_param(SARIMA).txt','w')
f.write('aic minimum{} - aic minimumのp,d,q{} - aic minimumのp,d,q,s{}'.format(minaic, minparam,minparams))
f.close()
――I don't know if I was able to optimize by force --Evaluation required for optimality ――The speed is dramatically slower due to the force work, so the speed is improved.