From 01a26e9cb2ebec43363caba758abf0dc59781532 Mon Sep 17 00:00:00 2001 From: seahearman Date: Fri, 6 Apr 2012 12:25:07 -0300 Subject: [PATCH] I want to sample a vector from a multivariate normal distribution. According to the Help file, I should use the method Sample(), but I found out that I can only sample a square matrix from this method, that is when I use the following code: var NRow = 3; var NCol = 3; var m = new DenseMatrix(NRow, NCol); var v = new DenseMatrix(NRow); for (var i = 0; i < NRow; i++) { v[i, i] = 1; } var k = new DenseMatrix(NCol); for (var i = 0; i < NCol; i++) { k[i, i] = 1; } var NewMatrix = new MatrixNormal(m, v, k); var Sampleone = NewMatrix.Sample(); It is fine, but when I change NRow=4, (but keep NCol=3) it does not work. I checked the code, I think the problem is in MatrixNormal.cs Line 286: var vector = SampleVectorNormal(rnd, new DenseVector(n * n, 0.0), vki); but I think it should be: var vector = SampleVectorNormal(rnd, new DenseVector(n * p, 0.0), vki); --- src/Numerics/Distributions/Multivariate/MatrixNormal.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Numerics/Distributions/Multivariate/MatrixNormal.cs b/src/Numerics/Distributions/Multivariate/MatrixNormal.cs index b2fa27c8..b4c2e923 100644 --- a/src/Numerics/Distributions/Multivariate/MatrixNormal.cs +++ b/src/Numerics/Distributions/Multivariate/MatrixNormal.cs @@ -283,7 +283,7 @@ namespace MathNet.Numerics.Distributions var vki = v.KroneckerProduct(k.Inverse()); // Sample a vector valued random variable with VKi as the covariance. - var vector = SampleVectorNormal(rnd, new DenseVector(n * n, 0.0), vki); + var vector = SampleVectorNormal(rnd, new DenseVector(n * p, 0.0), vki); // Unstack the vector v and add the mean. var r = m.Clone();