-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathanalysis.py
168 lines (136 loc) · 5.51 KB
/
analysis.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""
Analysis of Sales data
Dataset
-------
The given dataset contains monthly total sales of a company for the period 2013-2016.
Objectives
---------
1. To analyse the sales data and understand the performance of the company.
2. Find patterns and construct a model to forecast future sales.
"""
from time_series import TimeSeries
# Imports for data visualization
import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
from matplotlib.dates import DateFormatter
from matplotlib import dates as mpld
# Seasonal Decompose
from statsmodels.tsa.seasonal import seasonal_decompose
# Holt-Winters or Triple Exponential Smoothing model
from statsmodels.tsa.holtwinters import ExponentialSmoothing
register_matplotlib_converters()
ts = TimeSeries('dataset/monthly_sales.csv', train_size=0.8)
print("Sales Data\n")
print(ts.data.describe())
print("\nHead and Tail of the time series\n")
print(ts.data.head(5).iloc[:, 1:])
print(ts.data.tail(5).iloc[:, 1:])
# Plot of raw time series data
plt.plot(ts.data.index, ts.data.sales)
plt.gcf().autofmt_xdate()
date_format = mpld.DateFormatter('%Y-%m')
plt.gca().xaxis.set_major_formatter(date_format)
plt.title("Sales Data Analysis (2013-2016)")
plt.xlabel("Time")
plt.ylabel("Sales")
"""
Seasonal Decompose of the time series
-------------------------------------
Seasonal decompose is a method used to decompose the components of a time series into the following:
- Level - average value in the series.
- Trend - increasing or decreasing value in the series.
- Seasonality - repeating short-term cycle in the series.
- Noise - random variation in the series.
The analysis of the components individually provide better insights for model selection.
"""
result_add = seasonal_decompose(
ts.data.iloc[:, 1], period=12, model='additive')
result_add.plot()
plt.gcf().autofmt_xdate()
date_format = mpld.DateFormatter('%y-%m')
plt.gca().xaxis.set_major_formatter(date_format)
result_mul = seasonal_decompose(
ts.data.iloc[:, 1], period=12, model='multiplicative')
result_mul.plot()
plt.gcf().autofmt_xdate()
date_format = mpld.DateFormatter('%y-%m')
plt.gca().xaxis.set_major_formatter(date_format)
plt.show()
"""
Observations from Seasonal Decompose
------------------------------------
1. The time series seems to roughly have a constant seasonality but has an overall **increasing trend**.
2. A slightly decreasing trend is observed till 2014-07 after that an increasing trend is observed.
Model Selection
---------------
From the above observations we can evidently conclude that **Holt-Winter additive model** would
be an appropriate choice as there is a constant seasonality component along with an increasing trend.
"""
# Scaling down the data by a factor of 1000
ts.set_scale(1000)
# Training the model
model = ExponentialSmoothing(ts.train, trend='additive',
seasonal='additive', seasonal_periods=12).fit(damping_slope=1)
plt.plot(ts.train.index, ts.train, label="Train")
plt.plot(ts.test.index, ts.test, label="Actual")
# Create a 5 year forecast
plt.plot(model.forecast(60), label="Forecast")
plt.legend(['Train', 'Actual', 'Forecast'])
plt.gcf().autofmt_xdate()
date_format = mpld.DateFormatter('%Y-%m')
plt.gca().xaxis.set_major_formatter(date_format)
plt.title("Sales Data Analysis (2013-2016)")
plt.xlabel("Time")
plt.ylabel("Sales (x1000)")
plt.show()
"""
Validation of the model
-----------------------
Let's do a brief comparison between the additive and the multiplicative models.
"""
ts = TimeSeries('dataset/monthly_sales.csv', train_size=0.8)
# Additive model
model_add = ExponentialSmoothing(
ts.data.iloc[:, 1], trend='additive', seasonal='additive', seasonal_periods=12, damped=True).fit(damping_slope=0.98)
prediction = model_add.predict(
start=ts.data.iloc[:, 1].index[0], end=ts.data.iloc[:, 1].index[-1])
plt.plot(ts.data.iloc[:, 1].index, ts.data.iloc[:, 1], label="Train")
plt.plot(ts.data.iloc[:, 1].index, prediction, label="Model")
plt.plot(model_add.forecast(60))
plt.legend(['Actual', 'Model', 'Forecast'])
plt.gcf().autofmt_xdate()
date_format = mpld.DateFormatter('%Y-%m')
plt.gca().xaxis.set_major_formatter(date_format)
plt.title("Sales Data Analysis (2013-2016)")
plt.xlabel("Time")
plt.ylabel("Sales")
plt.show()
# Multiplicative model
model_mul = ExponentialSmoothing(
ts.data.iloc[:, 1], trend='additive', seasonal='multiplicative', seasonal_periods=12, damped=True).fit()
prediction = model_mul.predict(
start=ts.data.iloc[:, 1].index[0], end=ts.data.iloc[:, 1].index[-1])
plt.plot(ts.data.iloc[:, 1].index, ts.data.iloc[:, 1], label="Train")
plt.plot(ts.data.iloc[:, 1].index, prediction, label="Model")
plt.plot(model_mul.forecast(60))
plt.legend(['Actual', 'Model', 'Forecast'])
plt.gcf().autofmt_xdate()
date_format = mpld.DateFormatter('%Y-%m')
plt.gca().xaxis.set_major_formatter(date_format)
plt.title("Sales Data Analysis (2013-2016)")
plt.xlabel("Time")
plt.ylabel("Sales")
plt.show()
print(model_add.summary())
print(model_mul.summary())
"""
Conclusion of the analysis
--------------------------
From the model summary obtained it is clear that the sum of squared errors (SSE) for
the additive model (5088109579.122) < the SSE for the multiplicative(5235252441.242) model.
Hence the initial assumption that seasonality is roughly constant and therefore choosing
additive model was appropriate.
Note: The forecast made using multiplicative model seems to be unrealistic since the
variance between the high and low on an average is 100000 which is somewhat unexpected in
real world sales compared to 63000 incase of additive model.
"""