| 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.BetaDistribution; |
| 34 |
import org.w3c.dom.Document; |
| 35 |
import org.w3c.dom.Element; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class BetaDistributionModel extends AbstractModel implements ParametricDistributionModel { |
| 44 |
|
| 45 |
public static final String BETA_DISTRIBUTION_MODEL = "betaDistributionModel"; |
| 46 |
|
| 47 |
public BetaDistributionModel(Variable<Double> alpha, Variable<Double> beta) { |
| 48 |
this(alpha, beta, 0.0, 1.0); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public BetaDistributionModel(Variable<Double> alpha, Variable<Double> beta, double offset, double length) { |
| 56 |
|
| 57 |
super(BETA_DISTRIBUTION_MODEL); |
| 58 |
|
| 59 |
this.alpha = alpha; |
| 60 |
this.beta = beta; |
| 61 |
this.length = length; |
| 62 |
this.offset = offset; |
| 63 |
|
| 64 |
addVariable(alpha); |
| 65 |
alpha.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 66 |
|
| 67 |
addVariable(beta); |
| 68 |
beta.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 69 |
|
| 70 |
recomputeBetaDistribution(); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public double pdf(double x) { |
| 78 |
double xScaled = getXScaled(x); |
| 79 |
if (xScaled < 0.0 || xScaled > 1.0) return 0.0; |
| 80 |
|
| 81 |
return betaDistribution.pdf(xScaled); |
| 82 |
} |
| 83 |
|
| 84 |
public double logPdf(double x) { |
| 85 |
double xScaled = getXScaled(x); |
| 86 |
if (xScaled < 0.0 || xScaled > 1.0) return Double.NEGATIVE_INFINITY; |
| 87 |
|
| 88 |
return betaDistribution.logPdf(xScaled); |
| 89 |
} |
| 90 |
|
| 91 |
public double cdf(double x) { |
| 92 |
if (x < offset) return 0.0; |
| 93 |
return betaDistribution.cdf(getXScaled(x)); |
| 94 |
} |
| 95 |
|
| 96 |
public double quantile(double y) { |
| 97 |
return betaDistribution.quantile(getXScaled(y)) * length + offset; |
| 98 |
} |
| 99 |
|
| 100 |
public double mean() { |
| 101 |
return betaDistribution.mean() * length + offset; |
| 102 |
} |
| 103 |
|
| 104 |
public double variance() { |
| 105 |
return betaDistribution.variance() * length * length; |
| 106 |
} |
| 107 |
|
| 108 |
public final UnivariateFunction getProbabilityDensityFunction() { |
| 109 |
return pdfFunction; |
| 110 |
} |
| 111 |
|
| 112 |
private final UnivariateFunction pdfFunction = new UnivariateFunction() { |
| 113 |
public final double evaluate(double x) { |
| 114 |
double xScale = (x - offset) / length; |
| 115 |
return pdf(xScale); |
| 116 |
} |
| 117 |
|
| 118 |
public final double getLowerBound() { |
| 119 |
return offset; |
| 120 |
} |
| 121 |
|
| 122 |
public final double getUpperBound() { |
| 123 |
return offset + length; |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
@Override |
| 132 |
public double logPdf(double[] x) { |
| 133 |
return logPdf(x[0]); |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public Variable<Double> getLocationVariable() { |
| 138 |
throw new UnsupportedOperationException("Not implemented"); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public void handleModelChangedEvent(Model model, Object object, int index) { |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
public void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 150 |
recomputeBetaDistribution(); |
| 151 |
} |
| 152 |
|
| 153 |
protected void storeState() { |
| 154 |
storedBetaDistribution = betaDistribution; |
| 155 |
} |
| 156 |
|
| 157 |
protected void restoreState() { |
| 158 |
betaDistribution = storedBetaDistribution; |
| 159 |
} |
| 160 |
|
| 161 |
protected void acceptState() { |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public Element createElement(Document document) { |
| 169 |
throw new RuntimeException("Not implemented!"); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
private void recomputeBetaDistribution() { |
| 177 |
betaDistribution = new BetaDistribution(alpha.getValue(0), beta.getValue(0)); |
| 178 |
} |
| 179 |
|
| 180 |
private double getXScaled(double x) { |
| 181 |
return (x - offset) / length; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
private Variable<Double> alpha = null; |
| 189 |
private Variable<Double> beta = null; |
| 190 |
private double offset = 0.0; |
| 191 |
private double length = 0.0; |
| 192 |
|
| 193 |
private BetaDistribution betaDistribution = null; |
| 194 |
private BetaDistribution storedBetaDistribution = null; |
| 195 |
|
| 196 |
} |
| 197 |
|