Question

i have question how to calculate weighted correlations for matrices,from wikipedia i have created three following codes

1.weighted mean calculation

function [y]= weighted_mean(x,w);
n=length(x);
%assume that weight vector and input vector have same length
sum=0.0;
sum_weight=0.0;
 for i=1:n
    sum=sum+ x(i)*w(i);
    sum_weight=sum_weight+w(i);
 end
y=sum/sum_weight;
end

2.weighted covariance

function result=cov_weighted(x,y,w)
n=length(x);
sum_covar=0.0;
sum_weight=0;
 for i=1:n
     sum_covar=sum_covar+w(i)*(x(i)-weighted_mean(x,w))*(y(i)-weighted_mean(y,w));
     sum_weight=sum_weight+w(i);
 end
 result=sum_covar/sum_weight;
end

and finally weighted correlation

3.

function corr_weight=weighted_correlation(x,y,w);
corr_weight=cov_weighted(x,y,w)/sqrt(cov_weighted(x,x,w)*cov_weighted(y,y,w));
end

now i want to apply weighted correlation method for matrices,related to this link

http://www.mathworks.com/matlabcentral/fileexchange/20846-weighted-correlation-matrix/content/weightedcorrs.m

i did not understand anything how to apply,that why i have created my self,but need in case of input are matrices,thanks very much

Was it helpful?

Solution

@dato-datuashvili Maybe I am providing too much information...

1) I would like to stress that the evaluation of Weighted Correlation matrices are very uncommon. This happens because you have to provide beforehand the weights. Unless you have a clear reason to choose the weights, there is no clear way to provide them.

How can you tell that a measurement of your sample is more or less important than another measurement?

Having said that, the weights are up to you! Yo have to choose them!

So, people usually consider just the correlation matrix (no weights or all weights are the same e.g w_i=1).

If you have a clear way to choose good weights, just do not consider this part.

2) I understand that you want to test your code. So, in order to that, you have to have correlated random variables. How to generate them?

Multivariate normal distributions are the simplest case. See the wikipedia page about them: Multivariate Normal Distribution (see the item "Drawing values from the distribution". Wikipedia shows you how to generate the random numbers from this distribution using Choleski Decomposition). The 2-variate case is much simpler. See for instance Generate Correlated Normal Random Variables

The good news is that if you are using Matlab there is a function for you. See Matlab: Random numbers from the multivariate normal distribution.]

In order to use this function you have to provide the desired means and covariances. [Note that you are making the role of nature here. You are generating the data! In real life, you are going to apply your function to the real data. What I am trying to say is that this step is only useful for tests. Furthermore, pay attencion to the fact that in the Matlab function you are providing the variances and evaluating the correlations (covariances normalized by standard errors). In the 2-dimensional case (that is the case of your function it is possible to provide directly the correlation. See the page above that I provided to you of Math.Stackexchange]

3) Finally, you can apply them to your function. Generate X and Y from a normal multivarite distribution and provide the vector of weights w to your function corr_weight_correlation and you are done!

I hope I provide what you need!

Daniel

Update:

% From the matlab page
mu = [2 3];
SIGMA = [1 1.5; 1.5 3];
n=100;
[x,y] = mvnrnd(mu,SIGMA,n);
% Using your code
w=ones(n,1);
corr_weight=weighted_correlation(x,y,w); % Remember that Sigma is covariance and Corr_weight is correlation. In order to calculate the same thing, just use result=cov_weighted instead.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top