private MetropolisHastingsStep stepFunctionWithConstantProposal(ProbabilisticModel model, double constant, KeanuRandom random) { List<Vertex> latentVertices = (List<Vertex>) model.getLatentVariables(); return new MetropolisHastingsStep( model, constantProposal(constant), new RollBackToCachedValuesOnRejection(latentVertices), random ); }
@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); }
@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 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); }