Debian Codegraph @

src/dr/math/distributions/DirichletDistribution.java

raw symbols SCIP
1 /*
2 * DirichletDistribution.java
3 *
4 * Copyright (c) 2002-2016 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.math.distributions;
27
28 import dr.math.GammaFunction;
29
30 /**
31 * @author Marc A. Suchard
32 * @author Guy Baele
33 */
34 public class DirichletDistribution implements MultivariateDistribution {
35
36 public static final String TYPE = "dirichletDistribution";
37 public static final boolean DEBUG = false;
38
39 //4.0 != 3.9999999999999996
40 //Other BEAST classes uses 1E-8 or 1E-6
41 public static final double ACCURACY_THRESHOLD = 1E-12;
42
43 private double[] counts;
44 private double countSum = 0.0;
45 private double countParameterSum;
46 private int dim;
47
48 private boolean sumToNumberOfElements;
49
50 private double logNormalizingConstant;
51
52 public DirichletDistribution(double[] counts, boolean sumToNumberOfElements) {
53 this.counts = counts;
54 this.sumToNumberOfElements = sumToNumberOfElements;
55 if (this.sumToNumberOfElements) {
56 countParameterSum = (double)counts.length;
57 } else {
58 countParameterSum = 1.0;
59 }
60 dim = counts.length;
61 for (int i = 0; i < dim; i++) {
62 countSum += counts[i];
63 }
64
65 computeNormalizingConstant();
66 }
67
68 public DirichletDistribution(double[] counts, double countParameterSum) {
69 this.counts = counts;
70 this.countParameterSum = countParameterSum;
71
72 dim = counts.length;
73 for (int i = 0; i < dim; i++) {
74 countSum += counts[i];
75 }
76
77 computeNormalizingConstant();
78 }
79
80 private void computeNormalizingConstant() {
81 logNormalizingConstant = GammaFunction.lnGamma(countSum);
82 for (int i = 0; i < dim; i++) {
83 logNormalizingConstant -= GammaFunction.lnGamma(counts[i]);
84 }
85 logNormalizingConstant -= dim * Math.log(countParameterSum);
86 }
87
88
89 public double logPdf(double[] x) {
90
91 if (x.length != dim) {
92 throw new IllegalArgumentException("data array is of the wrong dimension");
93 }
94
95 double logPDF = logNormalizingConstant;
96 double parameterSum = 0.0;
97 for (int i = 0; i < dim; i++) {
98 logPDF += (counts[i] - 1) * (Math.log(x[i]) - Math.log(countParameterSum));
99 parameterSum += x[i];
100 // if ((!sumToNumberOfElements && x[i] >= 1.0) || x[i] <= 0.0) {
101 // if (DEBUG) {
102 // System.out.println("Invalid parameter value");
103 // }
104 // logPDF = Double.NEGATIVE_INFINITY;
105 // break;
106 // }
107 }
108 if (Math.abs(parameterSum - countParameterSum) > ACCURACY_THRESHOLD) {
109 if (DEBUG) {
110 System.out.println("Parameters do not sum to " + countParameterSum);
111 for (int i = 0; i < dim; i++) {
112 System.out.println("x[" + i + "] = " + x[i]);
113 }
114 System.out.println("Current sum = " + parameterSum);
115 }
116 logPDF = Double.NEGATIVE_INFINITY;
117 }
118
119 return logPDF;
120 }
121
122 public double[][] getScaleMatrix() {
123 return null;
124 }
125
126 public double[] getMean() {
127 double[] mean = new double[dim];
128 for (int i = 0; i < dim; i++)
129 mean[i] = counts[i] / countSum;
130 return mean;
131 }
132
133 public String getType() {
134 return TYPE;
135 }
136
137 public static void main(String[] args) {
138
139 //Test Dirichlet distribution for the standard n-simplex
140 System.out.println("Test Dirichlet distribution for the standard n-simplex");
141 //R: log(ddirichlet(c(0.5,0.2,0.3),c(1,2,3))) = 0.07696104
142 double[] counts = new double[3];
143 counts[0] = 1.0;
144 counts[1] = 2.0;
145 counts[2] = 3.0;
146 DirichletDistribution dd = new DirichletDistribution(counts, false);
147 double[] parameterValues = new double[3];
148 parameterValues[0] = 0.5;
149 parameterValues[1] = 0.2;
150 parameterValues[2] = 0.3;
151 System.out.println(dd.logPdf(parameterValues));
152
153 //Test Scaled Dirichlet distribution
154 System.out.println("Test Scaled Dirichlet distribution");
155 //R: log(ddirichlet(c(1.5,0.6,0.9)/3,c(1,2,3))/(3^3)) = -3.218876
156 dd = new DirichletDistribution(counts, true);
157 parameterValues[0] = 1.5;
158 parameterValues[1] = 0.6;
159 parameterValues[2] = 0.9;
160 System.out.println(dd.logPdf(parameterValues));
161
162 parameterValues[0] = 1.0;
163 parameterValues[1] = 1.0;
164 parameterValues[2] = 1.0;
165 System.out.println(dd.logPdf(parameterValues));
166
167 counts = new double[4];
168 counts[0] = 1.0;
169 counts[1] = 1.0;
170 counts[2] = 1.0;
171 counts[3] = 1.0;
172 dd = new DirichletDistribution(counts, true);
173 parameterValues = new double[4];
174 parameterValues[0] = 0.5;
175 parameterValues[1] = 1.2;
176 parameterValues[2] = 1.3;
177 parameterValues[3] = 1.0;
178 System.out.println(dd.logPdf(parameterValues));
179 parameterValues[0] = 1.0;
180 parameterValues[1] = 1.0;
181 parameterValues[2] = 1.0;
182 parameterValues[3] = 1.0;
183 System.out.println(dd.logPdf(parameterValues));
184
185 }
186
187 }