Scilab: Correlation Matrix
This is a Scilab-related post that may be of little interest to most of the people who read this blog.
The following code is provided as is with no warranties express or implied or statutory or whatever, standard disclaimers apply, use at your own risk, yatta, yatta, yatta. If you find any problems, please make a comment.
Here's a little Scilab function for calculating a Correlation Matrix.
function [y] = correlation(m)
// copy source
x = m;
// subtract mean and scale appropriately
[nrows,ncols] = size(m);
for i=1:ncols
mu = mean(x(:,i));
sgi = 1.0 / (sqrt(nrows-1) * stdev(x(:,i)));
x(:,i) = (x(:,i) - mu) * sgi;
end
// x'x
y = x' * x;
endfunction
And here's a shorter version...
The following code is provided as is with no warranties express or implied or statutory or whatever, standard disclaimers apply, use at your own risk, yatta, yatta, yatta. If you find any problems, please make a comment.
Here's a little Scilab function for calculating a Correlation Matrix.
function [y] = correlation(m)
// copy source
x = m;
// subtract mean and scale appropriately
[nrows,ncols] = size(m);
for i=1:ncols
mu = mean(x(:,i));
sgi = 1.0 / (sqrt(nrows-1) * stdev(x(:,i)));
x(:,i) = (x(:,i) - mu) * sgi;
end
// x'x
y = x' * x;
endfunction
And here's a shorter version...
function [y] = correlation(m)
vc = varcovar(m);
t = inv(diag(sqrt(diag(vc))));
y = t*vc*t;
endfunction
Note: If you're concerned with issues of stability and performance, see Knuth, this Wikipedia entry, etc.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home