Time-series-Forecasting Time Series Forecasting Jupyter Code - Ipynb at Main Chetandudhane Time-series-Forecasting GitHub
Time-series-Forecasting Time Series Forecasting Jupyter Code - Ipynb at Main Chetandudhane Time-series-Forecasting GitHub
main
time-series-forecasting / time_series_forecasting_jupyter_code.ipynb
chetandudhane
Add files via upload
1
contributor
8.42 MB
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 1/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [1]:
# Importing the necessary packages
import pandas as pd
import numpy as np
import statsmodels.tools.eval_measures as em
InteractiveShell.ast_node_interactivity = "all"
dfr.head().to_csv('rose_head.csv')
dfr.head()
Out[2]: Rose
YearMonth
1980-01-01 112.0
1980-02-01 118.0
1980-03-01 129.0
1980-04-01 99.0
1980-05-01 116.0
In [3]:
dfs = pd.read_csv('Sparkling.csv',parse_dates=True,index_col='YearMonth')
dfs.head().to_csv('spark_head.csv')
dfs.head()
Out[3]: Sparkling
YearMonth
1980-01-01 1686
1980-02-01 1591
1980-03-01 2304
1980-04-01 1712
1980-05-01 1471
In [4]:
dfr.info()
<class 'pandas.core.frame.DataFrame'>
dtypes: float64(1)
In [5]:
dfs.info()
<class 'pandas.core.frame.DataFrame'>
dtypes: int64(1)
In [ ]:
Sparkling 0
Out[6]:
dtype: int64
In [7]:
dfr.isna().sum()
Rose 2
Out[7]:
dtype: int64
In [9]:
dfr.isna().sum()
Rose 0
Out[9]:
dtype: int64
In [341…
# ROSE TIME SERIES
plt.plot(dfr, color='red');
plt.grid();
plt.legend(['Rose'], loc='best');
plt.tight_layout()
plt.savefig('rose_ts.jpg', bbox_inches='tight');
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 3/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [342…
# SPARKLING TIME SERIES
plt.plot(dfs);
plt.grid();
plt.legend(['Sparkling']);
plt.tight_layout()
plt.savefig('spark_ts.jpg', bbox_inches='tight');
round(dfr.describe().T,3)
In [344…
round(dfs.describe().T,3).to_csv('spark_describe.csv')
round(dfs.describe().T,3)
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 4/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
plt.grid();
plt.savefig('rose_monthwise_box.jpg', bbox_inches='tight');
In [346…
sns.boxplot(x = dfs.index.month_name(), y = dfs['Sparkling']);
plt.grid();
plt.savefig('spark_monthwise_box.jpg', bbox_inches='tight');
In [347…
from statsmodels.tsa.seasonal import seasonal_decompose
decompositionr.plot()
plt.savefig('decomposer.jpg', bbox_inches='tight');
In [349…
trend = decompositionr.trend
seasonality = decompositionr.seasonal
residual = decompositionr.resid
print('Trend','\n',trend.head(12),'\n')
print('Seasonality','\n',seasonality.head(12),'\n')
print('Residual','\n',residual.head(12),'\n')
trend.head(12).to_csv('rose_trend.csv')
seasonality.head(12).to_csv('rose_season.csv')
residual.head(12).to_csv('rose_resid.csv')
Trend
YearMonth
1980-01-01 NaN
1980-02-01 NaN
1980-03-01 NaN
1980-04-01 NaN
1980-05-01 NaN
1980-06-01 NaN
1980-07-01 147.083333
1980-08-01 148.125000
1980-09-01 148.375000
1980-10-01 148.083333
1980-11-01 147.416667
1980-12-01 145.125000
Seasonality
YearMonth
1980-01-01 -27.908647
1980-02-01 -17.435632
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 6/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1980-03-01 -9.285830
1980-04-01 -15.098330
1980-05-01 -10.196544
1980-06-01 -7.678687
1980-07-01 4.896908
1980-08-01 5.499686
1980-09-01 2.774686
1980-10-01 1.871908
1980-11-01 16.846908
1980-12-01 55.713575
Residual
YearMonth
1980-01-01 NaN
1980-02-01 NaN
1980-03-01 NaN
1980-04-01 NaN
1980-05-01 NaN
1980-06-01 NaN
1980-07-01 -33.980241
1980-08-01 -24.624686
1980-09-01 53.850314
1980-10-01 -2.955241
1980-11-01 -14.263575
1980-12-01 66.161425
In [350…
residual.plot()
<AxesSubplot:xlabel='YearMonth'>
Out[350…
decompositions.plot();
plt.savefig('decomposes.jpg', bbox_inches='tight');
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 7/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [352…
trend = decompositions.trend
seasonality = decompositions.seasonal
residual = decompositions.resid
print('Trend','\n',trend.head(12),'\n')
print('Seasonality','\n',seasonality.head(12),'\n')
print('Residual','\n',residual.head(12),'\n')
trend.head(12).to_csv('spark_trend.csv')
seasonality.head(12).to_csv('spark_season.csv')
residual.head(12).to_csv('spark_resid.csv')
Trend
YearMonth
1980-01-01 NaN
1980-02-01 NaN
1980-03-01 NaN
1980-04-01 NaN
1980-05-01 NaN
1980-06-01 NaN
1980-07-01 2360.666667
1980-08-01 2351.333333
1980-09-01 2320.541667
1980-10-01 2303.583333
1980-11-01 2302.041667
1980-12-01 2293.791667
Seasonality
YearMonth
1980-01-01 -854.260599
1980-02-01 -830.350678
1980-03-01 -592.356630
1980-04-01 -658.490559
1980-05-01 -824.416154
1980-06-01 -967.434011
1980-07-01 -465.502265
1980-08-01 -214.332821
1980-09-01 -254.677265
1980-10-01 599.769957
1980-11-01 1675.067179
1980-12-01 3386.983846
Residual
YearMonth
1980-01-01 NaN
1980-02-01 NaN
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 8/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1980-03-01 NaN
1980-04-01 NaN
1980-05-01 NaN
1980-06-01 NaN
1980-07-01 70.835599
1980-08-01 315.999487
1980-09-01 -81.864401
1980-10-01 -307.353290
1980-11-01 109.891154
1980-12-01 -501.775513
In [353…
cvsa = residual.std()/residual.mean()
cvsa
-296.08335294142995
Out[353…
In [ ]:
decompositionmr.plot();
plt.savefig('decomposemr.jpg', bbox_inches='tight');
In [355…
trend = decompositionmr.trend
seasonality = decompositionmr.seasonal
residual = decompositionmr.resid
print('Trend','\n',trend.head(12),'\n')
print('Seasonality','\n',seasonality.head(12),'\n')
print('Residual','\n',residual.head(12),'\n')
trend.head(12).to_csv('rose_trend_m.csv')
seasonality.head(12).to_csv('rose_season_m.csv')
residual.head(12).to_csv('rose_resid_m.csv')
Trend
YearMonth
1980 01 01 N N
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 9/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1980-01-01 NaN
1980-02-01 NaN
1980-03-01 NaN
1980-04-01 NaN
1980-05-01 NaN
1980-06-01 NaN
1980-07-01 147.083333
1980-08-01 148.125000
1980-09-01 148.375000
1980-10-01 148.083333
1980-11-01 147.416667
1980-12-01 145.125000
Seasonality
YearMonth
1980-01-01 0.670111
1980-02-01 0.806163
1980-03-01 0.901164
1980-04-01 0.854024
1980-05-01 0.889415
1980-06-01 0.923985
1980-07-01 1.058038
1980-08-01 1.035881
1980-09-01 1.017648
1980-10-01 1.022573
1980-11-01 1.192349
1980-12-01 1.628646
Residual
YearMonth
1980-01-01 NaN
1980-02-01 NaN
1980-03-01 NaN
1980-04-01 NaN
1980-05-01 NaN
1980-06-01 NaN
1980-07-01 0.758258
1980-08-01 0.840720
1980-09-01 1.357674
1980-10-01 0.970771
1980-11-01 0.853378
1980-12-01 1.129646
In [356…
residual.plot()
<AxesSubplot:xlabel='YearMonth'>
Out[356…
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 10/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
For the multiplicative series, we see that a lot of residuals are located around 1.
decompositionms.plot();
plt.savefig('decomposems.jpg', bbox_inches='tight');
In [358…
trend = decompositionms.trend
seasonality = decompositionms.seasonal
residual = decompositionms.resid
print('Trend','\n',trend.head(12),'\n')
print('Seasonality','\n',seasonality.head(12),'\n')
print('Residual','\n',residual.head(12),'\n')
trend.head(12).to_csv('spark_trend_m.csv')
seasonality.head(12).to_csv('spark_season_m.csv')
residual.head(12).to_csv('spark_resid_m.csv')
Trend
YearMonth
1980-01-01 NaN
1980-02-01 NaN
1980-03-01 NaN
1980-04-01 NaN
1980-05-01 NaN
1980-06-01 NaN
1980-07-01 2360.666667
1980-08-01 2351.333333
1980-09-01 2320.541667
1980-10-01 2303.583333
1980-11-01 2302.041667
1980-12-01 2293.791667
Seasonality
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 11/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
Seasonality
YearMonth
1980-01-01 0.649843
1980-02-01 0.659214
1980-03-01 0.757440
1980-04-01 0.730351
1980-05-01 0.660609
1980-06-01 0.603468
1980-07-01 0.809164
1980-08-01 0.918822
1980-09-01 0.894367
1980-10-01 1.241789
1980-11-01 1.690158
1980-12-01 2.384776
Residual
YearMonth
1980-01-01 NaN
1980-02-01 NaN
1980-03-01 NaN
1980-04-01 NaN
1980-05-01 NaN
1980-06-01 NaN
1980-07-01 1.029230
1980-08-01 1.135407
1980-09-01 0.955954
1980-10-01 0.907513
1980-11-01 1.050423
1980-12-01 0.946770
In [359…
cvsm = residual.std()/residual.mean()
cvsm
0.13886244212828963
Out[359…
Split the data into train and test and plot the
training and test data. [Test Data should start
from 1991]
In [360…
# ROSE DATA SPLIT
rtrain = dfr[dfr.index<'1991']
rtest = dfr[dfr.index>='1991']
In [361…
# SPARKLING DATA SPLIT
strain = dfs[dfs.index<'1991']
stest = dfs[dfs.index>='1991']
In [362…
print(rtrain.shape)
print(rtest.shape)
(132, 1)
(55, 1)
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 12/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [363…
print(strain.shape)
print(stest.shape)
(132, 1)
(55, 1)
In [364…
from IPython.display import display
In [365…
# ROSE DATA TRAIN AND TEST
display(rtrain.head())
display(rtrain.tail())
display(rtest.head())
display(rtest.tail())
rtrain.to_csv('rose_train.csv')
rtest.to_csv('rose_test.csv')
Rose
YearMonth
1980-01-01 112.0
1980-02-01 118.0
1980-03-01 129.0
1980-04-01 99.0
1980-05-01 116.0
Rose
YearMonth
1990-08-01 70.0
1990-09-01 83.0
1990-10-01 65.0
1990-11-01 110.0
1990-12-01 132.0
Rose
YearMonth
1991-01-01 54.0
1991-02-01 55.0
1991-03-01 66.0
1991-04-01 65.0
1991 05 01 60 0
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 13/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1991-05-01 60.0
Rose
YearMonth
1995-03-01 45.0
1995-04-01 52.0
1995-05-01 28.0
1995-06-01 40.0
1995-07-01 62.0
In [366…
# SPARKLING DATA TRAIN AND TEST
display(strain.head())
display(strain.tail())
display(stest.head())
display(stest.tail())
strain.to_csv('spark_train.csv')
stest.to_csv('spark_test.csv')
Sparkling
YearMonth
1980-01-01 1686
1980-02-01 1591
1980-03-01 2304
1980-04-01 1712
1980-05-01 1471
Sparkling
YearMonth
1990-08-01 1605
1990-09-01 2424
1990-10-01 3116
1990-11-01 4286
1990-12-01 6047
Sparkling
YearMonth
1991-01-01 1902
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 14/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1991-01-01 1902
1991-02-01 2049
1991-03-01 1874
1991-04-01 1279
1991-05-01 1432
Sparkling
YearMonth
1995-03-01 1897
1995-04-01 1862
1995-05-01 1670
1995-06-01 1688
1995-07-01 2031
In [367…
# ROSE DATA TRAIN TEST SPLIT TIME SERIES
rtrain['Rose'].plot(fontsize=14)
rtest['Rose'].plot(fontsize=14)
plt.grid()
plt.savefig('rose_split_ts.jpg', bbox_inches='tight')
plt.show();
In [368…
# SPARKLING DATA TRAIN TEST SPLIT TIME SERIES
strain['Sparkling'].plot(fontsize=14)
stest['Sparkling'].plot(fontsize=14)
plt.grid()
plt.savefig('spark_split_ts.jpg', bbox_inches='tight')
plt.show()
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 15/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
<AxesSubplot:xlabel='YearMonth'>
Out[368…
<AxesSubplot:xlabel='YearMonth'>
Out[368…
<matplotlib.legend.Legend at 0x7ffecc759350>
Out[368…
Text(0.5, 1.0, 'Sparkling Data Train and Test Split')
Out[368…
rtest.shape
(132, 1)
Out[369…
(55, 1)
Out[369…
In [370…
train_time = [i+1 for i in range(len(rtrain))]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 3
9, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 5
7, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 7
5, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 9
3, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 1
09, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123,
124, 125, 126, 127, 128, 129, 130, 131, 132]
[133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 1
47, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161,
162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 17
6, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187]
We see that we have successfully the generated the numerical time instance order
for both the training and test set. Now we will add these values in the training and
test set.
In [371…
LinearRegression_train = rtrain.copy()
LinearRegression_test = rtest.copy()
In [372…
LinearRegression_train['time'] = train_time
LinearRegression_test['time'] = test_time
display(LinearRegression_train.head())
display(LinearRegression_train.tail())
display(LinearRegression_test.head())
display(LinearRegression_test.tail())
Rose time
YearMonth
1980-01-01 112.0 1
1980-02-01 118.0 2
1980-03-01 129.0 3
1980-04-01 99.0 4
1980-05-01 116.0 5
Rose time
YearMonth
Rose time
YearMonth
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 17/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
Rose time
YearMonth
Now that our training and test data has been modified, let us go ahead use
LinearRegression to build the model on the training data and test the model on
–
the test data.
In [373…
from sklearn.linear_model import LinearRegression
In [374…
lr = LinearRegression()
In [375…
lr.fit(LinearRegression_train[['time']],LinearRegression_train['Rose'])
LinearRegression()
Out[375…
In [376…
train_predictions_model1 = lr.predict(LinearRegression_train[['ti
LinearRegression_train['RegOnTime'] = train_predictions_model1
test_predictions_model1 = lr.predict(LinearRegression_test[['time
LinearRegression_test['RegOnTime'] = test_predictions_model1
plt.plot(rtrain['Rose'], label='Train')
plt.plot(rtest['Rose'], label='Test')
plt.legend(loc='best')
plt.grid();
plt.savefig('lr_rose.jpg', bbox_inches='tight');
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 18/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
Model Evaluation
In [378…
rmse_model11_test = metrics.mean_squared_error(rtest['Rose'],test_predict
print("For RegressionOnTime forecast on the Test Data, RMSE is %3.3f " %
In [379…
resultsDf = pd.DataFrame({'Test RMSE Rose': [rmse_model11_test]},index=['
resultsDf
RegressionOnTime 15.268955
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 3
9, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 5
7, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 7
5, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 9
3, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 1
09, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123,
124, 125, 126, 127, 128, 129, 130, 131, 132]
[133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 1
47, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161,
162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 17
6, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187]
We see that we have successfully the generated the numerical time instance order
for both the training and test set. Now we will add these values in the training and
test set.
In [381…
LinearRegression_train = strain.copy()
LinearRegression_test = stest.copy()
In [382…
LinearRegression_train['time'] = train_time
LinearRegression_test['time'] = test_time
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 19/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
display(LinearRegression_train.head())
display(LinearRegression_train.tail())
display(LinearRegression_test.head())
display(LinearRegression_test.tail())
Sparkling time
YearMonth
1980-01-01 1686 1
1980-02-01 1591 2
1980-03-01 2304 3
1980-04-01 1712 4
1980-05-01 1471 5
Sparkling time
YearMonth
Sparkling time
YearMonth
Sparkling time
YearMonth
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 20/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
Now that our training and test data has been modified, let us go ahead use
LinearRegression to build the model on the training data and test the model on
–
the test data.
In [383…
from sklearn.linear_model import LinearRegression
In [384…
lr = LinearRegression()
In [385…
lr.fit(LinearRegression_train[['time']],LinearRegression_train['Sparkling
LinearRegression()
Out[385…
In [386…
train_predictions_model1 = lr.predict(LinearRegression_train[['ti
LinearRegression_train['RegOnTime'] = train_predictions_model1
test_predictions_model1 = lr.predict(LinearRegression_test[['time
LinearRegression_test['RegOnTime'] = test_predictions_model1
plt.plot(strain['Sparkling'], label='Train')
plt.plot(stest['Sparkling'], label='Test')
plt.legend(loc='best')
plt.grid();
plt.savefig('spark_lr.jpg', bbox_inches='tight');
Model Evaluation
In [388…
rmse_model12_test = metrics.mean_squared_error(stest['Sparkling'],test_pr
print("For RegressionOnTime forecast on the Test Data, RMSE is %3.3f " %
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 21/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [389…
resultsDf_12 = pd.DataFrame({'Test RMSE Sparkling': [rmse_model12_test]},
resultsDf
For this particular naive model, we say that the prediction for
tomorrow is the same as today and the prediction for day after
tomorrow is tomorrow and since the prediction of tomorrow is
same as today,therefore the prediction for day after tomorrow is
also today.
In [390…
NaiveModel_train = rtrain.copy()
NaiveModel_test = rtest.copy()
In [391…
rtrain.tail()
Out[391… Rose
YearMonth
1990-08-01 70.0
1990-09-01 83.0
1990-10-01 65.0
1990-11-01 110.0
1990-12-01 132.0
In [392…
NaiveModel_test['naive'] = np.asarray(rtrain['Rose'])[len(np.asarray(rtra
NaiveModel_test['naive'].head()
YearMonth
Out[392…
1991-01-01 132.0
1991-02-01 132.0
1991-03-01 132.0
1991-04-01 132.0
1991-05-01 132.0
In [393…
plt.plot(NaiveModel_train['Rose'], label='Train')
plt.plot(rtest['Rose'], label='Test')
plt.legend(loc='best')
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 22/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
plt.grid();
plt.savefig('rose_naive.jpg', bbox_inches='tight');
Model Evaluation
In [394…
rmse_model21_test = metrics.mean_squared_error(rtest['Rose'],NaiveModel_t
print("For NaiveModel forecast on the Test Data, RMSE is %3.3f" %(rmse_m
In [395…
resultsDf
In [396…
resultsDf_21 = pd.DataFrame({'Test RMSE Rose': [rmse_model21_test]},index
# resultsDf
# resultsDf.join(resultsDf_3, how='inner')
For this particular naive model, we say that the prediction for
tomorrow is the same as today and the prediction for day after
tomorrow is tomorrow and since the prediction of tomorrow is
same as today,therefore the prediction for day after tomorrow is
also today.
In [397…
NaiveModel_train = strain.copy()
In [398…
strain.tail()
Out[398… Sparkling
YearMonth
1990-08-01 1605
1990-09-01 2424
1990-10-01 3116
1990-11-01 4286
1990-12-01 6047
In [399…
NaiveModel_test['naive'] = np.asarray(strain['Sparkling'])[len(np.asarray
NaiveModel_test['naive'].head()
YearMonth
Out[399…
1991-01-01 6047
1991-02-01 6047
1991-03-01 6047
1991-04-01 6047
1991-05-01 6047
In [400…
plt.plot(NaiveModel_train['Sparkling'], label='Train')
plt.plot(stest['Sparkling'], label='Test')
plt.legend(loc='best')
plt.grid();
plt.savefig('Sparkling_naive.jpg', bbox_inches='tight');
Model Evaluation
In [401…
d l22 t t t i d ( t t['S kli '] N i M
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 24/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [401…
rmse_model22_test = metrics.mean_squared_error(stest['Sparkling'],NaiveMo
print("For NaiveModel forecast on the Test Data, RMSE is %3.3f" %(rmse_m
In [402…
resultsDf
In [403…
resultsDf_21
NaiveModel 79.718773
In [404…
resultsDf_22 = pd.DataFrame({'Test RMSE Sparkling': [rmse_model22_test]},
# resultsDf
# resultsDf.join(resultsDf_3, how='inner')
resultsDf1
In [405…
resultsDf3 = pd.concat([resultsDf, resultsDf1], axis=0)
resultsDf3
In [406…
resultsDf = resultsDf3.copy()
resultsDf.to_csv('results_nb.csv')
resultsDf
SimpleAverage_test = rtest.copy()
In [408…
SimpleAverage_test['mean_forecast'] = rtrain['Rose'].mean()
SimpleAverage_test.head()
YearMonth
In [409…
plt.plot(SimpleAverage_train['Rose'], label='Train')
plt.plot(SimpleAverage_test['Rose'], label='Test')
plt.legend(loc='best')
plt.grid();
plt.savefig('rose_simpleaverage.jpg', bbox_inches='tight');
Model Evaluation
In [410…
rmse_model31_test = metrics.mean_squared_error(rtest['Rose'],SimpleAverag
print("For Simple Average forecast on Rose Test Data, RMSE is %3.3f" %(r
In [411…
resultsDf 31 = pd.DataFrame({'Test RMSE Rose': [rmse model31 test]}
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 26/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
resultsDf_31 = pd.DataFrame({ Test RMSE Rose : [rmse_model31_test]}
,index=['SimpleAverageModel'])
resultsDf_31
# resultsDf
SimpleAverageModel 53.46057
SimpleAverage_test = stest.copy()
In [413…
SimpleAverage_test['mean_forecast'] = strain['Sparkling'].mean()
SimpleAverage_test.head()
YearMonth
In [414…
plt.plot(SimpleAverage_train['Sparkling'], label='Train')
plt.plot(SimpleAverage_test['Sparkling'], label='Test')
plt.legend(loc='best')
plt.grid();
plt.savefig('Sparkling_simpleaverage.jpg', bbox_inches='tight');
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 27/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
Model Evaluation
In [415…
rmse_model32_test = metrics.mean_squared_error(stest['Sparkling'],SimpleA
print("For Simple Average forecast on Sparkling Test Data, RMSE is %3.3f
In [416…
resultsDf_32 = pd.DataFrame({'Test RMSE Sparkling': [rmse_model32_test]}
,index=['SimpleAverageModel'])
resultsDf_32
# resultsDf
SimpleAverageModel 1275.081804
In [417…
resultsDf4 = resultsDf_31.join(resultsDf_32, how='outer')
resultsDf4
In [418…
resultsDf5 = pd.concat([resultsDf, resultsDf4], axis=0)
resultsDf5
In [419…
resultsDf = resultsDf5.copy()
resultsDf.to_csv('results_sa.csv')
resultsDf
In [420…
MovingAverage = dfr.copy()
MovingAverage.head()
Out[420… Rose
YearMonth
1980-01-01 112.0
1980-02-01 118.0
1980-03-01 129.0
1980-04-01 99.0
1980-05-01 116.0
MovingAverage['Trailing_4'] = MovingAverage['Rose'].rolling(4).mean()
MovingAverage['Trailing_6'] = MovingAverage['Rose'].rolling(6).mean()
MovingAverage['Trailing_9'] = MovingAverage['Rose'].rolling(9).mean()
MovingAverage.head()
YearMonth
In [422…
## Plotting on the whole data
plt.plot(MovingAverage['Rose'], label='Train')
plt.legend(loc = 'best')
plt.grid();
plt.savefig('rose_ma.jpg', bbox_inches='tight');
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 29/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
Let us split the data into train and test and plot this Time Series. The window of the
moving average is need to be carefully selected as too big a window will result in
not having any test set as the whole series might get averaged over.
In [423…
#Creating train and test set
trailing_MovingAverage_train=MovingAverage[MovingAverage.index<'1991']
trailing_MovingAverage_test=MovingAverage[MovingAverage.index>='1991']
In [424…
## Plotting on both the Training and Test data
plt.figure(figsize=(16,9))
plt.plot(trailing_MovingAverage_train['Rose'], label='Train')
plt.plot(trailing_MovingAverage_test['Rose'], label='Test')
plt.legend(loc = 'best')
plt.grid();
plt.savefig('rose_ma_test.jpg', bbox_inches='tight');
Model Evaluation
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 30/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [425…
## Test Data - RMSE --> 2 point Trailing MA
rmse_model4_test_2 = metrics.mean_squared_error(rtest['Rose'],trailing_Mo
print("For 2 point Moving Average Model forecast on Rose Training Data,
rmse_model4_test_4 = metrics.mean_squared_error(rtest['Rose'],trailing_Mo
print("For 4 point Moving Average Model forecast on Rose Training Data,
rmse_model4_test_6 = metrics.mean_squared_error(rtest['Rose'],trailing_Mo
print("For 6 point Moving Average Model forecast on Rose Training Data,
rmse_model4_test_9 = metrics.mean_squared_error(rtest['Rose'],trailing_Mo
print("For 9 point Moving Average Model forecast on Rose Training Data,
For 2 point Moving Average Model forecast on Rose Training Data, RMSE is
11.529
For 4 point Moving Average Model forecast on Rose Training Data, RMSE is
14.451
For 6 point Moving Average Model forecast on Rose Training Data, RMSE is
14.566
For 9 point Moving Average Model forecast on Rose Training Data, RMSE is
14.728
In [426…
resultsDf_41 = pd.DataFrame({'Test RMSE Rose': [rmse_model4_test_2,rmse_m
,rmse_model4_test_6,rmse_model4
,index=['2pointTrailingMovingAverage','4pointT
,'6pointTrailingMovingAverage','9point
resultsDf_41.to_csv('rose_ma.csv')
resultsDf_41
2pointTrailingMovingAverage 11.529278
4pointTrailingMovingAverage 14.451403
6pointTrailingMovingAverage 14.566327
9pointTrailingMovingAverage 14.727630
MovingAverage.head()
Out[427… Sparkling
YearMonth
1980-01-01 1686
1980 02 01 1591
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 31/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1980-02-01 1591
1980-03-01 2304
1980-04-01 1712
1980-05-01 1471
MovingAverage.head()
YearMonth
In [429…
## Plotting on the whole data
plt.plot(MovingAverage['Sparkling'], label='Train')
plt.legend(loc = 'best')
plt.grid();
plt.savefig('spark_ma.jpg', bbox_inches='tight');
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 32/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
Let us split the data into train and test and plot this Time Series. The window of the
moving average is need to be carefully selected as too big a window will result in
not having any test set as the whole series might get averaged over.
In [430…
#Creating train and test set
trailing_MovingAverage_train=MovingAverage[MovingAverage.index<'1991']
trailing_MovingAverage_test=MovingAverage[MovingAverage.index>='1991']
In [431…
## Plotting on Test data
plt.figure(figsize=(16,9))
plt.plot(trailing_MovingAverage_train['Sparkling'], label='Train')
plt.plot(trailing_MovingAverage_test['Sparkling'], label='Test')
plt.legend(loc = 'best')
plt.grid();
plt.savefig('spark_ma_test.jpg', bbox_inches='tight');
Model Evaluation
Done only on the test data.
In [432…
## Test Data - RMSE --> 2 point Trailing MA
rmse_model4_test_2 = metrics.mean_squared_error(stest['Sparkling'],traili
print("For 2 point Moving Average Model forecast on Sparkling Training Da
rmse_model4_test_4 = metrics.mean_squared_error(stest['Sparkling'],traili
print("For 4 point Moving Average Model forecast on Sparkling Training Da
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 33/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
rmse_model4_test_6 = metrics.mean_squared_error(stest['Sparkling'],traili
print("For 6 point Moving Average Model forecast on Sparkling Training Da
rmse_model4_test_9 = metrics.mean_squared_error(stest['Sparkling'],traili
print("For 9 point Moving Average Model forecast on Sparkling Training Da
In [433…
resultsDf_42 = pd.DataFrame({'Test RMSE Sparkling': [rmse_model4_test_2,r
,rmse_model4_test_6,rmse_model4
,index=['2pointTrailingMovingAverage','4pointT
,'6pointTrailingMovingAverage','9point
resultsDf_42.to_csv('spark_ma.csv')
resultsDf_42
2pointTrailingMovingAverage 813.400684
4pointTrailingMovingAverage 1156.589694
6pointTrailingMovingAverage 1283.927428
9pointTrailingMovingAverage 1346.278315
In [434…
resultsDf6 = resultsDf_41.join(resultsDf_42, how='outer')
resultsDf6
In [435…
resultsDf7 = pd.concat([resultsDf, resultsDf6], axis=0)
resultsDf7
In [436…
resultsDf = resultsDf7.copy()
resultsDf.to_csv('results_ma.csv')
resultsDf
LinearRegression_train = rtrain.copy()
LinearRegression_test = rtest.copy()
LinearRegression_test['time'] = test_time
LinearRegression_train['time'] = train_time
lr = LinearRegression()
lr.fit(LinearRegression_train[['time']],LinearRegression_train['Rose'])
test_predictions_model1 = lr.predict(LinearRegression_test[['time
LinearRegression_test['RegOnTime'] = test_predictions_model1
# NB on Rose
NaiveModel_train = rtrain.copy()
NaiveModel_test = rtest.copy()
NaiveModel_test['naive'] = np.asarray(rtrain['Rose'])[len(np.asarray(rtra
# SA on Rose
SimpleAverage_train = rtrain.copy()
SimpleAverage_test = rtest.copy()
SimpleAverage_test['mean_forecast'] = rtrain['Rose'].mean()
# MA on Rose
MovingAverage = dfr.copy()
MovingAverage['Trailing_2'] = MovingAverage['Rose'].rolling(2).mean()
trailing_MovingAverage_test = MovingAverage[MovingAverage.index>='1991']
LinearRegression()
Out[437…
In [438…
## Plotting on both rtraining and rtest data
plt.plot(rtrain['Rose'], label='train')
plt.legend(loc='best')
plt.grid();
LinearRegression_train = strain.copy()
LinearRegression_test = stest.copy()
LinearRegression_test['time'] = test_time
LinearRegression_train['time'] = train_time
lr = LinearRegression()
lr.fit(LinearRegression_train[['time']],LinearRegression_train['Sparkling
test_predictions_model1 = lr.predict(LinearRegression_test[['time
LinearRegression_test['RegOnTime'] = test_predictions_model1
# NB on Sparkling
NaiveModel_train = strain.copy()
NaiveModel_test = stest.copy()
NaiveModel_test['naive'] = np.asarray(strain['Sparkling'])[len(np.asarray
# SA on Sparkling
SimpleAverage_train = strain.copy()
SimpleAverage_test = stest.copy()
SimpleAverage_test['mean_forecast'] = strain['Sparkling'].mean()
# MA on Sparkling
MovingAverage = dfs.copy()
MovingAverage['Trailing_2'] = MovingAverage['Sparkling'].rolling(2).mean(
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 36/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
trailing_MovingAverage_test = MovingAverage[MovingAverage.index>='1991']
LinearRegression()
Out[439…
In [440…
## Plotting on both rtraining and rtest data
plt.plot(strain['Sparkling'], label='train')
plt.legend(loc='best')
plt.grid();
import statsmodels.tools.eval_measures as em
The fit() function is then called providing the fit configuration, the alpha value,
smoothing_level. If this is omitted or set to None, the model will automatically
optimize the value.
In [442…
# create class
model_SES = SimpleExpSmoothing(rtrain,initialization_method='estimated' )
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [443…
# Fitting the Simple Exponential Smoothing model and asking python to cho
model_SES_autofit = model_SES.fit(optimized=True)
In [444…
## Let us check the parameters
model_SES_autofit.params
{'smoothing_level': 0.09874920899865502,
Out[444…
'smoothing_trend': nan,
'smoothing_seasonal': nan,
'damping_trend': nan,
'initial_level': 134.3871074301239,
'initial_trend': nan,
'use_boxcox': False,
'lamda': None,
'remove_bias': False}
Here, Python has optimized the smoothing level to be almost 0.1
In [445…
# Using the fitted model on the training set to forecast on the test set
SES_predict_rose = model_SES_autofit.forecast(steps=len(rtest))
SES_predict_rose
1991-01-01 87.10498
Out[445…
1991-02-01 87.10498
1991-03-01 87.10498
1991-04-01 87.10498
1991-05-01 87.10498
1991-06-01 87.10498
1991-07-01 87.10498
1991-08-01 87.10498
1991-09-01 87.10498
1991-10-01 87.10498
1991-11-01 87.10498
1991-12-01 87.10498
1992-01-01 87.10498
1992-02-01 87.10498
1992-03-01 87.10498
1992-04-01 87.10498
1992-05-01 87.10498
1992-06-01 87.10498
1992-07-01 87.10498
1992-08-01 87.10498
1992 09 01 87 10498
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 38/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1992-09-01 87.10498
1992-10-01 87.10498
1992-11-01 87.10498
1992-12-01 87.10498
1993-01-01 87.10498
1993-02-01 87.10498
1993-03-01 87.10498
1993-04-01 87.10498
1993-05-01 87.10498
1993-06-01 87.10498
1993-07-01 87.10498
1993-08-01 87.10498
1993-09-01 87.10498
1993-10-01 87.10498
1993-11-01 87.10498
1993-12-01 87.10498
1994-01-01 87.10498
1994-02-01 87.10498
1994-03-01 87.10498
1994-04-01 87.10498
1994-05-01 87.10498
1994-06-01 87.10498
1994-07-01 87.10498
1994-08-01 87.10498
1994-09-01 87.10498
1994-10-01 87.10498
1994-11-01 87.10498
1994-12-01 87.10498
1995-01-01 87.10498
1995-02-01 87.10498
1995-03-01 87.10498
1995-04-01 87.10498
1995-05-01 87.10498
1995-06-01 87.10498
1995-07-01 87.10498
In [446…
## Plotting the Training data, Test data and the forecasted values
plt.plot(rtrain, label='Train')
plt.plot(rtest, label='Test')
plt.legend(loc='best')
plt.grid()
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 39/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [447…
## Mean Absolute Percentage Error (MAPE) - Function Definition
return np.mean((np.abs(y_true-y_pred))/(y_true))*100
In [448…
print('SES RMSE:',mean_squared_error(rtest.values,SES_predict_rose.values
#different way to calculate RMSE
In [449…
resultsDf_rses = pd.DataFrame({'Test RMSE Rose': [em.rmse(rtest.values,SE
resultsDf_rses
In [ ]:
The fit() function is then called providing the fit configuration, the alpha value,
smoothing_level. If this is omitted or set to None, the model will automatically
optimize the value.
In [450…
# create class
model_SES = SimpleExpSmoothing(strain,initialization_method='estimated' )
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [451…
# Fitting the Simple Exponential Smoothing model and asking python to cho
model_SES_autofit = model_SES.fit(optimized=True)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ho
ltwinters/model.py:922: ConvergenceWarning: Optimization failed to conver
ge. Check mle_retvals.
ConvergenceWarning,
In [452…
## Let us check the parameters
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 40/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
## Let us check the parameters
model_SES_autofit.params
{'smoothing_level': 0.07028781460389563,
Out[452…
'smoothing_trend': nan,
'smoothing_seasonal': nan,
'damping_trend': nan,
'initial_level': 1763.9269926897732,
'initial_trend': nan,
'use_boxcox': False,
'lamda': None,
'remove_bias': False}
In [453…
# Using the fitted model on the training set to forecast on the test set
SES_predict_spark = model_SES_autofit.forecast(steps=len(stest))
SES_predict_spark
1991-01-01 2804.662714
Out[453…
1991-02-01 2804.662714
1991-03-01 2804.662714
1991-04-01 2804.662714
1991-05-01 2804.662714
1991-06-01 2804.662714
1991-07-01 2804.662714
1991-08-01 2804.662714
1991-09-01 2804.662714
1991-10-01 2804.662714
1991-11-01 2804.662714
1991-12-01 2804.662714
1992-01-01 2804.662714
1992-02-01 2804.662714
1992-03-01 2804.662714
1992-04-01 2804.662714
1992-05-01 2804.662714
1992-06-01 2804.662714
1992-07-01 2804.662714
1992-08-01 2804.662714
1992-09-01 2804.662714
1992-10-01 2804.662714
1992-11-01 2804.662714
1992-12-01 2804.662714
1993-01-01 2804.662714
1993-02-01 2804.662714
1993-03-01 2804.662714
1993-04-01 2804.662714
1993-05-01 2804.662714
1993-06-01 2804.662714
1993-07-01 2804.662714
1993-08-01 2804.662714
1993-09-01 2804.662714
1993-10-01 2804.662714
1993-11-01 2804.662714
1993-12-01 2804.662714
1994-01-01 2804.662714
1994-02-01 2804.662714
1994-03-01 2804.662714
1994-04-01 2804.662714
1994-05-01 2804.662714
1994-06-01 2804.662714
1994-07-01 2804.662714
1994-08-01 2804.662714
1994-09-01 2804.662714
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 41/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1994-10-01 2804.662714
1994-11-01 2804.662714
1994-12-01 2804.662714
1995-01-01 2804.662714
1995-02-01 2804.662714
1995-03-01 2804.662714
1995-04-01 2804.662714
1995-05-01 2804.662714
1995-06-01 2804.662714
1995-07-01 2804.662714
In [454…
## Plotting the Training data, Test data and the forecasted values
plt.plot(strain, label='Train')
plt.plot(stest, label='Test')
plt.legend(loc='best')
plt.grid()
In [455…
## Mean Absolute Percentage Error (MAPE) - Function Definition
return np.mean((np.abs(y_true-y_pred))/(y_true))*100
In [456…
print('SES RMSE:',mean_squared_error(stest.values,SES_predict_spark.value
#different way to calculate RMSE
In [457…
resultsDf_sses = pd.DataFrame({'Test RMSE Sparkling': [em.rmse(stest.valu
resultsDf_sses
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 42/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [458…
resultsDf7 = resultsDf_rses.join(resultsDf_sses, how='outer')
resultsDf7
In [459…
resultsDf8 = pd.concat([resultsDf, resultsDf7], axis=0)
resultsDf8
In [460…
resultsDf = resultsDf8.copy()
In [ ]:
In [461…
# Initializing the Double Exponential Smoothing Model
model_DES = Holt(rtrain,initialization_method='estimated')
fi ()
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 43/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
model_DES = model_DES.fit()
print('')
print('')
print(model_DES.params)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [462…
# Forecasting using this model for the duration of the test set
DES_predict_rose = model_DES.forecast(len(rtest))
DES_predict_rose
1991-01-01 72.068983
Out[462…
1991-02-01 71.574632
1991-03-01 71.080281
1991-04-01 70.585930
1991-05-01 70.091580
1991-06-01 69.597229
1991-07-01 69.102878
1991-08-01 68.608528
1991-09-01 68.114177
1991-10-01 67.619826
1991-11-01 67.125475
1991-12-01 66.631125
1992-01-01 66.136774
1992-02-01 65.642423
1992-03-01 65.148072
1992-04-01 64.653722
1992-05-01 64.159371
1992-06-01 63.665020
1992-07-01 63.170669
1992-08-01 62.676319
1992-09-01 62.181968
1992-10-01 61.687617
1992-11-01 61.193267
1992-12-01 60.698916
1993-01-01 60.204565
1993-02-01 59.710214
1993-03-01 59.215864
1993-04-01 58.721513
1993-05-01 58.227162
1993-06-01 57.732811
1993-07-01 57.238461
1993-08-01 56.744110
1993-09-01 56.249759
1993-10-01 55.755409
1993-11-01 55.261058
1993-12-01 54.766707
1994-01-01 54.272356
1994-02-01 53.778006
1994-03-01 53.283655
1994-04-01 52.789304
1994-05-01 52.294953
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 44/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
05 0 5 . 53
1994-06-01 51.800603
1994-07-01 51.306252
1994-08-01 50.811901
1994-09-01 50.317551
1994-10-01 49.823200
1994-11-01 49.328849
1994-12-01 48.834498
1995-01-01 48.340148
1995-02-01 47.845797
1995-03-01 47.351446
1995-04-01 46.857095
1995-05-01 46.362745
1995-06-01 45.868394
1995-07-01 45.374043
In [463…
## Plotting the Training data, Test data and the forecasted values
plt.plot(rtrain, label='Train')
plt.plot(rtest, label='Test')
plt.legend(loc='best')
plt.grid()
We see that the double exponential smoothing is picking up the trend component
along with the level component as well.
In [464…
print('DES RMSE:',mean_squared_error(rtest.values,DES_predict_rose.values
In [465…
resultsDf_temp = pd.DataFrame({'Test RMSE Rose': [mean_squared_error(rtes
,index=['Double Exponential Smoothing'])
resultsDf_temp
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 45/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
model_DES = model_DES.fit()
print('')
print('')
print(model_DES.params)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ho
ltwinters/model.py:922: ConvergenceWarning: Optimization failed to conver
ge. Check mle_retvals.
ConvergenceWarning,
In [467…
# Forecasting using this model for the duration of the test set
DES_predict_spark = model_DES.forecast(len(stest))
DES_predict_spark
1991-01-01 5401.733026
Out[467…
1991-02-01 5476.005230
1991-03-01 5550.277433
1991-04-01 5624.549637
1991-05-01 5698.821840
1991-06-01 5773.094044
1991-07-01 5847.366248
1991-08-01 5921.638451
1991-09-01 5995.910655
1991-10-01 6070.182858
1991-11-01 6144.455062
1991-12-01 6218.727266
1992-01-01 6292.999469
1992-02-01 6367.271673
1992-03-01 6441.543876
1992-04-01 6515.816080
1992-05-01 6590.088284
1992-06-01 6664.360487
1992-07-01 6738.632691
1992-08-01 6812.904894
1992-09-01 6887.177098
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 46/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1992-10-01 6961.449302
1992-11-01 7035.721505
1992-12-01 7109.993709
1993-01-01 7184.265912
1993-02-01 7258.538116
1993-03-01 7332.810320
1993-04-01 7407.082523
1993-05-01 7481.354727
1993-06-01 7555.626930
1993-07-01 7629.899134
1993-08-01 7704.171338
1993-09-01 7778.443541
1993-10-01 7852.715745
1993-11-01 7926.987948
1993-12-01 8001.260152
1994-01-01 8075.532356
1994-02-01 8149.804559
1994-03-01 8224.076763
1994-04-01 8298.348967
1994-05-01 8372.621170
1994-06-01 8446.893374
1994-07-01 8521.165577
1994-08-01 8595.437781
1994-09-01 8669.709985
1994-10-01 8743.982188
1994-11-01 8818.254392
1994-12-01 8892.526595
1995-01-01 8966.798799
1995-02-01 9041.071003
1995-03-01 9115.343206
1995-04-01 9189.615410
1995-05-01 9263.887613
1995-06-01 9338.159817
1995-07-01 9412.432021
In [468…
## Plotting the Training data, Test data and the forecasted values
plt.plot(strain, label='Train')
plt.plot(stest, label='Test')
plt.legend(loc='best')
plt.grid()
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 47/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [469…
print('DES RMSE:',mean_squared_error(stest.values,DES_predict_spark.value
In [470…
resultsDf_temp1 = pd.DataFrame({'Test RMSE Sparkling': [mean_squared_erro
,index=['Double Exponential Smoothing'])
resultsDf_temp1
In [471…
resultsDf9 = resultsDf_temp.join(resultsDf_temp1, how='outer')
resultsDf9
In [472…
resultsDf10 = pd.concat([resultsDf, resultsDf9], axis=0)
resultsDf10
In [473…
resultsDf = resultsDf10.copy()
resultsDf
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 48/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
Inference
Here, we see that the Double Exponential Smoothing has actually done well when
compared to the Simple Exponential Smoothing. This is because of the fact that the
Double Exponential Smoothing model has picked up the trend component as well.
model_TES = model_TES.fit()
print('')
print(model_TES.params)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [475…
# Forecasting using this model for the duration of the test set
TES_predict_add_rose = model_TES.forecast(len(rtest))
TES_predict_add_rose
1991-01-01 42.574560
Out[475…
1991-02-01 54.372099
1991-03-01 61.821511
1991-04-01 50.671639
1991-05-01 58.932400
1991-06-01 63.751320
1991-07-01 73.204015
1991-08-01 78.742349
1991-09-01 74.177124
1991-10-01 71.738917
1991 11 01 89 317129
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 49/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1991-11-01 89.317129
1991-12-01 131.006255
1992-01-01 35.978169
1992-02-01 47.775707
1992-03-01 55.225119
1992-04-01 44.075248
1992-05-01 52.336008
1992-06-01 57.154928
1992-07-01 66.607623
1992-08-01 72.145958
1992-09-01 67.580733
1992-10-01 65.142526
1992-11-01 82.720737
1992-12-01 124.409864
1993-01-01 29.381777
1993-02-01 41.179316
1993-03-01 48.628728
1993-04-01 37.478856
1993-05-01 45.739616
1993-06-01 50.558536
1993-07-01 60.011232
1993-08-01 65.549566
1993-09-01 60.984341
1993-10-01 58.546134
1993-11-01 76.124346
1993-12-01 117.813472
1994-01-01 22.785386
1994-02-01 34.582924
1994-03-01 42.032336
1994-04-01 30.882464
1994-05-01 39.143225
1994-06-01 43.962145
1994-07-01 53.414840
1994-08-01 58.953175
1994-09-01 54.387949
1994-10-01 51.949742
1994-11-01 69.527954
1994-12-01 111.217080
1995-01-01 16.188994
1995-02-01 27.986533
1995-03-01 35.435945
1995-04-01 24.286073
1995-05-01 32.546833
1995-06-01 37.365753
1995-07-01 46.818448
In [476…
## Plotting the Training data, Test data and the forecasted values
plt.plot(rtrain, label='Train')
plt.plot(rtest, label='Test')
plt.legend(loc='best')
plt.grid()
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 50/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [477…
print('TES RMSE:',mean_squared_error(rtest.values,TES_predict_add_rose.va
In [478…
resultsDf_temp = pd.DataFrame({'Test RMSE Rose': [mean_squared_error(rtes
,index=['Triple Exponential Smoothing (Additiv
import textwrap
textwrap.TextWrapper(resultsDf_temp.index)
resultsDf_temp
<textwrap.TextWrapper at 0x7ffece2f2b50>
Out[478…
Inference
Triple Exponential Smoothing has performed the best on the test as expected since
the data had both trend and seasonality.
In [ ]:
model_TES = model_TES.fit()
print('')
print(model_TES.params)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [480…
# Forecasting using this model for the duration of the test set
TES_predict_add_spark = model_TES.forecast(len(stest))
TES_predict_add_spark
1991-01-01 1490.574426
Out[480…
1991-02-01 1204.714325
1991-03-01 1688.941106
1991-04-01 1551.450810
1991-05-01 1461.440427
1991-06-01 1278.907163
1991-07-01 1805.164022
1991-08-01 1679.251392
1991-09-01 2315.687649
1991-10-01 3225.308906
1991-11-01 4299.652360
1991-12-01 6411.161332
1992-01-01 1483.296578
1992-02-01 1197.436477
1992-03-01 1681.663258
1992-04-01 1544.172962
1992-05-01 1454.162580
1992-06-01 1271.629315
1992-07-01 1797.886174
1992-08-01 1671.973544
1992-09-01 2308.409801
1992-10-01 3218.031058
1992-11-01 4292.374513
1992-12-01 6403.883484
1993-01-01 1476.018731
1993-02-01 1190.158629
1993-03-01 1674.385411
1993-04-01 1536.895114
1993-05-01 1446.884732
1993-06-01 1264.351467
1993-07-01 1790.608327
1993-08-01 1664.695696
1993-09-01 2301.131954
1993-10-01 3210.753211
1993-11-01 4285.096665
1993-12-01 6396.605637
1994-01-01 1468.740883
1994-02-01 1182.880782
1994-03-01 1667.107563
1994-04-01 1529.617267
1994-05-01 1439.606884
1994-07-01 1783.330479
1994-08-01 1657.417849
1994-09-01 2293.854106
1994-10-01 3203.475363
1994-11-01 4277.818818
1994-12-01 6389.327789
1995-01-01 1461.463036
1995-02-01 1175.602934
1995-03-01 1659.829715
1995-04-01 1522.339419
1995-05-01 1432.329037
1995-06-01 1249.795772
1995-07-01 1776.052632
In [481…
## Plotting the Training data, Test data and the forecasted values
plt.plot(strain, label='Train')
plt.plot(stest, label='Test')
plt.legend(loc='best')
plt.grid()
In [482…
print('TES RMSE:',mean_squared_error(stest.values,TES_predict_add_spark.v
In [483…
resultsDf_temp1 = pd.DataFrame({'Test RMSE Sparkling': [mean_squared_erro
,index=['Triple Exponential Smoothing (Additiv
import textwrap
textwrap.TextWrapper(resultsDf_temp.index)
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 53/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
resultsDf_temp1
<textwrap.TextWrapper at 0x7ffece3ff050>
Out[483…
In [484…
resultsDf11 = resultsDf_temp.join(resultsDf_temp1, how='outer')
resultsDf11
In [485…
resultsDf12 = pd.concat([resultsDf, resultsDf11], axis=0)
resultsDf12
In [486…
resultsDf = resultsDf12.copy()
model_TES_am = model_TES_am.fit()
print('')
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [488…
# Forecasting using this model for the duration of the test set
TES_predict_am_rose = model_TES_am.forecast(len(rtest))
TES_predict_am_rose
1991-01-01 55.942246
Out[488…
1991-02-01 63.240624
1991-03-01 68.899674
1991-04-01 60.007486
1991-05-01 67.257150
1991-06-01 72.880875
1991-07-01 79.913317
1991-08-01 84.831135
1991-09-01 79.856761
1991-10-01 78.009465
1991-11-01 90.608817
1991-12-01 124.421985
1992-01-01 53.620022
1992-02-01 60.606323
1992-03-01 66.019647
1992-04-01 57.490388
1992-05-01 64.426058
1992-06-01 69.802261
1992-07-01 76.525716
1992-08-01 81.222314
1992-09-01 76.447471
1992-10-01 74.667150
1992-11-01 86.712771
1992-12-01 119.052784
1993-01-01 51.297798
1993-02-01 57.972023
1993-03-01 63.139621
1993-04-01 54.973289
1993-05-01 61.594966
1993-06-01 66.723648
1993-07-01 73.138115
1993-08-01 77.613494
1993-09-01 73.038180
1993-10-01 71.324834
1993-11-01 82.816725
1993-12-01 113.683583
1994-01-01 48.975574
1994-02-01 55.337723
1994-03-01 60.259594
1994-04-01 52.456191
1994-05-01 58.763874
1994-06-01 63.645034
1994-07-01 69.750514
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 55/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1994-08-01 74.004674
1994-09-01 69.628889
1994-10-01 67.982518
1994-11-01 78.920680
1994-12-01 108.314382
1995-01-01 46.653350
1995-02-01 52.703422
1995-03-01 57.379568
1995-04-01 49.939092
1995-05-01 55.932782
1995-06-01 60.566420
1995-07-01 66.362913
In [489…
## Plotting the Training data, Test data and the forecasted values
plt.plot(rtrain, label='Train')
plt.plot(rtest, label='Test')
plt.legend(loc='best')
plt.grid()
plt.savefig('rose_mul_tes.jpg', bbox_inches='tight');
In [491…
# resultsDf_temp = pd.DataFrame({'Test RMSE': [mean_squared_error(test.va
# ,index=['Alpha=0.74,Beta=2.73e-06,Gamma=5.2e
# resultsDf
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 56/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
resultsDf_temp = pd.DataFrame({'Test RMSE Rose': [mean_squared_error(rtes
,index=['Triple Exponential Smoothing (Multipl
resultsDf_temp
We see that the multiplicative seasonality model has not done that well when
compared to the additive seasonality Triple Exponential Smoothing model.
In [ ]:
model_TES_am = model_TES_am.fit()
print('')
print(model_TES_am.params)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [493…
# Forecasting using this model for the duration of the test set
TES_predict_am_spark = model_TES_am.forecast(len(stest))
TES_predict_am_spark
1991-01-01 1587.685845
Out[493…
1991-02-01 1356.590237
1991-03-01 1763.121866
1991-04-01 1656.379813
1991-05-01 1542.186697
1991-06-01 1355.276292
1991-07-01 1854.460265
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 57/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1991-08-01 1820.822870
1991-09-01 2277.203230
1991-10-01 3122.460625
1991-11-01 4129.148636
1991-12-01 5891.184787
1992-01-01 1538.578232
1992-02-01 1314.522051
1992-03-01 1708.305381
1992-04-01 1604.748228
1992-05-01 1493.989468
1992-06-01 1312.809887
1992-07-01 1796.200235
1992-08-01 1763.469447
1992-09-01 2205.285676
1992-10-01 3023.588340
1992-11-01 3998.053790
1992-12-01 5703.651532
1993-01-01 1489.470618
1993-02-01 1272.453864
1993-03-01 1653.488895
1993-04-01 1553.116644
1993-05-01 1445.792238
1993-06-01 1270.343481
1993-07-01 1737.940206
1993-08-01 1706.116024
1993-09-01 2133.368122
1993-10-01 2924.716056
1993-11-01 3866.958944
1993-12-01 5516.118277
1994-01-01 1440.363005
1994-02-01 1230.385677
1994-03-01 1598.672409
1994-04-01 1501.485059
1994-05-01 1397.595009
1994-06-01 1227.877076
1994-07-01 1679.680177
1994-08-01 1648.762600
1994-09-01 2061.450568
1994-10-01 2825.843772
1994-11-01 3735.864098
1994-12-01 5328.585023
1995-01-01 1391.255391
1995-02-01 1188.317490
1995-03-01 1543.855924
1995-04-01 1449.853475
1995-05-01 1349.397780
1995-06-01 1185.410670
1995-07-01 1621.420148
In [494…
## Plotting the Training data, Test data and the forecasted values
plt.plot(strain, label='Train')
plt.plot(stest, label='Test')
plt.legend(loc='best')
plt.grid()
plt.savefig('spark_mul_tes.jpg', bbox_inches='tight');
In [496…
# resultsDf_temp = pd.DataFrame({'Test RMSE': [mean_squared_error(test.va
# ,index=['Alpha=0.74,Beta=2.73e-06,Gamma=5.2e
# resultsDf
resultsDf_temp1
In [497…
resultsDf13 = resultsDf_temp.join(resultsDf_temp1, how='outer')
resultsDf13
In [498…
resultsDf14 = pd.concat([resultsDf, resultsDf13], axis=0)
resultsDf14
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 59/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
Out[498… Test RMSE Test RMSE
Rose Sparkling
In [499…
resultsDf = resultsDf14.copy()
resultsDf
In [500…
resultsDf.to_csv('results_all.csv')
In [ ]:
In [501…
# Initializing the Double Exponential Smoothing Model
model_TES = ExponentialSmoothing(rtrain,trend='additive',seasonal='additi
# Fitting the model
model_TES = model_TES.fit()
print('')
print(model_TES.params)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [502…
# Forecasting using this model for the duration of the test set
TES_predict_add_damp_rose = model_TES.forecast(len(rtest))
TES_predict_add_damp_rose
1991-01-01 48.231055
Out[502…
1991-02-01 61.045359
1991-03-01 68.342667
1991-04-01 58.197362
1991-05-01 65.575659
1991-06-01 70.640514
1991-07-01 79.851872
1991-08-01 85.224146
1991-09-01 83.117518
1991-10-01 81.244162
1991-11-01 99.227179
1991-12-01 140.741504
1992-01-01 47.004340
1992-02-01 59.849266
1992-03-01 67.176431
1992-04-01 57.060238
1992-05-01 64.466920
1992-06-01 69.559452
1992-07-01 78.797795
1992-08-01 84.196381
1992-09-01 82.115409
1992-10-01 80.267067
1992-11-01 98.274475
1992-12-01 139.812581
1993-01-01 46.098605
1993-02-01 58.966140
1993-03-01 66.315350
1993-04-01 56.220652
1993-05-01 63.648292
1993-06-01 68.761258
1993-07-01 78.019527
1993-08-01 83.437540
1993-09-01 81.375510
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 61/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1993-10-01 79.545638
1993-11-01 97.571055
1993-12-01 139.126720
1994-01-01 45.429864
1994-02-01 58.314093
1994-03-01 65.679579
1994-04-01 55.600751
1994-05-01 63.043865
1994-06-01 68.171919
1994-07-01 77.444899
1994-08-01 82.877256
1994-09-01 80.829212
1994-10-01 79.012977
1994-11-01 97.051690
1994-12-01 138.620320
1995-01-01 44.936105
1995-02-01 57.832659
1995-03-01 65.210163
1995-04-01 55.143052
1995-05-01 62.597592
1995-06-01 67.736786
1995-07-01 77.020628
In [503…
## Plotting the Training data, Test data and the forecasted values
plt.plot(rtrain, label='Train')
plt.plot(rtest, label='Test')
plt.legend(loc='best')
plt.grid()
In [504…
print('TES RMSE DAMPED ROSE:',mean_squared_error(rtest.values,TES_predict
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 62/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [505…
resultsDf_temp = pd.DataFrame({'Test RMSE Rose': [mean_squared_error(rtes
,index=['Triple Exponential Smoothing (Additiv
import textwrap
textwrap.TextWrapper(resultsDf_temp.index)
resultsDf_temp
<textwrap.TextWrapper at 0x7ffec3f07c10>
Out[505…
In [ ]:
model_TES = model_TES.fit()
print('')
print(model_TES.params)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [507…
# Forecasting using this model for the duration of the test set
TES_predict_add_damp_spark = model_TES.forecast(len(stest))
TES_predict_add_damp_spark
1991-01-01 1502.750406
Out[507…
1991-02-01 1198.527164
1991-03-01 1697.164113
1991-04-01 1543.572592
1991-05-01 1464.154046
1991-07-01 1801.409196
1991-08-01 1643.249695
1991-09-01 2325.526486
1991-10-01 3227.302621
1991-11-01 4302.234703
1991-12-01 6456.727997
1992-01-01 1501.617527
1992-02-01 1197.427979
1992-03-01 1696.097621
1992-04-01 1542.537820
1992-05-01 1463.150051
1992-06-01 1283.238499
1992-07-01 1800.464035
1992-08-01 1642.332646
1992-09-01 2324.636712
1992-10-01 3226.439311
1992-11-01 4301.397070
1992-12-01 6455.915278
1993-01-01 1500.828980
1993-02-01 1196.662885
1993-03-01 1695.355283
1993-04-01 1541.817561
1993-05-01 1462.451214
1993-06-01 1282.560448
1993-07-01 1799.806150
1993-08-01 1641.694328
1993-09-01 2324.017379
1993-10-01 3225.838400
1993-11-01 4300.814031
1993-12-01 6455.349579
1994-01-01 1500.280107
1994-02-01 1196.130337
1994-03-01 1694.838574
1994-04-01 1541.316221
1994-05-01 1461.964785
1994-06-01 1282.088486
1994-07-01 1799.348226
1994-08-01 1641.250023
1994-09-01 2323.586289
1994-10-01 3225.420131
1994-11-01 4300.408203
1994-12-01 6454.955822
1995-01-01 1499.898061
1995-02-01 1195.759654
1995-03-01 1694.478916
1995-04-01 1540.967260
1995-05-01 1461.626203
1995-06-01 1281.759974
1995-07-01 1799.029485
In [508…
## Plotting the Training data, Test data and the forecasted values
plt.plot(strain, label='Train')
plt.plot(stest, label='Test')
plt.legend(loc='best')
plt.grid()
In [509…
print('TES RMSE SPARK DAMPED:',mean_squared_error(stest.values,TES_predic
In [510…
resultsDf_temp1 = pd.DataFrame({'Test RMSE Sparkling': [mean_squared_erro
,index=['Triple Exponential Smoothing (Additiv
import textwrap
textwrap.TextWrapper(resultsDf_temp.index)
resultsDf_temp1
<textwrap.TextWrapper at 0x7ffece267ad0>
Out[510…
In [511…
resultsDf11 = resultsDf_temp.join(resultsDf_temp1, how='outer')
resultsDf11
In [512…
resultsDf12 = pd.concat([resultsDf, resultsDf11], axis=0)
resultsDf12
In [513…
resultsDf = resultsDf12.copy()
In [ ]:
model_TES_am = model_TES_am.fit()
print('')
print(model_TES_am.params)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [515…
# F ti i thi d l f th d ti f th t t t
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 66/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [515
# Forecasting using this model for the duration of the test set
TES_predict_am_damp_rose = model_TES_am.forecast(len(rtest))
TES_predict_am_damp_rose
1991-01-01 57.798263
Out[515…
1991-02-01 65.505362
1991-03-01 71.234453
1991-04-01 62.758312
1991-05-01 70.050683
1991-06-01 75.329037
1991-07-01 82.357168
1991-08-01 87.915176
1991-09-01 83.325352
1991-10-01 81.585835
1991-11-01 94.777358
1991-12-01 129.083606
1992-01-01 56.797501
1992-02-01 64.395159
1992-03-01 70.052745
1992-04-01 61.739320
1992-05-01 68.937473
1992-06-01 74.157439
1992-07-01 81.103580
1992-08-01 86.605569
1992-09-01 82.110665
1992-10-01 80.421979
1992-11-01 93.454316
1992-12-01 127.320364
1993-01-01 56.038348
1993-02-01 63.552986
1993-03-01 69.156331
1993-04-01 60.966337
1993-05-01 68.093018
1993-06-01 73.268694
1993-07-01 80.152639
1993-08-01 85.612134
1993-09-01 81.189233
1993-10-01 79.539106
1993-11-01 92.450689
1993-12-01 125.982812
1994-01-01 55.462472
1994-02-01 62.914134
1994-03-01 68.476332
1994-04-01 60.379971
1994-05-01 67.452436
1994-06-01 72.594513
1994-07-01 79.431278
1994-08-01 84.858537
1994-09-01 80.490257
1994-10-01 78.869380
1994-11-01 91.689361
1994-12-01 124.968176
1995-01-01 55.025627
1995-02-01 62.429517
1995-03-01 67.960502
1995-04-01 59.935168
1995-05-01 66.966505
1995-06-01 72.083096
1995-07-01 78.884071
In [516…
## Plotting the Training data, Test data and the forecasted values
plt.plot(rtrain, label='Train')
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 67/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
plt.plot(rtest, label='Test')
plt.legend(loc='best')
plt.grid()
plt.savefig('rose_mul_damp_tes.jpg', bbox_inches='tight');
In [518…
# resultsDf_temp = pd.DataFrame({'Test RMSE': [mean_squared_error(test.va
# ,index=['Alpha=0.74,Beta=2.73e-06,Gamma=5.2e
# resultsDf
resultsDf_temp
In [ ]:
H lt Wi t ETS(A A M) H lt Wi t '
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 68/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
model_TES_am = model_TES_am.fit()
print('')
print(model_TES_am.params)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [520…
# Forecasting using this model for the duration of the test set
TES_predict_am_damp_spark = model_TES_am.forecast(len(stest))
TES_predict_am_damp_spark
1991-01-01 1591.400712
Out[520…
1991-02-01 1348.109039
1991-03-01 1768.331811
1991-04-01 1654.048544
1991-05-01 1548.113612
1991-06-01 1363.568481
1991-07-01 1862.870528
1991-08-01 1797.339996
1991-09-01 2310.534562
1991-10-01 3163.672919
1991-11-01 4176.949302
1991-12-01 5994.180382
1992-01-01 1558.894787
1992-02-01 1320.798853
1992-03-01 1732.803577
1992-04-01 1621.090460
1992-05-01 1517.521178
1992-06-01 1336.845841
1992-07-01 1826.665407
1992-08-01 1762.698529
1992-09-01 2266.372327
1992-10-01 3103.708097
1992-11-01 4098.439393
1992-12-01 5882.455650
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 69/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1993-01-01 1530.082048
1993-02-01 1296.591536
1993-03-01 1701.311911
1993-04-01 1591.876934
1993-05-01 1490.404527
1993-06-01 1313.159315
1993-07-01 1794.573759
1993-08-01 1731.992878
1993-09-01 2227.227618
1993-10-01 3050.556224
1993-11-01 4028.849447
1993-12-01 5783.424610
1994-01-01 1504.542891
1994-02-01 1275.134551
1994-03-01 1673.398195
1994-04-01 1565.982526
1994-05-01 1466.368755
1994-06-01 1292.163952
1994-07-01 1766.128228
1994-08-01 1704.775874
1994-09-01 2192.530365
1994-10-01 3003.443242
1994-11-01 3967.166017
1994-12-01 5695.645057
1995-01-01 1481.905385
1995-02-01 1256.115418
1995-03-01 1648.655917
1995-04-01 1543.030130
1995-05-01 1445.063826
1995-06-01 1273.553993
1995-07-01 1740.914558
In [521…
## Plotting the Training data, Test data and the forecasted values
plt.plot(strain, label='Train')
plt.plot(stest, label='Test')
plt.legend(loc='best')
plt.grid()
plt.savefig('spark_mul_damp_tes.jpg', bbox_inches='tight');
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 70/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [523…
# resultsDf_temp = pd.DataFrame({'Test RMSE': [mean_squared_error(test.va
# ,index=['Alpha=0.74,Beta=2.73e-06,Gamma=5.2e
# resultsDf
resultsDf_temp1
In [524…
resultsDf13 = resultsDf_temp.join(resultsDf_temp1, how='outer')
resultsDf13
In [525…
resultsDf14 = pd.concat([resultsDf, resultsDf13], axis=0)
resultsDf14
In [526…
resultsDf = resultsDf14.copy()
resultsDf
In [527…
resultsDf.to_csv('results_all.csv')
In [ ]:
MovingAverage.head()
Out[528… Rose
YearMonth
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 72/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1980-01-01 112.0
1980-02-01 118.0
1980-03-01 129.0
1980-04-01 99.0
1980-05-01 116.0
MovingAverage.head()
YearMonth
In [530…
#Creating train and test set
trailing_MovingAverage_train=MovingAverage[MovingAverage.index<'1991']
trailing_MovingAverage_test=MovingAverage[MovingAverage.index>='1991']
In [531…
## Plotting on both the Training and Test data
plt.figure(figsize=(16,9))
plt.plot(trailing_MovingAverage_train['Rose'], label='Train')
plt.plot(trailing_MovingAverage_test['Rose'], label='Test')
plt.legend(loc = 'best')
plt.grid();
plt.savefig('rose_best_model_2pt.jpg', bbox_inches='tight');
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 73/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
model_TES_am = model_TES_am.fit()
print('')
print(model_TES_am.params)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [533…
# Forecasting using this model for the duration of the test set
TES_predict_am_damp_spark = model_TES_am.forecast(len(stest))
TES_predict_am_damp_spark
1991-01-01 1591.400712
Out[533…
1991-02-01 1348.109039
1991-03-01 1768.331811
1991-04-01 1654.048544
1991-05-01 1548.113612
1991-06-01 1363.568481
1991-07-01 1862.870528
1991-08-01 1797.339996
1991-09-01 2310.534562
1991-10-01 3163.672919
1991-11-01 4176.949302
1991-12-01 5994.180382
1992-01-01 1558.894787
1992-02-01 1320.798853
1992-03-01 1732.803577
1992-04-01 1621.090460
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 74/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1992-05-01 1517.521178
1992-06-01 1336.845841
1992-07-01 1826.665407
1992-08-01 1762.698529
1992-09-01 2266.372327
1992-10-01 3103.708097
1992-11-01 4098.439393
1992-12-01 5882.455650
1993-01-01 1530.082048
1993-02-01 1296.591536
1993-03-01 1701.311911
1993-04-01 1591.876934
1993-05-01 1490.404527
1993-06-01 1313.159315
1993-07-01 1794.573759
1993-08-01 1731.992878
1993-09-01 2227.227618
1993-10-01 3050.556224
1993-11-01 4028.849447
1993-12-01 5783.424610
1994-01-01 1504.542891
1994-02-01 1275.134551
1994-03-01 1673.398195
1994-04-01 1565.982526
1994-05-01 1466.368755
1994-06-01 1292.163952
1994-07-01 1766.128228
1994-08-01 1704.775874
1994-09-01 2192.530365
1994-10-01 3003.443242
1994-11-01 3967.166017
1994-12-01 5695.645057
1995-01-01 1481.905385
1995-02-01 1256.115418
1995-03-01 1648.655917
1995-04-01 1543.030130
1995-05-01 1445.063826
1995-06-01 1273.553993
1995-07-01 1740.914558
In [534…
## Plotting the Training data, Test data and the forecasted values
plt.plot(strain, label='Train')
plt.plot(stest, label='Test')
plt.legend(loc='best')
plt.grid()
plt.title('Best Model for Sparkling - Holt Winter ETS(A, Ad, M)', fontsiz
plt.savefig('spark_best_model.jpg', bbox_inches='tight');
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 75/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
===========================
===========================
We would want the series to be stationary for building ARIMA models and thus we
would want the p-value of this test to be less than the α value.
In [535…
from statsmodels.tsa.stattools import adfuller
In [536…
dfrs = adfuller(dfr,regression='ct')
Let us take one level of differencing to see whether the series becomes stationary.
In [537…
dfrs = adfuller(dfr.diff().dropna(),regression='ct')
In [538…
dfr.diff().dropna().plot(grid=True);
plt.savefig('rose_stationary.jpg', bbox_inches='tight');
We would want the series to be stationary for building ARIMA models and thus we
would want the p-value of this test to be less than the α value.
In [539…
dfss = adfuller(dfs,regression='ct')
Let us take one level of differencing to see whether the series becomes stationary.
In [540…
dfss = adfuller(dfs.diff().dropna(),regression='ct')
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 77/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [541…
dfs.diff().dropna().plot(grid=True);
plt.savefig('spark_stationary.jpg', bbox_inches='tight');
In [542…
rtrain.plot(grid=True);
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 78/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [543…
dftest = adfuller(rtrain,regression='ct')
The training data is non-stationary at 95% confidence level. Let us take a first level
of differencing to stationarize the Time Series.
In [544…
dftest = adfuller(rtrain.diff().dropna(),regression='ct')
p-value < 0.05 ------> So at 95% Confidence Interval. Train Rose data is Stationary
with lag 1
Now, let us go ahead and plot the differenced training data.
In [545…
rtrain.diff().dropna().plot(grid=True);
plt.savefig('rose_train_stationary.jpg', bbox_inches='tight');
In [546…
rtrain.info()
<class 'pandas.core.frame.DataFrame'>
dtypes: float64(1)
In [547…
## The following loop helps us in getting a combination of different para
## As seen before, Stationarity is achived with a difference of 1.
import itertools
p = q = range(0, 4)
d= range(1,2)
for i in range(0,len(pdq)):
print('Model: {}'.format(pdq[i]))
Model: (0, 1, 0)
Model: (0, 1, 1)
Model: (0, 1, 2)
Model: (0, 1, 3)
Model: (1, 1, 0)
Model: (1, 1, 1)
Model: (1, 1, 2)
Model: (1, 1, 3)
Model: (2, 1, 0)
Model: (2, 1, 1)
Model: (2, 1, 2)
Model: (2, 1, 3)
Model: (3, 1, 0)
Model: (3, 1, 1)
Model: (3, 1, 2)
Model: (3, 1, 3)
In [548…
# Creating an empty Dataframe with column names only
ROSE_ARIMA_AIC
In [549…
from statsmodels.tsa.arima.model import ARIMA
for param in pdq:# running a loop within the pdq parameters defined by it
ARIMA_model = ARIMA(rtrain['Rose'].values,order=param).fit()#fitting
#using the parameters from the loop
ARIMA(0, 1, 0) - AIC:1333.1546729124348
ARIMA(0, 1, 1) - AIC:1282.3098319748333
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 80/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
ARIMA(0, 1, 2) - AIC:1279.6715288535797
ARIMA(0, 1, 3) - AIC:1280.5453761734648
ARIMA(1, 1, 0) - AIC:1317.3503105381526
ARIMA(1, 1, 1) - AIC:1280.5742295380078
ARIMA(1, 1, 2) - AIC:1279.870723423192
ARIMA(1, 1, 3) - AIC:1281.8707223309914
ARIMA(2, 1, 0) - AIC:1298.6110341604958
ARIMA(2, 1, 1) - AIC:1281.5078621868524
ARIMA(2, 1, 2) - AIC:1281.8707222264584
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/st
atespace/sarimax.py:966: UserWarning: Non-stationary starting autoregress
ive parameters found. Using zeros as starting parameters.
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/st
atespace/sarimax.py:978: UserWarning: Non-invertible starting MA paramete
rs found. Using zeros as starting parameters.
ARIMA(2, 1, 3) - AIC:1274.6948698861079
ARIMA(3, 1, 0) - AIC:1297.4810917271725
ARIMA(3, 1, 1) - AIC:1282.419277627196
ARIMA(3, 1, 2) - AIC:1283.720740597716
ARIMA(3, 1, 3) - AIC:1278.6559777950524
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
In [550…
## Sort the above AIC values in the ascending order to get the parameters
ROSE_ARIMA_AIC.sort_values(by='AIC',ascending=True).head()
11 (2, 1, 3) 1274.694870
15 (3, 1, 3) 1278.655978
2 (0, 1, 2) 1279.671529
6 (1, 1, 2) 1279.870723
3 (0, 1, 3) 1280.545376
In [551…
auto_ARIMA = ARIMA(rtrain, order=(2,1,3))
results_auto_ARIMA = auto_ARIMA.fit()
print(results_auto_ARIMA.summary())
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 81/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
so inferred frequency MS will be used.
% freq, ValueWarning)
SARIMAX Results
=========================================================================
=====
- 12-01-1990
Covariance Type: opg
=========================================================================
=====
-------------------------------------------------------------------------
-----
=========================================================================
==========
=========================================================================
==========
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (co
mplex-step).
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
plt.savefig('rose_diag_plot.jpg', bbox_inches='tight')
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 82/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
Out[552…
predicted_auto_ARIMA
1991-01-01 85.580378
Out[553…
1991-02-01 90.550478
1991-03-01 81.951645
1991-04-01 92.757905
1991-05-01 80.892136
1991-06-01 92.927053
1991-07-01 81.380416
1991-08-01 91.984496
1991-09-01 82.606124
1991-10-01 90.614731
1991-11-01 84.011264
1991-12-01 89.255213
1992-01-01 85.268453
1992-02-01 88.136477
1992-03-01 86.229447
1992-04-01 87.339267
1992-05-01 86.866793
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 83/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1992 05 01 86.866793
1992-06-01 86.850806
1992-07-01 87.221937
1992-08-01 86.610859
1992-09-01 87.365742
1992-10-01 86.544423
1992-11-01 87.372422
1992-12-01 86.581630
1993-01-01 87.305125
1993-02-01 86.667435
1993-03-01 87.210192
1993-04-01 86.764195
1993-05-01 87.117017
1993-06-01 86.850023
1993-07-01 87.040905
1993-08-01 86.915188
1993-09-01 86.987029
1993-10-01 86.958099
1993-11-01 86.954288
1993-12-01 86.981765
1994-01-01 86.938438
1994-02-01 86.991114
1994-03-01 86.934302
1994-04-01 86.991241
1994-05-01 86.937103
1994-06-01 86.986449
1994-07-01 86.943102
1994-08-01 86.979874
1994-09-01 86.949762
1994-10-01 86.973491
1994-11-01 86.955619
1994-12-01 86.968314
1995-01-01 86.960037
1995-02-01 86.964675
1995-03-01 86.962925
1995-04-01 86.962481
1995-05-01 86.964501
1995-06-01 86.961435
1995-07-01 86.965107
In [554…
## Mean Absolute Percentage Error (MAPE) - Function Definition
return np.mean((np.abs(y_true-y_pred))/(y_true))*100
In [555…
rmse = mean_squared_error(rtest['Rose'],predicted_auto_ARIMA,squared=Fals
mape = mean_absolute_percentage_error(rtest['Rose'],predicted_auto_ARIMA)
print('RMSE:',rmse,'\nMAPE:',mape)
RMSE: 36.81224915333075
MAPE: 75.8371929255473
In [556…
resultsR0 = pd.DataFrame({'Test RMSE Rose': rmse,'Test MAPE Rose':mape}
,index=['ARIMA(2,1,3)'])
resultsR0.to_csv('rose_arima_auto_213.csv')
resultsR0
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 84/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [557…
## Plotting the Training data, Test data and the forecasted values
plt.plot(rtrain, label='Train')
plt.plot(rtest, label='Test')
plt.legend(loc='best')
plt.grid()
plt.savefig('rose_arima_auto_213.jpg', bbox_inches='tight');
In [ ]:
In [558…
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
In [559…
plot_acf(rtrain.diff(), missing='drop', alpha=0.05)
plt.savefig('rose_train_acf.jpg', bbox_inches='tight');
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 85/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [560…
plot_pacf(rtrain.diff().dropna(), zero=False, alpha=0.05, method='ywmle')
plt.title('Rose Training Data Partial Autocorrelation',fontsize=17)
plt.savefig('rose_train_pacf.jpg', bbox_inches='tight');
By looking at the above plots, we will take the value of p and q to be 2 and 2
respectively.
In [561…
manual_ARIMA = ARIMA(rtrain['Rose'], order=(2,1,2))
results_manual_ARIMA = manual_ARIMA.fit()
print(results_manual_ARIMA.summary())
SARIMAX Results
=========================================================================
=====
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 86/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
5.935
- 12-01-1990
Covariance Type: opg
=========================================================================
=====
-------------------------------------------------------------------------
-----
=========================================================================
==========
=========================================================================
==========
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (co
mplex-step).
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [562…
results_manual_ARIMA.plot_diagnostics();
plt.savefig('rose_train_manual_diag_plot.jpg', bbox_inches='tight');
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 87/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [564…
rmse = mean_squared_error(rtest['Rose'],predicted_manual_ARIMA,squared=Fa
mape = mean_absolute_percentage_error(rtest['Rose'],predicted_manual_ARIM
print('RMSE:',rmse,'\nMAPE:',mape)
RMSE: 36.871196615481196
MAPE: 76.05621270892694
In [565…
temp_resultsR2 = pd.DataFrame({'Test RMSE Rose': rmse,'Test MAPE Rose':ma
,index=['ARIMA(2,1,2)'])
resultsR2 = pd.concat([resultsR0,temp_resultsR2])
resultsR2
In [566…
resultsR0 = resultsR2.copy()
resultsR0
In [567…
plt.plot(rtrain, label='Train')
plt.plot(rtest, label='Test')
plt.legend(loc='best')
plt.savefig('rose_arima_manual_forecast.jpg', bbox_inches='tight');
In [568…
plot_acf(rtrain.diff(),title='Training Rose Data Autocorrelation',missing
In [569…
import itertools
p = q = range(0, 4)
d= range(1,2)
D = range(0,1)
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 89/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
Q [( [ ], [ ], [ ], ) ( p (p, , q))]
print('Examples of the parameter combinations for the Model are')
for i in range(1,len(pdq)):
In [570…
SARIMA_AIC = pd.DataFrame(columns=['param','seasonal', 'AIC'])
SARIMA_AIC
In [571…
import statsmodels.api as sm
SARIMA_model = sm.tsa.statespace.SARIMAX(rtrain['Rose'].values,
order=param,
seasonal_order=param_seasonal
enforce_stationarity=False,
enforce_invertibility=False)
results_SARIMA = SARIMA_model.fit(maxiter=1000)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
( ) ( )
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 90/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
SARIMA(0, 1, 0)x(3, 0, 1, 12) - AIC:851.748270274044
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 92/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
C W i )
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 93/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 95/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
In [572…
SARIMA_AIC.sort_values(by=['AIC']).head()
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 97/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [573…
import statsmodels.api as sm
auto_SARIMA = sm.tsa.statespace.SARIMAX(rtrain['Rose'],
order=(3, 1, 1),
seasonal_order=(3, 0, 2, 12),
enforce_stationarity=False,
enforce_invertibility=False)
results_auto_SARIMA = auto_SARIMA.fit(maxiter=1000)
print(results_auto_SARIMA.summary())
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
SARIMAX Results
=========================================================================
======================
- 12-01-1990
Covariance Type: opg
=========================================================================
=====
-------------------------------------------------------------------------
-----
=========================================================================
==========
=========================================================================
==========
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (co
mplex-step).
In [574…
results_auto_SARIMA.plot_diagnostics();
plt.savefig('rose_sarima_auto_30212_diag_plot.jpg', bbox_inches='tight')
Out[574…
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 99/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [576…
predicted_auto_SARIMA.summary_frame(alpha=0.05).head()
In [577…
predicted_auto_SARIMA1
<statsmodels.tsa.statespace.mlemodel.PredictionResultsWrapper at 0x7ffedd
Out[577…
d10a90>
In [578…
rmse = mean_squared_error(rtest['Rose'],predicted_auto_SARIMA.predicted_m
mape = mean_absolute_percentage_error(rtest['Rose'],predicted_auto_SARIMA
print('RMSE:',rmse,'\nMAPE:',mape)
RMSE: 18.88192892028345
MAPE: 36.375464857240615
In [579…
temp_resultsDf = pd.DataFrame({'Test RMSE Rose': rmse,'Test MAPE Rose':ma
,index=['SARIMA(3, 1, 1)(3, 0, 2, 12)'])
resultsDf = pd.concat([resultsR0,temp_resultsDf])
resultsDf
In [580…
resultsR0 = resultsDf.copy()
resultsR0.to_csv('rose_auto_sarima.csv')
resultsR0
In [581…
## Plotting the Training data, Test data and the forecasted values
plt.plot(rtrain, label='Train')
plt.plot(rtest, label='Test')
predicted_auto_SARIMA.predicted_mean.plot(label='SARIMA(3, 1, 1)(3, 0, 2,
plt.legend(loc='best')
plt.grid()
plt.savefig('rose_sarima_auto_30212.jpg', bbox_inches='tight');
In [582…
plot_acf(rtrain.diff(),title='Training Data Autocorrelation - Rose',missi
plot_pacf(rtrain.diff().dropna(),title='Training Data Partial Autocorrela
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 101/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [583…
import statsmodels.api as sm
manual_SARIMA = sm.tsa.statespace.SARIMAX(rtrain['Rose'],
order=(2,1,2),
seasonal_order=(2, 1, 2, 12),
enforce_stationarity=False,
enforce_invertibility=False)
results_manual_SARIMA = manual_SARIMA.fit(maxiter=1000)
print(results_manual_SARIMA.summary())
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
SARIMAX Results
=========================================================================
=================
- 12-01-1990
Covariance Type: opg
=========================================================================
=====
-------------------------------------------------------------------------
-----
=========================================================================
==========
=========================================================================
==========
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (co
mplex-step).
In [584…
import statsmodels.api as sm
manual_SARIMA = sm.tsa.statespace.SARIMAX(rtrain['Rose'],
order=(2,1,2),
seasonal_order=(3, 1, 2, 12),
enforce_stationarity=False,
enforce_invertibility=False)
results_manual_SARIMA1 = manual_SARIMA.fit(maxiter=1000)
print(results_manual_SARIMA1.summary())
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
i f d f MS ill b d
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 103/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
SARIMAX Results
=========================================================================
=================
- 12-01-1990
Covariance Type: opg
=========================================================================
=====
-------------------------------------------------------------------------
-----
=========================================================================
==========
=========================================================================
==========
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (co
mplex-step).
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 104/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [585…
results_manual_SARIMA.plot_diagnostics()
plt.savefig('rose_sarima_21212.jpg', bbox_inches='tight');
In [586…
results_manual_SARIMA1.plot_diagnostics()
plt.savefig('rose_sarima_31212.jpg', bbox_inches='tight');
predicted_manual_SARIMA1 = results_manual_SARIMA1.get_forecast(steps=len(
In [588…
rmse = mean_squared_error(rtest['Rose'],predicted_manual_SARIMA.predicted
mape = mean_absolute_percentage_error(rtest['Rose'],predicted_manual_SARI
print('RMSE:',rmse,'\nMAPE:',mape)
RMSE: 16.55096022982394
MAPE: 25 477542490098948
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 105/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
MAPE: 25.477542490098948
In [589…
rmse = mean_squared_error(rtest['Rose'],predicted_manual_SARIMA1.predicte
mape = mean_absolute_percentage_error(rtest['Rose'],predicted_manual_SARI
print('RMSE:',rmse,'\nMAPE:',mape)
RMSE: 15.357056655945197
MAPE: 22.955088595573685
In [590…
temp_resultsDf = pd.DataFrame({'Test RMSE Rose': [rmse],'Test MAPE Rose':
,index=['SARIMA(2,1,2)(3,1,2,12)'])
resultsDf = pd.concat([resultsDf,temp_resultsDf])
resultsDf
In [591…
resultsR0 = resultsDf.copy()
In [592…
plt.plot(rtrain, label='Train')
plt.legend(loc='best')
plt.savefig('rose_sarima_manual_forecast.jpg', bbox_inches='tight');
In [593…
plt.plot(rtrain, label='Train')
plt.legend(loc='best')
plt.savefig('rose_sarima_manual_forecast1.jpg', bbox_inches='tight');
In [594…
full_data_model = sm.tsa.statespace.SARIMAX(dfr['Rose'],
order=(2,1,2),
seasonal_order=(2,1,2,12),
enforce_stationarity=False,
enforce_invertibility=False)
results_full_data_model = full_data_model.fit(maxiter=1000)
print(results_full_data_model.summary())
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
SARIMAX Results
=========================================================================
=================
- 07-01-1995
Covariance Type: opg
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 107/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
Covariance Type: opg
=========================================================================
=====
-------------------------------------------------------------------------
-----
=========================================================================
==========
=========================================================================
==========
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (co
mplex-step).
In [596…
pred_full_manual_SARIMA_date = predicted_manual_SARIMA_full_data.summary_
pred_full_manual_SARIMA_date.head()
In [597…
rmse = mean_squared_error(dfr['Rose'],results_full_data_model.fittedvalue
print('RMSE of the Full Model',rmse)
In [598…
axis = dfr['Rose'].plot(label='Observed')
axis.set_xlabel('YearMonth')
axis.set_ylabel('Rose_Sales')
plt.legend(loc='best')
plt.grid();
In [599…
resultsDf
In [600…
resultsR0 = resultsDf.copy()
resultsR0.to_csv('rose_all_arima.csv')
In [602…
results_rose = pd.concat([resultsDf14, resultsR0], axis=0)
results_rose
Out[602… Test
Test RMSE Test MAPE
RMSE
Sparkling Rose
Rose
In [604…
results_rose.sort_values(by=['Test RMSE Rose']).to_csv('results_rose.csv'
In [601…
resultsDf.sort_values(by=['Test RMSE Rose'])
===========================
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 110/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [605…
strain.plot(grid=True);
In [606…
dftest = adfuller(strain,regression='ct')
The training data is non-stationary at 95% confidence level. Let us take a first level
of differencing to stationarize the Time Series.
In [607…
dftest = adfuller(strain.diff().dropna(),regression='ct')
In [608…
strain.diff().dropna().plot(grid=True);
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 111/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [609…
strain.info()
<class 'pandas.core.frame.DataFrame'>
dtypes: int64(1)
In [610…
## The following loop helps us in getting a combination of different para
## We have kept the value of d as 1 as we need to take a difference of th
import itertools
p = q = range(0, 4)
d= range(1,2)
for i in range(0,len(pdq)):
print('Model: {}'.format(pdq[i]))
Model: (0, 1, 0)
Model: (0, 1, 1)
Model: (0, 1, 2)
Model: (0, 1, 3)
Model: (1, 1, 0)
Model: (1, 1, 1)
Model: (1, 1, 2)
Model: (1, 1, 3)
Model: (2, 1, 0)
Model: (2, 1, 1)
Model: (2, 1, 2)
Model: (2, 1, 3)
Model: (3, 1, 0)
Model: (3, 1, 1)
Model: (3, 1, 2)
Model: (3, 1, 3)
In [611…
# Creating an empty Dataframe with column names only
ARIMA_AIC
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 112/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [612…
from statsmodels.tsa.arima.model import ARIMA
for param in pdq:# running a loop within the pdq parameters defined by it
ARIMA_model = ARIMA(strain['Sparkling'].values,order=param).fit()#fit
#using the parameters from the loop
ARIMA(0, 1, 0) - AIC:2267.6630357855465
ARIMA(0, 1, 1) - AIC:2263.060015591336
ARIMA(0, 1, 2) - AIC:2234.408323126579
ARIMA(0, 1, 3) - AIC:2233.9948577488954
ARIMA(1, 1, 0) - AIC:2266.6085393190087
ARIMA(1, 1, 1) - AIC:2235.7550946672563
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/st
atespace/sarimax.py:978: UserWarning: Non-invertible starting MA paramete
rs found. Using zeros as starting parameters.
ARIMA(1, 1, 2) - AIC:2234.5272004517738
ARIMA(1, 1, 3) - AIC:2235.60780915136
ARIMA(2, 1, 0) - AIC:2260.36574396809
ARIMA(2, 1, 1) - AIC:2233.777626315349
ARIMA(2, 1, 2) - AIC:2213.509212451414
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
ARIMA(2, 1, 3) - AIC:2232.8722214888976
ARIMA(3, 1, 0) - AIC:2257.72337899794
ARIMA(3, 1, 1) - AIC:2235.4989166426767
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
ARIMA(3, 1, 2) - AIC:2230.758014766513
ARIMA(3, 1, 3) - AIC:2221.4625120053342
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
In [613…
## Sort the above AIC values in the ascending order to get the parameters
ARIMA_AIC.sort_values(by='AIC',ascending=True).head()
10 (2, 1, 2) 2213.509212
15 (3, 1, 3) 2221.462512
14 (3, 1, 2) 2230.758015
11 (2, 1, 3) 2232.872221
9 (2, 1, 1) 2233.777626
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 113/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [614…
auto_ARIMA = ARIMA(strain, order=(2,1,2))
results_auto_ARIMA = auto_ARIMA.fit()
print(results_auto_ARIMA.summary())
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
SARIMAX Results
=========================================================================
=====
- 12-01-1990
Covariance Type: opg
=========================================================================
=====
-------------------------------------------------------------------------
-----
=========================================================================
==========
=========================================================================
==========
Warnings:
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 114/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
[1] Covariance matrix calculated using the outer product of gradients (co
mplex-step).
plt.savefig('spark_arima_diag_plot.jpg', bbox_inches='tight');
In [617…
## Mean Absolute Percentage Error (MAPE) - Function Definition
return np.mean((np.abs(y_true-y_pred))/(y_true))*100
In [618…
rmse = mean_squared_error(stest['Sparkling'],predicted_auto_ARIMA,squared
mape = mean_absolute_percentage_error(stest['Sparkling'],predicted_auto_A
print('RMSE:',rmse,'\nMAPE:',mape)
RMSE: 1299.9791952854541
MAPE: 47.09993235502316
In [619…
resultsDf = pd.DataFrame({'RMSE': rmse,'MAPE':mape}
,index=['ARIMA(2,1,2)'])
resultsDf.to_csv('spark_arima_auto_212.csv')
resultsDf
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 115/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [620…
plt.plot(strain, label='Train')
plt.plot(stest, label='Test')
plt.grid()
plt.legend(loc='best')
plt.savefig('spark_arima_auto_212.jpg', bbox_inches='tight');
In [621…
plot_acf(strain.diff(),missing='drop')
plt.savefig('spark_train_acf.jpg', bbox_inches='tight');
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 116/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [622…
plot_pacf(strain.diff().dropna(),title='Training Data Partial Autocorrela
plt.title('Sparkling Training Data Partial Autocorrelation', fontsize=17)
plt.savefig('spark_train_pacf.jpg', bbox_inches='tight');
By looking at the above plots, we will take the value of p and q to be 0 and 0
respectively.
In [623…
manual_ARIMA = ARIMA(strain['Sparkling'], order=(0,1,0))
results_manual_ARIMA = manual_ARIMA.fit()
print(results_manual_ARIMA.summary())
SARIMAX Results
=========================================================================
=====
- 12-01-1990
Covariance Type: opg
=========================================================================
=====
-------------------------------------------------------------------------
-----
=========================================================================
==========
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 117/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
=========================================================================
==========
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (co
mplex-step).
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [624…
results_manual_ARIMA.plot_diagnostics();
plt.savefig('spark_train_diag_plot.jpg', bbox_inches='tight');
In [626…
rmse = mean_squared_error(stest['Sparkling'],predicted_manual_ARIMA,squar
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 118/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
mape = mean_absolute_percentage_error(stest['Sparkling'],predicted_manual
print('RMSE:',rmse,'\nMAPE:',mape)
RMSE: 3864.2793518443914
MAPE: 201.32764950352743
In [627…
temp_resultsDf = pd.DataFrame({'RMSE': rmse,'MAPE':mape}
,index=['ARIMA(0,1,0)'])
resultsDf = pd.concat([resultsDf,temp_resultsDf])
resultsDf
In [628…
plt.plot(strain, label='Train')
plt.plot(stest, label='Test')
plt.grid()
plt.legend(loc='best')
plt.savefig('spark_manual_arima_010.jpg', bbox_inches='tight');
In [629…
plot_acf(strain.diff(),title='Training Data Autocorrelation',missing='dro
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 119/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [630…
import itertools
p = q = range(0, 4)
d= range(1,2)
D = range(0,1)
for i in range(1,len(pdq)):
In [631…
SARIMA_AIC = pd.DataFrame(columns=['param','seasonal', 'AIC'])
SARIMA_AIC
In [632…
import statsmodels.api as sm
SARIMA_model = sm.tsa.statespace.SARIMAX(strain['Sparkling'].valu
order=param,
seasonal_order=param_seasonal
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 120/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
enforce_stationarity=False,
enforce_invertibility=False)
results_SARIMA = SARIMA_model.fit(maxiter=1000)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 121/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 123/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
( , , ) ( , , , )
SARIMA(1, 1, 2)x(1, 0, 2, 12) - AIC:1555.5842471959754
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 125/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 126/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/base/m
odel.py:568: ConvergenceWarning: Maximum Likelihood optimization failed t
o converge. Check mle_retvals
ConvergenceWarning)
In [633…
SARIMA_AIC.sort_values(by=['AIC']).head(40)
In [634…
import statsmodels.api as sm
auto_SARIMA = sm.tsa.statespace.SARIMAX(strain['Sparkling'],
order=(3, 1, 1),
seasonal_order=(3, 0, 0, 12),
enforce_stationarity=False,
enforce_invertibility=False)
results_auto_SARIMA = auto_SARIMA.fit(maxiter=1000)
print(results_auto_SARIMA.summary())
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
SARIMAX Results
=========================================================================
==================
- 12-01-1990
Covariance Type: opg
=========================================================================
=====
-------------------------------------------------------------------------
-----
=========================================================================
==========
=========================================================================
==========
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (co
mplex-step).
In [635…
results_auto_SARIMA.plot_diagnostics()
plt.savefig('spark_sarima00312_diag_plot.jpg', bbox_inches='tight');
plt.show()
Out[635…
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 130/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [637…
predicted_auto_SARIMA.summary_frame(alpha=0.05).head()
In [638…
rmse = mean_squared_error(stest['Sparkling'],predicted_auto_SARIMA.predic
mape = mean_absolute_percentage_error(stest['Sparkling'],predicted_auto_S
print('RMSE:',rmse,'\nMAPE:',mape)
RMSE: 601.2438411947355
MAPE: 25.870694854181043
In [639…
temp_resultsDf = pd.DataFrame({'RMSE': rmse,'MAPE':mape}
,index=['SARIMA(3,1,1)(3,0,2,12)'])
resultsDf = pd.concat([resultsDf,temp_resultsDf])
resultsDf
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 131/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [640…
## Plotting the Training data, Test data and the forecasted values
plt.plot(strain, label='Train')
plt.plot(stest, label='Test')
plt.legend(loc='best')
plt.grid()
plt.savefig('spark_sarima_auto_30012.jpg', bbox_inches='tight');
In [641…
plot_acf(strain.diff(),title='Training Data Autocorrelation - Rose',missi
plot_pacf(strain.diff().dropna(),title='Training Data Partial Autocorrela
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 132/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [642…
import statsmodels.api as sm
manual_SARIMA = sm.tsa.statespace.SARIMAX(strain['Sparkling'],
order=(0,1,0),
seasonal_order=(1, 1, 1, 12),
enforce_stationarity=False,
enforce_invertibility=False)
results_manual_SARIMA = manual_SARIMA.fit(maxiter=1000)
print(results_manual_SARIMA.summary())
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
SARIMAX Results
=========================================================================
===================
- 12-01-1990
Covariance Type: opg
=========================================================================
=====
-------------------------------------------------------------------------
-----
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 133/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
=========================================================================
==========
=========================================================================
==========
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (co
mplex-step).
In [643…
import statsmodels.api as sm
manual_SARIMA = sm.tsa.statespace.SARIMAX(strain['Sparkling'],
order=(0,1,0),
seasonal_order=(2, 1, 2, 12),
enforce_stationarity=False,
enforce_invertibility=False)
results_manual_SARIMA1 = manual_SARIMA.fit(maxiter=1000)
print(results_manual_SARIMA1.summary())
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
SARIMAX Results
=========================================================================
======================
- 12-01-1990
Covariance Type: opg
=========================================================================
=====
-------------------------------------------------------------------------
-----
=========================================================================
==========
=========================================================================
==========
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (co
mplex-step).
In [644…
import statsmodels.api as sm
manual_SARIMA = sm.tsa.statespace.SARIMAX(strain['Sparkling'],
order=(0,1,0),
seasonal_order=(3, 1, 2, 12),
enforce_stationarity=False,
enforce_invertibility=False)
results_manual_SARIMA2 = manual_SARIMA.fit(maxiter=1000)
print(results_manual_SARIMA2.summary())
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
SARIMAX Results
=========================================================================
======================
- 12-01-1990
Covariance Type: opg
=========================================================================
=====
-------------------------------------------------------------------------
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 135/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
-----
=========================================================================
==========
=========================================================================
==========
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (co
mplex-step).
In [645…
results_manual_SARIMA.plot_diagnostics()
plt.savefig('spark_sarima_11112.jpg', bbox_inches='tight');
In [646…
results_manual_SARIMA1.plot_diagnostics()
plt.savefig('spark_sarima_21212.jpg', bbox_inches='tight');
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 136/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [647…
results_manual_SARIMA2.plot_diagnostics()
plt.savefig('spark_sarima_31212.jpg', bbox_inches='tight');
predicted_manual_SARIMA1 = results_manual_SARIMA1.get_forecast(steps=len(
predicted_manual_SARIMA2 = results_manual_SARIMA2.get_forecast(steps=len(
In [649…
rmse = mean_squared_error(stest['Sparkling'],predicted_manual_SARIMA.pred
mape = mean_absolute_percentage_error(stest['Sparkling'],predicted_manual
print('RMSE:',rmse,'\nMAPE:',mape)
RMSE: 1189.8357745333994
MAPE: 54.87253527143429
In [650…
rmse1 = mean_squared_error(stest['Sparkling'],predicted_manual_SARIMA1.pr
mape1 = mean_absolute_percentage_error(stest['Sparkling'],predicted_manua
print('RMSE:',rmse1,'\nMAPE:',mape1)
RMSE: 1757.7268040140987
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 137/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
MAPE: 81.78523135822165
In [651…
rmse2 = mean_squared_error(stest['Sparkling'],predicted_manual_SARIMA2.pr
mape2 = mean_absolute_percentage_error(stest['Sparkling'],predicted_manua
print('RMSE:',rmse2,'\nMAPE:',mape2)
RMSE: 1551.645770171487
MAPE: 71.56617248495675
In [652…
temp_resultsDf = pd.DataFrame({'RMSE': [rmse, rmse1, rmse2],'MAPE':[mape,
,index=['SARIMA(0,1,0)(3,1,2,12)', 'SARIMA(0,1
resultsDf = pd.concat([resultsDf,temp_resultsDf])
resultsDf
In [653…
resultsR0 = resultsDf.copy()
In [654…
plt.plot(strain, label='Train')
plt.legend(loc='best')
In [655
( i i )
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 138/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [655…
plt.plot(strain, label='Train')
plt.legend(loc='best')
In [656…
plt.plot(strain, label='Train')
plt.legend(loc='best')
In [657…
full_data_model = sm.tsa.statespace.SARIMAX(dfr['Rose'],
order=(2,1,2),
seasonal_order=(2,1,2,12),
enforce_stationarity=False,
enforce_invertibility=False)
results_full_data_model = full_data_model.fit(maxiter=1000)
print(results_full_data_model.summary())
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
SARIMAX Results
=========================================================================
=================
- 07-01-1995
Covariance Type: opg
=========================================================================
=====
-------------------------------------------------------------------------
-----
=========================================================================
==========
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 140/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
=========================================================================
==========
Warnings:
[1] Covariance matrix calculated using the outer product of gradients (co
mplex-step).
In [659…
pred_full_manual_SARIMA_date = predicted_manual_SARIMA_full_data.summary_
pred_full_manual_SARIMA_date.head()
In [660…
rmse = mean_squared_error(dfr['Rose'],results_full_data_model.fittedvalue
print('RMSE of the Full Model',rmse)
In [661…
axis = dfr['Rose'].plot(label='Observed')
axis.set_xlabel('YearMonth')
axis.set_ylabel('Rose_Sales')
plt.legend(loc='best')
plt.grid();
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 141/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [662…
resultsDf
In [663…
resultsS0 = resultsDf.copy()
resultsS0.to_csv('spark_all_arima.csv')
In [664…
results_all_spark = pd.concat([resultsDf14, resultsS0], axis=0)
results_all_spark
Out[664… Test
Test RMSE
RMSE RMSE MAPE
Sparkling
Rose
In [665…
results_all_spark.to_csv('results_all_spark.csv')
===========================
===========================
MovingAverage.head()
Out[761… Rose
YearMonth
1980-01-01 112.0
1980-02-01 118.0
1980-03-01 129.0
1980-04-01 99.0
1980-05-01 116.0
MovingAverage.head()
MovingAverage.tail()
YearMonth
YearMonth
In [774…
#Creating train and test set
trailing_MovingAverage_train=MovingAverage[MovingAverage.index<'1991']
trailing_MovingAverage_test=MovingAverage[MovingAverage.index>='1991']
MovingAverage.head()
In [764…
import pandas as pd
ts = pd.to_datetime(datelist, format="%Y-%m-%d")
MovingAverage['forecast_12'] = MovingAverage['Rose'].rolling(2).mean()
In [779…
MovingAverage.tail()
MovingAverage.shape
MovingAverage.fillna(0)
MovingAverage['Rose'].iloc[0:2]
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 144/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
(199, 3)
Out[779…
1980-01-01 112.0
Out[779…
1980-02-01 118.0
In [780…
import math
k = 2
yhat = []
for i in range(0,199):
tmp = 0.0
tmp = math.fsum(MovingAverage['Rose'].iloc[i:i+2])
yhat.append(tmp/k)
yhat
[115.0,
Out[780…
123.5,
114.0,
107.5,
142.0,
143.0,
123.5,
167.0,
176.0,
148.5,
208.5,
196.5,
127.5,
126.5,
110.5,
99.5,
114.5,
174.5,
218.0,
166.0,
129.5,
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 145/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
147.5,
190.0,
157.5,
83.0,
79.5,
89.5,
112.0,
124.0,
119.0,
117.0,
111.5,
109.0,
123.0,
151.5,
122.0,
91.5,
111.5,
100.0,
93.0,
104.5,
108.5,
116.5,
114.5,
100.0,
115.0,
149.5,
126.0,
86.5,
98.5,
99.5,
89.0,
89.0,
87.0,
114.5,
118.5,
101.5,
123.5,
149.0,
110.0,
71.5,
103.0,
108.5,
100.5,
91.5,
81.0,
95.0,
96.5,
99.0,
115.5,
126.0,
93.0,
61.0,
66.0,
69.0,
73.5,
71.5,
88.5,
114.0,
108.5,
92.0,
96.0,
124.0,
99.5,
61.5,
67 5
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 146/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
67.5,
78.0,
89.5,
83.5,
80.5,
80.0,
87.0,
100.5,
98.0,
126.5,
110.0,
89.0,
92.5,
68.0,
66.5,
75.0,
81.0,
78.0,
89.5,
109.0,
108.0,
117.5,
103.0,
65.5,
74.5,
81.5,
73.5,
82.0,
88.5,
80.0,
80.5,
87.0,
98.0,
123.0,
90.0,
56.0,
71.0,
75.0,
73.0,
72.5,
77.0,
74.0,
76.5,
74.0,
87.5,
121.0,
93.0,
54.5,
60.5,
65.5,
62.5,
62.5,
80.5,
75.5,
63.0,
67.0,
68.5,
90.0,
70.0,
40.5,
51.5,
54.5,
53.0,
54.0,
61.0,
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 147/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
,
59.5,
49.0,
48.5,
54.5,
74.5,
62.0,
36.5,
43.0,
45.5,
43.0,
48.0,
56.0,
55.5,
50.0,
49.0,
50.0,
62.5,
53.5,
32.5,
38.5,
45.0,
46.0,
44.5,
45.16666666666667,
45.5,
45.83333333333333,
48.5,
57.0,
73.5,
57.0,
34.5,
42.0,
48.5,
40.0,
34.0,
51.0,
nan,
nan,
nan,
nan,
nan,
nan,
nan,
nan,
nan,
nan,
nan,
nan,
nan]
In [669…
## Plotting on both the Training and Test data
plt.figure(figsize=(16,9))
plt.plot(trailing_MovingAverage_train['Rose'], label='Train')
plt.plot(trailing_MovingAverage_test['Rose'], label='Test')
plt.legend(loc = 'best')
plt.grid();
dfr1.head()
dfr1.tail()
YearMonth
YearMonth
In [41]:
## FORECAST ON NEXT 12 MONTHS
plt.figure(figsize=(16,9))
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 149/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
plt.figure(figsize=(16,9))
plt.legend(loc='best')
plt.grid()
plt.savefig('rose_forecast_2ptma.jpg', bbox_inches='tight');
model_TES_am = model_TES_am.fit()
print('')
print(model_TES_am.params)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
/t d l 527 V l W i N f i f ti id d
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 150/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [59]:
# Forecasting using this model for the duration of the test set
TES_predict_am_damp_spark = model_TES_am.forecast(steps=12)
TES_predict_am_damp_spark
prediction_1 = TES_predict_am_damp_spark
TES_predict_am_damp_spark.to_csv('spark_forecast_tes.csv')
1995-08-01 1931.441163
Out[59]:
1995-09-01 2351.978730
1995-10-01 3179.461286
1995-11-01 3918.068026
1995-12-01 5985.901895
1996-01-01 1357.574635
1996-02-01 1599.153662
1996-03-01 1830.307852
1996-04-01 1791.018797
1996-05-01 1641.838880
1996-06-01 1556.371947
1996-07-01 1965.995789
In [24]:
plt.rcParams["figure.figsize"] = [16,9]
In [19]:
pred_1_df = pd.DataFrame({'lower_CI':prediction_1 - 1.96*np.std(model_TES
'prediction':prediction_1,
In [26]:
## Plotting the Training data, Test data and the forecasted values
ax.fill_between(pred_1_df.index,pred_1_df['lower_CI'], pred_1_df['upper_c
plt.legend(loc='best')
plt.grid()
plt.savefig('spark_forecast_tes.jpg', bbox_inches='tight');
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 151/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
In [51]:
# Initializing the Double Exponential Smoothing Model
model_TES_am = ExponentialSmoothing(dfr,trend='add',seasonal='additive',i
# Fitting the model
model_TES_am = model_TES_am.fit()
print('')
print(model_TES_am.params)
/Users/chetan/DS/anaconda3/lib/python3.7/site-packages/statsmodels/tsa/ba
se/tsa_model.py:527: ValueWarning: No frequency information was provided,
so inferred frequency MS will be used.
% freq, ValueWarning)
In [56]:
# Forecasting using this model for the duration of the test set
TES_predict = model_TES_am.forecast(steps=12)
TES_predict
prediction_1 = TES_predict
TES_predict.to_csv('rose_forecast_tes.csv')
1995-08-01 50.020850
Out[56]:
1995-09-01 46.499553
1995-10-01 45.936706
1995-11-01 60.717805
1995-12-01 97.662698
1996-01-01 13.892676
1996-02-01 24.294943
1996-03-01 31 911300
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 152/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
1996-03-01 31.911300
1996-04-01 24.733799
1996-05-01 28.122724
1996-06-01 33.556066
1996-07-01 44.093271
In [53]:
plt.rcParams["figure.figsize"] = [16,9]
In [54]:
pred_1_df = pd.DataFrame({'lower_CI':prediction_1 - 1.96*np.std(model_TES
'prediction':prediction_1,
In [55]:
## Plotting the Training data, Test data and the forecasted values
ax.fill_between(pred_1_df.index,pred_1_df['lower_CI'], pred_1_df['upper_c
plt.legend(loc='best')
plt.grid()
plt.savefig('rose_forecast_tes.jpg', bbox_inches='tight');
In [ ]:
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 153/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 154/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 155/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 156/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 157/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 158/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 159/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 160/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 161/162
2/17/22, 9:59 PM time-series-forecasting/time_series_forecasting_jupyter_code.ipynb at main · chetandudhane/time-series-forecasting · GitHub
https://github.com/chetandudhane/time-series-forecasting/blob/main/time_series_forecasting_jupyter_code.ipynb 162/162