| 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.CompoundModel; |
| 29 |
import dr.inference.model.Likelihood; |
| 30 |
import dr.inference.model.Model; |
| 31 |
import dr.inference.model.Statistic; |
| 32 |
import org.w3c.dom.Document; |
| 33 |
import org.w3c.dom.Element; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class MixedDistributionLikelihood extends Likelihood.Abstract { |
| 46 |
|
| 47 |
public MixedDistributionLikelihood(ParametricDistributionModel[] distributions, Statistic data, Statistic indicators) { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
super(new CompoundModel("MixedDistributions")); |
| 53 |
|
| 54 |
final CompoundModel cm = (CompoundModel) this.getModel(); |
| 55 |
for (ParametricDistributionModel m : distributions) { |
| 56 |
cm.addModel(m); |
| 57 |
} |
| 58 |
|
| 59 |
this.distributions = distributions; |
| 60 |
this.data = data; |
| 61 |
this.indicators = indicators; |
| 62 |
|
| 63 |
if (indicators.getDimension() == data.getDimension() - 1) { |
| 64 |
impliedOne = true; |
| 65 |
} else if (indicators.getDimension() != data.getDimension()) { |
| 66 |
throw new IllegalArgumentException("Indicators length (" + indicators.getDimension() + |
| 67 |
") != data length (" + data.getDimension() + ")"); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public final double calculateLogLikelihood() { |
| 81 |
|
| 82 |
double logL = 0.0; |
| 83 |
|
| 84 |
for (int j = 0; j < data.getDimension(); j++) { |
| 85 |
|
| 86 |
int index; |
| 87 |
if (impliedOne) { |
| 88 |
if (j == 0) { |
| 89 |
index = 1; |
| 90 |
} else { |
| 91 |
index = (int) indicators.getStatisticValue(j - 1); |
| 92 |
} |
| 93 |
} else { |
| 94 |
index = (int) indicators.getStatisticValue(j); |
| 95 |
} |
| 96 |
|
| 97 |
logL += distributions[index].logPdf(data.getStatisticValue(j)); |
| 98 |
} |
| 99 |
|
| 100 |
return logL; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
protected boolean getLikelihoodKnown() { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public Element createElement(Document d) { |
| 115 |
throw new RuntimeException("Not implemented yet!"); |
| 116 |
} |
| 117 |
|
| 118 |
private final ParametricDistributionModel[] distributions; |
| 119 |
private final Statistic data; |
| 120 |
private final Statistic indicators; |
| 121 |
private boolean impliedOne = false; |
| 122 |
|
| 123 |
public Model[] getUniqueModels() { |
| 124 |
Model[] m = new Model[distributions[0] == distributions[1] ? 1 : 2]; |
| 125 |
m[0] = distributions[0]; |
| 126 |
if (m.length > 1) m[1] = distributions[1]; |
| 127 |
return m; |
| 128 |
} |
| 129 |
} |