-
Notifications
You must be signed in to change notification settings - Fork 0
/
es_weather.py
233 lines (198 loc) · 10.3 KB
/
es_weather.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error, r2_score
from statsmodels.tsa.holtwinters import SimpleExpSmoothing, Holt, ExponentialSmoothing
import warnings
warnings.filterwarnings("ignore")
# Pre-processing of the data
df_raw = pd.read_csv('assets/hourly_load&weather_data.csv', header=None, skiprows=1) # loading raw data from the CSV
df_raw_array = df_raw.values # numpy array
y_test = df_raw[1]/1000
x_test = np.delete(df_raw_array, 0, 1)
x_test = np.delete(x_test, 6, 1)
# print(x_test)
y_test = np.array(y_test)
# print("y_test: ", y_test.shape, "\n", y_test, "\n")
def single_exponential_smoothing(alpha, y_test):
# Accounts only for the level of the series
# Simple Exponential Smoothing
ses_model1 = SimpleExpSmoothing(endog=y_test).fit(smoothing_level=alpha, optimized=True)
y_pred_ses = ses_model1.predict(328).rename(r'$\alpha=%s$' % ses_model1.model.params['smoothing_level'])
fig = plt.figure(figsize=(60, 8))
y_pred_ses[1:].plot(color='grey', legend=True)
ses_model1.fittedvalues.plot(color='grey')
plt.title("Single Exponential Smoothing")
plt.show()
fig.savefig('results/SES_weather/final_output.jpg', bbox_inches='tight')
# print("Predicted values: ", y_pred, "\n")
mse_ses = mean_squared_error(y_test[327:-1]*1000, y_pred_ses*1000)
rmse_ses = mean_squared_error(y_pred_ses*1000, y_test[327:-1]*1000, squared=False)
r2_ses = r2_score(y_test[327:-1]*1000, y_pred_ses*1000)
# Storing the result in a file: 'load_forecasting_result.txt'
predicted_test_result = y_pred_ses
np.savetxt('results/SES_weather/predicted_values.txt', predicted_test_result)
actual_test_result = y_test
np.savetxt('results/SES_weather/test_values.txt', actual_test_result)
return mse_ses, rmse_ses, r2_ses, y_pred_ses
def double_exponential_smoothing(alpha, beta, y_test):
# Accounts for level + trend in the data
des_model = Holt(y_test).fit(smoothing_level=alpha, smoothing_trend=beta, optimized=False)
y_pred_des = des_model.predict(328).rename("Holt's Linear")
fig = plt.figure(figsize=(60, 8))
des_model.fittedvalues.plot(color='grey')
y_pred_des.plot(color='grey', legend=True)
fig.savefig('results/DES_weather/final_output.jpg', bbox_inches='tight')
plt.title("Holt's Method/Double Exponential Smoothing")
plt.show()
# print("Predicted values: ", y_pred_des, "\n")
mse_des = mean_squared_error(y_test[327:-1]*1000, y_pred_des*1000)
rmse_des = mean_squared_error(y_pred_des*1000, y_test[327:-1]*1000, squared=False)
r2_des = r2_score(y_test[327:-1]*1000, y_pred_des*1000)
np.savetxt('results/DES_weather/predicted_values_model.txt', y_pred_des)
actual_test_result = y_test
np.savetxt('results/DES_weather/test_values.txt', actual_test_result)
# Three models with different parameters
# #Model 1: Providing the model with the values of hyperparameters (alpha, beta)
# des_model1 = Holt(y_test).fit(smoothing_level=alpha, smoothing_trend=beta, optimized=False)
# y_pred_des1 = des_model1.predict(31924).rename("Holt's Linear")
#
# #Model 2: Exponential Model with same alpha & beta
# des_model2 = Holt(y_test, exponential=True).fit(smoothing_level=alpha, smoothing_trend=beta, optimized=False)
# y_pred_des2 = des_model2.predict(31924).rename("Exponential")
#
# #Model 3: Optimising the dampening parameter with same alpha & beta
# des_model3 = Holt(y_test, damped_trend=True).fit(smoothing_level=alpha, smoothing_trend=beta)
# y_pred_des3 = des_model3.predict(31924).rename("Additive damped trend")
#
# fig = plt.figure(figsize=(60, 8))
# des_model1.fittedvalues.plot(color='blue')
# y_pred_des1.plot(color='blue', legend=True)
# des_model2.fittedvalues.plot(color='red')
# y_pred_des2.plot(color='red', legend=True)
# des_model3.fittedvalues.plot(color='green')
# y_pred_des3.plot(color='green', legend=True)
# fig.savefig('results/DES/final_output.jpg', bbox_inches='tight')
# plt.title("Holt's Method/Double Exponential Smoothing")
# plt.show()
#
# print("Predicted values (Model 1): ", y_pred_des1, "\n")
# print("Predicted values (Model 2): ", y_pred_des2, "\n")
# print("Predicted values (Model 3): ", y_pred_des3, "\n")
# mse_des1 = mean_squared_error(y_test[31923:-1], y_pred_des1)
# mse_des2 = mean_squared_error(y_test[31923:-1], y_pred_des2)
# mse_des3 = mean_squared_error(y_test[31923:-1], y_pred_des3)
#
# np.savetxt('results/DES/predicted_values_model1.txt', y_pred_des1)
# np.savetxt('results/DES/predicted_values_model2.txt', y_pred_des2)
# np.savetxt('results/DES/predicted_values_model3.txt', y_pred_des3)
# actual_test_result = y_test
# np.savetxt('results/DES/test_values.txt', actual_test_result)
#
# return mse_des1, mse_des2, mse_des3
return mse_des, rmse_des, r2_des, y_pred_des
def triple_exponential_smoothing(season, y_test):
# Accounts for level + trend + seasonality in the data
# Three models with different parameters
# Model 1: Additive trend + season with box-cox transformation
tes_model = ExponentialSmoothing(y_test, seasonal_periods=season, trend='add', seasonal='add').fit(use_boxcox=True)
y_pred_tes = tes_model.predict(328).rename("TES")
fig = plt.figure(figsize=(60, 8))
tes_model.fittedvalues.plot(color='grey')
y_pred_tes.plot(color='grey', legend=True)
fig.savefig('results/TES_weather/final_output.jpg', bbox_inches='tight')
plt.title("Holt-Winters' Method/Triple Exponential Smoothing")
plt.show()
# print("Predicted values: ", y_pred_tes, "\n")
mse_tes = mean_squared_error(y_test[327:-1]*1000, y_pred_tes*1000)
rmse_tes = mean_squared_error(y_pred_tes*1000, y_test[327:-1]*1000, squared=False)
r2_tes = r2_score(y_test[327:-1]*1000, y_pred_tes*1000)
np.savetxt('results/TES_weather/predicted_values_model.txt', y_pred_tes)
actual_test_result = y_test
np.savetxt('results/TES_weather/test_values.txt', actual_test_result)
# #Three models with different parameters
#
# #Model 1: Additive trend + season with box-cox transformation
# tes_model1 = ExponentialSmoothing(y_test, seasonal_periods=season, trend='add', seasonal='add').fit(use_boxcox=True)
# y_pred_tes1 = tes_model1.predict(31924).rename("Model 1")
#
# #Model 2: Additive trend + Multiplicative season with box-cox transformation
# tes_model2 = ExponentialSmoothing(y_test, seasonal_periods=season, trend='add', seasonal='mul').fit(use_boxcox=True)
# y_pred_tes2 = tes_model2.predict(31924).rename("Model 2")
#
# #Model 3: Damped trend + Additive season with box-cox transformation
# tes_model3 = ExponentialSmoothing(y_test, seasonal_periods=season, trend='add', seasonal='add', damped_trend=True).fit(use_boxcox=True)
# y_pred_tes3 = tes_model3.predict(31924).rename("Model 3")
#
# # Model 4: Damped trend + Multiplicative season with box-cox transformation
# tes_model4 = ExponentialSmoothing(y_test, seasonal_periods=season, trend='add', seasonal='mul',
# damped_trend=True).fit()
# y_pred_tes4 = tes_model4.predict(31924).rename("Model 4")
#
# fig = plt.figure(figsize=(60, 8))
# tes_model1.fittedvalues.plot(color='blue')
# y_pred_tes1.plot(color='blue', legend=True)
# tes_model2.fittedvalues.plot(color='red')
# y_pred_tes2.plot(color='red', legend=True)
# tes_model3.fittedvalues.plot(color='green')
# y_pred_tes3.plot(color='green', legend=True)
# tes_model4.fittedvalues.plot(color='yellow')
# y_pred_tes4.plot(color='yellow', legend=True)
# fig.savefig('results/TES/final_output.jpg', bbox_inches='tight')
# plt.title("Holt-Winters' Method/Triple Exponential Smoothing")
# plt.show()
#
# print("Predicted values (Model 1): ", y_pred_tes1, "\n")
# print("Predicted values (Model 2): ", y_pred_tes2, "\n")
# print("Predicted values (Model 3): ", y_pred_tes3, "\n")
# print("Predicted values (Model 4): ", y_pred_tes4, "\n")
# mse_tes1 = mean_squared_error(y_test[31923:-1], y_pred_tes1)
# mse_tes2 = mean_squared_error(y_test[31923:-1], y_pred_tes2)
# mse_tes3 = mean_squared_error(y_test[31923:-1], y_pred_tes3)
# mse_tes4 = mean_squared_error(y_test[31923:-1], y_pred_tes4)
#
# np.savetxt('results/TES/predicted_values_model1.txt', y_pred_tes1)
# np.savetxt('results/TES/predicted_values_model2.txt', y_pred_tes2)
# np.savetxt('results/TES/predicted_values_model3.txt', y_pred_tes3)
# np.savetxt('results/TES/predicted_values_model4.txt', y_pred_tes4)
# actual_test_result = y_test
# np.savetxt('results/TES/test_values.txt', actual_test_result)
#
# return mse_tes1, mse_tes2, mse_tes3, mse_tes4
return mse_tes, rmse_tes, r2_tes, y_pred_tes
alpha = 0.8
beta = 0.2
season = 24
y_test = pd.DataFrame(y_test)
y = np.reshape(np.array(y_test[327:-1]), (36,))
print("---------------------------------------------------------")
mse_ses, rmse_ses, r2_ses, y_ses = single_exponential_smoothing(alpha, y_test)
print("MSE for SES: ", mse_ses)
print('RMSE for SES:', rmse_ses)
print('R-squared for SES:', r2_ses)
print('MAPE for SES:', np.mean(np.abs((y - np.array(y_ses)) / y)) * 100,'\n')
print("---------------------------------------------------------")
mse_des, rmse_des, r2_des, y_des = double_exponential_smoothing(alpha, beta, y_test)
print("MSE for DES: ", mse_des)
print('RMSE for DES:', rmse_des)
print('R-squared for DES:', r2_des)
print('MAPE for DES:', np.mean(np.abs((y - np.array(y_des)) / y)) * 100,'\n')
print("---------------------------------------------------------")
mse_tes, rmse_tes, r2_tes, y_tes = triple_exponential_smoothing(season, y_test)
print("MSE for TES: ", mse_tes)
print('RMSE for TES:', rmse_tes)
print('R-squared for TES:', r2_tes)
print('MAPE for TES:', np.mean(np.abs((y - np.array(y_tes)) / y)) * 100,'\n')
print("---------------------------------------------------------")
# Plotting the results
fig = plt.figure(figsize=(60, 8))
plt.plot(y_ses, label='SES')
plt.plot(y_des, label='DES')
plt.plot(y_tes, label='TES')
plt.plot(y_test[327:-1], label='Actual Values')
plt.legend(loc='upper right')
plt.xlabel('Hour')
plt.ylabel('Electricity load')
plt.title("Predicted Values of various ES methods", fontsize=14)
plt.show()
fig.savefig('results/ESweather_final_output.jpg', bbox_inches='tight')