Is there a covariance method for 2 dataset?

Mar 31, 2013 at 11:38 AM
Hi there,
I am trying to calc covariance for 2 dataset but can't find it. while I can find correlation class. I am wondering why we don't have a covarance method? Any reason?

thanks
chenming
Coordinator
Apr 4, 2013 at 6:21 PM
No particular reason, it just has not been added yet. (UserVoice)

Thanks,
Christoph
Apr 14, 2013 at 2:06 AM
Thx Chris. As my team is using this lib, can we add such mathematical methods and check in? Do you have a list of methods you plan to add? I can help on this.
Coordinator
Apr 14, 2013 at 8:03 PM
Yes, we accept contributions, and a covariance implementation would certainly be welcome.
We use github to coordinate contributions, see also http://numerics.mathdotnet.com/

Thanks,
Christoph
May 19, 2013 at 1:26 PM
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;
    }
Marked as answer by cdrnet on 10/3/2013 at 5:40 PM
May 21, 2013 at 6:00 PM

thanks, my team has implemented this and will submit to the branch once we are a bit free.