Debian Codegraph @

src/dr/inference/operators/UniformOperator.java

raw symbols SCIP
1 /*
2 * UniformOperator.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.inferencexml.operators.UniformOperatorParser;
31 import dr.math.MathUtils;
32
33 /**
34 * A generic uniform sampler/operator for use with a multi-dimensional parameter.
35 *
36 * @author Alexei Drummond
37 * @author Andrew Rambaut
38 * @version $Id: UniformOperator.java,v 1.16 2005/06/14 10:40:34 rambaut Exp $
39 */
40 public class UniformOperator extends SimpleMCMCOperator {
41
42 public UniformOperator(Parameter parameter, double weight) {
43 this(parameter, weight, null, null);
44 }
45
46 public UniformOperator(Parameter parameter, double weight, Double lowerBound, Double upperBound) {
47 this.parameter = parameter;
48 setWeight(weight);
49
50 this.lowerBound = lowerBound;
51 this.upperBound = upperBound;
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 */
64 public final double doOperation() {
65
66 final int index = MathUtils.nextInt(parameter.getDimension());
67 final Bounds<Double> bounds = parameter.getBounds();
68 final double lower = (lowerBound == null ? bounds.getLowerLimit(index) : Math.max(bounds.getLowerLimit(index), lowerBound));
69 final double upper = (upperBound == null ? bounds.getUpperLimit(index) : Math.min(bounds.getUpperLimit(index), upperBound));
70 final double newValue = (MathUtils.nextDouble() * (upper - lower)) + lower;
71
72 parameter.setParameterValue(index, newValue);
73
74 // System.out.println(newValue + "[" + lower + "," + upper + "]");
75 return 0.0;
76 }
77
78 //MCMCOperator INTERFACE
79 public final String getOperatorName() {
80 return "uniform(" + parameter.getParameterName() + ")";
81 }
82
83 public final void optimize(double targetProb) {
84
85 throw new RuntimeException("This operator cannot be optimized!");
86 }
87
88 public boolean isOptimizing() {
89 return false;
90 }
91
92 public void setOptimizing(boolean opt) {
93 throw new RuntimeException("This operator cannot be optimized!");
94 }
95
96 public String getPerformanceSuggestion() {
97 return "";
98 // final double acceptance = Utils.getAcceptanceProbability(this);
99 // if ( acceptance < getMinimumAcceptanceLevel()) {
100 // return "";
101 // } else if ( acceptance > getMaximumAcceptanceLevel() ) {
102 // return "";
103 // } else {
104 // return "";
105 // }
106 }
107
108 public String toString() {
109 return UniformOperatorParser.UNIFORM_OPERATOR + "(" + parameter.getParameterName() + ")";
110 }
111
112 //PRIVATE STUFF
113
114 private Parameter parameter = null;
115 private final Double lowerBound;
116 private final Double upperBound;
117 }