Debian Codegraph @

src/dr/inference/distribution/DirichletProcessLikelihood.java

raw symbols SCIP
1 /*
2 * DirichletProcessLikelihood.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.*;
29
30 /**
31 * A class that returns the log likelihood of a set of discrete entities
32 * being distributed in k classes according to a Dirichlet process.
33 *
34 * @author Andrew Rambaut
35 * @author Trevor Bedford
36 * @author Marc Suchard
37 * @version $Id: DirichletProcessLikelihood.java,v 1.7 2005/05/24 20:25:59 rambaut Exp $
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 // create a look up table for all log factorials up to N
60 logFactorials = new double[N];
61 logFactorials[0] = 0.0;
62 for (int j = 1; j < N; j++) {
63 // logFactorials[j] = 0; // the log factorial for 0
64 // for (int k = 1; k <= j; k++) {
65 // logFactorials[j] += Math.log(k);
66 // }
67 logFactorials[j] = logFactorials[j - 1] + Math.log(j);
68 }
69 }
70
71 // **************************************************************
72 // Likelihood IMPLEMENTATION
73 // **************************************************************
74
75
76 public Model getModel() {
77 return this;
78 }
79
80 /**
81 * Calculate the log likelihood of the current state.
82 *
83 * @return the log likelihood.
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 // double logFactorial = 0;
97 // for (int k = 1; k <= (eta - 1); k++) {
98 // logFactorial += Math.log(k);
99 // }
100 double logFactorial = logFactorials[eta - 1];
101 logEtaj += logFactorial;
102 // count the number of actually occupied classes
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 // DO NOTHING
121 }
122
123 public void restoreState() {
124 // DO NOTHING
125 }
126
127 public void storeState() {
128 // DO NOTHING
129 }
130
131 protected void handleModelChangedEvent(Model model, Object object, int index) {
132 // DO NOTHING
133 }
134
135 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
136 // DO NOTHING
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