Debian Codegraph @

src/dr/inference/operators/IndependentNormalDistributionSampler.java

raw symbols SCIP
1 /*
2 * IndependentNormalDistributionSampler.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 cern.jet.random.Normal;
29 import cern.jet.random.engine.MersenneTwister;
30 import cern.jet.random.engine.RandomEngine;
31 import dr.inference.distribution.NormalDistributionModel;
32 import dr.inference.model.Bounds;
33 import dr.inference.model.Parameter;
34 import dr.inference.model.Variable;
35 import dr.math.MathUtils;
36 import dr.xml.AttributeRule;
37 import dr.xml.ElementRule;
38 import dr.xml.XMLObject;
39 import dr.xml.XMLParseException;
40 import dr.xml.XMLSyntaxRule;
41
42 /**
43 * An independent normal distribution sampler to propose new (independent) values from a provided normal distribution model.
44 *
45 * @author Guy Baele
46 *
47 */
48 public class IndependentNormalDistributionSampler extends SimpleMCMCOperator {
49
50 public static final String OPERATOR_NAME = "independentNormalDistributionSampler";
51
52 private Variable<Double> variable = null;
53 private NormalDistributionModel model = null;
54 private boolean updateAllIndependently = true;
55
56 private static final boolean TRY_COLT = true;
57 private static RandomEngine randomEngine;
58 private static Normal coltNormal;
59
60 public IndependentNormalDistributionSampler(Variable variable, NormalDistributionModel model) {
61
62 this(variable, model, 1.0);
63
64 }
65
66 public IndependentNormalDistributionSampler(Variable variable, NormalDistributionModel model, double weight) {
67
68 this(variable, model, weight, true);
69
70 }
71
72 public IndependentNormalDistributionSampler(Variable variable, NormalDistributionModel model, double weight, boolean updateAllIndependently) {
73
74 this.variable = variable;
75 this.model = model;
76 this.updateAllIndependently = updateAllIndependently;
77 setWeight(weight);
78
79 if (TRY_COLT) {
80 randomEngine = new MersenneTwister(MathUtils.nextInt());
81 //create standard normal distribution, internal states will be bypassed anyway
82 coltNormal = new Normal(0.0, 1.0, randomEngine);
83 } else {
84 //no random draw with specified mean and stdev implemented in the normal distribution in BEAST (as far as I know)
85 throw new RuntimeException("Normal distribution in BEAST still needs a random sampler.");
86 }
87
88 }
89
90 public String getPerformanceSuggestion() {
91 return "";
92 }
93
94 public String getOperatorName() {
95 return "independentNormalDistribution(" + variable.getVariableName() + ")";
96 }
97
98 /**
99 * change the parameter and return the hastings ratio.
100 */
101 public double doOperation() {
102
103 double logq = 0;
104
105 double currentValue;
106 double newValue;
107
108 final Bounds<Double> bounds = variable.getBounds();
109 final int dim = variable.getSize();
110
111 if (updateAllIndependently) {
112 for (int i = 0; i < dim; i++) {
113
114 //both current and new value of the variable needed for the hastings ratio
115 currentValue = variable.getValue(i);
116
117 //use the current mean and precision (standard deviation)
118 newValue = coltNormal.nextDouble(model.getMean().getValue(i), 1.0 / Math.sqrt(model.getPrecision().getValue(i)));
119
120 //System.out.println("normal distribution model: N(" + model.getMean().getValue(i) + "," + model.getPrecision().getValue(i) + ")");
121 //System.out.println("current value: " + currentValue + " -- new value: " + newValue);
122
123 logq += (model.logPdf(currentValue) - model.logPdf(newValue));
124
125 if (newValue < bounds.getLowerLimit(i) || newValue > bounds.getUpperLimit(i)) {
126 // throw new OperatorFailedException("proposed value outside boundaries");
127 return Double.NEGATIVE_INFINITY;
128 }
129
130 variable.setValue(i, newValue);
131
132 }
133 }
134
135 return logq;
136 }
137
138 public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() {
139
140 public String getParserName() {
141 return OPERATOR_NAME;
142 }
143
144 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
145
146 double weight = xo.getDoubleAttribute(WEIGHT);
147
148 NormalDistributionModel model = (NormalDistributionModel) xo.getChild(NormalDistributionModel.class);
149 Parameter parameter = (Parameter) xo.getChild(Parameter.class);
150
151 return new IndependentNormalDistributionSampler(parameter, model, weight);
152 }
153
154 //************************************************************************
155 // AbstractXMLObjectParser implementation
156 //************************************************************************
157
158 public XMLSyntaxRule[] getSyntaxRules() {
159 return rules;
160 }
161
162 private final XMLSyntaxRule[] rules = {
163 AttributeRule.newDoubleRule(WEIGHT),
164 new ElementRule(NormalDistributionModel.class),
165 new ElementRule(Parameter.class)
166 };
167
168 public String getParserDescription() {
169 return "This element returns an independence sampler from a provided normal distribution model.";
170 }
171
172 public Class getReturnType() {
173 return MCMCOperator.class;
174 }
175
176 };
177
178 }