Debian Codegraph @

src/dr/inference/distribution/MixedDistributionLikelihood.java

raw symbols SCIP
1 /*
2 * MixedDistributionLikelihood.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * A class that returns the log likelihood of a multidimensional statistic
37 * being distributed according to a mixture of parametric distributions, where
38 * the distribution membership of each element is determined by a separate vector
39 * of indices.
40 *
41 * @author Alexei Drummond
42 * @version $Id: DistributionLikelihood.java,v 1.11 2005/05/25 09:35:28 rambaut Exp $
43 */
44
45 public class MixedDistributionLikelihood extends Likelihood.Abstract {
46
47 public MixedDistributionLikelihood(ParametricDistributionModel[] distributions, Statistic data, Statistic indicators) {
48 // Mixed Distribution Likelihood contains two distribution models, not necessarily constant.
49 // To cater for that, they need to be returned as the "Model" of this likelyhood so that their state is correctly
50 // restored when an operation involving their parameters fails.
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 // Likelihood IMPLEMENTATION
73 // **************************************************************
74
75 /**
76 * Calculate the log likelihood of the current state.
77 *
78 * @return the log likelihood.
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 //System.err.println("mixed: " + logL);
100 return logL;
101 }
102
103 /**
104 * Overridden to always return false.
105 */
106 protected boolean getLikelihoodKnown() {
107 return false;
108 }
109
110 // **************************************************************
111 // XMLElement IMPLEMENTATION
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 }