【问题标题】:Find covariance of Math Net matrix求数学网络矩阵的协方差
【发布时间】:2015-08-27 18:33:22
【问题描述】:

我正在使用 MathNet Numerics 矩阵并试图找出矩阵的协方差。

如何求矩阵的协方差?

我们有方法可以在 Statistics 命名空间下找到两个 IEnumerable 之间的协方差。

http://numerics.mathdotnet.com/api/MathNet.Numerics.Statistics/Statistics.htm

但我不知道如何使用它来查找矩阵。

例如:在 matlab/octave 中

在 C# 中也是如此。我们该如何实施?

【问题讨论】:

标签: c# statistics covariance math.net mathnet-numerics


【解决方案1】:

我认为 Math.NET 中没有任何方法可以模仿 Matlab/Octave 中的 cov 函数,但您可以轻松编写自己的方法:

public static Matrix<double> GetCovarianceMatrix(Matrix<double> matrix)
{
    var columnAverages = matrix.ColumnSums() / matrix.RowCount;
    var centeredColumns = matrix.EnumerateColumns().Zip(columnAverages, (col, avg) => col - avg);
    var centered = DenseMatrix.OfColumnVectors(centeredColumns);
    var normalizationFactor = matrix.RowCount == 1 ? 1 : matrix.RowCount - 1;
    return centered.TransposeThisAndMultiply(centered) / normalizationFactor;
}

【讨论】:

    【解决方案2】:

    您可以使用 Accord.Statistics nuget 包中的 Tools.Covariance() 方法。 http://accord-framework.net/docs/html/M_Accord_Statistics_Tools_Covariance.htm
    做这样的事情:

    using Accord.Statistics;
    public void CalculateMatrixCovariance()
    {
        var matrix = new double[,] {
            {3,5},
            {9,10}};
    
        var covMatrix = matrix.Covariance();
    
        Console.WriteLine(covMatrix[0,0] + " " + covMatrix[0, 1]);
        Console.WriteLine(covMatrix[1,0] + " " + covMatrix[1, 1]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 2020-04-13
      • 2014-03-06
      • 1970-01-01
      • 2017-08-27
      • 2019-03-26
      相关资源
      最近更新 更多