MultiNormal implements Sampler<Vector> { @Override public Vector sample() { Vector v = new DenseVector(dimension).assign( new DoubleFunction() { @Override public double apply(double ignored) { return gen.nextGaussian(); } } ); if (mean != null) { if (scale != null) { return scale.times(v).plus(mean); } else { return v.plus(mean); } } else { if (scale != null) { return scale.times(v); } else { return v; } } } MultiNormal(Vector diagonal);  MultiNormal(Vector diagonal, Vector mean);  MultiNormal(Matrix a, Vector mean);  MultiNormal(int dimension);  MultiNormal(double radius, Vector mean); private  MultiNormal(Matrix scale, Vector mean, int dimension); @Override Vector sample(); Vector getScale();  }
@Test public void testDiagonal() { DenseVector offset = new DenseVector(new double[]{6, 3, 0}); MultiNormal n = new MultiNormal( new DenseVector(new double[]{1, 2, 5}), offset); OnlineSummarizer[] s = { new OnlineSummarizer(), new OnlineSummarizer(), new OnlineSummarizer() }; OnlineSummarizer[] cross = { new OnlineSummarizer(), new OnlineSummarizer(), new OnlineSummarizer() }; for (int i = 0; i < 10000; i++) { Vector v = n.sample(); for (int j = 0; j < 3; j++) { s[j].add(v.get(j) - offset.get(j)); int k1 = j % 2; int k2 = (j + 1) / 2 + 1; cross[j].add((v.get(k1) - offset.get(k1)) * (v.get(k2) - offset.get(k2))); } } for (int j = 0; j < 3; j++) { assertEquals(0, s[j].getMean() / s[j].getSD(), 0.04); assertEquals(0, cross[j].getMean() / cross[j].getSD(), 0.04); } }
@Test public void testRadius() { MultiNormal gen = new MultiNormal(0.1, new DenseVector(10)); OnlineSummarizer s = new OnlineSummarizer(); for (int i = 0; i < 10000; i++) { double x = gen.sample().norm(2) / Math.sqrt(10); s.add(x); } assertEquals(0.1, s.getMean(), 0.01); }