| 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.math.UnivariateFunction; |
| 33 |
import dr.math.distributions.GammaDistribution; |
| 34 |
import org.apache.commons.math.MathException; |
| 35 |
import org.apache.commons.math.distribution.GammaDistributionImpl; |
| 36 |
import org.w3c.dom.Document; |
| 37 |
import org.w3c.dom.Element; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class GammaDistributionModel extends AbstractModel implements ParametricDistributionModel { |
| 48 |
|
| 49 |
public enum GammaParameterizationType { |
| 50 |
ShapeScale, |
| 51 |
ShapeRate, |
| 52 |
ShapeMean, |
| 53 |
OneParameter |
| 54 |
} |
| 55 |
|
| 56 |
public static final String GAMMA_DISTRIBUTION_MODEL = "gammaDistributionModel"; |
| 57 |
public static final String ONE_P_GAMMA_DISTRIBUTION_MODEL = "onePGammaDistributionModel"; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public GammaDistributionModel(Variable<Double> shape, Variable<Double> scale) { |
| 63 |
this(GammaParameterizationType.ShapeScale, shape, scale, 0.0); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public GammaDistributionModel(Variable<Double> shape) { |
| 70 |
this(GammaParameterizationType.OneParameter, shape, null, 0.0); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public GammaDistributionModel(GammaParameterizationType parameterization, Variable<Double> shape, Variable<Double> parameter2, double offset) { |
| 78 |
|
| 79 |
super(GAMMA_DISTRIBUTION_MODEL); |
| 80 |
|
| 81 |
this.offset = offset; |
| 82 |
|
| 83 |
this.parameterization = parameterization; |
| 84 |
this.shape = shape; |
| 85 |
addVariable(shape); |
| 86 |
shape.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 87 |
|
| 88 |
switch (parameterization) { |
| 89 |
case ShapeScale: |
| 90 |
this.scale = parameter2; |
| 91 |
addVariable(scale); |
| 92 |
scale.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 93 |
rate = null; |
| 94 |
mean = null; |
| 95 |
break; |
| 96 |
case ShapeRate: |
| 97 |
this.rate = parameter2; |
| 98 |
addVariable(rate); |
| 99 |
rate.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 100 |
scale = null; |
| 101 |
mean = null; |
| 102 |
break; |
| 103 |
case ShapeMean: |
| 104 |
this.mean = parameter2; |
| 105 |
addVariable(mean); |
| 106 |
mean.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 107 |
scale = null; |
| 108 |
rate = null; |
| 109 |
break; |
| 110 |
case OneParameter: |
| 111 |
scale = null; |
| 112 |
rate = null; |
| 113 |
mean = null; |
| 114 |
break; |
| 115 |
default: |
| 116 |
throw new IllegalArgumentException("Unknown parameterization type"); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public double pdf(double x) { |
| 125 |
if (x < offset) return 0.0; |
| 126 |
return GammaDistribution.pdf(x - offset, getShape(), getScale()); |
| 127 |
} |
| 128 |
|
| 129 |
public double logPdf(double x) { |
| 130 |
if (x < offset) return Double.NEGATIVE_INFINITY; |
| 131 |
return GammaDistribution.logPdf(x - offset, getShape(), getScale()); |
| 132 |
} |
| 133 |
|
| 134 |
public double cdf(double x) { |
| 135 |
if (x < offset) return 0.0; |
| 136 |
return GammaDistribution.cdf(x - offset, getShape(), getScale()); |
| 137 |
} |
| 138 |
|
| 139 |
public double quantile(double y) { |
| 140 |
try { |
| 141 |
return (new GammaDistributionImpl(getShape(), getScale())).inverseCumulativeProbability(y) + offset; |
| 142 |
} catch (MathException e) { |
| 143 |
return Double.NaN; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
public double mean() { |
| 148 |
return GammaDistribution.mean(getShape(), getScale()) + offset; |
| 149 |
} |
| 150 |
|
| 151 |
public double variance() { |
| 152 |
return GammaDistribution.variance(getShape(), getScale()); |
| 153 |
} |
| 154 |
|
| 155 |
public final UnivariateFunction getProbabilityDensityFunction() { |
| 156 |
return pdfFunction; |
| 157 |
} |
| 158 |
|
| 159 |
private final UnivariateFunction pdfFunction = new UnivariateFunction() { |
| 160 |
public final double evaluate(double x) { |
| 161 |
return pdf(x); |
| 162 |
} |
| 163 |
|
| 164 |
public final double getLowerBound() { |
| 165 |
return offset; |
| 166 |
} |
| 167 |
|
| 168 |
public final double getUpperBound() { |
| 169 |
return Double.POSITIVE_INFINITY; |
| 170 |
} |
| 171 |
}; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public void handleModelChangedEvent(Model model, Object object, int index) { |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
protected void storeState() { |
| 186 |
} |
| 187 |
|
| 188 |
protected void restoreState() { |
| 189 |
} |
| 190 |
|
| 191 |
protected void acceptState() { |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public Element createElement(Document document) { |
| 199 |
throw new RuntimeException("Not implemented!"); |
| 200 |
} |
| 201 |
|
| 202 |
public double getShape() { |
| 203 |
return shape.getValue(0); |
| 204 |
} |
| 205 |
|
| 206 |
public double getScale() { |
| 207 |
switch (parameterization) { |
| 208 |
case ShapeScale: |
| 209 |
return scale.getValue(0); |
| 210 |
case ShapeRate: |
| 211 |
return (1.0 / rate.getValue(0)); |
| 212 |
case ShapeMean: |
| 213 |
return (mean.getValue(0) / getShape()); |
| 214 |
case OneParameter: |
| 215 |
return (1.0 / getShape()); |
| 216 |
default: |
| 217 |
throw new IllegalArgumentException("Unknown parameterization type"); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
@Override |
| 226 |
public double logPdf(double[] x) { |
| 227 |
return logPdf(x[0]); |
| 228 |
} |
| 229 |
|
| 230 |
@Override |
| 231 |
public Variable<Double> getLocationVariable() { |
| 232 |
throw new UnsupportedOperationException("Not implemented"); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
private final GammaParameterizationType parameterization; |
| 240 |
|
| 241 |
private final Variable<Double> shape; |
| 242 |
private final Variable<Double> scale; |
| 243 |
private final Variable<Double> rate; |
| 244 |
private final Variable<Double> mean; |
| 245 |
|
| 246 |
private final double offset; |
| 247 |
|
| 248 |
} |
| 249 |
|