據我所知,沒有現成的函數可以給你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);
}
您可以添加解決方案的解釋嗎? – Kmeixner
該解決方案使用Math.net庫(http://www.mathdotnet.com/),它具有多元隨機正態數字函數(http://numerics.mathdotnet.com/api/mathnet.numerics.distributions/ matrixnormal.htm)。 Mu和Sigma的定義是在此線程原始問題中引用的Mathworks頁面(http://in.mathworks.com/help/stats/mvnrnd.html)中。 – porsove