/// <summary> /// Performs a Pearson correlation test for association. /// </summary> /// <param name="x">The values of the first variable.</param> /// <param name="y">The values of the second variable.</param> /// <returns>The result of the test.</returns> /// <remarks> /// <para>This test measures the strength of the linear correlation between two variables. The /// test statistic r is simply the covariance of the two variables, scaled by their respective /// standard deviations so as to obtain a number between -1 (perfect linear anti-correlation) /// and +1 (perfect linear correlation).</para> /// <para>The Pearson test cannot reliably detect or rule out non-linear associations. For example, /// variables with a perfect quadratic association may have only a weak linear correlation. If /// you wish to test for associations that may not be linear, consider using the Spearman or /// Kendall tests instead.</para> /// <para>The Pearson correlation test requires O(N) operations.</para> /// <para>The Pearson test requires at least three bivariate values.</para> /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="x"/> or <paramref name="y"/> is <see langword="null"/>.</exception> /// <exception cref="DimensionMismatchException"><paramref name="x"/> and <paramref name="y"/> do not contain the same number of entries.</exception> /// <exception cref="InsufficientDataException">There are fewer than 3 entries in the sample.</exception> /// <seealso cref="SpearmanRhoTest(IReadOnlyList{double},IReadOnlyList{double})"/> /// <seealso cref="KendallTauTest"/> /// <seealso href="http://en.wikipedia.org/wiki/Pearson_correlation_coefficient" /> public static TestResult PearsonRTest(IReadOnlyList <double> x, IReadOnlyList <double> y) { if (x == null) { throw new ArgumentNullException(nameof(x)); } if (y == null) { throw new ArgumentNullException(nameof(y)); } if (x.Count != y.Count) { throw new DimensionMismatchException(); } int n = x.Count; if (n < 3) { throw new InsufficientDataException(); } double xMean, yMean, xxSum, yySum, xySum; ComputeBivariateMomentsUpToTwo(x, y, out n, out xMean, out yMean, out xxSum, out yySum, out xySum); double r = xySum / Math.Sqrt(xxSum * yySum); ContinuousDistribution p = new PearsonRDistribution(n); return(new TestResult("r", r, p, TestType.TwoTailed)); }
/// <summary> /// Performs a Pearson correlation test for association. /// </summary> /// <returns>The result of the test.</returns> /// <remarks> /// <para>This test measures the strength of the linear correlation between two variables. The /// test statistic r is simply the covariance of the two variables, scaled by their respective /// standard deviations so as to obtain a number between -1 (perfect linear anti-correlation) /// and +1 (perfect linear correlation).</para> /// <para>The Pearson test cannot reliably detect or rule out non-linear associations. For example, /// variables with a perfect quadratic association may have only a weak linear correlation. If /// you wish to test for associations that may not be linear, consider using the Spearman or /// Kendall tests instead.</para> /// <para>The Pearson correlation test requires O(N) operations.</para> /// <para>The Pearson test requires at least three bivariate values.</para> /// </remarks> /// <exception cref="InsufficientDataException"><see cref="Count"/> is less than three.</exception> /// <seealso cref="SpearmanRhoTest"/> /// <seealso cref="KendallTauTest"/> /// <seealso href="http://en.wikipedia.org/wiki/Pearson_correlation_coefficient" /> public TestResult PearsonRTest() { if (Count < 3) { throw new InsufficientDataException(); } double r = this.Covariance / Math.Sqrt(xData.Variance * yData.Variance); Distribution p = new PearsonRDistribution(Count); return(new TestResult(r, p)); }