| 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.inference.distribution.TruncatedNormalDistributionModel; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.xml.*; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class TruncatedNormalDistributionModelParser extends AbstractXMLObjectParser { |
| 36 |
|
| 37 |
public static final String TRUNCATED_NORMAL_DISTRIBUTION_MODEL = "truncatedNormalDistributionModel"; |
| 38 |
public static final String MEAN = "mean"; |
| 39 |
public static final String STDEV = "stdev"; |
| 40 |
public static final String MINIMUM = "minimum"; |
| 41 |
public static final String MAXIMUM = "maximum"; |
| 42 |
public static final String PREC = "precision"; |
| 43 |
|
| 44 |
public String getParserName() { |
| 45 |
return TRUNCATED_NORMAL_DISTRIBUTION_MODEL; |
| 46 |
} |
| 47 |
|
| 48 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 49 |
|
| 50 |
Parameter meanParam; |
| 51 |
Parameter stdevParam; |
| 52 |
Parameter minParam; |
| 53 |
Parameter maxParam; |
| 54 |
Parameter precParam; |
| 55 |
|
| 56 |
XMLObject cxo = xo.getChild(MEAN); |
| 57 |
if (cxo.getChild(0) instanceof Parameter) { |
| 58 |
meanParam = (Parameter) cxo.getChild(Parameter.class); |
| 59 |
} else { |
| 60 |
meanParam = new Parameter.Default(cxo.getDoubleChild(0)); |
| 61 |
} |
| 62 |
|
| 63 |
if(xo.hasChildNamed(MINIMUM)){ |
| 64 |
cxo = xo.getChild(MINIMUM); |
| 65 |
if(cxo.getChild(0) instanceof Parameter) { |
| 66 |
minParam = (Parameter) cxo.getChild(Parameter.class); |
| 67 |
} else { |
| 68 |
minParam = new Parameter.Default(cxo.getDoubleChild(0)); |
| 69 |
} |
| 70 |
} else { |
| 71 |
minParam = new Parameter.Default(Double.NEGATIVE_INFINITY); |
| 72 |
} |
| 73 |
|
| 74 |
if(xo.hasChildNamed(MAXIMUM)){ |
| 75 |
cxo = xo.getChild(MAXIMUM); |
| 76 |
if(cxo.getChild(0) instanceof Parameter) { |
| 77 |
maxParam = (Parameter) cxo.getChild(Parameter.class); |
| 78 |
} else { |
| 79 |
maxParam = new Parameter.Default(cxo.getDoubleChild(0)); |
| 80 |
} |
| 81 |
} else { |
| 82 |
maxParam = new Parameter.Default(Double.POSITIVE_INFINITY); |
| 83 |
} |
| 84 |
|
| 85 |
if (xo.getChild(STDEV) != null) { |
| 86 |
|
| 87 |
cxo = xo.getChild(STDEV); |
| 88 |
if (cxo.getChild(0) instanceof Parameter) { |
| 89 |
stdevParam = (Parameter) cxo.getChild(Parameter.class); |
| 90 |
} else { |
| 91 |
stdevParam = new Parameter.Default(cxo.getDoubleChild(0)); |
| 92 |
} |
| 93 |
|
| 94 |
return new TruncatedNormalDistributionModel(meanParam, stdevParam, minParam, maxParam); |
| 95 |
} |
| 96 |
|
| 97 |
cxo = xo.getChild(PREC); |
| 98 |
if (cxo.getChild(0) instanceof Parameter) { |
| 99 |
precParam = (Parameter) cxo.getChild(Parameter.class); |
| 100 |
} else { |
| 101 |
precParam = new Parameter.Default(cxo.getDoubleChild(0)); |
| 102 |
} |
| 103 |
return new TruncatedNormalDistributionModel(meanParam, precParam, minParam, maxParam, true); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 111 |
return rules; |
| 112 |
} |
| 113 |
|
| 114 |
private final XMLSyntaxRule[] rules = { |
| 115 |
new ElementRule(MEAN, |
| 116 |
new XMLSyntaxRule[]{ |
| 117 |
new XORRule( |
| 118 |
new ElementRule(Parameter.class), |
| 119 |
new ElementRule(Double.class) |
| 120 |
)} |
| 121 |
), |
| 122 |
new XORRule( |
| 123 |
new ElementRule(STDEV, |
| 124 |
new XMLSyntaxRule[]{ |
| 125 |
new XORRule( |
| 126 |
new ElementRule(Parameter.class), |
| 127 |
new ElementRule(Double.class) |
| 128 |
)} |
| 129 |
), |
| 130 |
new ElementRule(PREC, |
| 131 |
new XMLSyntaxRule[]{ |
| 132 |
new XORRule( |
| 133 |
new ElementRule(Parameter.class), |
| 134 |
new ElementRule(Double.class) |
| 135 |
)} |
| 136 |
) |
| 137 |
), |
| 138 |
new OrRule( |
| 139 |
new ElementRule(MINIMUM, |
| 140 |
new XMLSyntaxRule[]{ |
| 141 |
new XORRule( |
| 142 |
new ElementRule(Parameter.class), |
| 143 |
new ElementRule(Double.class) |
| 144 |
)} |
| 145 |
), |
| 146 |
new ElementRule(MAXIMUM, |
| 147 |
new XMLSyntaxRule[]{ |
| 148 |
new XORRule( |
| 149 |
new ElementRule(Parameter.class), |
| 150 |
new ElementRule(Double.class) |
| 151 |
) |
| 152 |
} |
| 153 |
) |
| 154 |
) |
| 155 |
}; |
| 156 |
|
| 157 |
public String getParserDescription() { |
| 158 |
return "Describes a truncated normal distribution with a given mean, standard deviation and minimum or" + |
| 159 |
"maximum (or both) values that can be used in a distributionLikelihood element"; |
| 160 |
} |
| 161 |
|
| 162 |
public Class getReturnType() { |
| 163 |
return TruncatedNormalDistributionModel.class; |
| 164 |
} |
| 165 |
|
| 166 |
} |