| 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.inference.distribution; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
import dr.inference.model.AbstractModel; |
| 52 |
import dr.inference.model.Model; |
| 53 |
import dr.inference.model.Parameter; |
| 54 |
import dr.inference.model.Variable; |
| 55 |
import dr.inferencexml.distribution.NormalDistributionModelParser; |
| 56 |
import dr.inferencexml.distribution.TruncatedNormalDistributionModelParser; |
| 57 |
import dr.math.UnivariateFunction; |
| 58 |
import dr.math.distributions.TruncatedNormalDistribution; |
| 59 |
import org.w3c.dom.Document; |
| 60 |
import org.w3c.dom.Element; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public class TruncatedNormalDistributionModel extends AbstractModel implements ParametricDistributionModel { |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public TruncatedNormalDistributionModel(Variable<Double> mean, Variable<Double> stdev, Variable<Double> minimum, |
| 74 |
Variable<Double> maximum) { |
| 75 |
super(TruncatedNormalDistributionModelParser.TRUNCATED_NORMAL_DISTRIBUTION_MODEL); |
| 76 |
this.mean = mean; |
| 77 |
this.stdev = stdev; |
| 78 |
this.minimum = minimum; |
| 79 |
this.maximum = maximum; |
| 80 |
this. |
| 81 |
addVariable(mean); |
| 82 |
mean.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); |
| 83 |
addVariable(stdev); |
| 84 |
stdev.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 85 |
addVariable(minimum); |
| 86 |
minimum.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); |
| 87 |
addVariable(maximum); |
| 88 |
maximum.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); |
| 89 |
recomputeTruncatedNormalDistribution(); |
| 90 |
} |
| 91 |
|
| 92 |
public TruncatedNormalDistributionModel(Parameter meanParameter, Parameter scale, Parameter minParameter, Parameter |
| 93 |
maxParameter, boolean isPrecision) { |
| 94 |
super(NormalDistributionModelParser.NORMAL_DISTRIBUTION_MODEL); |
| 95 |
this.minimum = minParameter; |
| 96 |
this.maximum = maxParameter; |
| 97 |
this.hasPrecision = isPrecision; |
| 98 |
this.mean = meanParameter; |
| 99 |
addVariable(meanParameter); |
| 100 |
meanParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); |
| 101 |
addVariable(maxParameter); |
| 102 |
maxParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); |
| 103 |
addVariable(minParameter); |
| 104 |
minParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); |
| 105 |
if (isPrecision) { |
| 106 |
this.precision = scale; |
| 107 |
this.stdev = null; |
| 108 |
} else { |
| 109 |
this.stdev = scale; |
| 110 |
} |
| 111 |
addVariable(scale); |
| 112 |
scale.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 113 |
recomputeTruncatedNormalDistribution(); |
| 114 |
} |
| 115 |
|
| 116 |
public double getStdev() { |
| 117 |
if (hasPrecision) |
| 118 |
return 1.0 / Math.sqrt(precision.getValue(0)); |
| 119 |
return stdev.getValue(0); |
| 120 |
} |
| 121 |
|
| 122 |
public Variable<Double> getMean() { |
| 123 |
return mean; |
| 124 |
} |
| 125 |
|
| 126 |
public Variable<Double> getMaximum() { |
| 127 |
return maximum; |
| 128 |
} |
| 129 |
|
| 130 |
public Variable<Double> getMinimum() { |
| 131 |
return minimum; |
| 132 |
} |
| 133 |
|
| 134 |
public Variable<Double> getPrecision() { |
| 135 |
if (hasPrecision) |
| 136 |
return precision; |
| 137 |
return null; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public double pdf(double x) { |
| 145 |
return distribution.pdf(x); |
| 146 |
} |
| 147 |
|
| 148 |
public double logPdf(double x) { |
| 149 |
return distribution.logPdf(x); |
| 150 |
} |
| 151 |
|
| 152 |
public double cdf(double x) { |
| 153 |
return distribution.cdf(x); |
| 154 |
} |
| 155 |
|
| 156 |
public double quantile(double y) { |
| 157 |
return distribution.quantile(y); |
| 158 |
} |
| 159 |
|
| 160 |
public double mean() { |
| 161 |
return mean.getValue(0); |
| 162 |
} |
| 163 |
|
| 164 |
public double minimum() { |
| 165 |
return minimum.getValue(0); |
| 166 |
} |
| 167 |
|
| 168 |
public double maximum() { |
| 169 |
return maximum.getValue(0); |
| 170 |
} |
| 171 |
|
| 172 |
public double variance() { |
| 173 |
if (hasPrecision) |
| 174 |
return 1.0 / precision.getValue(0); |
| 175 |
double sd = stdev.getValue(0); |
| 176 |
return sd * sd; |
| 177 |
} |
| 178 |
|
| 179 |
public final UnivariateFunction getProbabilityDensityFunction() { |
| 180 |
return pdfFunction; |
| 181 |
} |
| 182 |
|
| 183 |
private void recomputeTruncatedNormalDistribution(){ |
| 184 |
distribution = new TruncatedNormalDistribution(mean.getValue(0), stdev.getValue(0), minimum.getValue(0), |
| 185 |
maximum.getValue(0)); |
| 186 |
}; |
| 187 |
|
| 188 |
private final UnivariateFunction pdfFunction = new UnivariateFunction() { |
| 189 |
public final double evaluate(double x) { |
| 190 |
return pdf(x); |
| 191 |
} |
| 192 |
|
| 193 |
public final double getLowerBound() { |
| 194 |
return Double.NEGATIVE_INFINITY; |
| 195 |
} |
| 196 |
|
| 197 |
public final double getUpperBound() { |
| 198 |
return Double.POSITIVE_INFINITY; |
| 199 |
} |
| 200 |
}; |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
@Override |
| 207 |
public double logPdf(double[] x) { |
| 208 |
return logPdf(x[0]); |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
public Variable<Double> getLocationVariable() { |
| 213 |
return mean; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public void handleModelChangedEvent(Model model, Object object, int index) { |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 225 |
recomputeTruncatedNormalDistribution(); |
| 226 |
} |
| 227 |
|
| 228 |
protected void storeState() { |
| 229 |
storedDistribution = distribution; |
| 230 |
} |
| 231 |
|
| 232 |
protected void restoreState() { |
| 233 |
distribution = storedDistribution; |
| 234 |
} |
| 235 |
|
| 236 |
protected void acceptState() { |
| 237 |
} |
| 238 |
|
| 239 |
public Element createElement(Document document) { |
| 240 |
throw new RuntimeException("Not implemented!"); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
private final Variable<Double> mean; |
| 249 |
private final Variable<Double> stdev; |
| 250 |
private final Variable<Double> minimum; |
| 251 |
private final Variable<Double> maximum; |
| 252 |
private Variable<Double> precision; |
| 253 |
private boolean hasPrecision = false; |
| 254 |
private TruncatedNormalDistribution distribution = null; |
| 255 |
private TruncatedNormalDistribution storedDistribution = null; |
| 256 |
|
| 257 |
} |