Debian Codegraph @

src/dr/inference/operators/RegressionMetropolizedIndicatorOperator.java

raw symbols SCIP
1 /*
2 * RegressionMetropolizedIndicatorOperator.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.LinearRegression;
29 import dr.inference.distribution.MultivariateDistributionLikelihood;
30 import dr.inference.model.Parameter;
31 import dr.inferencexml.distribution.GeneralizedLinearModelParser;
32 import dr.inferencexml.model.MaskedParameterParser;
33 import dr.math.MathUtils;
34 import dr.math.distributions.MultivariateNormalDistribution;
35 import dr.xml.*;
36
37 /**
38 * @author Marc Suchard
39 */
40 public class RegressionMetropolizedIndicatorOperator extends SimpleMCMCOperator {
41
42 public static final String MH_OPERATOR = "regressionMetropolizedIndicatorOperator";
43
44 public RegressionMetropolizedIndicatorOperator(LinearRegression linearModel, Parameter effect,
45 Parameter indicators, MultivariateDistributionLikelihood effectPrior,
46 Parameter mask) {
47
48 effectOperator = new RegressionGibbsEffectOperator(linearModel, effect, indicators, effectPrior);
49 this.effect = effect;
50 this.indicators = indicators;
51 this.mask = mask;
52 }
53
54 /**
55 * @return a short descriptive message of the performance of this operator.
56 */
57 public String getPerformanceSuggestion() {
58 return null;
59 }
60
61 public String getOperatorName() {
62 return MH_OPERATOR;
63 }
64
65 public double doOperation() {
66
67 double logHastingsRatio = 0.0;
68
69 if (mask != null) {
70 int sum = 0;
71 for(int i=0; i<mask.getDimension(); i++)
72 sum += mask.getParameterValue(i);
73 if (sum == 0)
74 throw new RuntimeException("Mask parameter has all zeros");
75 }
76
77 if (mean == null) {
78 final int dim = effect.getDimension();
79 mean = new double[dim];
80 variance = new double[dim][dim];
81 precision = new double[dim][dim];
82 }
83
84 // Compute back transition probability
85 effectOperator.computeForwardDensity(mean, variance,precision);
86 logHastingsRatio += MultivariateNormalDistribution.logPdf(effect.getParameterValues(),
87 mean, precision,
88 Math.log(MultivariateNormalDistribution.calculatePrecisionMatrixDeterminate(precision)),
89 1.0);
90
91 // Do update
92 int index;
93 do {
94 index = MathUtils.nextInt(indicators.getDimension());
95 } while( (mask != null) && (mask.getParameterValue(index) == 0));
96
97 indicators.setParameterValue(index, 1 - indicators.getParameterValue(index));
98
99 effectOperator.doOperation(); // Gibbs sample new coefficients given updated indicator
100
101 // Get forward transition probability
102 mean = effectOperator.getLastMean();
103 variance = effectOperator.getLastVariance();
104 precision = effectOperator.getLastPrecision();
105 logHastingsRatio -= MultivariateNormalDistribution.logPdf(effect.getParameterValues(),
106 mean, precision,
107 Math.log(MultivariateNormalDistribution.calculatePrecisionMatrixDeterminate(precision)),
108 1.0);
109
110 return logHastingsRatio;
111 }
112
113 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
114
115 public String getParserName() {
116 return MH_OPERATOR;
117 }
118
119 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
120
121 double weight = xo.getDoubleAttribute(WEIGHT);
122
123 LinearRegression linearModel = (LinearRegression) xo.getChild(LinearRegression.class);
124 Parameter effect = (Parameter) xo.getChild(Parameter.class);
125 MultivariateDistributionLikelihood prior = (MultivariateDistributionLikelihood)
126 xo.getChild(MultivariateDistributionLikelihood.class);
127
128 if (prior.getDistribution().getType().compareTo(MultivariateNormalDistribution.TYPE) != 0)
129 throw new XMLParseException("Only a multivariate normal prior is conjugate");
130
131 XMLObject cxo = (XMLObject) xo.getChild(GeneralizedLinearModelParser.INDICATOR);
132 Parameter indicators = (Parameter) cxo.getChild(Parameter.class);
133
134 cxo = (XMLObject) xo.getChild(MaskedParameterParser.MASKING);
135 Parameter mask = null;
136 if (cxo != null) {
137 mask = (Parameter) cxo.getChild(Parameter.class);
138 if (mask.getDimension() != indicators.getDimension())
139 throw new XMLParseException("Indicator and mask parameter must have the same dimension");
140 }
141
142 RegressionMetropolizedIndicatorOperator operator = new RegressionMetropolizedIndicatorOperator(linearModel, effect,
143 indicators, prior, mask);
144 operator.setWeight(weight);
145 return operator;
146 }
147
148 //************************************************************************
149 // AbstractXMLObjectParser implementation
150 //************************************************************************
151
152 public String getParserDescription() {
153 return "This element returns a multivariate Gibbs operator on an internal node trait.";
154 }
155
156 public Class getReturnType() {
157 return MCMCOperator.class;
158 }
159
160 public XMLSyntaxRule[] getSyntaxRules() {
161 return rules;
162 }
163
164 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
165 AttributeRule.newDoubleRule(WEIGHT),
166 new ElementRule(Parameter.class),
167 new ElementRule(MultivariateDistributionLikelihood.class),
168 new ElementRule(LinearRegression.class),
169 new ElementRule(GeneralizedLinearModelParser.INDICATOR,
170 new XMLSyntaxRule[]{
171 new ElementRule(Parameter.class)
172 }),
173 new ElementRule(MaskedParameterParser.MASKING,
174 new XMLSyntaxRule[]{
175 new ElementRule(Parameter.class)
176 }, true)
177 };
178
179 };
180
181 private Parameter mask;
182 private Parameter indicators;
183 private Parameter effect;
184 private RegressionGibbsEffectOperator effectOperator;
185 private double[] mean = null;
186 private double[][] variance = null;
187 private double[][] precision = null;
188
189 }