Debian Codegraph @

src/dr/inferencexml/distribution/GeneralizedLinearModelParser.java

raw symbols SCIP
1 /*
2 * GeneralizedLinearModelParser.java
3 *
4 * Copyright (c) 2002-2016 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.inferencexml.distribution;
27
28 import cern.colt.matrix.impl.DenseDoubleMatrix2D;
29 import cern.colt.matrix.linalg.SingularValueDecomposition;
30 import dr.inference.distribution.GeneralizedLinearModel;
31 import dr.inference.distribution.LinearRegression;
32 import dr.inference.distribution.LogLinearModel;
33 import dr.inference.distribution.LogisticRegression;
34 import dr.inference.model.DesignMatrix;
35 import dr.inference.model.Likelihood;
36 import dr.inference.model.Parameter;
37 import dr.xml.*;
38
39 /**
40 *
41 */
42 public class GeneralizedLinearModelParser extends AbstractXMLObjectParser {
43
44 public static final String GLM_LIKELIHOOD = "glmModel";
45
46 public static final String DEPENDENT_VARIABLES = "dependentVariables";
47 public static final String INDEPENDENT_VARIABLES = "independentVariables";
48 public static final String BASIS_MATRIX = "basis";
49 public static final String FAMILY = "family";
50 public static final String SCALE_VARIABLES = "scaleVariables";
51 public static final String INDICATOR = "indicator";
52 public static final String LOGISTIC_REGRESSION = "logistic";
53 public static final String NORMAL_REGRESSION = "normal";
54 public static final String LOG_NORMAL_REGRESSION = "logNormal";
55 public static final String LOG_LINEAR = "logLinear";
56 // public static final String LOG_TRANSFORM = "logDependentTransform";
57 public static final String RANDOM_EFFECTS = "randomEffects";
58 public static final String CHECK_IDENTIFIABILITY = "checkIdentifiability";
59 public static final String CHECK_FULL_RANK = "checkFullRank";
60
61 public String getParserName() {
62 return GLM_LIKELIHOOD;
63 }
64
65 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
66
67
68 // System.err.println("PASSED 0");
69 XMLObject cxo = xo.getChild(DEPENDENT_VARIABLES);
70 Parameter dependentParam = null;
71 if (cxo != null) {
72 dependentParam = (Parameter) cxo.getChild(Parameter.class);
73 }
74
75 String family = xo.getStringAttribute(FAMILY);
76 GeneralizedLinearModel glm;
77 if (family.compareTo(LOGISTIC_REGRESSION) == 0) {
78 glm = new LogisticRegression(dependentParam);
79 } else if (family.compareTo(NORMAL_REGRESSION) == 0) {
80 glm = new LinearRegression(dependentParam, false);
81 } else if (family.compareTo(LOG_NORMAL_REGRESSION) == 0) {
82 glm = new LinearRegression(dependentParam, true);
83 } else if (family.compareTo(LOG_LINEAR) == 0) {
84 glm = new LogLinearModel(dependentParam);
85 } else
86 throw new XMLParseException("Family '" + family + "' is not currently implemented");
87
88 if (glm.requiresScale()) {
89 cxo = xo.getChild(SCALE_VARIABLES);
90 Parameter scaleParameter = null;
91 // DesignMatrix designMatrix = null;
92 Parameter scaleDesign = null;
93 if (cxo != null) {
94 scaleParameter = (Parameter) cxo.getChild(Parameter.class);
95 XMLObject gxo = cxo.getChild(INDICATOR);
96 if (gxo != null)
97 scaleDesign = (Parameter) gxo.getChild(Parameter.class);
98 // designMatrix = (DesignMatrix) cxo.getChild(DesignMatrix.class);
99 }
100 if (scaleParameter == null)
101 throw new XMLParseException("Family '" + family + "' requires scale parameters");
102 if (scaleDesign == null)
103 scaleDesign = new Parameter.Default(dependentParam.getDimension(), 0.0);
104 else {
105 if (scaleDesign.getDimension() != dependentParam.getDimension())
106 throw new XMLParseException("Scale ("+dependentParam.getDimension()+") and scaleDesign parameters ("+scaleDesign.getDimension()+") must be the same dimension");
107 for (int i = 0; i < scaleDesign.getDimension(); i++) {
108 double value = scaleDesign.getParameterValue(i);
109 if (value < 1 || value > scaleParameter.getDimension())
110 throw new XMLParseException("Invalid scaleDesign value");
111 scaleDesign.setParameterValue(i, value - 1);
112 }
113 }
114
115 glm.addScaleParameter(scaleParameter, scaleDesign);
116 }
117 // System.err.println("START 0");
118 addIndependentParameters(xo, glm, dependentParam);
119 // System.err.println("START 1");
120 addRandomEffects(xo, glm, dependentParam);
121 // System.err.println("START 2");
122
123 boolean checkIdentifiability = xo.getAttribute(CHECK_IDENTIFIABILITY, true);
124 if (checkIdentifiability) {
125 if (!glm.getAllIndependentVariablesIdentifiable()) {
126 throw new XMLParseException("All design matrix predictors are not identifiable in "+ xo.getId());
127 }
128 }
129 // System.err.println("PASSED B");
130 checkFullRankOfMatrix = xo.getAttribute(CHECK_FULL_RANK,true);
131 // System.err.println("PASSED C");
132 return glm;
133 }
134
135 public void addRandomEffects(XMLObject xo, GeneralizedLinearModel glm,
136 Parameter dependentParam) throws XMLParseException {
137 int totalCount = xo.getChildCount();
138
139 for (int i = 0; i < totalCount; i++) {
140 if (xo.getChildName(i).compareTo(RANDOM_EFFECTS) == 0) {
141 XMLObject cxo = (XMLObject) xo.getChild(i);
142 Parameter randomEffect = (Parameter) cxo.getChild(Parameter.class);
143 checkRandomEffectsDimensions(randomEffect, dependentParam);
144 glm.addRandomEffectsParameter(randomEffect);
145 }
146 }
147 }
148
149 public void addIndependentParameters(XMLObject xo, GeneralizedLinearModel glm,
150 Parameter dependentParam) throws XMLParseException {
151 int totalCount = xo.getChildCount();
152 // System.err.println("number of independent parameters = "+totalCount);
153
154 for (int i = 0; i < totalCount; i++) {
155 if (xo.getChildName(i).compareTo(INDEPENDENT_VARIABLES) == 0) {
156 XMLObject cxo = (XMLObject) xo.getChild(i);
157 Parameter independentParam = (Parameter) cxo.getChild(Parameter.class);
158 DesignMatrix designMatrix = (DesignMatrix) cxo.getChild(DesignMatrix.class);
159 checkDimensions(independentParam, dependentParam, designMatrix);
160 cxo = cxo.getChild(INDICATOR);
161 Parameter indicator = null;
162 if (cxo != null) {
163 indicator = (Parameter) cxo.getChild(Parameter.class);
164 if (indicator.getDimension() <= 1) {
165 // if a dimension hasn't been set, then set it automatically
166 indicator.setDimension(independentParam.getDimension());
167 }
168 if (indicator.getDimension() != independentParam.getDimension())
169 throw new XMLParseException("dim(" + independentParam.getId() + ") != dim(" + indicator.getId() + ")");
170 }
171 // System.err.println("A");
172 if (checkFullRankOfMatrix) {
173 checkFullRank(designMatrix);
174 }
175 // System.err.println("B");
176
177 // System.err.println(new Matrix(designMatrix.getParameterAsMatrix()));
178 // System.exit(-1);
179
180 glm.addIndependentParameter(independentParam, designMatrix, indicator);
181 // System.err.println("C");
182 }
183 }
184 }
185
186 private boolean checkFullRankOfMatrix;
187
188 private void checkFullRank(DesignMatrix designMatrix) throws XMLParseException {
189 int fullRank = designMatrix.getColumnDimension();
190 // System.err.println("designMatrix getColumnDimension = "+fullRank);
191 SingularValueDecomposition svd = new SingularValueDecomposition(
192 new DenseDoubleMatrix2D(designMatrix.getParameterAsMatrix()));
193 int realRank = svd.rank();
194 if (realRank != fullRank) {
195 throw new XMLParseException(
196 "rank(" + designMatrix.getId() + ") = " + realRank +
197 ".\nMatrix is not of full rank as colDim(" + designMatrix.getId() + ") = " + fullRank
198 );
199 }
200 }
201
202 private void checkRandomEffectsDimensions(Parameter randomEffect, Parameter dependentParam)
203 throws XMLParseException {
204 if (dependentParam != null) {
205 if (randomEffect.getDimension() <= 1) {
206 // if a dimension hasn't been set, then set it automatically
207 randomEffect.setDimension(dependentParam.getDimension());
208 }
209 if (randomEffect.getDimension() != dependentParam.getDimension()) {
210 throw new XMLParseException(
211 "dim(" + dependentParam.getId() + ") != dim(" + randomEffect.getId() + ")"
212 );
213 }
214 }
215 }
216
217 private void checkDimensions(Parameter independentParam, Parameter dependentParam, DesignMatrix designMatrix)
218 throws XMLParseException {
219 if (dependentParam != null) {
220 if (dependentParam.getDimension() <= 1) {
221 // if a dimension hasn't been set, then set it automatically
222 dependentParam.setDimension(designMatrix.getRowDimension());
223 }
224 if ((dependentParam.getDimension() != designMatrix.getRowDimension()) ||
225 (independentParam.getDimension() != designMatrix.getColumnDimension()))
226 throw new XMLParseException(
227 "dim(" + dependentParam.getId() + ") != dim(" + designMatrix.getId() + " %*% " + independentParam.getId() + ")"
228 );
229 } else {
230 if (independentParam.getDimension() <= 1) {
231 // if a dimension hasn't been set, then set it automatically
232 independentParam.setDimension(designMatrix.getColumnDimension());
233 }
234 if (independentParam.getDimension() != designMatrix.getColumnDimension()) {
235 throw new XMLParseException(
236 "dim(" + independentParam.getId() + ") is incompatible with dim (" + designMatrix.getId() + ")"
237 );
238 }
239 // System.err.println(independentParam.getId()+" and "+designMatrix.getId());
240 }
241 }
242
243 //************************************************************************
244 // AbstractXMLObjectParser implementation
245 //************************************************************************
246
247 public XMLSyntaxRule[] getSyntaxRules() {
248 return rules;
249 }
250
251 private final XMLSyntaxRule[] rules = {
252 AttributeRule.newStringRule(FAMILY),
253 AttributeRule.newBooleanRule(CHECK_IDENTIFIABILITY, true),
254 AttributeRule.newBooleanRule(CHECK_FULL_RANK, true),
255 new ElementRule(DEPENDENT_VARIABLES,
256 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, true),
257 new ElementRule(INDEPENDENT_VARIABLES,
258 new XMLSyntaxRule[]{
259 new ElementRule(Parameter.class, true),
260 new ElementRule(DesignMatrix.class),
261 new ElementRule(INDICATOR,
262 new XMLSyntaxRule[]{
263 new ElementRule(Parameter.class)
264 }, true),
265 }, 1, 3),
266 new ElementRule(RANDOM_EFFECTS,
267 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, 0, 3),
268 // new ElementRule(BASIS_MATRIX,
269 // new XMLSyntaxRule[]{new ElementRule(DesignMatrix.class)})
270 };
271
272 public String getParserDescription() {
273 return "Calculates the generalized linear model likelihood of the dependent parameters given one or more blocks of independent parameters and their design matrix.";
274 }
275
276 public Class getReturnType() {
277 return Likelihood.class;
278 }
279 }