-
Notifications
You must be signed in to change notification settings - Fork 2
/
load_forecasting.m
196 lines (134 loc) · 5.22 KB
/
load_forecasting.m
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
%% Data Driven Fitting with High Dimensional Data
% Copyright (c) 2011, The MathWorks, Inc.
%% Format my data
% Clean Up
clear all
clc
% Import my data
ds = dataset('xlsfile', 'EnergyData.xlsx');
% plot Load = f(time)
plot(ds.SystemLoad(1:336))
%% Divide my data into a test set and a training set
% 2004 - 2007 = training set
% 2008 = test set
[yr, mo, da] = datevec(ds.Date);
Test = ds(yr == 2008, :);
Training = ds(yr ~= 2008, :);
Training_Y = Training.SystemLoad;
Training_X = double(Training(:,3:end));
%% Create a Load Forecasting Model using a Boosted Decision Tree
t = RegressionTree.template('Surrogate','on');
rens = fitensemble(Training_X, Training_Y, 'LSBoost', 1000, t, ...
'CategoricalPredictors', [3 4 5],...
'PredictorNames', Training.Properties.VarNames(3:end))
%% Generate Predictions from the test set
% Create a test set
Test_Y = Test.SystemLoad;
Test_X = double(Test(:,3:end));
% Generate Predictions
Forecast_Load = predict(rens,Test_X);
% Use a simple chart to show predicted versus actual for two weeks of data
figure
plot(Test_Y(1:336), 'b');
hold on
plot(Forecast_Load(1:336), 'r');
% Use a scatter plot to show Predicted versus Residuals
figure
scatter(Test_Y, Forecast_Load - Test_Y, '.', 'r');
refline(0,1)
xlabel('Predicted')
ylabel('Residuals')
%% Inspect some of the methods associated with "rens"
methods(rens)
display = dataset;
display.Variable = rens.PredictorNames';
display.Importance = predictorImportance(rens)';
sortrows(display, 'Importance', 'descend')
loss(rens, Training_X, Training_Y)
%% Evaluate Goodness of Fit
% Errors = Predicted - observed
Errors = predict(rens,Test_X) - Test_Y;
% Inspect the Errors
figure
histfit(Errors)
figure
normplot(Errors)
%% Segment the data into groups
% By Hour
figure;
boxplot(Errors, Test.Hour, 'plotstyle', 'compact');
xlabel('Hour'); ylabel('Errors');
title('Breakdown of Errors by hour');
% By Weekday
figure
boxplot(Errors, Test.Weekday, 'labels', {'Sun','Mon','Tue','Wed','Thu','Fri','Sat'});
ylabel('Percent Error Statistics');
title('Breakdown of Errors by weekday');
% By Month
figure
boxplot(Errors, datestr(Test.Date,'mmm'));
ylabel('Percent Error Statistics');
title('Breakdown of Errors by month');
%% Use Industry Specific Error Metrics
Error_Percentage = (abs(Errors)./Test_Y) * 100;
fL = reshape(Forecast_Load, 24, length(Forecast_Load)/24)';
tY = reshape(Test_Y, 24, length(Test_Y)/24)';
peakerrpct = abs(max(tY,[],2) - max(fL,[],2))./max(tY,[],2) * 100;
MAE = mean(abs(Error_Percentage));
MAPE = mean(Error_Percentage(~isinf(Error_Percentage)));
fprintf('Mean Absolute Percent Error (MAPE): %0.2f%% \nMean Absolute Error (MAE): %0.2f MWh\nDaily Peak MAPE: %0.2f%%\n',...
MAPE, MAE, mean(peakerrpct))
%% Switch algorithms
tic
rens2 = fitensemble(Training_X, Training_Y, 'Bag', 100, 'Tree', ...
'type', 'regression',...
'CategoricalPredictors', [3 4 5],...
'PredictorNames', Training.Properties.VarNames(3:end))
toc
%% Contrast Models
Forecast_Load2 = predict(rens2,Test_X);
% Use a simple chart to show predicted versus actual for two weeks of data
plot(Test_Y(1:336), 'b');
hold on
plot(Forecast_Load(1:336), 'r');
plot(Forecast_Load2(1:336), 'k');
residuals2 = Forecast_Load2 - Test_Y;
figure
histfit(residuals2)
Error_Percentage2 = (abs(residuals2)./Test_Y) * 100;
fL2 = reshape(Forecast_Load2, 24, length(Forecast_Load2)/24)';
peakerrpct2 = abs(max(tY,[],2) - max(fL2,[],2))./max(tY,[],2) * 100;
MAE2 = mean(abs(Error_Percentage2));
MAPE2 = mean(Error_Percentage2(~isinf(Error_Percentage2)));
fprintf('Mean Absolute Percent Error (MAPE): %0.2f%% \nMean Absolute Error (MAE): %0.2f MWh\nDaily Peak MAPE: %0.2f%%\n',...
MAPE, MAE, mean(peakerrpct))
fprintf('Mean Absolute Percent Error 2: (MAPE): %0.2f%% \nMean Absolute Error 2 (MAE): %0.2f MWh\nDaily Peak MAPE 2: %0.2f%%\n',...
MAPE2, MAE2, mean(peakerrpct2))
%% Neural Network
tic
net = newfit(Training_X', Training_Y', 20);
net.performFcn = 'mae';
net = train(net, Training_X', Training_Y');
toc
Forecast_Load3 = sim(net, Test_X')';
% Use a simple chart to show predicted versus actual for two weeks of data
figure
plot(Test_Y(1:336), 'b');
hold on
plot(Forecast_Load(1:336), 'r');
plot(Forecast_Load2(1:336), 'k');
plot(Forecast_Load3(1:336), 'm');
residuals3 = Forecast_Load3 - Test_Y;
figure
histfit(residuals3)
Error_Percentage3 = (abs(residuals3)./Test_Y) * 100;
fL3 = reshape(Forecast_Load3, 24, length(Forecast_Load3)/24)';
peakerrpct3 = abs(max(tY,[],2) - max(fL3,[],2))./max(tY,[],2) * 100;
MAE3 = mean(abs(Error_Percentage3));
MAPE3 = mean(Error_Percentage3(~isinf(Error_Percentage3)));
fprintf('Mean Absolute Percent Error (MAPE): %0.2f%% \nMean Absolute Error (MAE): %0.2f MWh\nDaily Peak MAPE: %0.2f%%\n',...
MAPE, MAE, mean(peakerrpct))
fprintf('Mean Absolute Percent Error 2 (MAPE): %0.2f%% \nMean Absolute Error 2 (MAE): %0.2f MWh\nDaily Peak MAPE 2: %0.2f%%\n',...
MAPE2, MAE2, mean(peakerrpct2))
fprintf('Mean Absolute Percent Error 3 (MAPE): %0.2f%% \nMean Absolute Error 3 (MAE): %0.2f MWh\nDaily Peak MAPE 3: %0.2f%%\n',...
MAPE3, MAE3, mean(peakerrpct3))