Debian Codegraph @

src/dr/inference/operators/DeltaExchangeOperator.java

raw symbols SCIP
1 /*
2 * DeltaExchangeOperator.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.Bounds;
29 import dr.inference.model.Parameter;
30 import dr.math.MathUtils;
31
32 /**
33 * A generic operator for use with a sum-constrained (possibly weighted) vector parameter.
34 *
35 * @author Alexei Drummond
36 * @author Andrew Rambaut
37 * @version $Id: DeltaExchangeOperator.java,v 1.18 2005/06/14 10:40:34 rambaut Exp $
38 */
39 public class DeltaExchangeOperator extends AbstractAdaptableOperator {
40
41 public DeltaExchangeOperator(Parameter parameter, double delta) {
42
43 super(AdaptationMode.ADAPTATION_ON);
44
45 this.parameter = parameter;
46 this.delta = delta;
47 setWeight(1.0);
48 this.isIntegerOperator = false;
49
50 parameterWeights = new int[parameter.getDimension()];
51 for (int i = 0; i < parameterWeights.length; i++) {
52 parameterWeights[i] = 1;
53 }
54 }
55
56 public DeltaExchangeOperator(Parameter parameter, int[] parameterWeights, double delta, double weight, boolean isIntegerOperator, AdaptationMode mode) {
57
58 super(mode);
59
60 this.parameter = parameter;
61 this.delta = delta;
62 setWeight(weight);
63 this.isIntegerOperator = isIntegerOperator;
64 this.parameterWeights = parameterWeights;
65
66 if (isIntegerOperator && delta != Math.round(delta)) {
67 throw new IllegalArgumentException("Can't be an integer operator if delta is not integer");
68 }
69 }
70
71 /**
72 * @return the parameter this operator acts on.
73 */
74 public Parameter getParameter() {
75 return parameter;
76 }
77
78 /**
79 * change the parameter and return the hastings ratio.
80 * performs a delta exchange operation between two scalars in the vector
81 * and return the hastings ratio.
82 */
83 public final double doOperation() {
84
85 // get two dimensions
86 final int dim = parameter.getDimension();
87 final int dim1 = MathUtils.nextInt(dim);
88 int dim2 = dim1;
89 while (dim1 == dim2) {
90 dim2 = MathUtils.nextInt(dim);
91 }
92
93 double scalar1 = parameter.getParameterValue(dim1);
94 double scalar2 = parameter.getParameterValue(dim2);
95
96 if (isIntegerOperator) {
97 int d = MathUtils.nextInt((int) Math.round(delta)) + 1;
98
99 if (parameterWeights[dim1] != parameterWeights[dim2]) throw new RuntimeException();
100 scalar1 = Math.round(scalar1 - d);
101 scalar2 = Math.round(scalar2 + d);
102 } else {
103
104 // exchange a random delta
105 final double d = MathUtils.nextDouble() * delta;
106 scalar1 -= d;
107 if (parameterWeights[dim1] != parameterWeights[dim2]) {
108 scalar2 += d * (double) parameterWeights[dim1] / (double) parameterWeights[dim2];
109 } else {
110 scalar2 += d;
111 }
112
113 }
114 Bounds<Double> bounds = parameter.getBounds();
115
116 if (scalar1 < bounds.getLowerLimit(dim1) ||
117 scalar1 > bounds.getUpperLimit(dim1) ||
118 scalar2 < bounds.getLowerLimit(dim2) ||
119 scalar2 > bounds.getUpperLimit(dim2)) {
120 return Double.NEGATIVE_INFINITY;
121 }
122 parameter.setParameterValue(dim1, scalar1);
123 parameter.setParameterValue(dim2, scalar2);
124
125 // symmetrical move so return a zero hasting ratio
126 return 0.0;
127 }
128
129 // Interface MCMCOperator
130 public final String getOperatorName() {
131 return parameter.getParameterName();
132 }
133
134 public double getAdaptableParameter() {
135 return Math.log(delta);
136 }
137
138 public void setAdaptableParameter(double value) {
139 delta = Math.exp(value);
140 }
141
142 public double getRawParameter() {
143 return delta;
144 }
145
146 public String getAdaptableParameterName() {
147 return "delta";
148 }
149
150 public String toString() {
151 return getOperatorName() + "(windowsize=" + delta + ")";
152 }
153
154 // Private instance variables
155
156 private Parameter parameter = null;
157 private final int[] parameterWeights;
158 private double delta = 0.02;
159 private boolean isIntegerOperator = false;
160 }