Debian Codegraph @

src/dr/inference/operators/RegressionGibbsPrecisionOperator.java

raw symbols SCIP
1 /*
2 * RegressionGibbsPrecisionOperator.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.*;
29 import dr.inference.model.Parameter;
30 import dr.math.distributions.GammaDistribution;
31 import dr.math.distributions.Distribution;
32 import dr.math.MathUtils;
33 import dr.xml.*;
34
35
36 /**
37 * @author Marc Suchard
38 */
39 public class RegressionGibbsPrecisionOperator extends SimpleMCMCOperator implements GibbsOperator {
40
41 public static final String GIBBS_OPERATOR = "regressionGibbsPrecisionOperator";
42
43 private LinearRegression linearModel;
44 private Parameter precision;
45 private int dim;
46 private int N;
47 private int[] scaleDesign;
48 private Distribution prior;
49
50 public RegressionGibbsPrecisionOperator(LinearRegression linearModel, Parameter precision, Distribution prior) {
51
52 super();
53 if (!(prior instanceof GammaDistribution || prior instanceof GammaDistributionModel))
54 throw new RuntimeException("Precision prior must be Gamma");
55 this.prior = prior;
56 this.linearModel = linearModel;
57 this.precision = precision;
58 this.dim = precision.getDimension();
59 scaleDesign = linearModel.getScaleDesign();
60 N = linearModel.getDependentVariable().getDimension();
61 }
62
63 public int getStepCount() {
64 return 1;
65 }
66
67 public double doOperation() {
68
69 double[] Y = linearModel.getTransformedDependentParameter();
70 double[] xBeta = linearModel.getXBeta();
71
72 final double priorMean = prior.mean();
73 final double priorVariance = prior.variance();
74
75 double priorRate;
76 double priorShape;
77
78 if (priorMean == 0) {
79 priorRate = 0;
80 priorShape = -0.5; // Uninformative prior
81 } else {
82 priorRate = priorMean / priorVariance;
83 priorShape = priorMean * priorRate;
84 }
85
86 for (int k = 0; k < dim; k++) { // Do draw for precision[k]
87
88 // Calculate weighted sum-of-squares
89 double SSE = 0;
90 int n = 0;
91
92 for(int i=0; i<N; i++) {
93 if(scaleDesign[i] == k) {
94 SSE += (Y[i] - xBeta[i])*(Y[i] - xBeta[i]);
95 n++;
96 }
97 }
98
99 final double shape = priorShape + n / 2.0;
100 final double rate = priorRate + 0.5 * SSE;
101
102 final double draw = MathUtils.nextGamma(shape, rate); // Gamma( \alpha + n/2 , \beta + (1/2)*SSE )
103 precision.setParameterValue(k, draw);
104 }
105
106 return 0;
107 }
108
109 public String getPerformanceSuggestion() {
110 return null;
111 }
112
113 public String getOperatorName() {
114 return GIBBS_OPERATOR;
115 }
116
117 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
118
119 public String getParserName() {
120 return GIBBS_OPERATOR;
121 }
122
123 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
124
125 double weight = xo.getDoubleAttribute(WEIGHT);
126
127 LinearRegression linearModel = (LinearRegression) xo.getChild(LinearRegression.class);
128 Parameter precision = (Parameter) xo.getChild(Parameter.class);
129 DistributionLikelihood prior = (DistributionLikelihood) xo.getChild(DistributionLikelihood.class);
130
131 if (!((prior.getDistribution() instanceof GammaDistribution) ||
132 (prior.getDistribution() instanceof GammaDistributionModel)
133 ))
134 throw new XMLParseException("Gibbs operator assumes normal-gamma model");
135
136 RegressionGibbsPrecisionOperator operator = new RegressionGibbsPrecisionOperator(linearModel,precision,prior.getDistribution());
137 operator.setWeight(weight);
138 return operator;
139 }
140
141 //************************************************************************
142 // AbstractXMLObjectParser implementation
143 //************************************************************************
144
145 public String getParserDescription() {
146 return "This element returns a multivariate Gibbs operator on an internal node trait.";
147 }
148
149 public Class getReturnType() {
150 return MCMCOperator.class;
151 }
152
153 public XMLSyntaxRule[] getSyntaxRules() {
154 return rules;
155 }
156
157 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
158 AttributeRule.newDoubleRule(WEIGHT),
159 new ElementRule(Parameter.class),
160 new ElementRule(DistributionLikelihood.class),
161 new ElementRule(LinearRegression.class),
162 };
163
164 };
165
166 }