Skip to content
Browse files

I want to sample a vector from a multivariate normal distribution. A…

…ccording 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);
  • Loading branch information...
1 parent ab489a9 commit 01a26e9cb2ebec43363caba758abf0dc59781532 @seahearman seahearman committed with
Showing with 1 addition and 1 deletion.
  1. +1 −1 src/Numerics/Distributions/Multivariate/MatrixNormal.cs
View
2 src/Numerics/Distributions/Multivariate/MatrixNormal.cs
@@ -283,7 +283,7 @@ public static Matrix<double> Sample(Random rnd, Matrix<double> m, Matrix<double>
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();

0 comments on commit 01a26e9

Please sign in to comment.
Something went wrong with that request. Please try again.