Debian Codegraph @

src/dr/inference/operators/CenteredScaleOperator.java

raw symbols SCIP
1 /*
2 * CenteredScaleOperator.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 vector parameter.
33 *
34 * @author Alexei Drummond
35 * @version $Id: CenteredScaleOperator.java,v 1.20 2005/06/14 10:40:34 rambaut Exp $
36 */
37
38 // AR - I don't see how this can work...
39 @Deprecated
40 public class CenteredScaleOperator extends AbstractAdaptableOperator {
41
42 public CenteredScaleOperator(Parameter parameter) {
43 super(AdaptationMode.DEFAULT);
44 this.parameter = parameter;
45 }
46
47 public CenteredScaleOperator(Parameter parameter, double scale, int weight, AdaptationMode mode) {
48 super(mode);
49 this.parameter = parameter;
50 this.scaleFactor = scale;
51 setWeight(weight);
52 }
53
54 /**
55 * @return the parameter this operator acts on.
56 */
57 public Parameter getParameter() {
58 return parameter;
59 }
60
61 /**
62 * Change the parameter and return the hastings ratio.
63 * Performs a centered scale operation on the vector
64 * and returns the hastings ratio.
65 * This operator changes the variance but maintains the order
66 * of the scalars.
67 */
68 public final double doOperation() {
69
70 double total = 0.0;
71
72 for (int i = 0; i < parameter.getDimension(); i++) {
73 total += parameter.getParameterValue(i);
74 }
75 double mean = total / parameter.getDimension();
76 double scaleFactor = getRandomScaleFactor();
77 double logq = parameter.getDimension() * Math.log(1.0 / scaleFactor);
78
79 for (int i = 0; i < parameter.getDimension(); i++) {
80
81 double newScalar = (parameter.getParameterValue(i) - mean) * scaleFactor + mean;
82 if (newScalar < parameter.getBounds().getLowerLimit(i) || newScalar > parameter.getBounds().getUpperLimit(i)) {
83 // throw new OperatorFailedException("Proposed value out of bounds");
84 return Double.NEGATIVE_INFINITY;
85 }
86 parameter.setParameterValue(i, newScalar);
87 }
88
89 // non-symmetrical move
90 return logq;
91 }
92
93 public final double getRandomScaleFactor() {
94 return scaleFactor + (MathUtils.nextDouble() * ((1 / scaleFactor) - scaleFactor));
95 }
96
97 // Interface MCMCOperator
98 public final String getOperatorName() {
99 return parameter.getParameterName();
100 }
101
102 public double getAdaptableParameter() {
103 return Math.log(1.0 / scaleFactor - 1.0);
104 }
105
106 public void setAdaptableParameter(double value) {
107 scaleFactor = 1.0 / (Math.exp(value) + 1.0);
108 }
109
110 public double getRawParameter() {
111 return scaleFactor;
112 }
113
114 public String getAdaptableParameterName() {
115 return "scaleFactor";
116 }
117
118 public String toString() {
119 return getOperatorName() + "(scaleFactor=" + scaleFactor + ")";
120 }
121
122 // Private instance variables
123
124 private Parameter parameter = null;
125 public double scaleFactor = 0.5;
126 }