Debian Codegraph @

src/dr/inference/operators/SimpleMCMCOperator.java

raw symbols SCIP
1 /*
2 * SimpleMCMCOperator.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.operators;
27
28 import dr.inference.model.Likelihood;
29
30 public abstract class SimpleMCMCOperator implements MCMCOperator {
31
32 public abstract String getOperatorName();
33
34 /**
35 * @return the weight of this operator.
36 */
37 public final double getWeight() {
38 return weight;
39 }
40
41 public void setPathParameter(double beta) {
42 throw new IllegalArgumentException("Path parameter has no effect on Metropolis-Hastings kernels." +
43 "\nGibbs samplers need an implementation for use in power-posteriors");
44 }
45
46 /**
47 * Sets the weight of this operator.
48 */
49 public final void setWeight(double w) {
50 if( w > 0 ) {
51 weight = w;
52 } else {
53 throw new IllegalArgumentException(
54 "Weight must be a positive real, but tried to set weight to "
55 + w);
56 }
57 }
58
59 public void accept(double deviation) {
60 lastDeviation = deviation;
61
62 if( !operateAllowed ) {
63 operateAllowed = true;
64 acceptCount += 1;
65 sumDeviation += deviation;
66
67 // spanDeviation[0] = Math.min(spanDeviation[0], deviation);
68 // spanDeviation[1] = Math.max(spanDeviation[1], deviation);
69 // spanCount += 1;
70 } else {
71 throw new RuntimeException(
72 "Accept/reject methods called twice without operate called in between!");
73 }
74 }
75
76 public void reject() {
77 if( !operateAllowed ) {
78 operateAllowed = true;
79 rejectCount += 1;
80 } else {
81 throw new RuntimeException(
82 "Accept/reject methods called twice without operate called in between!");
83 }
84 }
85
86 public void reset() {
87 operateAllowed = true;
88 acceptCount = 0;
89 rejectCount = 0;
90 lastDeviation = 0.0;
91 sumDeviation = 0.0;
92 }
93
94 public final long getCount() {
95 return acceptCount + rejectCount;
96 }
97
98 public final long getAcceptCount() {
99 return acceptCount;
100 }
101
102 public final void setAcceptCount(long acceptCount) {
103 this.acceptCount = acceptCount;
104 }
105
106 public final long getRejectCount() {
107 return rejectCount;
108 }
109
110 public final void setRejectCount(long rejectCount) {
111 this.rejectCount = rejectCount;
112 }
113
114 public final double getMeanDeviation() {
115 return sumDeviation / acceptCount;
116 }
117
118 public final double getDeviation() {
119 return lastDeviation;
120 }
121
122 public final double getSumDeviation() {
123 return sumDeviation;
124 }
125
126 public final void setSumDeviation(double sumDeviation) {
127 this.sumDeviation = sumDeviation;
128 }
129
130 // public double getSpan(boolean reset) {
131 // double span = 0;
132 // if( spanDeviation[1] > spanDeviation[0] && spanCount > 2000 ) {
133 // span = spanDeviation[1] - spanDeviation[0];
134 //
135 // if( reset ) {
136 // spanDeviation[0] = Double.MAX_VALUE;
137 // spanDeviation[1] = -Double.MAX_VALUE;
138 // spanCount = 0;
139 // }
140 // }
141 // return span;
142 // }
143
144 public final double operate() {
145 if( operateAllowed ) {
146 operateAllowed = false;
147 return doOperation();
148 } else {
149 throw new RuntimeException(
150 "Operate called twice without accept/reject in between!");
151 }
152 }
153
154 public final double operate(Likelihood likelihood) {
155 if( operateAllowed ) {
156 operateAllowed = false;
157 return doOperation(likelihood);
158 } else {
159 throw new RuntimeException(
160 "Operate called twice without accept/reject in between!");
161 }
162 }
163
164 public final double getAcceptanceProbability() {
165 return (double) acceptCount / (double) (acceptCount + rejectCount);
166 }
167
168 /**
169 * Called by operate(), does the actual operation.
170 *
171 * @return the hastings ratio
172 */
173 public double doOperation(Likelihood likelihood) {
174 return 0.0;
175 }
176
177 public double getMeanEvaluationTime() {
178 return (double) sumEvaluationTime / (double) (acceptCount + rejectCount);
179 }
180
181 public long getTotalEvaluationTime() {
182 return sumEvaluationTime;
183 }
184
185 public void addEvaluationTime(long time) {
186 sumEvaluationTime += time;
187 }
188
189 /**
190 * Called by operate(), does the actual operation.
191 *
192 * @return the hastings ratio
193 */
194 public abstract double doOperation();
195
196 private double weight = 1.0;
197 private long acceptCount = 0;
198 private long rejectCount = 0;
199
200 private double sumDeviation = 0.0;
201 private double lastDeviation = 0.0;
202
203 private boolean operateAllowed = true;
204
205 private long sumEvaluationTime = 0;
206
207 // private final double[] spanDeviation = {Double.MAX_VALUE, -Double.MAX_VALUE};
208 // private int spanCount = 0;
209 }