Professional Documents
Culture Documents
ARIMA Predict Forecast
ARIMA Predict Forecast
ARIMA Predict Forecast
data=pd.read_excel('Downloads/Daily_temp.xlsx','Sheet1')
In [3]: data['Date']=pd.to_datetime(data['Date'])
In [5]: data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3650 entries, 0 to 3649
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Date 3650 non-null datetime64[ns]
1 Temp 3650 non-null float64
dtypes: datetime64[ns](1), float64(1)
memory usage: 57.2 KB
In [7]: d.head(3)
Out[7]:
Temp
Date
1981-01-01 20.7
1981-01-02 17.9
1981-01-03 18.8
Trend: Constant
Critical Values: -3.43 (1%), -2.86 (5%), -2.57 (10%)
Null Hypothesis: The process contains a unit root.
Alternative Hypothesis: The process is weakly stationary.
Trend: Constant
Critical Values: -3.43 (1%), -2.86 (5%), -2.57 (10%)
Null Hypothesis: The process contains a unit root.
Alternative Hypothesis: The process is weakly stationary.
In [22]: a = d.values
In [27]: print(Model_AR8.summary())
Out[35]:
lb_stat lb_pvalue
1 0.120086 0.728941
2 0.492914 0.781565
3 1.286879 0.732251
4 3.055532 0.548576
5 6.022535 0.304032
6 10.872029 0.092413
7 14.968504 0.036405
8 38.589406 0.000006
9 39.281441 0.000010
10 39.826915 0.000018
In [41]: print(Model.summary())
SARIMAX Results
==============================================================================
Dep. Variable: y No. Observations: 3650
Model: ARIMA(3, 0, 1) Log Likelihood -8382.800
Date: Wed, 06 Mar 2024 AIC 16777.600
Time: 22:53:01 BIC 16814.815
Sample: 0 HQIC 16790.853
- 3650
Covariance Type: opg
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
const 11.4717 0.801 14.322 0.000 9.902 13.042
ar.L1 1.4851 0.018 80.334 0.000 1.449 1.521
ar.L2 -0.6204 0.027 -23.367 0.000 -0.672 -0.568
ar.L3 0.1304 0.018 7.386 0.000 0.096 0.165
ma.L1 -0.8928 0.012 -76.046 0.000 -0.916 -0.870
sigma2 5.7833 0.128 45.330 0.000 5.533 6.033
===================================================================================
Ljung-Box (L1) (Q): 0.01 Jarque-Bera (JB): 16.70
Prob(Q): 0.94 Prob(JB): 0.00
Heteroskedasticity (H): 0.86 Skew: 0.08
Prob(H) (two-sided): 0.01 Kurtosis: 3.28
===================================================================================
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (complex-step).
Out[50]:
lb_stat lb_pvalue
1 0.006790 0.934329
2 0.008225 0.995896
3 0.009125 0.999769
4 0.082014 0.999182
5 0.094185 0.999860
6 0.310555 0.999444
7 0.577923 0.999108
8 0.673870 0.999589
9 0.916520 0.999607
10 2.172116 0.994843
In [56]: rss=np.sum(res**2)
R-Squared: 0.6501153054406361
In [51]: #Model Comparison and choose a better model and use for prediction
In [53]: len(a)
Out[53]: 3650
In [69]: #train set will have only 3643 and test set should have the remaining 7
train = a[:len(a)-20]
In [71]: len(train)
Out[71]: 3630
In [68]: test=a[len(a)-20:]
In [70]: len(test)
Out[70]: 20
In [74]: plt.plot(predict)
plt.plot(test, color='red')
plt.show()
In [79]: rmse #Predictions are off by 1.65 units - on an average, the predicted termreratures differ from the actual
# by 1.65 degree celsius ( depending on the scale used)
Out[79]: 1.6598401226331614
In [80]: #Forecast
pred_t=Model.get_forecast(steps=15)
In [84]: fc = pred_t.predicted_mean
In [85]: fc
In [97]: # we need it in dataframe or tabular form- Date and Forecasted value as columns
n=15
fcast_index= pd.date_range(start=d.index[-1], periods=n+1, freq=d.index.freq)
In [89]: d.index[-1]
In [98]: fcast_index
In [100]: # Dataframe
Future_temp =pd.DataFrame({'Date': fcast_index[1:], 'Forecasted Temp':fc})
In [101]: Future_temp
Out[101]:
Date Forecasted Temp
0 1991-01-01 12.989125
1 1991-01-02 13.328300
2 1991-01-03 13.486813
3 1991-01-04 13.510388
4 1991-01-05 13.491272
5 1991-01-06 13.468920
6 1991-01-07 13.450656
7 1991-01-08 13.434908
8 1991-01-09 13.419936
9 1991-01-10 13.405090
10 1991-01-11 13.390278
11 1991-01-12 13.375538
12 1991-01-13 13.360903
13 1991-01-14 13.346380
14 1991-01-15 13.331970
In [104]: Future_temp
Out[104]:
Forecasted Temp
Date
1991-01-01 12.989125
1991-01-02 13.328300
1991-01-03 13.486813
1991-01-04 13.510388
1991-01-05 13.491272
1991-01-06 13.468920
1991-01-07 13.450656
1991-01-08 13.434908
1991-01-09 13.419936
1991-01-10 13.405090
1991-01-11 13.390278
1991-01-12 13.375538
1991-01-13 13.360903
1991-01-14 13.346380
1991-01-15 13.331970