3

How can we implement below matlab function in C# using math.net library.

Multivariate normal random distribution- http://in.mathworks.com/help/stats/mvnrnd.html

r = mvnrnd(MU,SIGMA,cases)

Also below math.net function not returning any results. I've tried other methods like Selectpermutations/SelectVariations with/without repetition.But none of methods are returning any resuts.

IEnumerable<double> input=new double[] { 1, 2, 3, 4, 5 };
var re = input.SelectCombinationWithRepetition(3);

enter image description here

Am I missing anything??

malkam
  • 2,337
  • 1
  • 14
  • 17

2 Answers2

1

As far as I know, there is no readily available function which gives you multivariate random normal numbers in Math.net. However you can easily write a specific function for that purpose which uses the Cholesky decomposition of the covariance matrix. Indeed when each elements of p-variate vector Z are independently distributed according to standard normal distribution N(0,1), the vector X = M + L * Z is distributed according to p-variate normal distribution whose population mean vector is M and whose covariance matrix is S (where S = L*L').

As I am a vb guy, I will show here the vb code to write such a function:

Public Function MvNRnd(Mu As Vector, Covariance As Matrix, Cases As Double) As Matrix

        Dim standardNormalDistribution As New Normal(0, 1)
        Dim randomValues(Cases - 1) As Vector
        Dim cholesky As Factorization.Cholesky(Of Double) = Covariance.Cholesky


        For i As Integer = 0 To Cases - 1

            'generate independent standard normal random numbers
            randomValues(i) = DenseVector.CreateRandom(Mu.Count, standardNormalDistribution)

            'generate multivariate normal random numbers
            cholesky.Factor.Multiply(randomValues(i), randomValues(i))
            randomValues(i) += Mu

        Next


        Return DenseMatrix.OfRowVectors(randomValues)

    End Function

The equivalent C# code should look like this (translated via http://converter.telerik.com):

public Matrix MvNRnd(Vector Mu, Matrix Covariance, double Cases)
{

    Normal standardNormalDistribution = new Normal(0, 1);
    Vector[] randomValues = new Vector[Cases];
    Factorization.Cholesky<double> cholesky = Covariance.Cholesky;



    for (int i = 0; i <= Cases - 1; i++) {
        //generate independent standard normal random numbers
        randomValues(i) = DenseVector.CreateRandom(Mu.Count, standardNormalDistribution);

        //generate multivariate normal random numbers
        cholesky.Factor.Multiply(randomValues(i), randomValues(i));
        randomValues(i) += Mu;

    }


    return DenseMatrix.OfRowVectors(randomValues);

}
Philippe.H
  • 302
  • 4
  • 13
1
Random randomSource = new SystemRandomSource();

var mu = new DenseMatrix(2, 1, new[] { 2.0, 3 });
var sigma = new DenseMatrix(2, 2, new[] { 1, 1.5, 1.5, 3 });
var one = DenseMatrix.CreateIdentity(1);

var mvnrnd = new MatrixNormal(mu, sigma, one, randomSource);
var sample = mvnrnd.Sample();
porsove
  • 11
  • 1
  • Could you add an explanation of your solution? – Kmeixner Mar 10 '16 at 19:59
  • The solution uses Math.net library (http://www.mathdotnet.com/), that does have a function for multivariate random normal numbers (http://numerics.mathdotnet.com/api/mathnet.numerics.distributions/matrixnormal.htm). Mu and Sigma are defined as at the Mathworks page (http://in.mathworks.com/help/stats/mvnrnd.html) that was referenced in the original question of this thread. – porsove Mar 11 '16 at 01:29