Chapter 8 - Forecasting
Chapter 8 - Forecasting
Chapter 8: Forecasting
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
0 1 3002666 105 1
1 2 4401553 145 0
2 3 3205279 118 1
3 4 4245349 130 0
4 5 3001940 98 1
5 6 4377766 156 0
6 7 2798343 98 1
7 8 4303668 144 0
8 9 2958185 112 1
9 10 3623386 120 0
wsb_df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 48 entries, 0 to 47
Data columns (total 4 columns):
Month 48 non-null int64
Sale Quantity 48 non-null int64
Promotion Expenses 48 non-null int64
Competition Promotion 48 non-null int64
dtypes: int64(4)
memory usage: 1.6 KB
0 3002666 nan
1 4401553 nan
2 3205279 nan
3 4245349 nan
4 3001940 nan
5 4377766 nan
6 2798343 nan
7 4303668 nan
8 2958185 nan
9 3623386 nan
10 3279115 nan
11 2843766 nan
12 4447581 3503418.00
13 3675305 3623827.58
14 3477156 3563306.92
15 3720794 3585963.33
16 3834086 3542250.42
17 3888913 3611595.92
18 3871342 3570858.17
19 3679862 3660274.75
20 3358242 3608290.92
21 3361488 3641629.00
22 3670362 3619804.17
23 3123966 3652408.08
24 4634047 3675758.08
25 3772879 3691296.92
26 3187110 3699428.08
27 3093683 3675257.58
28 4557363 3622998.33
29 3816956 3683271.42
30 4410887 3677275.00
31 3694713 3722237.08
32 3822669 3723474.67
33 3689286 3762176.92
34 3728654 3789493.42
35 4732677 3794351.08
36 3216483 3928410.33
37 3453239 3810280.00
38 5431651 3783643.33
39 4241851 3970688.42
40 3909887 4066369.08
41 3216438 4012412.75
42 4222005 3962369.58
43 3621034 3946629.42
44 5162201 3940489.50
45 4627177 4052117.17
46 4623945 4130274.75
47 4599368 4204882.33
plt.figure( figsize=(10,4))
plt.xlabel( "Months" )
plt.ylabel( "Quantity" )
plt.plot( wsb_df['Sale Quantity'][12:] );
plt.plot( wsb_df['mavg_12'][12:], '.' );
plt.legend();
14.04
734725.8359239782
pd.options.display.float_format = '{:.2f}'.format
wsb_df[36:]
11.15
wsb_df['seasonal'] = ts_decompse.seasonal
wsb_df['trend'] = ts_decompse.trend
vimana_df.head(5)
Month demand
0 1 457
1 2 439
2 3 404
3 4 392
4 5 403
vimana_df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 37 entries, 0 to 36
Data columns (total 2 columns):
Month 37 non-null int64
demand 37 non-null int64
dtypes: int64(2)
memory usage: 672.0 bytes
Building AR Model
ar_model.summary2()
Df Model: 2 Sample: 0
Df Residuals: 28 0
AIC: 371.5300
forecast_31_37
get_mape( vimana_df.demand[30:],
forecast_31_37 )
19.12
Df Model: 2 Sample: 0
Df Residuals: 28 0
AIC: 374.5946
17.8
Df Model: 3 Sample: 0
Df Residuals: 27 0
AIC: 371.6916
20.27
store_df = pd.read_excel('store.xls')
Date demand
0 2014-10-01 15
1 2014-10-02 7
2 2014-10-03 8
3 2014-10-04 10
4 2014-10-05 13
store_df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 115 entries, 0 to 114
Data columns (total 2 columns):
Date 115 non-null datetime64[ns]
demand 115 non-null int64
dtypes: datetime64[ns](1), int64(1)
memory usage: 1.9 KB
demand
Date
2015-01-19 18
2015-01-20 22
2015-01-21 22
2015-01-22 21
2015-01-23 17
adfuller_test( store_df.demand )
8.5.4.3 Differencing
store_df.head(5)
demand demand_diff
Date
2014-10-01 15 nan
2014-10-02 7 -8.00
2014-10-03 8 1.00
2014-10-04 10 2.00
2014-10-05 13 3.00
store_diff_df = store_df.dropna()
store_train = store_df[0:100]
store_test = store_df[100:]
Df Model: 3 Sample: 1
Df Residuals: 96 0
AIC: 521.7706
acf_plot = plot_acf(arima_model.resid,
lags = 20)
store_predict
get_mape( store_df.demand[100:],
store_predict )
24.17