| 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.MatrixParameter; |
| 32 |
import dr.inference.model.Variable; |
| 33 |
import dr.math.Binomial; |
| 34 |
import org.w3c.dom.Document; |
| 35 |
import org.w3c.dom.Element; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class HierarchicalGraphLikelihood extends AbstractModelLikelihood { |
| 44 |
|
| 45 |
public static final String HIERARCHICAL_GRAPH_LIKELIHOOD = "hierarchicalGraphLikelihood"; |
| 46 |
|
| 47 |
public HierarchicalGraphLikelihood(Parameter hierarchicalIndicator, MatrixParameter strataIndicatorMatrix, Parameter prob) { |
| 48 |
|
| 49 |
super(HIERARCHICAL_GRAPH_LIKELIHOOD); |
| 50 |
|
| 51 |
this.hierarchicalIndicator = hierarchicalIndicator; |
| 52 |
this.strataIndicatorMatrix = strataIndicatorMatrix; |
| 53 |
this.prob = prob; |
| 54 |
addVariable(hierarchicalIndicator); |
| 55 |
addVariable(strataIndicatorMatrix); |
| 56 |
addVariable(prob); |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public Parameter getHierarchicalIndicator() { |
| 67 |
return this.hierarchicalIndicator; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
public MatrixParameter getStrataMatrix() { |
| 72 |
return this.strataIndicatorMatrix; |
| 73 |
} |
| 74 |
|
| 75 |
public Parameter getProb() { |
| 76 |
return this.prob; |
| 77 |
} |
| 78 |
|
| 79 |
public Model getModel() { |
| 80 |
return this; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public double getLogLikelihood() { |
| 89 |
|
| 90 |
double p = prob.getParameterValue(0); |
| 91 |
if (p <= 0 || p >= 1) return Double.NEGATIVE_INFINITY; |
| 92 |
|
| 93 |
double logP = Math.log(p); |
| 94 |
double log1MinusP = Math.log(1.0 - p); |
| 95 |
|
| 96 |
if ( hierarchicalIndicator.getDimension()!= strataIndicatorMatrix.getRowDimension()) return Double.NEGATIVE_INFINITY; |
| 97 |
|
| 98 |
double logL = 0.0; |
| 99 |
|
| 100 |
|
| 101 |
for (int j =0; j < strataIndicatorMatrix.getColumnDimension();j++){ |
| 102 |
int diff = 0; |
| 103 |
for (int i = 0; i < hierarchicalIndicator.getDimension(); i++) { |
| 104 |
diff += (int) Math.abs(Math.round(hierarchicalIndicator.getParameterValue(i)-strataIndicatorMatrix.getParameterValue(i,j))); |
| 105 |
|
| 106 |
} logL += geometricLogLikelihood( diff, logP, log1MinusP); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
return logL; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public void makeDirty() { |
| 130 |
} |
| 131 |
|
| 132 |
public void acceptState() { |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
public void restoreState() { |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
public void storeState() { |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
private double binomialLogLikelihood(int trials, int count, double logP, double log1MinusP) { |
| 157 |
return Math.log(Binomial.choose(trials, count)) + (logP * count) + (log1MinusP * (trials - count)); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
private double geometricLogLikelihood( int count, double logP, double log1MinusP) { |
| 166 |
return (log1MinusP ) + (logP * count); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public Element createElement(Document d) { |
| 176 |
throw new RuntimeException("Not implemented yet!"); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
Parameter hierarchicalIndicator; |
| 181 |
MatrixParameter strataIndicatorMatrix; |
| 182 |
Parameter prob; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|