Correlation Coefficient through time

조회 수: 10 (최근 30일)
Daniele Ferreti
Daniele Ferreti 2020년 3월 27일
답변: Peter Perkins 2020년 4월 14일
So I'm getting myelf familiar with correlation coeffiecient for a future project. I have a couple objectives I wanted to understand: corrcoef as a whole, and then corrcoef through time (meaning seeing a 'live' corrcoef as the data changes).
So far I made this that works as I wanted:
%% Random Numbers & single output corr. coeff.
rand_1 = randi([0 5],1,20); % 20 random numbers between 0 & 5
rand_2 = randi([0 5],1,20); % 20 random numbers between 0 & 5
rand_3 = randi([0 5],1,20); % 20 random numbers between 0 & 5
r_1 = corrcoef(rand_1,rand_2); % Corr. Coeff. calculation #1 between 1 & 2
R_1 = r_1(2,1) % Actual Value
r_2 = corrcoef(rand_1,rand_3); % Corr. Coeff. calculation #2 between 1 & 3
R_2 = r_2(2,1) % Actual Value
r_3 = corrcoef(rand_2,rand_3); % Corr. Coeff. calculation #3 between 2 & 3
R_3 = r_3(2,1) % Actual Value
%(If you have any input in how to make these ^^^ calculations simpler I would also really appreciate any help)
x = [1:20]; % x axis for plots
figure
subplot(3,2,[1,2])
plot(x,rand_1,'r',x,rand_2,'b') % Plotting for R_1
title(['Random 1: between 1 & 2, C.C. = ',num2str(R_1),''])
axis([1 20 -1 6])
subplot(3,2,[3,4])
plot(x,rand_1,'r',x,rand_3,'g') % Plotting for R_2
title(['Random 2: between 1 & 3, C.C. = ',num2str(R_2),''])
axis([1 20 -1 6])
subplot(3,2,[5,6])
plot(x,rand_2,'b',x,rand_3,'g') % Plotting for R_3
title(['Random 3: between 2 & 3, C.C. = ',num2str(R_3),''])
axis([1 20 -1 6])
Now I wanted to make something similar for the time series but I'm having some trouble understanding where I am going wrong and how to continue:
%% Random Numbers & Output corr. coeff. over time
L = 20; % length of random numbers sample
rand2_1 = randi([0 5],1,L); % L random numbers between 0 & 5
rand2_2 = randi([0 5],1,L); % L random numbers between 0 & 5
rand2_3 = randi([0 5],1,L); % L random numbers between 0 & 5
K = 2*L;
r2_1 = zeros(2,K); r2_2 = zeros(2,K); r2_3 = zeros(2,K); % Creating space
for t = 1:1:L % corr. coeff. #4 through time between 1 & 2
j = 2*t-1;
r2_1(1:2,j:j+1) = corrcoef(rand2_1([t t+1]),rand2_2([t t+1]));
end
I understood that depending on the size of the matrix/data given to corrcoef it would give a different size matrix as an answer (for example a 2x2 in the initial example code's case), but I have not been able to figure that out for this second part. Perhaps I'm extracting the answer wrongly from corrcoef, or perhaps my matrix manipulation is simply wrong.
Anyways, any help would be very much appreciated! Thanks

채택된 답변

Daniele Ferreti
Daniele Ferreti 2020년 3월 30일
편집: Daniele Ferreti 2020년 3월 30일
%% Random Numbers & Output corr. coeff. over time
L = 20; % length of random numbers sample
F = 0; % Min random number
G = 5; % Max random number
rand2_1 = randi([F G],1,L); % L random numbers between F & G
rand2_2 = randi([F G],1,L);
rand2_3 = randi([F G],1,L);
R2_1 = zeros(1,L)
for t = 1:1:L-2
a = corrcoef(rand2_1([t t+1 t+2]),rand2_2([t t+1 t+2])); % Corr. coeff. calculation between 1 & 2 (by couples of 3)
b = a(2,1); % Actual wanted value
R2_1(t) = b; % Compiled for plotting
end

추가 답변 (2개)

Navya Seelam
Navya Seelam 2020년 3월 30일
편집: Navya Seelam 2020년 3월 30일
You are trying to access rand2_1([20 21]) in the for loop when t=20 but size of rand2_1 is only 1x20
  댓글 수: 1
Daniele Ferreti
Daniele Ferreti 2020년 4월 3일
Thanks for the help! Figured the rest out on my own.

댓글을 달려면 로그인하십시오.


Peter Perkins
Peter Perkins 2020년 4월 14일
"%(If you have any input in how to make these ^^^ calculations simpler I would also really appreciate any help)"
Does this count as simpler?
>> rand_123 = randi([0 5],20,3);
>> R = corrcoef(rand_123);
>> r12 = R(1,2), r13 = R(1,3), r23 = R(2,3)
r12 =
-0.3379
r13 =
-0.1516
r23 =
-0.033438
Also if you have access to the Econometrics Tbx, you might check out autocorr. Or xcorr, although beware: that's writen in the jargon of signal processing, so you have to pick the right parameters to get the thing you are looking for.
There's also the corr fucntion in Stats&MachineLearnign, which does not only linear (Pearson) correlation, but rank (Spearmand and Kendal) correlations too.

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by