Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
GH-636: Add an Example for Measures.Correlation Method (Double[][])
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarsouza committed Sep 24, 2017
1 parent 81469bf commit 86c8991
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
22 changes: 22 additions & 0 deletions Sources/Accord.Math/Accord.Statistics/Measures.Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,15 @@ public static double[][] Scatter(double[][] matrix, double[] means, double divis
/// </remarks>
/// <param name="matrix">A multi-dimensional array containing the matrix values.</param>
/// <returns>The correlation matrix.</returns>
///
/// <example>
/// <code source="Unit Tests\Accord.Tests.Statistics\ToolsTest.cs" region="doc_correlation" />
/// </example>
///
/// <example>
/// <code source="Unit Tests\Accord.Tests.Statistics\ToolsTest.cs" region="doc_correlation" />
/// </example>
///
public static double[,] Correlation(this double[,] matrix)
{
double[] means = Mean(matrix, dimension: 0);
Expand All @@ -1530,6 +1539,11 @@ public static double[][] Scatter(double[][] matrix, double[] means, double divis
/// </remarks>
/// <param name="matrix">A multi-dimensional array containing the matrix values.</param>
/// <returns>The correlation matrix.</returns>
///
/// <example>
/// <code source="Unit Tests\Accord.Tests.Statistics\ToolsTest.cs" region="doc_correlation" />
/// </example>
///
public static double[][] Correlation(this double[][] matrix)
{
double[] means = Mean(matrix, dimension: 0);
Expand All @@ -1551,6 +1565,10 @@ public static double[][] Correlation(this double[][] matrix)
///
/// <returns>The correlation matrix.</returns>
///
/// <example>
/// <code source="Unit Tests\Accord.Tests.Statistics\ToolsTest.cs" region="doc_correlation" />
/// </example>
///
public static double[,] Correlation(this double[,] matrix, double[] means, double[] standardDeviations)
{
int rows = matrix.Rows();
Expand Down Expand Up @@ -1593,6 +1611,10 @@ public static double[][] Correlation(this double[][] matrix)
///
/// <returns>The correlation matrix.</returns>
///
/// <example>
/// <code source="Unit Tests\Accord.Tests.Statistics\ToolsTest.cs" region="doc_correlation" />
/// </example>
///
public static double[][] Correlation(this double[][] matrix, double[] means, double[] standardDeviations)
{
int rows = matrix.Rows();
Expand Down
22 changes: 20 additions & 2 deletions Unit Tests/Accord.Tests.Statistics/ToolsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,9 @@ public void CorrelationTest()
{
// http://www.solvemymath.com/online_math_calculator/statistics/descriptive/correlation.php

#region doc_correlation
// Let's say we have a matrix containing 5
// samples (rows) of 3 dimensions (columns):
double[,] matrix = new double[,]
{
{ 4.0, 2.0, 0.60 },
Expand All @@ -983,7 +986,10 @@ public void CorrelationTest()
{ 4.1, 2.2, 0.63 }
};

// We can compute their correlation matrix using
double[,] corr1 = Measures.Correlation(matrix);

// The matrix should be equal to:
double[,] expected = new double[,]
{
{ 1.000000, 0.5669467, 0.533745 },
Expand All @@ -992,10 +998,22 @@ public void CorrelationTest()
};


double[,] actual = Measures.Correlation(matrix);
// The same could be repeated with a jagged matrix instead:
double[][] jagged = new double[][]
{
new double[] { 4.0, 2.0, 0.60 },
new double[] { 4.2, 2.1, 0.59 },
new double[] { 3.9, 2.0, 0.58 },
new double[] { 4.3, 2.1, 0.62 },
new double[] { 4.1, 2.2, 0.63 }
};

Assert.IsTrue(Matrix.IsEqual(expected, actual, 0.001));
// And the value would be the same:
double[][] corr2 = Measures.Correlation(jagged);
#endregion

Assert.IsTrue(Matrix.IsEqual(expected, corr1, 1e-5));
Assert.IsTrue(Matrix.IsEqual(expected, corr2, 1e-5));
}


Expand Down

0 comments on commit 86c8991

Please sign in to comment.