Rolling correlation

  • Follow


I'm trying to create a rolling correlation figure for two assets using a defined window period.

I'm trying to make the function:

function RC = Rcor(asset1, asset2, window)
cmat = [asset1 asset2]; % Create correlation matrix
RC = size(window:length(cmat)); % Pre-allocate matrix to hold the rolling correlation

for i = window:length(cmat) 
    matx = cmat(i-window:i,1:2); % Trying to make moving 5x2 matrix from cmat
    matxc = corrcoef(matx); % 2x2 Correlation matrix
    RC(i) = matxc(1,2); % Pull out xy correlation value
end

I fall down when trying to index through cmat to create the matx 'moving matrix'. Currently I have the error "Subscript indices must either be real positive integers or
logicals." I appreciate the whole method i'm using for the function is probably wrong too but I haven;t got to the stage of fixing that yet as I can't work around this problem, any help/guidance/pointers much appreciated.

Kind Regards
0
Reply Ralph 10/18/2010 10:35:04 PM

Look at matx = cmat(i-window:i,1:2);  for the first i value, which is
window.

i-window = 0, and that's not allowed as a matrix row value.  You
probably want i-window+1.

0
Reply ImageAnalyst 10/18/2010 11:32:02 PM


ImageAnalyst <imageanalyst@mailinator.com> wrote in message <af464093-c43f-4035-9534-ef2f7d84b840@l35g2000vbr.googlegroups.com>...
> Look at matx = cmat(i-window:i,1:2);  for the first i value, which is
> window.
> 
> i-window = 0, and that's not allowed as a matrix row value.  You
> probably want i-window+1.

Perfect, Thanks!!
0
Reply Ralph 10/19/2010 8:33:07 AM

2 Replies
714 Views

(page loaded in 0.047 seconds)

Similiar Articles:








7/26/2012 1:54:58 AM


Reply: