private static Tree treeBuilderBaseCase(List<? extends Variable<DoubleTensor, ?>> latentVariables,
ProbabilisticModelWithGradient logProbGradientCalculator,
final List<? extends Variable> sampleFromVariables,
Leapfrog leapfrog,
double logU,
int buildDirection,
double epsilon,
double logOfMasterPMinusMomentumBeforeLeapfrog) {
leapfrog = leapfrog.step(latentVariables, logProbGradientCalculator, epsilon * buildDirection);
final double logOfMasterPAfterLeapfrog = logProbGradientCalculator.logProb();
final double logOfMasterPMinusMomentum = logOfMasterPAfterLeapfrog - leapfrog.halfDotProductMomentum();
final int acceptedLeapfrogCount = logU <= logOfMasterPMinusMomentum ? 1 : 0;
final boolean shouldContinueFlag = logU < DELTA_MAX + logOfMasterPMinusMomentum;
final Map<VariableReference, ?> sampleAtAcceptedPosition = takeSample((List<? extends Variable<Object, ?>>)sampleFromVariables);
final double deltaLikelihoodOfLeapfrog = Math.min(
1.0,
Math.exp(logOfMasterPMinusMomentum - logOfMasterPMinusMomentumBeforeLeapfrog)
);
return new Tree(
leapfrog,
logOfMasterPAfterLeapfrog,
sampleAtAcceptedPosition,
acceptedLeapfrogCount,
shouldContinueFlag,
deltaLikelihoodOfLeapfrog,
STARTING_TREE_SIZE
);
}