Debian Codegraph @

src/dr/inference/operators/UpDownOperator.java

raw symbols SCIP
1 /*
2 * UpDownOperator.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.math.MathUtils;
29
30 public class UpDownOperator extends AbstractAdaptableOperator {
31
32 private Scalable[] upParameter = null;
33 private Scalable[] downParameter = null;
34 private double scaleFactor;
35
36 public UpDownOperator(Scalable[] upParameter, Scalable[] downParameter,
37 double scale, double weight, AdaptationMode mode) {
38
39 super(mode);
40 setWeight(weight);
41
42 this.upParameter = upParameter;
43 this.downParameter = downParameter;
44 this.scaleFactor = scale;
45 }
46
47 public final double getScaleFactor() {
48 return scaleFactor;
49 }
50
51 public final void setScaleFactor(double sf) {
52 if( (sf > 0.0) && (sf < 1.0) ) {
53 scaleFactor = sf;
54 } else {
55 throw new IllegalArgumentException("scale must be between 0 and 1");
56 }
57 }
58
59 /**
60 * change the parameter and return the hastings ratio.
61 */
62 public final double doOperation() {
63
64 final double scale = (scaleFactor + (MathUtils.nextDouble() * ((1.0 / scaleFactor) - scaleFactor)));
65 int goingUp = 0, goingDown = 0;
66
67 if( upParameter != null ) {
68 for( Scalable up : upParameter ) {
69 goingUp += up.scale(scale, -1, false);
70 // try {
71 // goingUp += up.scale(scale, -1, true);
72 // } catch (RuntimeException re) {
73 // return Double.NEGATIVE_INFINITY;
74 // }
75 }
76 for( Scalable up : upParameter ) {
77 if (!up.testBounds()) {
78 return Double.NEGATIVE_INFINITY;
79 }
80 }
81 }
82
83 if( downParameter != null ) {
84 for( Scalable dn : downParameter ) {
85 goingDown += dn.scale(1.0 / scale, -1, false);
86 // try {
87 // goingDown += dn.scale(1.0 / scale, -1, true);
88 // } catch (RuntimeException re) {
89 // return Double.NEGATIVE_INFINITY;
90 // }
91 }
92 for( Scalable dn : downParameter ) {
93 if (!dn.testBounds()) {
94 return Double.NEGATIVE_INFINITY;
95 }
96 }
97 }
98
99
100 return (goingUp - goingDown - 2) * Math.log(scale);
101 }
102
103 public String getAdaptableParameterName() {
104 return "scaleFactor";
105 }
106
107 public final String getOperatorName() {
108 String name = "";
109 if( upParameter != null ) {
110 name = "up:";
111 for( Scalable up : upParameter ) {
112 name = name + up.getName() + " ";
113 }
114 }
115
116 if( downParameter != null ) {
117 name += "down:";
118 for( Scalable dn : downParameter ) {
119 name = name + dn.getName() + " ";
120 }
121 }
122 return name;
123 }
124
125 public double getAdaptableParameter() {
126 return Math.log(1.0 / scaleFactor - 1.0) / Math.log(10);
127 }
128
129 public void setAdaptableParameter(double value) {
130 scaleFactor = 1.0 / (Math.pow(10.0, value) + 1.0);
131 }
132
133 public double getRawParameter() {
134 return scaleFactor;
135 }
136
137 }
138
139