Debian Codegraph @

src/dr/inference/operators/SimpleMetropolizedGibbsOperator.java

raw symbols SCIP
1 /*
2 * SimpleMetropolizedGibbsOperator.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 /**
27 *
28 */
29 package dr.inference.operators;
30
31 import dr.inference.model.Likelihood;
32 import dr.inference.model.Model;
33
34 import java.util.logging.Logger;
35
36 /**
37 * @author Sebastian Hoehna
38 */
39 public abstract class SimpleMetropolizedGibbsOperator extends SimpleOperator implements GeneralOperator {
40
41 /**
42 *
43 */
44 public SimpleMetropolizedGibbsOperator() {
45 // Do nothing
46 }
47
48 public abstract double doOperation(Likelihood likelihood)
49 ;
50
51 /*
52 * (non-Javadoc)
53 *
54 * @see dr.inference.operators.GibbsOperator#getStepCount()
55 */
56 public abstract int getStepCount();
57
58 /*
59 * (non-Javadoc)
60 *
61 * @see dr.inference.operators.SimpleOperator#getOperatorName()
62 */
63 @Override
64 public abstract String getOperatorName();
65
66 public final double operate() {
67 return operate(null);
68 }
69
70 public final double operate(Likelihood likelihood) {
71 if (operateAllowed) {
72 operateAllowed = false;
73 return doOperation(likelihood);
74 } else
75 throw new RuntimeException(
76 "Operate called twice without accept/reject in between!");
77 }
78
79 /*
80 * (non-Javadoc)
81 *
82 * @see dr.inference.operators.MCMCOperator#getMaximumAcceptanceLevel()
83 */
84 public final double getMaximumAcceptanceLevel() {
85 return 1.0;
86 }
87
88 /*
89 * (non-Javadoc)
90 *
91 * @see dr.inference.operators.MCMCOperator#getMaximumGoodAcceptanceLevel()
92 */
93 public final double getMaximumGoodAcceptanceLevel() {
94 return 1.0;
95 }
96
97 /*
98 * (non-Javadoc)
99 *
100 * @see dr.inference.operators.MCMCOperator#getMinimumAcceptanceLevel()
101 */
102 public final double getMinimumAcceptanceLevel() {
103 return 0.005;
104 }
105
106 /*
107 * (non-Javadoc)
108 *
109 * @see dr.inference.operators.MCMCOperator#getMinimumGoodAcceptanceLevel()
110 */
111 public final double getMinimumGoodAcceptanceLevel() {
112 return 0.01;
113 }
114
115 /*
116 * (non-Javadoc)
117 *
118 * @see dr.inference.operators.MCMCOperator#getPerformanceSuggestion()
119 */
120 public final String getPerformanceSuggestion() {
121 return "";
122 }
123
124 /*
125 * (non-Javadoc)
126 *
127 * @see dr.inference.operators.MCMCOperator#getTargetAcceptanceProbability()
128 */
129 public final double getTargetAcceptanceProbability() {
130 return 1.0;
131 }
132
133 protected double evaluate(Likelihood likelihood, double pathParameter) {
134
135 double logPosterior = 0.0;
136
137 final double logLikelihood = likelihood.getLogLikelihood() * pathParameter;
138
139 if (Double.isNaN(logLikelihood)) {
140 return Double.NEGATIVE_INFINITY;
141 }
142 // System.err.println("** " + logPosterior + " + " + logLikelihood + " =
143 // " + (logPosterior + logLikelihood));
144 logPosterior += logLikelihood;
145
146 return logPosterior;
147 }
148
149 protected void restore(Likelihood likelihood,
150 Model currentModel, MCMCOperator mcmcOperator, double oldScore) {
151 currentModel.restoreModelState();
152
153 // This is a test that the state is correctly restored. The restored
154 // state is fully evaluated and the likelihood compared with that before
155 // the operation was made.
156 likelihood.makeDirty();
157 final double testScore = evaluate(likelihood, 1.0);
158
159 if (Math.abs(testScore - oldScore) > 1e-6) {
160 Logger.getLogger("error").severe(
161 "State was not correctly restored after reject step.\n"
162 + "Likelihood before: " + oldScore
163 + " Likelihood after: " + testScore + "\n"
164 + "Operator: " + mcmcOperator + " "
165 + mcmcOperator.getOperatorName());
166 }
167 }
168
169 }