0

Suppose that DD is a time series data (one column and X rows). I separate 11 samples from end of data as out-of-sample and train neural network by MATLAB. The performance of neural network is good in train, test and validation data (default properties of fitting app neural network: fitnet).

for ii = 31 : (numel(DD)-1)
    D_0(ii-30,1) = DD(ii-0);
    D_1(ii-30,1) = DD(ii-1);
    D_2(ii-30,1) = DD(ii-2);
    D_3(ii-30,1) = DD(ii-3);
    D_4(ii-30,1) = DD(ii-4);
    D_5(ii-30,1) = DD(ii-5);
    D_6(ii-30,1) = DD(ii-6);
    D_7(ii-30,1) = DD(ii-7);
    D_14(ii-30,1) = DD(ii-14);
    D_30(ii-30,1) = DD(ii-30);

    D_plus_1(ii-30,1) = DD(ii+1);
end

x = [D_0  D_1  D_2  D_3  D_4  D_5  D_6  D_7 D_14 D_30]';
t = D_plus_1';


%% Out-of-sample data

x_oos = x(:,end-10:end);
t_oos = t(:,end-10:end);

x(:,end-10:end) = [];
t(:,end-10:end) = [];

    hiddenLayerSize = 5;
    trainFcn = 'trainlm';  % Levenberg-Marquardt backpropagation.

    net = fitnet(hiddenLayerSize,trainFcn);

    net.input.processFcns = {'removeconstantrows','mapminmax'};
    net.output.processFcns = {'removeconstantrows','mapminmax'};

    net.divideFcn = 'dividerand';  % Divide data randomly
    net.divideMode = 'sample';  % Divide up every sample
    net.divideParam.trainRatio = 70/100;
    net.divideParam.valRatio = 15/100;
    net.divideParam.testRatio = 15/100;

    % Choose a Performance Function
    % For a list of all performance functions type: help nnperformance
    net.performFcn = 'mse';  % Mean Squared Error
    [net,tr] = train(net,x,t);

x is input of neural network and t is output(target) of neural network. After training i used this code to predict results for out of sample data:

y_Out = net(x_oos);

This is comparison between real and predicted output of out-of-sample data:

enter image description here

But i think there is a lag between two outputs. Now check this with one step lag:

enter image description here

Why we have this behavior in out-of-sample data? I checked different time lengths of out-of-sample and we have same behavior (one step lag). Is this data behavior?

PS.

All data (x variable as input of trained net and comparing with t variable) as input of trained neural network:

enter image description here There is a lag between all data as input!. Neural network trained with this lag in data! Is this a bug?! this is a daily time series for near two years without any missing data.

Eghbal
  • 3,892
  • 13
  • 51
  • 112
  • So the lag time is always 1 time unit? – GameOfThrows Jan 13 '16 at 16:08
  • @GameOfThrows. Yes. That's true. This is one step ahead prediction. – Eghbal Jan 13 '16 at 16:09
  • 2
    Which makes me feel that this a programming error, your NN is definitely working fine, but there might be an error perhaps with the indexing or the starting/ending of a loop? I cannot be sure, but if this is known, you can shift the data back by 1 time unit to obtain the correct result - This is manual intervention. – GameOfThrows Jan 13 '16 at 16:12
  • @GameOfThrows. I add some other codes to main question. This is default neural network (`fitnet`) in MATLAB R2015b. I can't find any problem in codes. – Eghbal Jan 13 '16 at 17:02
  • Naming your variables as `D_0`, `D_1`, `D_2` etc is called "Dynamic variable naming", which is considered to be [very bad practise](http://stackoverflow.com/questions/32467029/how-to-put-these-images-together/32467170#32467170). Besides making your code difficult by having to write all those lines, it makes it difficult to read and difficult to evaluate, due it being prone to mistyping errors. – Adriaan Jan 15 '16 at 11:10
  • @Adriaan. Thank you for your comment. What is your proposed variable naming in my case? Do you think we have this lag because of variable naming problem? – Eghbal Jan 15 '16 at 11:12
  • @Adriaan I think this is only vectorized structure of extracting data. This makes any change in results?! – Eghbal Jan 15 '16 at 11:33
  • 2
    Nope, this will not change the results, just resolve a bad programming habit. `tmpIdx = -[0 1 2 3 4 5 6 7 14 30]+ii` that's the indices you want. The `x = DD(tmpIdx)` is sufficient – Adriaan Jan 15 '16 at 11:39
  • 1
    @Adriaan. Thank you for comment and your nice indexing procedure. – Eghbal Jan 15 '16 at 11:45

1 Answers1

1

Nothing wrong with your network. What is happening is that your network is degenerating into a naive predictor (look it up); i.e.: it can't resolve the relationships between your inputs and output...

Molasar
  • 184
  • 1
  • 8
  • See discussion here: https://www.mathworks.com/matlabcentral/answers/315480-nar-network-outputing-previous-t-1-value-why – Molasar Jun 08 '17 at 01:35