Debian Codegraph @

src/dr/inference/distribution/BinomialLikelihood.java

raw symbols SCIP
1 /*
2 * BinomialLikelihood.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.Variable;
32 import dr.math.Binomial;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35
36 /**
37 * A class that returns the log likelihood of a set of data (statistics)
38 * being distributed according to a binomial distribution.
39 *
40 * @author Alexei Drummond
41 * @version $Id: BinomialLikelihood.java,v 1.5 2005/05/24 20:25:59 rambaut Exp $
42 */
43
44 public class BinomialLikelihood extends AbstractModelLikelihood {
45
46 public static final String BINOMIAL_LIKELIHOOD = "binomialLikelihood";
47
48 public BinomialLikelihood(Parameter trialsParameter, Parameter proportionParameter, Parameter countsParameter,
49 boolean onLogitScale) {
50
51 super(BINOMIAL_LIKELIHOOD);
52
53 this.trialsParameter = trialsParameter;
54 this.proportionParameter = proportionParameter;
55 this.countsParameter = countsParameter;
56 addVariable(trialsParameter);
57 addVariable(proportionParameter);
58 addVariable(countsParameter);
59
60 this.onLogitScale = onLogitScale;
61
62 }
63
64 // **************************************************************
65 // Likelihood IMPLEMENTATION
66 // **************************************************************
67
68
69 public Model getModel() {
70 return this;
71 }
72
73 /**
74 * Calculate the log likelihood of the current state.
75 *
76 * @return the log likelihood.
77 */
78 public double getLogLikelihood() {
79
80 // Get first success probability statistics
81 SufficientStatistics ss = new SufficientStatistics(proportionParameter.getParameterValue(0), onLogitScale);
82 if (ss.outOfBounds) return Double.NEGATIVE_INFINITY;
83
84 final boolean hasMultipleProportions = proportionParameter.getDimension() > 1;
85
86 double logL = 0.0;
87 for (int i = 0; i < trialsParameter.getDimension(); i++) {
88
89 if (hasMultipleProportions && i > 0) {
90 ss = new SufficientStatistics(proportionParameter.getParameterValue(i), onLogitScale);
91 if (ss.outOfBounds) return Double.NEGATIVE_INFINITY;
92 }
93
94 int trials = (int) Math.round(trialsParameter.getParameterValue(i));
95 int counts = (int) Math.round(countsParameter.getParameterValue(i));
96
97 if (counts > trials) return Double.NEGATIVE_INFINITY;
98 logL += binomialLogLikelihood(trials, counts, ss.logP, ss.log1MinusP);
99 }
100 return logL;
101 }
102
103 class SufficientStatistics {
104
105 double logP;
106 double log1MinusP;
107 boolean outOfBounds;
108
109 SufficientStatistics(double theta, boolean onLogitScale) {
110 if (onLogitScale) {
111 // e(x) / (1 + e(x)) and 1 / (1 + e(x))
112 final double logDenominator = Math.log(1.0 + Math.exp(theta));
113 logP = theta - logDenominator;
114 log1MinusP = -logDenominator;
115 outOfBounds = false;
116 } else {
117 logP = Math.log(theta);
118 log1MinusP = Math.log(1.0 - theta);
119 outOfBounds = (theta <= 0 || theta >= 1);
120 }
121 }
122 }
123
124 public void makeDirty() {
125 }
126
127 public void acceptState() {
128 // DO NOTHING
129 }
130
131 public void restoreState() {
132 // DO NOTHING
133 }
134
135 public void storeState() {
136 // DO NOTHING
137 }
138
139 protected void handleModelChangedEvent(Model model, Object object, int index) {
140 // DO NOTHING
141 }
142
143 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
144 // DO NOTHING
145 }
146
147 /**
148 * @return the binomial likelihood of obtaining the gicen count in the given number of trials,
149 * when the log of the probability is logP.
150 */
151 private double binomialLogLikelihood(int trials, int count, double logP, double log1MinusP) {
152 return Binomial.logChoose(trials, count) + (logP * count) + (log1MinusP * (trials - count));
153 }
154
155 // **************************************************************
156 // XMLElement IMPLEMENTATION
157 // **************************************************************
158
159 public Element createElement(Document d) {
160 throw new RuntimeException("Not implemented yet!");
161 }
162
163 Parameter trialsParameter;
164 Parameter proportionParameter;
165 Parameter countsParameter;
166
167 private final boolean onLogitScale;
168 }
169