Debian Codegraph @

src/dr/inference/distribution/HierarchicalGraphLikelihood.java

raw symbols SCIP
1 /*
2 * HierarchicalGraphLikelihood.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.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 * @author Gabriela Cybis
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 // Likelihood IMPLEMENTATION
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 * Calculate the log likelihood of the current state.
85 *
86 * @return the log likelihood.
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 /** double logL += binomialLogLikelihood(hierarchicalIndicator.getDimension(), diff, logP, log1MinusP);
110 *binomialLogLikelihood(hierarchicalIndicator.getDimension(), diff, logP, log1MinusP);
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 // DO NOTHING
134 }
135
136 public void restoreState() {
137 // DO NOTHING
138 }
139
140 public void storeState() {
141 // DO NOTHING
142 }
143
144 protected void handleModelChangedEvent(Model model, Object object, int index) {
145 // DO NOTHING
146 }
147
148 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
149 // DO NOTHING
150 }
151
152 /**
153 * @return the bernoulli loglikelihood
154 * when the log of the probability is logP.
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 * @return the geometric loglikelihood
163 * when the log of the probability is logP.
164 */
165 private double geometricLogLikelihood( int count, double logP, double log1MinusP) {
166 return (log1MinusP ) + (logP * count);
167 }
168
169
170
171 // **************************************************************
172 // XMLElement IMPLEMENTATION
173 // **************************************************************
174
175 public Element createElement(Document d) {
176 throw new RuntimeException("Not implemented yet!");
177 }
178
179 // Binomial binom = new Binomial();
180 Parameter hierarchicalIndicator;
181 MatrixParameter strataIndicatorMatrix;
182 Parameter prob;
183 }
184
185
186
187
188