Debian Codegraph @

src/dr/inference/operators/GibbsIndependentNormalDistributionOperator.java

raw symbols SCIP
1 /*
2 * GibbsIndependentNormalDistributionOperator.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 GibbsIndependentNormalDistributionOperator extends SimpleMCMCOperator implements GibbsOperator {
49
50 public static final String OPERATOR_NAME = "GibbsIndependentNormalDistributionOperator";
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 GibbsIndependentNormalDistributionOperator(Variable variable, NormalDistributionModel model) {
61
62 this(variable, model, 1.0);
63
64 }
65
66 public GibbsIndependentNormalDistributionOperator(Variable variable, NormalDistributionModel model, double weight) {
67
68 this(variable, model, weight, true);
69
70 }
71
72 public GibbsIndependentNormalDistributionOperator(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 //takes mean and standard deviation
83 coltNormal = new Normal(0.0, 1.0, randomEngine);
84 } else {
85 //no random draw with specified mean and stdev implemented in the normal distribution in BEAST (as far as I know)
86 throw new RuntimeException("Normal distribution in BEAST still needs a random sampler.");
87 }
88
89 }
90
91 public String getPerformanceSuggestion() {
92 return "";
93 }
94
95 public String getOperatorName() {
96 return "GibbsIndependentNormalDistribution(" + variable.getVariableName() + ")";
97 }
98
99 public int getStepCount() {
100 return 1;
101 }
102
103 /**
104 * change the parameter and return the hastings ratio.
105 */
106 public double doOperation() {
107
108 //double logq = 0;
109
110 //double currentValue;
111 double newValue;
112
113 final Bounds<Double> bounds = variable.getBounds();
114 final int dim = variable.getSize();
115
116 if (updateAllIndependently) {
117 for (int i = 0; i < dim; i++) {
118
119 //both current and new value of the variable needed for the hastings ratio
120 //currentValue = variable.getValue(i);
121
122 //use the current mean and precision (standard deviation)
123 newValue = coltNormal.nextDouble(model.getMean().getValue(i), 1.0 / Math.sqrt(model.getPrecision().getValue(i)));
124
125 //newValue = (double)model.nextRandom();
126
127 //System.out.print("normal distribution model: N(" + model.getMean().getValue(i) + "," + model.getPrecision().getValue(i) + ") ");
128 //System.out.println(1.0 / Math.sqrt(model.getPrecision().getValue(i)));
129 //System.out.println("current value: " + currentValue + " -- new value: " + newValue);
130
131 //logq += (model.logPdf(currentValue) - model.logPdf(newValue));
132
133 if (newValue < bounds.getLowerLimit(i) || newValue > bounds.getUpperLimit(i)) {
134 // throw new OperatorFailedException("proposed value outside boundaries");
135 return Double.NEGATIVE_INFINITY;
136 }
137
138 variable.setValue(i, newValue);
139
140 }
141 }
142
143 //return logq;
144 return 0;
145
146 }
147
148 public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() {
149
150 public String getParserName() {
151 return OPERATOR_NAME;
152 }
153
154 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
155
156 double weight = xo.getDoubleAttribute(WEIGHT);
157
158 NormalDistributionModel model = (NormalDistributionModel) xo.getChild(NormalDistributionModel.class);
159 Parameter parameter = (Parameter) xo.getChild(Parameter.class);
160
161 return new GibbsIndependentNormalDistributionOperator(parameter, model, weight);
162 }
163
164 //************************************************************************
165 // AbstractXMLObjectParser implementation
166 //************************************************************************
167
168 public XMLSyntaxRule[] getSyntaxRules() {
169 return rules;
170 }
171
172 private final XMLSyntaxRule[] rules = {
173 AttributeRule.newDoubleRule(WEIGHT),
174 new ElementRule(NormalDistributionModel.class),
175 new ElementRule(Parameter.class)
176 };
177
178 public String getParserDescription() {
179 return "This element returns an independence sampler, disguised as a Gibbs operator, from a provided normal distribution model.";
180 }
181
182 public Class getReturnType() {
183 return MCMCOperator.class;
184 }
185
186 };
187
188 }