| 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.AbstractModelLikelihood; |
| 29 |
import dr.inference.model.Model; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inference.model.Variable; |
| 32 |
import dr.math.Binomial; |
| 33 |
import org.w3c.dom.Document; |
| 34 |
import org.w3c.dom.Element; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class BinomialLikelihood extends AbstractModelLikelihood { |
| 45 |
|
| 46 |
public static final String BINOMIAL_LIKELIHOOD = "binomialLikelihood"; |
| 47 |
|
| 48 |
public BinomialLikelihood(Parameter trialsParameter, Parameter proportionParameter, Parameter countsParameter, |
| 49 |
boolean onLogitScale) { |
| 50 |
|
| 51 |
super(BINOMIAL_LIKELIHOOD); |
| 52 |
|
| 53 |
this.trialsParameter = trialsParameter; |
| 54 |
this.proportionParameter = proportionParameter; |
| 55 |
this.countsParameter = countsParameter; |
| 56 |
addVariable(trialsParameter); |
| 57 |
addVariable(proportionParameter); |
| 58 |
addVariable(countsParameter); |
| 59 |
|
| 60 |
this.onLogitScale = onLogitScale; |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public Model getModel() { |
| 70 |
return this; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public double getLogLikelihood() { |
| 79 |
|
| 80 |
|
| 81 |
SufficientStatistics ss = new SufficientStatistics(proportionParameter.getParameterValue(0), onLogitScale); |
| 82 |
if (ss.outOfBounds) return Double.NEGATIVE_INFINITY; |
| 83 |
|
| 84 |
final boolean hasMultipleProportions = proportionParameter.getDimension() > 1; |
| 85 |
|
| 86 |
double logL = 0.0; |
| 87 |
for (int i = 0; i < trialsParameter.getDimension(); i++) { |
| 88 |
|
| 89 |
if (hasMultipleProportions && i > 0) { |
| 90 |
ss = new SufficientStatistics(proportionParameter.getParameterValue(i), onLogitScale); |
| 91 |
if (ss.outOfBounds) return Double.NEGATIVE_INFINITY; |
| 92 |
} |
| 93 |
|
| 94 |
int trials = (int) Math.round(trialsParameter.getParameterValue(i)); |
| 95 |
int counts = (int) Math.round(countsParameter.getParameterValue(i)); |
| 96 |
|
| 97 |
if (counts > trials) return Double.NEGATIVE_INFINITY; |
| 98 |
logL += binomialLogLikelihood(trials, counts, ss.logP, ss.log1MinusP); |
| 99 |
} |
| 100 |
return logL; |
| 101 |
} |
| 102 |
|
| 103 |
class SufficientStatistics { |
| 104 |
|
| 105 |
double logP; |
| 106 |
double log1MinusP; |
| 107 |
boolean outOfBounds; |
| 108 |
|
| 109 |
SufficientStatistics(double theta, boolean onLogitScale) { |
| 110 |
if (onLogitScale) { |
| 111 |
|
| 112 |
final double logDenominator = Math.log(1.0 + Math.exp(theta)); |
| 113 |
logP = theta - logDenominator; |
| 114 |
log1MinusP = -logDenominator; |
| 115 |
outOfBounds = false; |
| 116 |
} else { |
| 117 |
logP = Math.log(theta); |
| 118 |
log1MinusP = Math.log(1.0 - theta); |
| 119 |
outOfBounds = (theta <= 0 || theta >= 1); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
public void makeDirty() { |
| 125 |
} |
| 126 |
|
| 127 |
public void acceptState() { |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
public void restoreState() { |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
public void storeState() { |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
private double binomialLogLikelihood(int trials, int count, double logP, double log1MinusP) { |
| 152 |
return Binomial.logChoose(trials, count) + (logP * count) + (log1MinusP * (trials - count)); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public Element createElement(Document d) { |
| 160 |
throw new RuntimeException("Not implemented yet!"); |
| 161 |
} |
| 162 |
|
| 163 |
Parameter trialsParameter; |
| 164 |
Parameter proportionParameter; |
| 165 |
Parameter countsParameter; |
| 166 |
|
| 167 |
private final boolean onLogitScale; |
| 168 |
} |
| 169 |
|