2

我们如何使用 math.net 库在 C# 中实现下面的 matlab 函数。

多元正态随机分布- http://in.mathworks.com/help/stats/mvnrnd.html

r = mvnrnd(MU,SIGMA,cases)

同样在 math.net 函数下方不返回任何结果。我已经尝试过其他方法,例如 Selectpermutations/SelectVariations 有/没有重复。但是没有一个方法返回任何结果。

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

在此处输入图像描述

我错过了什么吗??

4

2 回答 2

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();
于 2016-03-10T19:41:00.033 回答
1

据我所知,在 Math.net 中没有现成的函数可以为您提供多元随机正态数。但是,您可以轻松地为此目的编写一个特定的函数,该函数使用协方差矩阵的 Cholesky 分解。实际上,当 p 变量向量 Z 的每个元素根据标准正态分布 N(0,1) 独立分布时,向量 X = M + L * Z 根据总体均值向量为 M 的 p 变量正态分布分布,并且其协方差矩阵为 S(其中 S = L*L')。

由于我是一个 vb 人,我将在这里展示编写这样一个函数的 vb 代码:

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

等效的 C# 代码应如下所示(通过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);

}
于 2015-09-23T16:51:33.440 回答