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