Debian Codegraph @

src/dr/inference/distribution/DistributionLikelihood.java

raw symbols SCIP
1 /*
2 * DistributionLikelihood.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.math.distributions.Distribution;
29 import dr.math.matrixAlgebra.Vector;
30 import dr.util.Attribute;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33
34 /**
35 * A class that returns the log likelihood of a set of data (statistics)
36 * being distributed according to the given parametric distribution.
37 *
38 * @author Alexei Drummond
39 * @version $Id: DistributionLikelihood.java,v 1.11 2005/05/25 09:35:28 rambaut Exp $
40 */
41
42 public class DistributionLikelihood extends AbstractDistributionLikelihood {
43
44 public final static boolean DEBUG = false;
45
46 public static final String DISTRIBUTION_LIKELIHOOD = "distributionLikelihood";
47
48 private int from = -1;
49 private int to = Integer.MAX_VALUE;
50 private final boolean evaluateEarly;
51
52 public DistributionLikelihood(Distribution distribution) {
53 this(distribution, 0.0, false, 1.0);
54 }
55
56 public DistributionLikelihood(Distribution distribution, double offset) {
57 this(distribution, offset, offset > 0.0, 1.0);
58 }
59
60 public DistributionLikelihood(Distribution distribution, double offset, double scale){
61 this(distribution, offset, offset>0.0, scale);
62 }
63
64 public DistributionLikelihood(Distribution distribution, boolean evaluateEarly) {
65 this(distribution, 0.0, evaluateEarly, 1.0);
66 }
67
68 public DistributionLikelihood(Distribution distribution, double offset, boolean evaluateEarly, double scale) {
69 super(null);
70 this.distribution = distribution;
71 this.offset = offset;
72 this.evaluateEarly = evaluateEarly;
73 this.scale=scale;
74 }
75
76 public DistributionLikelihood(ParametricDistributionModel distributionModel) {
77 super(distributionModel);
78 this.distribution = distributionModel;
79 this.offset = 0.0;
80 this.evaluateEarly = false;
81 this.scale=1.0;
82 }
83
84 public Distribution getDistribution() {
85 return distribution;
86 }
87
88 public void setRange(int from, int to) {
89 this.from = from;
90 this.to = to;
91 }
92
93 // **************************************************************
94 // Likelihood IMPLEMENTATION
95 // **************************************************************
96
97 /**
98 * Calculate the log likelihood of the current state.
99 *
100 * @return the log likelihood.
101 */
102 public double calculateLogLikelihood() {
103
104 if (DEBUG) {
105 System.err.println("Calling DistributionLikelihood.calculateLogLikelihood()");
106 System.err.println(distribution.toString());
107 System.err.println(dataList.toString() + "\n");
108 }
109
110 double logL = 0.0;
111 int count = 0;
112
113 for( Attribute<double[]> data : dataList ) {
114
115 // Using this in the loop is incredibly wasteful, especially in the loop condition to get the length
116 final double[] attributeValue = data.getAttributeValue();
117
118 if (DEBUG) {
119 System.err.println("\t" + new Vector(attributeValue));
120 }
121
122 for (int j = Math.max(0, from); j < Math.min(attributeValue.length, to); j++) {
123
124 final double value = attributeValue[j] - offset;
125
126 if (offset > 0.0 && value < 0.0) {
127 // fixes a problem with the offset on exponential distributions not
128 // actually bounding the distribution. This only performs this check
129 // if a non-zero offset is actually given otherwise it assumes the
130 // parameter is either legitimately allowed to go negative or is bounded
131 // at zero anyway.
132 return Double.NEGATIVE_INFINITY;
133 }
134 logL += getLogPDF(value, count);
135 count += 1;
136 }
137
138 }
139 return logL;
140 }
141
142 protected double getLogPDF(double value, int i){
143 return distribution.logPdf(value/scale) - Math.log(scale);
144 }
145
146 @Override
147 public boolean evaluateEarly() {
148 return evaluateEarly;
149 }
150
151 // **************************************************************
152 // XMLElement IMPLEMENTATION
153 // **************************************************************
154
155 public Element createElement(Document d) {
156 throw new RuntimeException("Not implemented yet!");
157 }
158
159 @Override
160 public String prettyName() {
161 String s = distribution.getClass().getName();
162 String[] parts = s.split("\\.");
163 s = parts[parts.length - 1];
164 if( s.endsWith("Distribution") ) {
165 s = s.substring(0, s.length() - "Distribution".length());
166 }
167 s = s + '(';
168 for( Attribute<double[]> data : dataList ) {
169 String name = data.getAttributeName();
170 if( name == null ) {
171 name = "?";
172 }
173 s = s + name + ',';
174 }
175 s = s.substring(0,s.length()-1) + ')';
176
177 return s;
178 }
179
180 protected Distribution distribution;
181 private final double offset;
182 private final double scale;
183 }
184