| 1 | /* |
| 2 | * MCMCCriterion.java |
| 3 | * |
| 4 | * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 | * |
| 6 | * This file is part of BEAST. |
| 7 | * See the NOTICE file distributed with this work for additional |
| 8 | * information regarding copyright ownership and licensing. |
| 9 | * |
| 10 | * BEAST is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 |
| 13 | * of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * BEAST is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with BEAST; if not, write to the |
| 22 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | * Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | package dr.inference.mcmc; |
| 27 | |
| 28 | import dr.math.MathUtils; |
| 29 | import dr.inference.markovchain.Acceptor; |
| 30 | |
| 31 | /** |
| 32 | * This class encapsulates the acceptance criterion for an MCMC proposal. |
| 33 | * |
| 34 | * @author Alexei Drummond |
| 35 | * |
| 36 | * @version $Id: MCMCCriterion.java,v 1.12 2005/05/24 20:25:59 rambaut Exp $ |
| 37 | */ |
| 38 | public class MCMCCriterion implements Acceptor { |
| 39 | |
| 40 | // this parameter is actually 1/T, when the temperature parameter is 0.0, then the distribution |
| 41 | // is flat and will always accept (symmetric) proposals, i.e. hastings ratio of 0 in log space. |
| 42 | // As this temperature parameter increases, the posterior gets more peaked. |
| 43 | protected double temperature = 1.0; |
| 44 | |
| 45 | public MCMCCriterion() { |
| 46 | |
| 47 | temperature = 1.0; |
| 48 | } |
| 49 | |
| 50 | public MCMCCriterion(double t) { |
| 51 | temperature = t; |
| 52 | } |
| 53 | |
| 54 | public double getAcceptanceValue(double oldScore, double hastingsRatio) { |
| 55 | |
| 56 | final double acceptanceValue = |
| 57 | (MathUtils.randomLogDouble() + (oldScore * temperature) - hastingsRatio) / temperature; |
| 58 | |
| 59 | return acceptanceValue; |
| 60 | } |
| 61 | |
| 62 | public boolean accept(double oldScore, double newScore, double hastingsRatio, double[] logr) { |
| 63 | |
| 64 | logr[0] = (newScore - oldScore) * temperature + hastingsRatio; |
| 65 | |
| 66 | // for coercedAcceptanceProbability |
| 67 | if (logr[0] > 0) logr[0] = 0.0; |
| 68 | |
| 69 | final double v = MathUtils.randomLogDouble(); |
| 70 | final boolean accept = v < logr[0]; |
| 71 | |
| 72 | return accept; |
| 73 | } |
| 74 | |
| 75 | public double getTemperature() { |
| 76 | return temperature; |
| 77 | } |
| 78 | |
| 79 | public void setTemperature(double temperature) { |
| 80 | this.temperature = temperature; |
| 81 | } |
| 82 | |
| 83 | } |