| 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.*; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class DirichletProcessLikelihood extends AbstractModelLikelihood { |
| 41 |
|
| 42 |
public static final String DIRICHLET_PROCESS_LIKELIHOOD = "dirichletProcessLikelihood"; |
| 43 |
|
| 44 |
public DirichletProcessLikelihood(Statistic etaParameter, Parameter chiParameter) { |
| 45 |
|
| 46 |
super(DIRICHLET_PROCESS_LIKELIHOOD); |
| 47 |
|
| 48 |
this.etaParameter = etaParameter; |
| 49 |
this.chiParameter = chiParameter; |
| 50 |
addVariable(chiParameter); |
| 51 |
|
| 52 |
K = etaParameter.getDimension(); |
| 53 |
int count = 0; |
| 54 |
for (int i = 0; i < K; i++) { |
| 55 |
count += (int)etaParameter.getStatisticValue(i); |
| 56 |
} |
| 57 |
N = count; |
| 58 |
|
| 59 |
|
| 60 |
logFactorials = new double[N]; |
| 61 |
logFactorials[0] = 0.0; |
| 62 |
for (int j = 1; j < N; j++) { |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
logFactorials[j] = logFactorials[j - 1] + Math.log(j); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public Model getModel() { |
| 77 |
return this; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public double getLogLikelihood() { |
| 86 |
double chi = chiParameter.getParameterValue(0); |
| 87 |
|
| 88 |
double logEtaj = 0; |
| 89 |
int K1 = 0; |
| 90 |
for (int j = 0; j < K; j++) { |
| 91 |
int eta = (int)etaParameter.getStatisticValue(j); |
| 92 |
if (eta > N) { |
| 93 |
throw new RuntimeException("Illegal eta value"); |
| 94 |
} |
| 95 |
if (eta > 0) { |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
double logFactorial = logFactorials[eta - 1]; |
| 101 |
logEtaj += logFactorial; |
| 102 |
|
| 103 |
K1++; |
| 104 |
} |
| 105 |
} |
| 106 |
double logDenominator = 0; |
| 107 |
for (int i = 1; i <= N; i++) { |
| 108 |
logDenominator += Math.log(chi + i - 1); |
| 109 |
} |
| 110 |
|
| 111 |
double logP = K1 * Math.log(chi) + logEtaj - logDenominator; |
| 112 |
|
| 113 |
return logP; |
| 114 |
} |
| 115 |
|
| 116 |
public void makeDirty() { |
| 117 |
} |
| 118 |
|
| 119 |
public void acceptState() { |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
public void restoreState() { |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
public void storeState() { |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
public Statistic getEtaParameter() { |
| 140 |
return etaParameter; |
| 141 |
} |
| 142 |
|
| 143 |
public Parameter getChiParameter() { |
| 144 |
return chiParameter; |
| 145 |
} |
| 146 |
|
| 147 |
public int getN() { |
| 148 |
return N; |
| 149 |
} |
| 150 |
|
| 151 |
private final Statistic etaParameter; |
| 152 |
private final Parameter chiParameter; |
| 153 |
private final int N, K; |
| 154 |
private final double[] logFactorials; |
| 155 |
} |
| 156 |
|