Re: Weighted correlation [message #76876] |
Sun, 10 July 2011 06:55  |
Gray
Messages: 253 Registered: February 2010
|
Senior Member |
|
|
On Jul 10, 6:29 am, Fermiona Fermiona <fermion...@gmail.com> wrote:
> Hello,
>
> Is there a function in IDL that calculates the correlation coeff for
> data with weights?
>
> Many thanks!
This seems like it would be relatively easy to do:
function weighted_correlation, x, y, w
total_weights = total(w)
mean_x = total(w*x)/total_weights
mean_y = total(w*y)/total_weights
cov_xy = total(w*(x-mean_x)*(y-mean_y))/total_weights
cov_xx = total(w*(x-mean_x)*(x-mean_x))/total_weights
cov_yy = total(w*(y-mean_y)*(y-mean_y))/total_weights
return, cov_xy / sqrt(cov_xx * cov_yy)
end
You'd need to add error checking and type checking, and if your arrays
are very large you might want to do total(/double), but this should
get you there.
|
|
|
|