Is there a way to get a spearmans correlation matrix for more than two arrays?

StackOverflow https://stackoverflow.com/questions/19139355

  •  30-06-2022
  •  | 
  •  

Frage

I have code that produces N arrays of doubles and I'd like to create a spearmans correlation matrix. Is there a function to do it for me or do I have to iterate through all the combinations and build my own correlation matrix with Correlation.Spearman()?

War es hilfreich?

Lösung

There is no routine to compute a correlation matrix yet, but I've opened ticket #161 to track it at GitHub.

In the meantime, you could use the following routine (I'm using v3.0.0-alpha5 here):

Matrix<double> Spearman(double[][] data)
{
    var m = Matrix<double>.Build.DenseIdentity(data.Length);
    for(int i=0; i<data.Length; i++)
    for(int j=i+1; j<data.Length; j++)
    {
        var c = Correlation.Spearman(data[i], data[j]);
        m.At(i,j,c);
        m.At(j,i,c);
    }
    return m;
}

var vectors = new[] {
    new[] { 1.0, 2.0, 3.0, 4.0 },
    new[] { 2.0, 4.0, 6.0, 8.0 },
    new[] { 4.0, 3.0, 2.0, 1.0 },
    new[] { 0.0, 10.0, 10.0, 20.0 },
    new[] { 2.0, 4.0, -4.0, 2.0 }
};

Spearman(vectors);

Which will return:

DenseMatrix 5x5-Double
           1            1           -1     0.948683    -0.316228
           1            1           -1     0.948683    -0.316228
          -1           -1            1    -0.948683     0.316228
    0.948683     0.948683    -0.948683            1            0
   -0.316228    -0.316228     0.316228            0            1

Update 2013-10-20:

Added to master, available in V3.0.0-alpha6 and newer:

  • Correlation.PearsonMatrix(params double[][] vectors)
  • Correlation.PearsonMatrix(IEnumerable<double[]> vectors)
  • Correlation.SpearmanMatrix(params double[][] vectors)
  • Correlation.SpearmanMatrix(IEnumerable<double[]> vectors)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top