Debian Codegraph @

src/dr/inference/operators/DeltaMixOperator.java

raw symbols SCIP
1 /*
2 * DeltaMixOperator.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.Parameter;
29 import dr.math.MathUtils;
30
31 /**
32 * A generic operator for use with a sum-constrained (possibly weighted) vector parameter. This proposal is
33 * non-symmetrical, performs better than DeltaExchangeOperator when the components are on vastly different scales,
34 * does not propose states outside the sum-constraints and thus does not require bounds checking on the proposed state
35 *
36 * @author Alexander V. Alekseyenko
37 * @version $Id: DeltaMixOperator.java,v 1.6 2010/09/23 $
38 */
39 public class DeltaMixOperator extends AbstractAdaptableOperator {
40
41 public DeltaMixOperator(Parameter parameter, double delta) {
42
43 super(AdaptationMode.ADAPTATION_ON);
44
45 this.parameter = parameter;
46 this.delta = delta;
47 setWeight(1.0);
48
49 parameterWeights = new int[parameter.getDimension()];
50 for (int i = 0; i < parameterWeights.length; i++) {
51 parameterWeights[i] = 1;
52 }
53 }
54
55 public DeltaMixOperator(Parameter parameter, int[] parameterWeights, double delta, double weight, AdaptationMode mode) {
56
57 super(mode);
58
59 this.parameter = parameter;
60 this.delta = delta;
61 setWeight(weight);
62 this.parameterWeights = parameterWeights;
63 }
64
65 /**
66 * @return the parameter this operator acts on.
67 */
68 public Parameter getParameter() {
69 return parameter;
70 }
71
72 /**
73 * change the parameter and return the hastings ratio.
74 * performs a delta exchange operation between two scalars in the vector
75 * and return the hastings ratio.
76 */
77 public final double doOperation() {
78
79 // get two dimensions
80 final int dim = parameter.getDimension();
81 final int dim1 = MathUtils.nextInt(dim);
82 int dim2 = dim1;
83 while (dim1 == dim2) {
84 dim2 = MathUtils.nextInt(dim);
85 }
86
87 double scalar1 = parameter.getParameterValue(dim1);
88 double scalar2 = parameter.getParameterValue(dim2);
89
90 // exchange a random fraction of dim1 component with dim2
91 final double d = MathUtils.nextDouble() * delta * scalar1;
92 scalar1 -= d;
93 if (parameterWeights[dim1] != parameterWeights[dim2]) {
94 scalar2 += d * (double) parameterWeights[dim1] / (double) parameterWeights[dim2];
95 } else {
96 scalar2 += d;
97 }
98
99 parameter.setParameterValue(dim1, scalar1);
100 parameter.setParameterValue(dim2, scalar2);
101
102 return Math.log(scalar2 / (scalar1 + d));
103 }
104
105 // Interface MCMCOperator
106 public final String getOperatorName() {
107 return parameter.getParameterName();
108 }
109
110 public double getAdaptableParameter() {
111 return Math.log(delta)-Math.log(1-delta);
112 }
113
114 public void setAdaptableParameter(double value) {
115 delta = 1/(1+Math.exp(-value));
116 }
117
118 public double getRawParameter() {
119 return delta;
120 }
121
122 public String getAdaptableParameterName() {
123 return "delta";
124 }
125
126 public String toString() {
127 return getOperatorName() + "(windowsize=" + delta + ")";
128 }
129
130 // Private instance variables
131
132 private Parameter parameter = null;
133 private final int[] parameterWeights;
134 private double delta = 0.2;
135 }