Browse Source

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);
la-knuth
seahearman 15 years ago
committed by Christoph Ruegg
parent
commit
01a26e9cb2
  1. 2
      src/Numerics/Distributions/Multivariate/MatrixNormal.cs

2
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();

Loading…
Cancel
Save