I compute well-known "sample Pearson correlation coefficient" of two vectors:


$r(X,Y) = \frac{\sum ^n \_{i=1}(X\_i - \bar{X})(Y\_i - \bar{Y})}{\sqrt{\sum ^n \_{i=1}(X\_i - \bar{X})^2} \sqrt{\sum ^n \_{i=1}(Y\_i - \bar{Y})^2}}$ . So far so good.


I need to generalize it as follows. 
I need to add "weights", $w\_i\in[0..1]$, to the formula.  

The idea is that *smaller values* of $w\_i$ make affect of $(X\_i,Y\_i)$ on $r$ *smaller*.  

(Proportionally to value of $w\_i$.)  

Without $w\_i$, every $(X\_i,Y\_i)$ and $(Y\_j,Y\_j)$ affec $r$ equaly.  

$w\_i$ makes then affect $r$ differently, with different "weight".


Would it be right to calculate $r(X,Y,w)$ as  

$r(X',Y')$ where $X'\_i=w\_i(X\_i-\bar{X})$, and $Y'\_i=w\_i(Y\_i-\bar{Y})$ ? Or else, what would be the  

right way to insert $w\_i$ into the formula ? 



Generalize Pearson
I think that the correct thing to do is treat the values as if they were repeated $w\_i$ times. That is, assume first that the weights $w\_i$ are non-negative integers, the data actually comes in the form such that the pair $(x\_i, y\_i)$ appears $w\_i$ times, and compute the standard Pearson correlation for such (repeated) data. Doing so gives: 
$\bar{X}' = \frac{\sum\_{i=1}^n w\_i x\_i}{\sum\_{i=1}^n w\_i}$
(and similarly for $Y$).
And in terms of the original $n$ data points, the formula is: 
$$
r(X,Y,w) = \frac{\sum\_{i=1}^n w\_i (X\_i-\bar{X}')(Y\_i-\bar{Y}')}{\sqrt{\sum\_{i=1}^n w\_i (X\_i - \bar{X}')^2} \sqrt{\sum\_{i=1}^n w\_i (Y\_i - \bar{Y}')^2}}
$$


and then you can use it of course for general real weights $w\_i$. I don't think this is equivalent to your suggestion.


