Debian Codegraph @

src/dr/inference/operators/GibbsIndependentJointNormalGammaOperator.java

raw symbols SCIP
1 /*
2 * GibbsIndependentJointNormalGammaOperator.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.math.distributions.GammaDistribution;
37 import dr.xml.*;
38
39 /**
40 * An independent joint normal gamma sampler to propose new (independent) values from provided normal and gamma distribution models.
41 *
42 * @author Guy Baele
43 *
44 */
45 public class GibbsIndependentJointNormalGammaOperator extends SimpleMCMCOperator implements GibbsOperator {
46
47 public static final String OPERATOR_NAME = "GibbsIndependentJointNormalGammaOperator";
48 public static final String MEAN = "mean";
49 public static final String PREC = "precision";
50 public static final String SHAPE = "shape";
51 public static final String SCALE = "scale";
52
53 private Variable<Double> mean = null;
54 private Variable<Double> precision = null;
55 private NormalDistributionModel model = null;
56 private GammaDistribution gamma = null;
57 private boolean updateAllIndependently = true;
58
59 private static final boolean TRY_COLT = true;
60 private static final boolean DEBUG = false;
61 private static RandomEngine randomEngine;
62 private static Normal coltNormal;
63 //private static Gamma coltGamma;
64
65 public GibbsIndependentJointNormalGammaOperator(Variable mean, Variable precision, NormalDistributionModel model, GammaDistribution gamma) {
66
67 this(mean, precision, model, gamma, 1.0);
68
69 }
70
71 public GibbsIndependentJointNormalGammaOperator(Variable mean, Variable precision, NormalDistributionModel model, GammaDistribution gamma, double weight) {
72
73 this(mean, precision, model, gamma, weight, true);
74
75 }
76
77 public GibbsIndependentJointNormalGammaOperator(Variable mean, Variable precision, NormalDistributionModel model, GammaDistribution gamma, double weight, boolean updateAllIndependently) {
78
79 this.mean = mean;
80 this.precision = precision;
81 this.model = model;
82 this.gamma = gamma;
83 this.updateAllIndependently = updateAllIndependently;
84 setWeight(weight);
85
86 if (TRY_COLT) {
87 randomEngine = new MersenneTwister(MathUtils.nextInt());
88 //create standard normal distribution, internal states will be bypassed anyway
89 //takes mean and standard deviation
90 coltNormal = new Normal(0.0, 1.0, randomEngine);
91 //coltGamma = new Gamma(gamma.getShape(), 1.0/gamma.getScale(), randomEngine);
92 } else {
93 //no random draw with specified mean and stdev implemented in the normal distribution in BEAST (as far as I know)
94 throw new RuntimeException("Normal distribution in BEAST still needs a random sampler.");
95 }
96
97 }
98
99 public String getPerformanceSuggestion() {
100 return "";
101 }
102
103 public String getOperatorName() {
104 return "GibbsIndependentJointNormalGamma(" + mean.getVariableName() + "," + precision.getVariableName() + ")";
105 }
106
107 public int getStepCount() {
108 return 1;
109 }
110
111 /**
112 * change the parameter and return the hastings ratio.
113 */
114 public double doOperation() {
115
116 //double logq = 0;
117
118 //double currentValue;
119 double newValue;
120
121 final Bounds<Double> meanBounds = mean.getBounds();
122 final Bounds<Double> precBounds = precision.getBounds();
123 final int dim = mean.getSize();
124
125 if (updateAllIndependently) {
126 for (int i = 0; i < dim; i++) {
127
128 if (DEBUG) {
129 System.out.println("old precision value: " + precision.getValue(i));
130 System.out.println("old mean value: " + mean.getValue(i));
131 System.out.println("model mean check: " + model.getMean().getValue(i));
132 System.out.println("model precision check: " + model.getPrecision().getValue(i) + "\n");
133 }
134 //start with updating the precision
135 newValue = gamma.nextGamma();
136 //newValue = coltGamma.nextDouble();
137 while (newValue == 0.0) {
138 newValue = gamma.nextGamma();
139 //newValue = coltGamma.nextDouble();
140 }
141
142 if (newValue < precBounds.getLowerLimit(i) || newValue > precBounds.getUpperLimit(i)) {
143 throw new RuntimeException("proposed value from gamma distribution outside boundaries");
144 }
145
146 precision.setValue(i, newValue);
147
148 if (DEBUG) {
149 System.out.println("new precision value: " + precision.getValue(i));
150 System.out.println("old mean value: " + mean.getValue(i));
151 System.out.println("model mean check: " + model.getMean().getValue(i));
152 System.out.println("model precision check: " + model.getPrecision().getValue(i) + "\n");
153 }
154
155 //use the current mean and precision (standard deviation)
156 newValue = coltNormal.nextDouble(model.getMean().getValue(i), 1.0 / Math.sqrt(model.getPrecision().getValue(i)));
157
158 //newValue = (double)model.nextRandom();
159
160 //System.out.print("normal distribution model: N(" + model.getMean().getValue(i) + "," + model.getPrecision().getValue(i) + ") ");
161 //System.out.println(1.0 / Math.sqrt(model.getPrecision().getValue(i)));
162 //System.out.println("current value: " + currentValue + " -- new value: " + newValue);
163
164 //logq += (model.logPdf(currentValue) - model.logPdf(newValue));
165
166 if (newValue < meanBounds.getLowerLimit(i) || newValue > meanBounds.getUpperLimit(i)) {
167 throw new RuntimeException("proposed value from normal distribution outside boundaries");
168 }
169
170 mean.setValue(i, newValue);
171
172 if (DEBUG) {
173 System.out.println("new precision value: " + precision.getValue(i));
174 System.out.println("new mean value: " + mean.getValue(i));
175 System.out.println("model mean check: " + model.getMean().getValue(i));
176 System.out.println("model precision check: " + model.getPrecision().getValue(i) + "\n");
177 }
178
179 }
180 }
181
182 //return logq;
183 return 0;
184
185 }
186
187 public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() {
188
189 public String getParserName() {
190 return OPERATOR_NAME;
191 }
192
193 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
194
195 double weight = xo.getDoubleAttribute(WEIGHT);
196 double shape = xo.getDoubleAttribute(SHAPE);
197 double scale = xo.getDoubleAttribute(SCALE);
198
199 NormalDistributionModel model = (NormalDistributionModel) xo.getChild(NormalDistributionModel.class);
200
201 XMLObject cxo = xo.getChild(MEAN);
202 Parameter mean = (Parameter) cxo.getChild(Parameter.class);
203
204 cxo = xo.getChild(PREC);
205 Parameter precision = (Parameter) cxo.getChild(Parameter.class);
206
207 return new GibbsIndependentJointNormalGammaOperator(mean, precision, model, new GammaDistribution(shape, scale), weight);
208 }
209
210 //************************************************************************
211 // AbstractXMLObjectParser implementation
212 //************************************************************************
213
214 public XMLSyntaxRule[] getSyntaxRules() {
215 return rules;
216 }
217
218 private final XMLSyntaxRule[] rules = {
219 AttributeRule.newDoubleRule(WEIGHT),
220 AttributeRule.newDoubleRule(SHAPE),
221 AttributeRule.newDoubleRule(SCALE),
222 new ElementRule(NormalDistributionModel.class),
223 new ElementRule(MEAN, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
224 new ElementRule(PREC, new XMLSyntaxRule[]{new ElementRule(Parameter.class)})
225 };
226
227 public String getParserDescription() {
228 return "This element returns an independence sampler, disguised as a Gibbs operator, from provided normal and gamma distributions.";
229 }
230
231 public Class getReturnType() {
232 return MCMCOperator.class;
233 }
234
235 };
236
237 }