1

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()?

4

1 回答 1

1

目前还没有计算相关矩阵的例程,但我在 GitHub 上打开了票证#161来跟踪它。

同时,您可以使用以下例程(我在这里使用v3.0.0-alpha5):

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);

哪个会返回:

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

2013 年 10 月 20 日更新:

添加到 master,在 V3.0.0-alpha6 和更新版本中可用:

  • Correlation.PearsonMatrix(params double[][] vectors)
  • Correlation.PearsonMatrix(IEnumerable<double[]> vectors)
  • Correlation.SpearmanMatrix(params double[][] vectors)
  • Correlation.SpearmanMatrix(IEnumerable<double[]> vectors)
于 2013-10-03T12:18:02.777 回答