### Question:
MetropolisHastingsStep { public StepResult step(final Set<Variable> chosenVariables, final double logProbabilityBeforeStep) { return step(chosenVariables, logProbabilityBeforeStep, DEFAULT_TEMPERATURE); } MetropolisHastingsStep(ProbabilisticModel model,
                           ProposalDistribution proposalDistribution,
                           ProposalRejectionStrategy rejectionStrategy,
                           KeanuRandom random); StepResult step(final Set<Variable> chosenVariables,
                           final double logProbabilityBeforeStep); StepResult step(final Set<Variable> chosenVariables,
                           final double logProbabilityBeforeStep,
                           final double temperature);  }### Answer:
@Test public void doesCalculateCorrectLogProbAfterAcceptingStep() { DoubleVertex A = new GaussianVertex(0, 1); A.setValue(1.0); DoubleVertex B = A.times(2); DoubleVertex observedB = new GaussianVertex(B, 1); observedB.observe(5); BayesianNetwork bayesNet = new BayesianNetwork(A.getConnectedGraph()); KeanuProbabilisticModel model = new KeanuProbabilisticModel(bayesNet); double logProbBeforeStep = model.logProb(); MetropolisHastingsStep mhStep = new MetropolisHastingsStep( model, new PriorProposalDistribution(), new RollBackToCachedValuesOnRejection(), alwaysAccept ); MetropolisHastingsStep.StepResult result = mhStep.step( Collections.singleton(A), logProbBeforeStep ); assertTrue(result.isAccepted()); assertEquals(model.logProb(), result.getLogProbabilityAfterStep(), 1e-10); }
@Category(Slow.class) @Test public void doesAllowCustomProposalDistribution() { DoubleVertex A = new GaussianVertex(0, 1); A.setValue(0.0); ProbabilisticModel model = new KeanuProbabilisticModel(A.getConnectedGraph()); MetropolisHastingsStep mhStep = stepFunctionWithConstantProposal(model, 1.0, alwaysAccept); MetropolisHastingsStep.StepResult result = mhStep.step( Collections.singleton(A), model.logProb() ); assertTrue(result.isAccepted()); assertEquals(1.0, A.getValue(0), 1e-10); }
@Test public void doesRejectOnImpossibleProposal() { DoubleVertex A = new UniformVertex(0, 1); A.setValue(0.5); ProbabilisticModel model = new KeanuProbabilisticModel(A.getConnectedGraph()); MetropolisHastingsStep mhStep = stepFunctionWithConstantProposal(model, -1, alwaysAccept); MetropolisHastingsStep.StepResult result = mhStep.step( Collections.singleton(A), model.logProb() ); assertFalse(result.isAccepted()); assertEquals(0.5, A.getValue(0), 1e-10); }
@Test public void doesRejectWhenRejectProbabilityIsOne() { DoubleVertex A = new GaussianVertex(0, 1); A.setValue(0.5); DoubleVertex B = A.times(2); DoubleVertex C = new GaussianVertex(B, 1); C.observe(5.0); ProbabilisticModel model = new KeanuProbabilisticModel(A.getConnectedGraph()); MetropolisHastingsStep mhStep = stepFunctionWithConstantProposal(model, 10, alwaysReject); MetropolisHastingsStep.StepResult result = mhStep.step( Collections.singleton(A), model.logProb() ); assertFalse(result.isAccepted()); assertEquals(0.5, A.getValue(0), 1e-10); }