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