### Question:
LeapfrogIntegrator { public LeapfrogState step(LeapfrogState fromState, final ProbabilisticModelWithGradient logProbGradientCalculator, final double timeStep) { final double halfTimeStep = timeStep / 2.0; Map<VariableReference, DoubleTensor> nextMomentum = stepMomentum(halfTimeStep, fromState.getMomentum(), fromState.getGradient()); Map<VariableReference, DoubleTensor> nextVelocity = potential.getVelocity(nextMomentum); Map<VariableReference, DoubleTensor> nextPosition = stepPosition(timeStep, nextVelocity, fromState.getPosition()); Map<? extends VariableReference, DoubleTensor> nextPositionGradient = logProbGradientCalculator.logProbGradients(nextPosition); final double nextPositionLogProb = logProbGradientCalculator.logProb(); nextMomentum = stepMomentum(halfTimeStep, nextMomentum, nextPositionGradient); return new LeapfrogState(nextPosition, nextMomentum, nextPositionGradient, nextPositionLogProb, potential); }  LeapfrogState step(LeapfrogState fromState, final ProbabilisticModelWithGradient logProbGradientCalculator, final double timeStep);  }### Answer:
@Test public void canLeapForward() { LeapfrogState leap = integrator.step(start, gradientCalculator, 1.0); assertEquals(0.5, leap.getPosition().get(vertex.getId()).scalar(), 1e-6); }
@Test public void canLeapForwardAndBack() { LeapfrogState leap = integrator.step(start, gradientCalculator, 1.0); assertEquals(initialMomentum, leap.getPosition().get(vertex.getId()).scalar(), 1e-6); LeapfrogState leapBack = integrator.step(leap, gradientCalculator, -1.0); assertEquals(initialPosition, leapBack.getPosition().get(vertex.getId()).scalar(), 1e-6); }
@Test public void leapsForwardWithMinimalEnergyLoss() { LeapfrogState leap = start; for (int i = 0; i < 1000; i++) { leap = integrator.step(leap, gradientCalculator, 1e-3); } assertEquals(start.getEnergy(), leap.getEnergy(), 1e-6); }
@Test public void doesDecreaseKineticEnergyWhenLogProbDecreases() { double startEnergy = start.getKineticEnergy(); LeapfrogState leap = integrator.step(start, gradientCalculator, 1e-3); double afterLeapEnergy = leap.getKineticEnergy(); assertThat(startEnergy, greaterThan(afterLeapEnergy)); }