From ed40a44de7746002103968e318187c6b442e58c5 Mon Sep 17 00:00:00 2001 From: Oleksandr Date: Mon, 29 Jun 2026 18:34:11 +0200 Subject: [PATCH] Document NaN behavior for zero-variance inputs in Correlation.Pearson/WeightedPearson/Spearman Pearson, WeightedPearson and Spearman return NaN when one of the input series has zero variance (e.g. fewer than two elements, or all values equal/tied after ranking), since the underlying formula divides by the standard deviation of each series. This was previously undocumented, which led to confusion in #650. No behavior change; XML doc comments only. --- src/Numerics/Statistics/Correlation.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Numerics/Statistics/Correlation.cs b/src/Numerics/Statistics/Correlation.cs index 918cf93ce..cc8e6c00a 100644 --- a/src/Numerics/Statistics/Correlation.cs +++ b/src/Numerics/Statistics/Correlation.cs @@ -161,7 +161,7 @@ static double[] AutoCorrelationFft(double[] x, int kLow, int kHigh) /// /// Sample data A. /// Sample data B. - /// The Pearson product-moment correlation coefficient. + /// The Pearson product-moment correlation coefficient. Returns NaN if either series has zero variance (e.g. fewer than two elements, or all values equal). public static double Pearson(IEnumerable dataA, IEnumerable dataB) { int n = 0; @@ -217,7 +217,7 @@ public static double Pearson(IEnumerable dataA, IEnumerable data /// Sample data A. /// Sample data B. /// Corresponding weights of data. - /// The Weighted Pearson product-moment correlation coefficient. + /// The Weighted Pearson product-moment correlation coefficient. Returns NaN if either series has zero weighted variance. public static double WeightedPearson(IEnumerable dataA, IEnumerable dataB, IEnumerable weights) { double meanA = 0; @@ -310,7 +310,7 @@ public static Matrix PearsonMatrix(IEnumerable vectors) /// /// Sample data series A. /// Sample data series B. - /// The Spearman ranked correlation coefficient. + /// The Spearman ranked correlation coefficient. Returns NaN if either series has zero variance after ranking (e.g. fewer than two elements, or all values tied). public static double Spearman(IEnumerable dataA, IEnumerable dataB) { return Pearson(Rank(dataA), Rank(dataB));