Debian Codegraph @

src/dr/inference/operators/RegressionGibbsEffectOperator.java

raw symbols SCIP
1 /*
2 * RegressionGibbsEffectOperator.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.math.distributions.MultivariateDistribution;
33 import dr.math.distributions.MultivariateNormalDistribution;
34 import dr.math.matrixAlgebra.SymmetricMatrix;
35 import dr.xml.*;
36
37
38 /**
39 * @author Marc Suchard
40 */
41 public class RegressionGibbsEffectOperator extends SimpleMCMCOperator implements GibbsOperator {
42
43 public static final String GIBBS_OPERATOR = "regressionGibbsEffectOperator";
44
45 private LinearRegression linearModel;
46 private Parameter effect;
47 private Parameter indicators;
48 private boolean hasNoIndicators = true;
49 private MultivariateDistribution effectPrior;
50 private int dim;
51 private int effectNumber;
52 private int N;
53 private int numEffects;
54 private double[][] X;
55
56 private double[] mean = null;
57 private double[][] variance = null;
58 private double[][] precision = null;
59
60 public RegressionGibbsEffectOperator(LinearRegression linearModel, Parameter effect, Parameter indicators,
61 MultivariateDistributionLikelihood effectPrior) {
62 super();
63 this.linearModel = linearModel;
64 this.effect = effect;
65 this.indicators = indicators;
66 if (indicators != null) {
67 hasNoIndicators = false;
68 if (indicators.getDimension() != effect.getDimension())
69 throw new RuntimeException("Indicator and effect dimensions must match");
70 }
71 effectNumber = linearModel.getEffectNumber(effect);
72 this.effectPrior = effectPrior.getDistribution();
73 dim = effect.getDimension();
74 N = linearModel.getDependentVariable().getDimension();
75 numEffects = linearModel.getNumberOfFixedEffects();
76 X = linearModel.getX(effectNumber);
77 }
78
79 public int getStepCount() {
80 return 1;
81 }
82
83 public void computeForwardDensity(double[] outMean, double[][] outVariance, double[][] outPrecision) {
84
85 double[] W = linearModel.getTransformedDependentParameter();
86 double[] P = linearModel.getScale(); // outcome precision, fresh copy
87
88 for (int k = 0; k < numEffects; k++) {
89 if (k != effectNumber) {
90 double[] thisXBeta = linearModel.getXBeta(k);
91 for (int i = 0; i < N; i++)
92 W[i] -= thisXBeta[i];
93 }
94 }
95
96 double[] priorBetaMean = effectPrior.getMean();
97 double[][] priorBetaScale = effectPrior.getScaleMatrix();
98
99 double[][] XtP = new double[dim][N];
100 for (int j = 0; j < dim; j++) {
101 if (hasNoIndicators || indicators.getParameterValue(j) == 1) {
102 for (int i = 0; i < N; i++)
103 XtP[j][i] = X[i][j] * P[i];
104 } // else already filled with zeros
105 }
106
107 double[][] XtPX = new double[dim][dim];
108 for (int i = 0; i < dim; i++) {
109 if (hasNoIndicators || indicators.getParameterValue(i) == 1) {
110 for (int j = i; j < dim; j++) {// symmetric
111 if (hasNoIndicators || indicators.getParameterValue(j) == 1) {
112 for (int k = 0; k < N; k++)
113 XtPX[i][j] += XtP[i][k] * X[k][j];
114 XtPX[j][i] = XtPX[i][j]; // symmetric
115 }
116 }
117 }
118 }
119
120 double[][] XtPX_plus_P0 = new double[dim][dim];
121 for (int i = 0; i < dim; i++) {
122 for (int j = i; j < dim; j++) // symmetric
123 XtPX_plus_P0[j][i] = XtPX_plus_P0[i][j] = XtPX[i][j] + priorBetaScale[i][j];
124 }
125
126 double[] XtPW = new double[dim];
127 for (int i = 0; i < dim; i++) {
128 for (int j = 0; j < N; j++)
129 XtPW[i] += XtP[i][j] * W[j];
130 }
131
132 double[] P0Mean0 = new double[dim];
133 for (int i = 0; i < dim; i++) {
134 for (int j = 0; j < dim; j++)
135 P0Mean0[i] += priorBetaScale[i][j] * priorBetaMean[j];
136 }
137
138 double[] unscaledMean = new double[dim];
139 for (int i = 0; i < dim; i++)
140 unscaledMean[i] = P0Mean0[i] + XtPW[i];
141
142 double[][] variance = new SymmetricMatrix(XtPX_plus_P0).inverse().toComponents();
143
144 for (int i = 0; i < dim; i++) {
145 outMean[i] = 0.0;
146 for (int j = 0; j < dim; j++) {
147 outMean[i] += variance[i][j] * unscaledMean[j];
148 outVariance[i][j] = variance[i][j];
149 outPrecision[i][j] = XtPX_plus_P0[i][j];
150 }
151 }
152 }
153
154 public double[] getLastMean() { return mean; }
155
156 public double[][] getLastVariance() { return variance; }
157
158 public double[][] getLastPrecision() { return precision; }
159
160 public double doOperation() {
161
162 if (mean == null)
163 mean = new double[dim];
164
165 if (variance == null)
166 variance = new double[dim][dim];
167
168 if (precision == null)
169 precision = new double[dim][dim];
170
171
172 computeForwardDensity(mean,variance,precision);
173
174 double[] draw = MultivariateNormalDistribution.nextMultivariateNormalVariance(
175 mean, variance);
176
177 for (int i = 0; i < dim; i++)
178 effect.setParameterValue(i, draw[i]);
179
180 return 0;
181 }
182
183 public String getPerformanceSuggestion() {
184 return null;
185 }
186
187 public String getOperatorName() {
188 return GIBBS_OPERATOR;
189 }
190
191 public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() {
192
193 public String getParserName() {
194 return GIBBS_OPERATOR;
195 }
196
197 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
198
199 double weight = xo.getDoubleAttribute(WEIGHT);
200
201 LinearRegression linearModel = (LinearRegression) xo.getChild(LinearRegression.class);
202 Parameter effect = (Parameter) xo.getChild(Parameter.class);
203 MultivariateDistributionLikelihood prior = (MultivariateDistributionLikelihood) xo.getChild(MultivariateDistributionLikelihood.class);
204
205 if (prior.getDistribution().getType().compareTo(MultivariateNormalDistribution.TYPE) != 0)
206 throw new XMLParseException("Only a multivariate normal prior is conjugate");
207
208 XMLObject cxo = xo.getChild(GeneralizedLinearModelParser.INDICATOR);
209 Parameter indicators = null;
210 if (cxo != null) {
211 indicators = (Parameter) cxo.getChild(Parameter.class);
212 }
213
214 RegressionGibbsEffectOperator operator = new RegressionGibbsEffectOperator(linearModel, effect, indicators, prior);
215 operator.setWeight(weight);
216 return operator;
217 }
218
219 //************************************************************************
220 // AbstractXMLObjectParser implementation
221 //************************************************************************
222
223 public String getParserDescription() {
224 return "This element returns a multivariate Gibbs operator on an internal node trait.";
225 }
226
227 public Class getReturnType() {
228 return MCMCOperator.class;
229 }
230
231 public XMLSyntaxRule[] getSyntaxRules() {
232 return rules;
233 }
234
235 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
236 AttributeRule.newDoubleRule(WEIGHT),
237 new ElementRule(Parameter.class),
238 new ElementRule(MultivariateDistributionLikelihood.class),
239 new ElementRule(LinearRegression.class),
240 new ElementRule(GeneralizedLinearModelParser.INDICATOR,
241 new XMLSyntaxRule[] {
242 new ElementRule(Parameter.class)
243 },true)
244 };
245
246 };
247
248 }