Debian Codegraph @

src/dr/inference/operators/IndependentGammaSampler.java

raw symbols SCIP
1 /*
2 * IndependentGammaSampler.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.inference.model.Variable;
31 import dr.math.distributions.GammaDistribution;
32 import dr.xml.AttributeRule;
33 import dr.xml.ElementRule;
34 import dr.xml.XMLObject;
35 import dr.xml.XMLParseException;
36 import dr.xml.XMLSyntaxRule;
37
38 /**
39 * An independent gamma sampler to propose new (independent) values from a provided gamma prior (distribution).
40 *
41 * @author Guy Baele
42 *
43 */
44 public class IndependentGammaSampler extends SimpleMCMCOperator {
45
46 public static final String OPERATOR_NAME = "independentGammaSampler";
47 public static final String SHAPE = "shape";
48 public static final String SCALE = "scale";
49
50 private Variable<Double> variable = null;
51 private GammaDistribution gamma = null;
52 private boolean updateAllIndependently = true;
53
54 public IndependentGammaSampler(Variable variable, GammaDistribution gamma) {
55
56 this(variable, gamma, 1.0);
57
58 }
59
60 public IndependentGammaSampler(Variable variable, GammaDistribution gamma, double weight) {
61
62 this(variable, gamma, weight, true);
63
64 }
65
66 public IndependentGammaSampler(Variable variable, GammaDistribution gamma, double weight, boolean updateAllIndependently) {
67
68 this.variable = variable;
69 this.gamma = gamma;
70 this.updateAllIndependently = updateAllIndependently;
71 setWeight(weight);
72
73 }
74
75 public String getPerformanceSuggestion() {
76 return "";
77 }
78
79 public String getOperatorName() {
80 return "independentGamma(" + variable.getVariableName() + ")";
81 }
82
83 /**
84 * change the parameter and return the hastings ratio.
85 */
86 public double doOperation() {
87
88 double logq = 0;
89
90 double currentValue;
91 double newValue;
92
93 final Bounds<Double> bounds = variable.getBounds();
94 final int dim = variable.getSize();
95
96 if (updateAllIndependently) {
97 for (int i = 0; i < dim; i++) {
98
99 //both current and new value of the variable needed for the hastings ratio
100 currentValue = variable.getValue(i);
101
102 newValue = gamma.nextGamma();
103 while (newValue == 0.0) {
104 newValue = gamma.nextGamma();
105 }
106
107 //System.err.println("newValue: " + newValue + " - logPdf: " + gamma.logPdf(newValue));
108
109 logq += (gamma.logPdf(currentValue) - gamma.logPdf(newValue));
110
111 if (newValue < bounds.getLowerLimit(i) || newValue > bounds.getUpperLimit(i)) {
112 throw new RuntimeException("proposed value outside boundaries");
113 }
114
115 variable.setValue(i, newValue);
116
117 }
118 }
119
120 return logq;
121 }
122
123 public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() {
124
125 public String getParserName() {
126 return OPERATOR_NAME;
127 }
128
129 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
130
131 double weight = xo.getDoubleAttribute(WEIGHT);
132 double shape = xo.getDoubleAttribute(SHAPE);
133 double scale = xo.getDoubleAttribute(SCALE);
134
135 if (! (shape > 0 && scale > 0)) {
136 throw new XMLParseException("Shape and scale must be positive values.");
137 }
138
139 Parameter parameter = (Parameter) xo.getChild(Parameter.class);
140
141 return new IndependentGammaSampler(parameter, new GammaDistribution(shape, scale), weight);
142 }
143
144 //************************************************************************
145 // AbstractXMLObjectParser implementation
146 //************************************************************************
147
148 public XMLSyntaxRule[] getSyntaxRules() {
149 return rules;
150 }
151
152 private final XMLSyntaxRule[] rules = {
153 AttributeRule.newDoubleRule(WEIGHT),
154 AttributeRule.newDoubleRule(SHAPE),
155 AttributeRule.newDoubleRule(SCALE),
156 new ElementRule(Parameter.class)
157 };
158
159 public String getParserDescription() {
160 return "This element returns an independence sampler from a provided gamma prior.";
161 }
162
163 public Class getReturnType() {
164 return MCMCOperator.class;
165 }
166
167 };
168
169 }