covariance between 2 ts over time

4 visualizaciones (últimos 30 días)
Newuser
Newuser el 29 de Abr. de 2011
I havea matrix(X) with 3 vectors with 380 elements each. I want to see how the covariance of 1st vector(A) and 2(B) with TS3 (C) evolves over time (25 periods used to compute the cov)
I supposed this should be done by means of a circular loop.
The problem is that the Cov(function) gives a matrix as a result .. and this fact translates into an error in the loop procedure. does someone know if there is a fromula to compute the COV without getting all the cov matrix?
the loop code i've written is :
for i = 26:380
for u = 1:3
covaariances(i,u)= ((cov(X(i,u),x(i,3))))
end
end
this results in an error. could someone of you advise me a way to get what I am aiming for??? thank u for ur valuable time
  3 comentarios
Walter Roberson
Walter Roberson el 30 de Abr. de 2011
You must still have an error: the (i.25) would not be valid. Perhaps (i+25) ?
Walter Roberson
Walter Roberson el 30 de Abr. de 2011
x(i:(i-25),3) would be the empty vector, as i is always going to be greater than i-25.

Iniciar sesión para comentar.

Respuesta aceptada

Oleg Komarov
Oleg Komarov el 30 de Abr. de 2011
EDIT
X = rand(1500,50);
% Window size
ws = 25;
% Preallocate
szX = size(X);
covC = cell(szX(1)-24,1);
covM = zeros(szX(1)-24,sx(2));
for n = 1:szX(1)-24
covC{n} = cov(X(0+n:24+n,:));
covM(n,:) = covC{n}(1,:);
end
You can also save on computations by implementing your own covariance calculation for the first series against the other 49 to get a column of values.
Or you can simply save on memory (even though covC is not more than 30 mb) by substituting the "covM line" inside the loop with:
covC{n} = covC{n}(1,:);
and calling outside of the loop:
covC = cat(1,covC{:});
  2 comentarios
Newuser
Newuser el 30 de Abr. de 2011
Thanks for your help.
The end result I am aiming at is a matrix containing the covariances (evolving over time) of all the 50 vectors with respect to the first vector.
This would mean that the first column would be of ones (1,1,1,1,1,1,)
Newuser
Newuser el 30 de Abr. de 2011
I mean ... of the fist matrix covC(1,1) ... i need the first line (this would be the first row of my new matric containig the covariances)
of the secon matrix covC(2,1) ... i need the first line (this would be the second row of my new matric containig the covariances)
and so on

Iniciar sesión para comentar.

Más respuestas (4)

Teja Muppirala
Teja Muppirala el 30 de Abr. de 2011
The COV function does return a matrix. But this is not a problem since you can just extract out the relevant pieces.
I think there are running covariance algorithms out there that do this calculation very efficiently, but even just using a plain old loop is very fast (this code is only slow because I'm plotting it).
t = 0.01*(0:379)';
X = [sin(20*t.^2) sin(5*t.^3) sin(2*t.^4)];
figure;
a1 = subplot(2,1,1);
plot(X);
legend({'X1' 'X2' 'X3'});
title('X');
blk = 25;
C = zeros(size(X,1)-blk+1,2);
a2 = subplot(2,1,2);
h = plot(C);
set(h(1),'color',[0 0.5 0]);
set(h(2),'color','r');
title('Running Covariance');
for n = 0:size(X,1)-blk
c = cov(X(n + (1:blk),:));
C(n+1,:) = c(2:3,1);
set(h(1),'Ydata',C(:,1));
set(h(2),'Ydata',C(:,2));
drawnow;
end
legend({'cov(X1 , X2)' 'cov(X1 , X3)'});
linkaxes([a1 a2],'x');
If you're really against calculating the 3x3 covariance matrix, then you could do it using the formula for covariance which you can find on Wikipedia.
  1 comentario
Newuser
Newuser el 30 de Abr. de 2011
Thank u for the help. Could u please suggest me some of the efficient running cov algorithms u were referring to? ...
I'm using the one u posted ... but with a X matrix of dimension (1581,500). It's taking hours for the operation

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 30 de Abr. de 2011
Covariance is inherently an operation that returns a matrix. It measures the correlation of every component of the first vector with every component of the second vector.
If you are looking for a single value that tells you how "similar" the two vectors are, then covariance is the wrong measure. Possibly you wish to use kstest2()

Newuser
Newuser el 30 de Abr. de 2011
Thank you all for the prompt and useful advices. Here is another questions for you.
If instead of 3 "assets" (vectors of the Main matrix" I were to have 350 "assets" and would like to compute the correlation/covariance between them? at each point in time, how could I do it? ... would it be possible using the algorithm postedy by Teja?
Thanks again

Newuser
Newuser el 30 de Abr. de 2011
Hallo!
I tried to use the Teja script ... with 50 colums vector containing each 1500 obs.
I've started the calculation 5 minutes ago ... and it's still "busy"
could someone suggest me a more efficient way ?
  2 comentarios
Oleg Komarov
Oleg Komarov el 30 de Abr. de 2011
It takes an instant to generate all the covariance matrices! Then you can plot all at once.
Newuser
Newuser el 30 de Abr. de 2011
cosa intendi precisamente?

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by