Debian Codegraph @

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