Quantcast
Channel: Math.NET Numerics
Viewing all articles
Browse latest Browse all 971

New Post: Is there a covariance method for 2 dataset?

$
0
0
You can use the Pearsons correlation to compute the elements of a correlation matrix:
I am sure this is not the fastest method and ignores the fact that the matrix could be better implemented with a square symmetric matrix but show how to get started

public static Matrix<double> Correl(Matrix<double> x)
    {
        Matrix<double> correlM = new DenseMatrix(x.RowCount, x.RowCount);

        //off diagonal elements
        foreach (var rowx in x.RowEnumerator())
        {
            foreach (var rowy in x.RowEnumerator())
            {
                if (rowy.Item1 > rowx.Item1)
                {
                    correlM[rowx.Item1, rowy.Item1] = MathNet.Numerics.Statistics.Correlation.Pearson(rowx.Item2, rowy.Item2);
                }
                if (rowy.Item1 < rowx.Item1)
                {
                    correlM[rowx.Item1, rowy.Item1] = correlM[rowy.Item1, rowx.Item1];
                }

            }
        }
        //Diagonal elements
        foreach (var rowx in x.RowEnumerator())
        {
            correlM[rowx.Item1, rowx.Item1] = MathNet.Numerics.Statistics.Correlation.Pearson(rowx.Item2, rowx.Item2);
        }

        return correlM;
    }

Viewing all articles
Browse latest Browse all 971

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>