| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.math.distributions; |
| 27 |
|
| 28 |
import dr.math.GammaFunction; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class DirichletDistribution implements MultivariateDistribution { |
| 35 |
|
| 36 |
public static final String TYPE = "dirichletDistribution"; |
| 37 |
public static final boolean DEBUG = false; |
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 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 |
|
| 140 |
System.out.println("Test Dirichlet distribution for the standard n-simplex"); |
| 141 |
|
| 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 |
|
| 154 |
System.out.println("Test Scaled Dirichlet distribution"); |
| 155 |
|
| 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 |
} |