An example of using MATLAB to fit time series data.



% Generate some random numbers:
for i = 1:1000,
r(i) = random('Normal',0.0,1.0);
end

plot(r)
Postscript version

% Define a dynamic system and find system behavior:
% coefficients:
c1 = 1.791;
c2 = -0.9;
% initial conditions:
a(1) = 1;
a(2) = 1;
% find "free" response:
for i = 3:100,
a(i) = c1*a(i-1) + c2*a(i-2);
end
plot(a)
Postscript version

% Now drive system with random noise to generate some data:
a(1) = 1;
a(2) = 1;
for i = 3:1000,
a(i) = c1*a(i-1) + c2*a(i-2) + r(i-2);
end
plot(a)
Postscript version

% LOAD INPUTS AND OUTPUTS
%    INPUT defines many 2-element input vectors (column vectors):
%    OUTPUT defines the associated 1-element targets (column vectors):
for i = 3:1000,
INPUT(1,i-2) = a(i-1);
INPUT(2,i-2) = a(i-2);
OUTPUT(i-2) = a(i);
end

% Now let's make a linear model (and recover coefficients c1 and c2):
parameters = INPUT'\OUTPUT'
%parameters =
%    1.7804
%   -0.8919
predictions = INPUT' *  parameters;
plot(predictions)
Postscript version

residuals = predictions - OUTPUT';
plot(residuals)
% Note change of vertical scale. 
Postscript version

plot(residuals + r(1:998)') 
% Note change of vertical scale. 
Postscript version

% So the residuals were basically the random drive noise
% Let's compare the linear model and the original free response
point(1) = 1;
point(2) = 1;
for i = 1:100
response(i) = parameters(1)*point(1) + parameters(2)*point(2);
point(2) = point(1);
point(1) = response(i);
end
plot(1:100,response,'r-',1:100,a,'b--')
Postscript version


%    FUNCTION APPROXIMATION WITH TANSIG/PURELIN NETWORK:
%    INITFF   - Initializes a feed-forware network.
%    TRAINBP  - Trains a feed-forward network with backpropagation.
%    SIMUFF    - Simulates a feed-forward network.

Figure out input ranges 
min(INPUT')
max(INPUT')

IRANGES = [-22 22; -22 22];
ORANGES = [-22 22];

%    A two-layer TANSIG/PURELIN network will be trained.
%    The number of hidden TANSIG neurons should reflect the
%    complexity of the problem.

S1 = 1;  % Only need 1 neuron to handle linear fit.

%    INITFF is used to initialize the weights and biases for
%    the TANSIG/PURELIN network.

[w1,b1,w2,b2] = initff(IRANGES,S1,'tansig',ORANGES,'purelin');

%    TRAINBP uses backpropagation to train feed-forward networks.
% I couldn't get trainbp to work on this problem.
% This works!

[w1,b1,w2,b2,ep,tr] = trainlm(w1,b1,'tansig',w2,b2,'purelin',INPUT,OUTPUT)

TRAINLM: 0/1000 epochs, mu = 0.001, SSE = 38842.1.
TRAINLM: 25/1000 epochs, mu = 0.1, SSE = 892.679.
TRAINLM: 50/1000 epochs, mu = 0.01, SSE = 885.482.
TRAINLM: 75/1000 epochs, mu = 0.001, SSE = 884.993.
TRAINLM: 94/1000 epochs, mu = 1e+10, SSE = 884.975.
TRAINLM: Error gradient is too small to continue learning.
  Further training may be necessary, or try different
  initial weights and biases and/or more hidden neurons.
w1 =
   -0.0230    0.0115
b1 =
    0.0060
w2 =
  -77.7332
b2 =
    0.4180
...

% The training curve:
Postscript version

%    USING THE TRAINED NETWORK
%    ============================

%    We can now test the original inputs

for i = 1:length(OUTPUT),
point(1) = INPUT(1,i);
point(2) = INPUT(2,i);
prediction(i) = simuff(point',w1,b1,'tansig',w2,b2,'purelin');
error(i) = prediction(i) - OUTPUT(i);
end
plot(prediction)
Postscript version

plot(error)
% Note change of vertical scale. 
Postscript version

plot(error +  r(1:998))
% Note change of vertical scale. 
Postscript version

% Let's compare the neural net and the original free response
point(1) = 1;
point(2) = 1;
for i = 1:100
response(i) = simuff(point',w1,b1,'tansig',w2,b2,'purelin');
point(2) = point(1);
point(1) = response(i);
end
plot(1:100,response,'r-',1:100,a,'b--')
Postscript version