% demo1ff2.m  
%

clf;
figure(clf);
setfsize(500,200);
echo on
clc

% ======================================================================
%  DEMO1FF2
% Time Series Prediction Using a Neural Network with Time-Delayed Inputs
%
% By: Alex T. Nelson, Eric A. Wan
%     Oregon Graduate Institute
%     11/22/95
% ======================================================================
%
% weight_init	 - Initializes the weights for a network with time delays.
% bp_ffnet2 	 - Trains the two layer network with backpropagation.
% ffnet2 	 - Simulates the two layer network.

% Using the above functions, a network is used to predict
% a given time series.

pause % Strike any key to continue...
clc
% PREPARING THE DATA
% ==================

% We begin by loading the time series data from a file and putting it
% into row form (the kth column has data at time k):
load henonN.dat; 
henonN = henonN';

% Next, determine the length and dimension of the time series:
[Ni,Np] = size(henonN);

% For time series prediction, the kth target value should equal the
% (k+1)th input value. 
inpT = henonN(1:Np-1);   % The input series
desT = henonN(2:Np);	 % The target series

pause % Strike any key to continue...
clc
% THE NETWORK ARCHITECTURE
% ========================

% The dimension of the time series determines the number of input and 
% output nodes in the network. Here we arbitrarily choose 7 hidden nodes:
Nodes = [Ni 7 Ni];  % Number of nodes in each layer

% The following specifies that values of the input at times t, t-1, t-2,
% and t-3 (a total of 4 taps) will be used to drive the network at time t.
Taps = [4];          % Number of time-delays

% We are now ready to create the random initial weight matrices for the 
% network:
[W1,B1,W2,B2] = weight_init(Nodes,Taps);

pause % Strike any key to continue...
clc
% TRAINING PARAMETERS
% ===================

% Here we specify the number of times to train on the training set (epochs),
% and the learning rate or step size (mu).
epochs = 75;
mu = 0.005;

% We will use stochastic weight updates:
trainingMode = 0; 

% The MSE will be printed to screen on every 5th epoch:
disp_freq = 5;

% The training parameter vector combines the above parameters.
TP = [epochs mu trainingMode disp_freq];

pause % Strike any key to start training...
clc
% TRAINING
% ========

% The random initial weights, input and target series, and training
% parameters are passed to the training function; the returned values
% are the trained weights, and MSE over each training epoch:

[W1,B1,W2,B2,mse] = bp_ffnet2(W1,B1,W2,B2,inpT,desT,TP);

% Here is a plot of the Mean Squared Error for each epoch.
semilogy(mse,'b');

pause % Strike any key to continue...
clc
% PREDICTION
% ==========

% The network can be used to predict the time series "one-step-ahead":
[pred] = ffnet2(W1,B1,W2,B2,inpT);

% For comparison the prediction is plotted in red, and the original 
% time series in yellow. Only 100 time points are shown.
P = [0 pred];  D = [inpT 0];
plot(P(1101:1200),'r')
hold on
plot(D(1101:1200),'y')
hold off

pause % Strike any key to plot the difference between the data and prediction.
clc
% Here's a plot of the difference between the original time series data
% and the one-step prediction.
plot(D-P,'g')

pause % Strike any key to continue...
clc
% ITERATIVE PREDICTION
% ====================

% We can also generate a prediction iteratively. The following loop takes
% the last window of inputs in the time series, and generates a prediction
% of the time series for 1000 steps into the future by using a succession
% of one-step-ahead predictions.

PP = 1000;
inpSeed = inpT(Np-sum(Taps)-1 : Np-1);  % the last window in the data
Nw = length(inpSeed);			% the length of the window
predTMP = [inpSeed zeros(Ni,PP)];	% append some zeros
 for k = 1:PP;
   inpP = predTMP(k-Taps+Nw-1:k+Nw-1);	% use the previous prediction
   p1  = ffnet2(W1,B1,W2,B2,inpP);	% to generate the next one.
   predTMP(:,k+Nw) = p1(:,Taps+1);	% store the prediction.
 end
pred = predTMP(:, Nw+1:Nw+PP);		% look at the predicted series
setfsize(500,500);
plot(pred(2:PP),pred(1:PP-1),'.');	% plot the phase portrait

pause % Strike any key to continue...
clc

% The computations on the previous page can also be completed by using the
% the following .m function:  iterate_ffnet2(W1,B1,W2,B2,inpSeed,PP), where
% inpSeed is the initializing window of the time series data, and PP is the
% number of iterative one-step-ahead predictions to compute.

echo off
disp('End of DEMO1FF2')

 











