Predicting one period ahead using neural networks
5 views (last 30 days)
Show older comments
Hi, I have prices and i am trying to predict next period's price using neural network. I found a code on Mathworks website and modified it (the data is attached). However, i am failing to predict one period ahead because the code is generating errors that are the differences between real data and the model data for the validation data. I am using neural networks for the first time and i don't know how to generate the forecast for the next period (last day in the sample is 28th july 2017 and i want to predict the price of 29th of July 2017) any help is greatly appreciated
close all, clear all, clc, format compact
load Data0
% data settings
N = length(Price)*0.70; % number of samples
Nu = length(Price)*0.30; % number of learning samples
% prepare training data
yt = con2seq(Price(1:Nu)');
% prepare validation data
yv = con2seq(Price(Nu+1:end)');
inputDelays = 3;
hiddenSizes =2;
% nonlinear autoregressive neural network
net = narnet(inputDelays, hiddenSizes);
[Xs,Xi,Ai,Ts] = preparets(net,{},{},yt);
%%Prepare input and target time series data for network training
% [Xs,Xi,Ai,Ts,EWs,shift] = preparets(net,Xnf,Tnf,Tf,EW)
%
% This function simplifies the normally complex and error prone task of
% reformatting input and target timeseries. It automatically shifts input
% and target time series as many steps as are needed to fill the initial
% input and layer delay states. If the network has open loop feedback,
% then it copies feedback targets into the inputs as needed to define the
% open loop inputs.
%
% net : Neural network
% Xnf : Non-feedback inputs
% Tnf : Non-feedback targets
% Tf : Feedback targets
% EW : Error weights (default = {1})
%
% Xs : Shifted inputs
% Xi : Initial input delay states
% Ai : Initial layer delay states
% Ts : Shifted targets
[Xs,Xi,Ai,Ts] = preparets(net,{},{},yt);
% train net with prepared training data
net = train(net,Xs,Ts,Xi,Ai);
% close feedback for recursive prediction
net = closeloop(net);
%%Recursive prediction on validation data
% prepare validation data for network simulation
yini = yt(end-max(inputDelays)+1:end); % initial values from training data
% combine initial values and validation data 'yv'
[Xs,Xi,Ai] = preparets(net,{},{},[yini yv]);
% predict on validation data
predict = net(Xs,Xi,Ai);
% validation data
Yv = cell2mat(yv);
% prediction
Yp = cell2mat(predict);
% error
e = Yv - Yp;
0 Comments
Answers (1)
Greg Heath
on 8 Dec 2017
You do not understand the basics regarding the use of
1. training/validation/testing data division
2. Significant delays determined from the auto and cross correlation functions.
3. Necessary and sufficient number of hidden nodes
Please see my tutorials and posts in the NEWSGROUP and ANSWERS
greg narx
greg narxnet
Hope this helps
Greg
0 Comments
See Also
Categories
Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!