-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpredweek.py
186 lines (139 loc) · 5.06 KB
/
predweek.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pylab as hac
import matplotlib.pyplot
import csv
from datetime import datetime
#%matplotlib inline
from matplotlib.pylab import rcParams
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.stattools import acf, pacf
from statsmodels.tsa.arima_model import ARIMA
def main():
rcParams['figure.figsize'] = 15, 6
# data = pd.read_csv('weeklyrating.csv')
# print data.head()
# print '\n Data Types:'
# print data.dtypes
dateparse = lambda dates: pd.datetime.strptime(dates, '%Y-%m-%d')
# dateparse('1962-01')\n",
data = pd.read_csv('weeklyrating1.csv', parse_dates=['From Date'], index_col=['From Date'], date_parser=dateparse)
data1 = pd.read_csv('weeklyrating.csv', parse_dates=['To Date'], index_col=['To Date'], date_parser=dateparse)
#data1 = pd.read_csv('weeklyrating.csv', parse_dates=['From Date'], index_col=['From Date'], date_parser=dateparse)
#data = pd.read_csv('weeklyrating.csv', parse_dates=['From_Date','To_Date'], index_col=['From_Date','To_Date'], date_parser=dateparse)
#data = pd.read_csv('AirPassengers.csv', parse_dates='Month', index_col='Month',date_parser=dateparse)
# print data.index
# print data.head()
ts = data['Rating']
ts1 = data1['Rating']
#ts = data1['Rating']
#print ts.head(10)
hac.ylim(0,10)
#hac.plot(ts)
#hac.show()
ts_log = np.log(ts)
#hac.plot(ts_log)
#hac.show()
#moving_avg = pd.rolling_mean(ts_log,12)
# hac.plot(moving_avg, color='red')
# hac.plot(ts_log)
# hac.show();
#ts_log_moving_avg_diff = ts_log - moving_avg
#print ts_log_moving_avg_diff.head(12)
#ts_log_moving_avg_diff.dropna(inplace=True)
expwighted_avg = pd.ewma(ts_log, halflife=12)
# hac.plot(ts_log)
# hac.plot(expwighted_avg, color='red')
# hac.show()
ts_log_diff = ts_log - ts_log.shift()
#hac.plot(ts_log_diff)
#hac.show()
#Decomposition
# decomposition = seasonal_decompose(ts_log)
# trend = decomposition.trend
# seasonal = decomposition.seasonal
# residual = decomposition.resid
ts_log_ewma_diff = ts_log - expwighted_avg
#Original
# hac.subplot(411)
# hac.plot(ts_log, label='Original')
# hac.legend(loc='best')
# hac.subplot(412)
# hac.plot(trend, label='Trend')
# hac.legend(loc='best')
# hac.subplot(413)
# hac.plot(seasonal,label='Seasonality')
# hac.legend(loc='best')
# hac.subplot(414)
# hac.plot(residual, label='Residuals')
# hac.legend(loc='best')
# hac.tight_layout()
# hac.show()
# lag_acf = acf(expwighted_avg, nlags=20)
# lag_pacf = pacf(expwighted_avg, nlags=20, method='ols')
# lag_acf = acf(ts_log_ewma_diff, nlags=20)
# lag_pacf = pacf(ts_log_ewma_diff, nlags=20, method='ols')
ts_diff = ts - ts.shift()
lag_acf = acf(ts_log_diff, nlags=20)
lag_pacf = pacf(ts_log_ewma_diff, nlags=20, method='ols')
#lag_pacf = pacf(ts_log_ewma_diff, nlags=20)
#Plot ACF:
# hac.subplot(121)
# hac.plot(lag_acf)
# hac.axhline(y=0,linestyle='--',color='gray')
# hac.axhline(y=-1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
# hac.axhline(y=1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
# hac.title('Autocorrelation Function')
# #Plot PACF:
# hac.subplot(122)
# hac.plot(lag_pacf)
# hac.axhline(y=0,linestyle='--',color='gray')
# hac.axhline(y=-1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
# hac.axhline(y=1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
# hac.title('Partial Autocorrelation Function')
# hac.tight_layout()
#hac.show()
#AR Model
# model = ARIMA(ts_log, order=(2, 1, 0))
# result_AR = model.fit(disp=-1)
# hac.plot(ts_log_diff)
# hac.plot(result_AR.fittedvalues, color='red')
# hac.title('RSS: %.4f'% sum((result_AR.fittedvalues-ts_log_diff)**2))
# hac.show()
#MA Model
# model = ARIMA(ts_log, order=(0, 1, 2))
# results_MA = model.fit(disp=-1)
# hac.plot(ts_log_diff)
# hac.plot(results_MA.fittedvalues, color='red')
# hac.title('RSS: %.4f'% sum((results_MA.fittedvalues-ts_log_diff)**2))
# hac.show()
#Combined
model = ARIMA(ts_log, order=(8, 1, 1))
results_ARIMA = model.fit(disp=-1)
# hac.plot(ts_log_diff)
# hac.plot(results_ARIMA.fittedvalues, color='red')
# hac.title('RSS: %.4f'% sum((results_ARIMA.fittedvalues-ts_log_diff)**2))
# hac.show()
predictions_ARIMA_diff = pd.Series(results_ARIMA.fittedvalues, copy=True)
#print(predictions_ARIMA_diff.head())
predictions_ARIMA_diff_cumsum =predictions_ARIMA_diff.cumsum()
#print(predictions_ARIMA_diff_cumsum.head())
predictions_ARIMA_log = pd.Series(ts_log.ix[0], index=ts_log.index)
predictions_ARIMA_log = predictions_ARIMA_log.add(predictions_ARIMA_diff_cumsum, fill_value=0)
#print(predictions_ARIMA_log.head())
predictions_ARIMA = np.exp(predictions_ARIMA_log)
#print predictions_ARIMA
hac.xlabel('Month')
hac.ylabel('Rating')
#hac.title('RMSE: %.4f'% np.sqrt(sum((predictions_ARIMA-ts)**2)/len(ts)))
hac.title('Time Series Forecasting Review')
hac.legend(loc="upper left")
hac.plot(ts1)
hac.plot(predictions_ARIMA, linestyle='--')
hac.show()
#save figure to png
hac.savefig("premajic.png")
print("\nForecasting plotted")
if __name__ == "__main__":
main()