diff --git a/src/Numerics/Statistics/Correlation.cs b/src/Numerics/Statistics/Correlation.cs
index f880e504..5a27f4e6 100644
--- a/src/Numerics/Statistics/Correlation.cs
+++ b/src/Numerics/Statistics/Correlation.cs
@@ -96,6 +96,73 @@ namespace MathNet.Numerics.Statistics
return r / Math.Sqrt(varA * varB);
}
+ ///
+ /// Computes the Weighted Pearson Product-Moment Correlation coefficient.
+ ///
+ /// Sample data A.
+ /// Sample data B.
+ /// Corresponding weights of data.
+ /// The Weighted Pearson product-moment correlation coefficient.
+ public static double WeightedPearson(IEnumerable dataA, IEnumerable dataB, IEnumerable weights)
+ {
+ int n = 0;
+
+ double meanA = 0;
+ double meanB = 0;
+ double varA = 0;
+ double varB = 0;
+ double sumWeight = 0;
+
+ double covariance = 0;
+
+ using (IEnumerator ieA = dataA.GetEnumerator())
+ using (IEnumerator ieB = dataB.GetEnumerator())
+ using (IEnumerator ieW = weights.GetEnumerator())
+ {
+ while (ieA.MoveNext())
+ {
+ if (!ieB.MoveNext())
+ {
+ throw new ArgumentOutOfRangeException("dataB", Resources.ArgumentArraysSameLength);
+ }
+ if (!ieW.MoveNext())
+ {
+ throw new ArgumentOutOfRangeException("weights", Resources.ArgumentArraysSameLength);
+ }
+ ++n;
+
+ double xi = ieA.Current;
+ double yi = ieB.Current;
+ double wi = ieW.Current;
+
+ double temp = sumWeight + wi;
+
+ double deltaX = xi - meanA;
+ double rX = deltaX * wi / temp;
+ meanA += rX;
+ varA += sumWeight * deltaX * rX;
+
+ double deltaY = yi - meanB;
+ double rY = deltaY * wi / temp;
+ meanB += rY;
+ varB += sumWeight * deltaY * rY;
+
+ sumWeight = temp;
+
+ covariance += deltaX * deltaY * (n - 1) * wi / n;
+ }
+ if (ieB.MoveNext())
+ {
+ throw new ArgumentOutOfRangeException("dataB", Resources.ArgumentArraysSameLength);
+ }
+ if (ieW.MoveNext())
+ {
+ throw new ArgumentOutOfRangeException("weights", Resources.ArgumentArraysSameLength);
+ }
+ }
+ return covariance / Math.Sqrt(varA * varB);
+ }
+
///
/// Computes the Pearson Product-Moment Correlation matrix.
///