| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inferencexml.distribution; |
| 27 |
|
| 28 |
import dr.inferencexml.distribution.MultivariateOUModel; |
| 29 |
import dr.oldevomodel.substmodel.SubstitutionModel; |
| 30 |
import dr.inference.distribution.GeneralizedLinearModel; |
| 31 |
import dr.inference.model.DesignMatrix; |
| 32 |
import dr.inference.model.MatrixParameter; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.inferencexml.distribution.GeneralizedLinearModelParser; |
| 35 |
import dr.xml.*; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class MultivariateOUModelParser extends AbstractXMLObjectParser { |
| 41 |
|
| 42 |
public static final String MVOU_MODEL = "multivariateOUModel"; |
| 43 |
public static final String MVOU_TYPE = "MVOU"; |
| 44 |
public static final String DATA = "data"; |
| 45 |
public static final String TIME = "times"; |
| 46 |
public static final String DESIGN = "design"; |
| 47 |
|
| 48 |
public String getParserName() { |
| 49 |
return MVOU_MODEL; |
| 50 |
} |
| 51 |
|
| 52 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 53 |
|
| 54 |
SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class); |
| 55 |
Parameter effectParameter = (Parameter) ((XMLObject) xo.getChild(DATA)).getChild(Parameter.class); |
| 56 |
Parameter timesParameter = (Parameter) ((XMLObject) xo.getChild(TIME)).getChild(Parameter.class); |
| 57 |
Parameter designParameter = (Parameter) ((XMLObject) xo.getChild(DESIGN)).getChild(Parameter.class); |
| 58 |
MatrixParameter gammaParameter = (MatrixParameter) xo.getChild(MatrixParameter.class); |
| 59 |
|
| 60 |
if (effectParameter.getDimension() != timesParameter.getDimension() || |
| 61 |
effectParameter.getDimension() != designParameter.getDimension()) { |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
throw new XMLParseException("dim(" + effectParameter.getStatisticName() + |
| 66 |
") != dim(" + timesParameter.getStatisticName() + ") != dim(" + designParameter.getStatisticName() + |
| 67 |
") in " + xo.getName() + " element"); |
| 68 |
} |
| 69 |
|
| 70 |
MultivariateOUModel glm = new MultivariateOUModel(substitutionModel, effectParameter, gammaParameter, |
| 71 |
timesParameter.getParameterValues(), designParameter.getParameterValues()); |
| 72 |
|
| 73 |
addIndependentParameters(xo, glm, effectParameter); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
return glm; |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
public void addIndependentParameters(XMLObject xo, GeneralizedLinearModel glm, |
| 83 |
Parameter dependentParam) throws XMLParseException { |
| 84 |
int totalCount = xo.getChildCount(); |
| 85 |
|
| 86 |
for (int i = 0; i < totalCount; i++) { |
| 87 |
if (xo.getChildName(i).compareTo(GeneralizedLinearModelParser.INDEPENDENT_VARIABLES) == 0) { |
| 88 |
XMLObject cxo = (XMLObject) xo.getChild(i); |
| 89 |
Parameter independentParam = (Parameter) cxo.getChild(Parameter.class); |
| 90 |
DesignMatrix designMatrix = (DesignMatrix) cxo.getChild(DesignMatrix.class); |
| 91 |
checkDimensions(independentParam, dependentParam, designMatrix); |
| 92 |
glm.addIndependentParameter(independentParam, designMatrix, null); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
private void checkDimensions(Parameter independentParam, Parameter dependentParam, DesignMatrix designMatrix) |
| 98 |
throws XMLParseException { |
| 99 |
if ((dependentParam.getDimension() != designMatrix.getRowDimension()) || |
| 100 |
(independentParam.getDimension() != designMatrix.getColumnDimension())) { |
| 101 |
System.err.println(dependentParam.getDimension()); |
| 102 |
System.err.println(independentParam.getDimension()); |
| 103 |
System.err.println(designMatrix.getRowDimension() + " rows"); |
| 104 |
System.err.println(designMatrix.getColumnDimension() + " cols"); |
| 105 |
throw new XMLParseException( |
| 106 |
"dim(" + dependentParam.getId() + ") != dim(" + designMatrix.getId() + " %*% " + independentParam.getId() + ")" |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 116 |
return rules; |
| 117 |
} |
| 118 |
|
| 119 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 120 |
new ElementRule(SubstitutionModel.class), |
| 121 |
new ElementRule(MatrixParameter.class), |
| 122 |
new ElementRule(DATA, new XMLSyntaxRule[]{ |
| 123 |
new ElementRule(Parameter.class)}), |
| 124 |
new ElementRule(TIME, new XMLSyntaxRule[]{ |
| 125 |
new ElementRule(Parameter.class)}), |
| 126 |
new ElementRule(DESIGN, new XMLSyntaxRule[]{ |
| 127 |
new ElementRule(Parameter.class)}), |
| 128 |
new ElementRule(GeneralizedLinearModelParser.INDEPENDENT_VARIABLES, |
| 129 |
new XMLSyntaxRule[]{new ElementRule(MatrixParameter.class)}, 0, 3), |
| 130 |
}; |
| 131 |
|
| 132 |
public String getParserDescription() { |
| 133 |
return "Describes a multivariate OU process"; |
| 134 |
} |
| 135 |
|
| 136 |
public Class getReturnType() { |
| 137 |
return MultivariateOUModel.class; |
| 138 |
} |
| 139 |
} |