Code covered by the BSD License

# Electricity Load and Price Forecasting Webinar Case Study

### Ameya Deoras (view profile)

10 Sep 2010 (Updated )

Slides and MATLABĀ® code for the day-ahead system load and price forecasting case study.

```function y = loadForecast(date, temperature, isHoliday)
% Neural-Network or Bagged Regression Tree model
%
% USAGE:
% y = loadForecast(model, date, hour, temperature, isWorkingDay))

% Process inputs
date = datenum(date);
if date < 7e5 % Convert from Excel numeric date to MATLAB numeric date if necessary
date = x2mdate(date);
end

% Check if date is a holiday
if iscell(isHoliday)
isHoliday = isHoliday{1};
end
if ischar(isHoliday)
if strcmpi(isHoliday(1),'N')
isWorkingDay = true;
else
isWorkingDay = false;
end
else
isWorkingDay = ~isHoliday;
end
isWorkingDay = logical(isWorkingDay) & ~ismember(weekday(date),[1 7]);

% Import historical loads from the database
try
catch ME %#ok<NASGU>
% Import historical loads from MAT file
data = s.data;
ind = data.NumDate >= date-7 & floor(data.NumDate) <= date-1;
data.Hour = data.Hour(ind);
data.DryBulb = data.DryBulb(ind);
data.DewPnt = data.DewPnt(ind);
data.NumDate = data.NumDate(ind);
end
error('Not enough historical data for forecast.');
end

% Create predictor matrix
% Drybulb, Dewpnt, Hour, Day, isWkDay, PrevWeek, PrevDay, Prev24
X = [temperature (1:24)' weekday(date)*ones(24,1) isWorkingDay*ones(24,1) loadPredictors];

try
% Load from a location where updated models can be stored
catch %#ok<CTCH>
end

% Perform prediction
try
y1 = sim(model1.net, X')';
catch ME
% For debugging purposes if necessary
save C:\error.mat ME model1 model2
y1 = zeros(24,1);
end
y2 = predict(model2.model, X);

fig = clf;
if isdeployed
set(fig,'Visible','off')
end
plot([y1 y2]/1e3, '.-');
xlabel('Hour');
title(sprintf('Load Forecast Profile for %s', datestr(date)))
grid on;
legend('NeuralNet','BaggedTree','Location','best');
print -dmeta

y = [y1 y2];

%#function TreeBagger
%#function CompactTreeBagger
%#function network
%#function network\sim
%#function mae```