From faf1ae2b860831c693ba9c3bb17fab50ba1503de Mon Sep 17 00:00:00 2001 From: Alexander Fisher Date: Tue, 19 Nov 2024 13:15:55 -0500 Subject: [PATCH] log determinant directly and small const typo --- .../ApproximateTreeDataLikelihood.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/dr/evomodel/treedatalikelihood/ApproximateTreeDataLikelihood.java b/src/dr/evomodel/treedatalikelihood/ApproximateTreeDataLikelihood.java index 0998da0cf5..29dda47472 100644 --- a/src/dr/evomodel/treedatalikelihood/ApproximateTreeDataLikelihood.java +++ b/src/dr/evomodel/treedatalikelihood/ApproximateTreeDataLikelihood.java @@ -65,7 +65,7 @@ public ApproximateTreeDataLikelihood(MaximizerWrtParameter maximizer) { this.likelihood = maximizer.getLikelihood(); final GradientWrtParameterProvider gradient = maximizer.getGradient(); this.parameter = gradient.getParameter(); - this.marginalLikelihoodConst = Math.log(2) - parameter.getDimension() / 2 *Math.log(Math.PI); + this.marginalLikelihoodConst = (parameter.getDimension() - 1) * Math.log(2 * Math.PI); // todo: get Numerical Hessian. if (gradient instanceof HessianWrtParameterProvider) { this.hessianWrtParameterProvider = (HessianWrtParameterProvider) gradient; @@ -139,14 +139,12 @@ public double[][] getHessianLogDensity() { private void updateMarginalLikelihood() { double[] diagonalHessian = hessianWrtParameterProvider.getDiagonalHessianLogDensity(); - double diagonalDeterminant = 1; + double logDiagonalDeterminant = 0; for (int i = 0; i < parameter.getDimension(); i++) { - diagonalDeterminant *= Math.abs(diagonalHessian[i]); + logDiagonalDeterminant += Math.log(Math.abs(diagonalHessian[i])); } // 2pi^{-k/2} * det(Sigma)^{-1/2} * likelihood(map) * prior(map) - // todo: eval posterior(map) - // todo: log likelihood - this.marginalLikelihood = marginalLikelihoodConst + 0.5 * Math.log(diagonalDeterminant) + likelihood.getLogLikelihood(); + this.marginalLikelihood = marginalLikelihoodConst + 0.5 * logDiagonalDeterminant + likelihood.getLogLikelihood(); likelihoodKnown = true; }