### Question:
MetropolisHastings implements PosteriorSamplingAlgorithm { @Override public NetworkSamples getPosteriorSamples(ProbabilisticModel model, List<? extends Variable> variablesToSampleFrom, int sampleCount) { return generatePosteriorSamples(model, variablesToSampleFrom) .generate(sampleCount); }  static MetropolisHastingsBuilder builder(); @Override NetworkSamples getPosteriorSamples(ProbabilisticModel model,
                                              List<? extends Variable> variablesToSampleFrom,
                                              int sampleCount); @Override NetworkSamplesGenerator generatePosteriorSamples(final ProbabilisticModel model,
                                                            final List<? extends Variable> variablesToSampleFrom);  }### Answer:
@Category(Slow.class) @Test public void samplesContinuousTensorPrior() { long[] shape = new long[]{1, 1}; DoubleVertex A = new GaussianVertex(shape, 20.0, 1.0); DoubleVertex B = new GaussianVertex(shape, 20.0, 1.0); A.setValue(20.0); B.setValue(20.0); DoubleVertex Cobserved = new GaussianVertex(A.plus(B), 1.0); Cobserved.observe(46.0); BayesianNetwork bayesNet = new BayesianNetwork(Arrays.asList(A, B, Cobserved)); bayesNet.probeForNonZeroProbability(100); KeanuProbabilisticModel model = new KeanuProbabilisticModel(bayesNet); NetworkSamples posteriorSamples = Keanu.Sampling.MetropolisHastings.withDefaultConfig().getPosteriorSamples( model, Arrays.asList(A, B), 5000 ); DoubleTensor averagePosteriorA = posteriorSamples.getDoubleTensorSamples(A).getAverages(); DoubleTensor averagePosteriorB = posteriorSamples.getDoubleTensorSamples(B).getAverages(); DoubleTensor allActuals = averagePosteriorA.plus(averagePosteriorB); for (double actual : allActuals.asFlatDoubleArray()) { assertEquals(44.0, actual, 0.1); } }
@Test public void samplesSimpleDiscretePriorWithDefaults() { MCMCTestCase testCase = new SingleVariateDiscreteTestCase(); BayesianNetwork bayesNet = testCase.getModel(); KeanuProbabilisticModel model = new KeanuProbabilisticModel(bayesNet); NetworkSamples posteriorSamples = Keanu.Sampling.MetropolisHastings.withDefaultConfig().getPosteriorSamples( model, model.getLatentVertices(), 10000 ); testCase.assertExpected(posteriorSamples); }
@Category(Slow.class) @Test public void samplesComplexDiscretePriorWithDefaults() { MCMCTestCase testCase = new MultiVariateDiscreteTestCase(); BayesianNetwork bayesNet = testCase.getModel(); KeanuProbabilisticModel model = new KeanuProbabilisticModel(bayesNet); NetworkSamples posteriorSamples = Keanu.Sampling.MetropolisHastings.withDefaultConfig().getPosteriorSamples( model, model.getLatentVertices(), 1000 ); testCase.assertExpected(posteriorSamples); }
@Test public void samplesFromPriorWithObservedDeterministic() { BernoulliVertex A = new BernoulliVertex(0.5); BernoulliVertex B = new BernoulliVertex(0.5); BooleanVertex C = A.or(B); C.observe(false); BayesianNetwork net = new BayesianNetwork(A.getConnectedGraph()); net.probeForNonZeroProbability(100); KeanuProbabilisticModel model = new KeanuProbabilisticModel(net); NetworkSamples posteriorSamples = Keanu.Sampling.MetropolisHastings.withDefaultConfig().getPosteriorSamples( model, Collections.singletonList(A), 10000 ); double postProbTrue = posteriorSamples.get(A).probability(v -> v.scalar()); assertEquals(0.0, postProbTrue, 0.01); }