据我所知,在 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);
}