### Question:
MetropolisHastings implements PosteriorSamplingAlgorithm { @Override public NetworkSamplesGenerator generatePosteriorSamples(final ProbabilisticModel model, final List<? extends Variable> variablesToSampleFrom) { return new NetworkSamplesGenerator(setupSampler(model, variablesToSampleFrom), StatusBar::new); }  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:
@Test public void doesNotStoreSamplesThatWillBeDropped() { int sampleCount = 1000; int dropCount = 100; int downSampleInterval = 2; GaussianVertex A = new GaussianVertex(0, 1); KeanuProbabilisticModel model = new KeanuProbabilisticModel(A.getConnectedGraph()); NetworkSamples samples = Keanu.Sampling.MetropolisHastings.withDefaultConfig() .generatePosteriorSamples(model, model.getLatentVariables()) .dropCount(dropCount) .downSampleInterval(downSampleInterval) .generate(sampleCount); assertEquals((sampleCount - dropCount) / downSampleInterval, samples.size()); assertEquals(0.0, samples.getDoubleTensorSamples(A).getAverages().scalar(), 0.1); }
@Test public void canStreamSamples() { int sampleCount = 1000; int dropCount = 100; int downSampleInterval = 1; GaussianVertex A = new GaussianVertex(0, 1); KeanuProbabilisticModel model = new KeanuProbabilisticModel(A.getConnectedGraph()); MetropolisHastings algo = Keanu.Sampling.MetropolisHastings.withDefaultConfig(); double averageA = algo.generatePosteriorSamples(model, model.getLatentVariables()) .dropCount(dropCount) .downSampleInterval(downSampleInterval) .stream() .limit(sampleCount) .mapToDouble(networkState -> networkState.get(A).scalar()) .average().getAsDouble(); assertEquals(0.0, averageA, 0.1); }