Debian Codegraph @

src/dr/inference/operators/NormalGammaPrecisionGibbsOperator.java

raw symbols SCIP
1 /*
2 * NormalGammaPrecisionGibbsOperator.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.distribution.DistributionLikelihood;
29 import dr.inference.distribution.GammaDistributionModel;
30 import dr.inference.distribution.LogNormalDistributionModel;
31 import dr.inference.distribution.NormalDistributionModel;
32 import dr.inference.model.Parameter;
33 import dr.math.MathUtils;
34 import dr.math.distributions.Distribution;
35 import dr.math.distributions.GammaDistribution;
36 import dr.util.Attribute;
37 import dr.xml.*;
38
39 import java.util.List;
40
41 /**
42 * @author Marc A. Suchard
43 * @author Philippe Lemey
44 */
45 public class NormalGammaPrecisionGibbsOperator extends SimpleMCMCOperator implements GibbsOperator {
46
47 public static final String OPERATOR_NAME = "normalGammaPrecisionGibbsOperator";
48 public static final String LIKELIHOOD = "likelihood";
49 public static final String PRIOR = "prior";
50
51 public NormalGammaPrecisionGibbsOperator(DistributionLikelihood inLikelihood, Distribution prior,
52 double weight) {
53
54 if (!(prior instanceof GammaDistribution || prior instanceof GammaDistributionModel))
55 throw new RuntimeException("Precision prior must be Gamma");
56
57 Distribution likelihood = inLikelihood.getDistribution();
58 this.dataList = inLikelihood.getDataList();
59
60 if (likelihood instanceof NormalDistributionModel) {
61 this.precisionParameter = (Parameter) ((NormalDistributionModel) likelihood).getPrecision();
62 this.meanParameter = (Parameter) ((NormalDistributionModel) likelihood).getMean();
63 } else if (likelihood instanceof LogNormalDistributionModel) {
64 if (((LogNormalDistributionModel) likelihood).getParameterization() == LogNormalDistributionModel.Parameterization.MU_PRECISION) {
65 this.meanParameter = ((LogNormalDistributionModel) likelihood).getMuParameter();
66 } else {
67 throw new RuntimeException("Must characterize likelihood in terms of mu and precision parameters");
68 }
69 this.precisionParameter = ((LogNormalDistributionModel) likelihood).getPrecisionParameter();
70 isLog = true;
71 } else
72 throw new RuntimeException("Likelihood must be Normal or log Normal");
73
74 if (precisionParameter == null)
75 throw new RuntimeException("Must characterize likelihood in terms of a precision parameter");
76
77 this.prior = prior;
78 setWeight(weight);
79 }
80
81 /**
82 * @return a short descriptive message of the performance of this operator.
83 */
84 public String getPerformanceSuggestion() {
85 return null;
86 }
87
88 public String getOperatorName() {
89 return OPERATOR_NAME;
90 }
91
92 /**
93 * Called by operate(), does the actual operation.
94 *
95 * @return the hastings ratio
96 */
97 public double doOperation() {
98
99 final double priorMean = prior.mean();
100 final double priorVariance = prior.variance();
101
102 double priorRate;
103 double priorShape;
104
105 if (priorMean == 0) {
106 priorRate = 0;
107 priorShape = -0.5; // Uninformative prior
108 } else {
109 priorRate = priorMean / priorVariance;
110 priorShape = priorMean * priorRate;
111 }
112
113 // Calculate weighted sum-of-squares
114 final double mu = meanParameter.getParameterValue(0);
115 double SSE = 0;
116 int n = 0;
117 for (Attribute<double[]> statistic : dataList) {
118 for (double x : statistic.getAttributeValue()) {
119 if (isLog) {
120 final double logX = Math.log(x);
121 SSE += (logX - mu) * (logX - mu);
122 } else {
123 SSE += (x - mu) * (x - mu);
124 }
125 n++;
126 }
127 }
128
129 final double shape = priorShape + n / 2.0;
130 final double rate = priorRate + 0.5 * SSE;
131
132 final double draw = MathUtils.nextGamma(shape, rate); // Gamma( \alpha + n/2 , \beta + (1/2)*SSE )
133 precisionParameter.setParameterValue(0, draw);
134
135 return 0;
136 }
137
138 /**
139 * @return the number of steps the operator performs in one go.
140 */
141 public int getStepCount() {
142 return 1;
143 }
144
145 public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() {
146
147 public String getParserName() {
148 return OPERATOR_NAME;
149 }
150
151 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
152
153 double weight = xo.getDoubleAttribute(WEIGHT);
154
155 DistributionLikelihood likelihood = (DistributionLikelihood) ((XMLObject) xo.getChild(LIKELIHOOD)).getChild(DistributionLikelihood.class);
156 DistributionLikelihood prior = (DistributionLikelihood) ((XMLObject) xo.getChild(PRIOR)).getChild(DistributionLikelihood.class);
157
158 // System.err.println("class: " + prior.getDistribution().getClass());
159
160 if (!((prior.getDistribution() instanceof GammaDistribution) ||
161 (prior.getDistribution() instanceof GammaDistributionModel)
162 ) ||
163 !((likelihood.getDistribution() instanceof NormalDistributionModel) ||
164 (likelihood.getDistribution() instanceof LogNormalDistributionModel)
165 ))
166 throw new XMLParseException("Gibbs operator assumes normal-gamma model");
167
168 return new NormalGammaPrecisionGibbsOperator(likelihood, prior.getDistribution(), weight);
169 }
170
171 //************************************************************************
172 // AbstractXMLObjectParser implementation
173 //************************************************************************
174
175 public String getParserDescription() {
176 return "This element returns a operator on the precision parameter of a normal model with gamma prior.";
177 }
178
179 public Class getReturnType() {
180 return MCMCOperator.class;
181 }
182
183 public XMLSyntaxRule[] getSyntaxRules() {
184 return rules;
185 }
186
187 private final XMLSyntaxRule[] rules = {
188 AttributeRule.newDoubleRule(WEIGHT),
189 new ElementRule(LIKELIHOOD,
190 new XMLSyntaxRule[]{
191 new ElementRule(DistributionLikelihood.class)
192 }),
193 new ElementRule(PRIOR,
194 new XMLSyntaxRule[]{
195 new ElementRule(DistributionLikelihood.class)
196 }),
197 };
198
199 };
200
201 private final Distribution prior;
202 private boolean isLog = false;
203
204 private final List<Attribute<double[]>> dataList;
205 private final Parameter meanParameter;
206 private final Parameter precisionParameter;
207 }