Auto correlation and cross correlation with NARXnet to find ID and FD

4 views (last 30 days)
Hello,
I have recently started working on neural networks and time series with Matlab. So far, I have been using NARX networks and have been training them and then looking at the inerrcorr and errcorr plots to know if I needed to use bigger delays but it has come to my attention that there is a more "rigorous" way of doing it using the nncorr function.
I have looked at many questions and answers but there are still a few steps along the process that I don't understand. I am going to use a code derived from the one found in the topic NARXnet non-integer delays problem to show which parts I don't get:
X = inputs; % 1x2323 mat
T = targets; % 1x2323 mat
N = lenght(T)
crosscorrXT = nncorr(X,T,N-1);
autocorrT = nncorr(T,T,N-1);
crosscorrXT(1:N-1) = []; % Delete negative delays
autocorrT(1:N-1) = [];
sigthresh95 = 0.21 % Significance threshold
sigcrossind = crosscorrXT( crosscorrXT >= sigthresh95 )
sigautoind = autocorrT( autocorrT >= sigthresh95 )
inputdelays = sigcrossind(sigcrossind <= 35)
feedbackdelays = sigautoind(sigautoind <= 35)
feedbackdelays(1)=[] % Delete zero delay
Though I understand from the answer given there that the value used isn't correct, I still don't understand how to get the 95% confidence limit. I also don't understand what the last three lines of her/his code do.
I found on another topic another way of getting sigthresh95:
sigthresh95 = crosscorrXT(floor(0.95*(2*N-1)))
but this line returns the error " Attempted to access crosscorrXT(4412); index out of bounds because numel(crosscorrXT)=2323" so clearly I am not using it correctly and I don't understand how it is supposed to work.
I think I'm not not clear on what the 95% is of.

Accepted Answer

Greg Heath
Greg Heath on 28 Jul 2015
Look for my recent NEWSGROUP posts (2015)
greg nncorr 2015
Especially
8 Jul 2015 OPENLOOP NARNET TUTORIAL EXAMPLE
2 Jun 2015 SIGNIFICANT AUTOCORRELATION DELAYS OF THE SIMPLENAR_DATASET
13 Apr 2015 NARNET TUTORIAL ON MULTISTEP AHEAD PREDICTIONS: MESSAGES 1, 7, 11
Hope this helps.
Thank you for formally accepting my answer
Greg
  1 Comment
Nikita OKS
Nikita OKS on 29 Jul 2015
Edited: Nikita OKS on 29 Jul 2015
Dear Greg,
Thank you for your help and for the suggested posts. I found the first two right after posting my question here and I used them to write my own code but here is what it returns:
[I N] = size(inputs); % [1 2323]
[O N] = size(targets); % [1 2323]
MSE00 = mean(var(targets',1)) % 8.0153e+08
Ntrn = N-2*round(0.15*N) % 1627
Ntrneq = Ntrn*O % 1627
H = 25;
Nw = (I+1)*H+(H+1)*O % 76
r = Ntrneq/Nw % 21.4079
MSEgoal = 0.01*MSE00 % 8.0153e+06
and
[O N] = size(targets); % [1 2323]
zt = zscore(targets',1)';
L = floor (0.95*(2*N-1)); % 4412
rng('default')
for i = 1:100
zn=zscore(randn(1,N));
autocorrn = nncorr(zn,zn,N-1,'biased');
sortabsautocorrn = sort(abs(autocorrn));
thresh95(i) = sortabsautocorrn(L);
end
meanthresh95 = mean(thresh95); %0.0304
autocorrt = nncorr(zt,zt, N-1,'biased');
sigthresh = find(abs(autocorrt(N+1:2*N-1))>=meanthresh95)
% sigthresh 1x1953 mat
% sigthresh = [1:660 742:2035]
FD = sigthresh;
NFD = numel(FD); % 1953
Ntrn = N-2*round(0.15*N) % 1627
Ntrneq = Ntrn*O % 1627
Hub = floor((Ntrneq-O)/(NFD*O+O+1)) % 0
As you can tell, my sigthresh is very large and I don't see how I could use it for my delays ( and I haven't even tried to create a double-loop net yet because of the Hub equal to zero).
Because I am not sure to understand some parts of this code (especially how the hub is calculated or how the for loop is used to compute the confidence limit), I fail to write a code that works with my own data.
Finally, I also noticed a slight difference in the way you obtained sigthresh in the two posts as one defines it as shown above when the other adds a -1 to the formula and I am not sure to understand why.
sigthresh = -1+find(abs(autocorrt(N+1:2*N-1))>=meanthresh95)
Again, thank you for your help
Nikita

Sign in to comment.

More Answers (2)

Greg Heath
Greg Heath on 30 Jul 2015
[ I N ] = size(inputs); % [1 2323]
[ O N ] = size(targets); % [1 2323]
MSE00 = mean(var(targets',1)) % 8.0153e+08
%GEH1: With a value this huge, why didn't you normalize??
Ntrn = N-2*round(0.15*N) % 1627
Ntrneq = Ntrn*O % 1627
H = 25;
%GEH2: Why H so high?? Typically, you want H and Nw to be as small as possible to increase the stability, robustness and generalization capability of the design. At this point I would suggest starting with the default H=10.
Nw = (I+1)*H+(H+1)*O % 76
r = Ntrneq/Nw % 21.408
MSEgoal = 0.01*MSE00 % 8015300
% GEH3: This only leads to an Rsquared of 0.990. For an openloop net, It is probably better to try for 0.995 or 0.999
[O N ] = size(targets); % [1 2323]
zt = zscore(targets',1)';
L = floor (0.95*(2*N-1)); % 4412
% GEH4: To keep validation and test subset predictions as unbiased as possible, probably should only use the training or, if needed, design (training+validation) subsets to determine significant lags and number of hidden nodes. e.g., use Ntrn above.
rng('default')
for i = 1:100
zn = zscore(randn(1,N));
% GEH5: Use biased form : zscore(..., 1)
autocorrn = nncorr(zn,zn,N-1,'biased');
sortabsautocorrn = sort(abs(autocorrn));
thresh95(i) = sortabsautocorrn(L);
end
meanthresh95 = mean(thresh95); %0.0304
autocorrt = nncorr(zt,zt, N-1,'biased');
sigthresh = find(abs(autocorrt(N+1:2*N-1))>=meanthresh95)
% sigthresh 1x1953 mat % sigthresh = [1:660 742:2035]
% GEH6: Should use Ntrn and ttrn in all above calculations
% GEH7: For Narxnet also need crosscorrxt calculations.
FD = sigthresh;
NFD = numel(FD); % 1953
% GEH8: No, No, No! Only use a subset of FD long enough to achieve MSEgoal.
Ntrn = N-2*round(0.15*N) % 1627
Ntrneq = Ntrn*O % 1627
Hub = floor((Ntrneq-O)/(NFD*O+O+1)) % 0
As you can tell, my sigthresh is very large and I don't see how I could use it for my delays ( and I haven't even tried to create a double-loop net yet because of the Hub equal to zero).
% See GEH8
Because I am not sure to understand some parts of this code (especially how the hub is calculated or how the for loop is used to compute the confidence limit), I fail to write a code that works with my own data.
%GEH9: Hub is the upper bound for Nw <= Ntrneq (No. of unknowns no greater than the number of training equations)
% GEH10: See your stats refererence or wikipedia re defining statistical significance w.r.t. noise statistics ( Remember p- values etc?)
Finally, I also noticed a slight difference in the way you obtained sigthresh in the two posts as one defines it as shown above when the other adds a -1 to the formula and I am not sure to understand why. sigthresh = -1+find(abs(autocorrt(N+1:2*N-1))>=meanthresh95)
% GEH11: Cannot have FD = 0 when loop is closed. But can have ID=0
sigthreshxcorr = -1+ find(abs(crosscorrxt(N:2*N-1))>=meanthresh95)
sigthreshacorr = find(abs(autocorrt(N+1:2*N-1))>=meanthresh95)
Hope this helps.
Greg
  3 Comments
Greg Heath
Greg Heath on 31 Jul 2015
Don't be sorry !! I enjoyed it because it reminded me of a few things that I forgot to include in some tutorials AND the joint optimization problem with partial correlations which I had sidestepped.
There are still a few things about trainbr that bother me so, ... be careful.
1. Begin with zscore transformation of training data
2. Perform Outlier & BadData checks using zscore parameters:
a. Apply (max-trnmean)/trnstd & (trnmean-min)/trnstd to trn/val/tst
b. Plots
3. You can use ID >= 0 but FD > 0
4. Use Ntrn to find ID and FD. Only if Ntrn is small should you ever consider using Ntrn+Nval. Otherwise you lose the credibility of assuming val performance estimates are relatively unbiased.
5. A huge Hub just means you have a lot of data and probably don't have to worry too much about overtraining an overfit net.
6. a. I have never looped to optimize ID and FD. Instead, I plot autocorr and crosscorr in blue or black with the significant correlations points overlayed in red circles 'ro'.
b. Sometimes looking at the plots have caused me to use the median or mean+std to define significance.
7. a.Why did you feel the following statements were necessary?
trainFcn = 'trainbr'
net.inputs{1}.processFcns = {'removeconstantrows', 'mapminmax'};
net.inputs{2}.processFcns = {'removeconstantrows', 'mapminmax'};
net.divideFcn = 'dividetrain';
net.performFcn = 'mse';
net.trainParam.epochs = 5000;
net.plotFcns = {'plotperform','plottrainstate', 'ploterrhist', ...
'plotregression', 'plotresponse', 'ploterrcorr', 'plotinerrcorr'};
b. Are trainbr and dividetrain compatible?
8. You have spent quite a bit of time trying to optimize delays given a fixed H. Then planning to fix those delays and optimizing H. Interesting because I had decided to go the other way. BUT, after I have gotten a reasonable combination, I just accepted it and didn't worry about a joint optimum solution.
9. Be aware that there is a flaw in this type of joint optimality search: The straightforward correlation functions do not take interactions into account. For example, assume the top two significant delays are 1 and 4, respectively. If the 1 delay correlation is removed by subtraction, the remaining top significant correlation delay may no longer be 4.
This is taken into account using the the theory of PARTIAL CORRELATIONS, the theory of which I am not familiar. However, it probably is not much different than the familiar stepwise and stagewise methods for selection of regression variables.
I think the Econometrics Tool Box does use partial correlations for stepwise or stagewise variable selection.
10. You did not include the final delay states xf and af in your outputs.
Hope this helps.
Thank you for formally accepting my answer
Greg
Nikita OKS
Nikita OKS on 2 Aug 2015
Dear Greg,
Thank you for your reply.
b. Are trainbr and dividetrain compatible?
They seem to be, yes. But I do have a question about dividetrain as I remember seeing in the user's manual that they left the divideFcn parameter blank (i.e. just the quotes) and I don't know if there's a difference between not telling the code how to divide the series and telling it to just use all the data for training.
Anyway, I haven't had time to add the modifications you suggested to my code yet as something else has required my attention but I will as soon as I can and I think I should be good after that.
Thank you for your much appreciated help.
Nikita

Sign in to comment.


mohamad Macabangin
mohamad Macabangin on 2 Dec 2017
I am new here. I have been searching for greg's NEWSGROUP posts (2015) and I always ended up with Matlab answers post. Can I have the link?

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!