-
Notifications
You must be signed in to change notification settings - Fork 9
/
corrone.m
executable file
·73 lines (60 loc) · 1.8 KB
/
corrone.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function r=corrone(y,X)
% corrone: product-moment correlation of one variable with several others
% r=corrone(y,X);
% Last revised 7-24-99
%
% Correlation coefficients between one variable and several other variables.
% Written for use as an alternative to corrcoef.m when you do not need the
% full cross-correlation matrix between all variables. Ran about 40 times
% faster for an example of 1000 time series of length 50 years.
%
%*** INPUT
%
% y (my x 1)r key variable; desire correlations between this and all others
% X (mX x nX)r other variables
%
%*** OUTPUT
%
% r (nX x 1)r product moment correlation between y and all series in X
%
%*** REFERENCES -- none
%*** UW FUNCTION CALLED -- none
%*** TOOLBOXES NEEDED -- none
%
%*** NOTES
%
% No NaN allowed
%
% corrone.m took 0.22 seconds to get correlations between a 50-yr series y and 1000
% other series. Corrcoef took 9.39 seconds for the same task. So corrone is 40+ times
% as fast as corrcoef for that example.
%--- Check input
[my,ny]=size(y);
[mX,nX]=size(X);
if my ~=mX;
error('X and y must be same row-size');
end;
if ny~=1 | my <2;
error('y must be a column vector');
end;
%--- Compute covariance matrix of X
xmean = mean (X);
XM = repmat(xmean,mX,1); % means matrix for X
%-- De-mean X
X1 = X-XM; % X as departures from col means
%-- Compute mean and standard dev of y
ymean=mean(y);
ys = std(y);
%-- Make a departures matrix of y, same size as X1
ydep = y - ymean;
Y1 = repmat(ydep,1,nX);
%-- Compute covariance matrix of y and X
C1 = Y1 .* X1; % cross products
nsize = repmat(mX-1,1,nX); % sample size for covariance computation
C2= sum(C1) ./ nsize ; % row vector of sample covariances
%-- Compute standard deviations, sx and sy
sy = std(y);
sy = repmat(sy,1,nX);
sx = std(X); % std devs of X
%-- Compute correlations coefs
r = C2 ./ (sx .* sy);