| 1 | /* randist/dirichlet.c |
| 2 | * |
| 3 | * Copyright (C) 2007 Brian Gough |
| 4 | * Copyright (C) 2002 Gavin E. Crooks <[email protected]> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or (at |
| 9 | * your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | |
| 21 | #include "gsl__config.h" |
| 22 | #include <math.h> |
| 23 | #include "gsl_math.h" |
| 24 | #include "gsl_rng.h" |
| 25 | #include "gsl_randist.h" |
| 26 | #include "gsl_sf_gamma.h" |
| 27 | |
| 28 | |
| 29 | /* The Dirichlet probability distribution of order K-1 is |
| 30 | |
| 31 | p(\theta_1,...,\theta_K) d\theta_1 ... d\theta_K = |
| 32 | (1/Z) \prod_i=1,K \theta_i^{alpha_i - 1} \delta(1 -\sum_i=1,K \theta_i) |
| 33 | |
| 34 | The normalization factor Z can be expressed in terms of gamma functions: |
| 35 | |
| 36 | Z = {\prod_i=1,K \Gamma(\alpha_i)} / {\Gamma( \sum_i=1,K \alpha_i)} |
| 37 | |
| 38 | The K constants, \alpha_1,...,\alpha_K, must be positive. The K parameters, |
| 39 | \theta_1,...,\theta_K are nonnegative and sum to 1. |
| 40 | |
| 41 | The random variates are generated by sampling K values from gamma |
| 42 | distributions with parameters a=\alpha_i, b=1, and renormalizing. |
| 43 | See A.M. Law, W.D. Kelton, Simulation Modeling and Analysis (1991). |
| 44 | |
| 45 | Gavin E. Crooks <[email protected]> (2002) |
| 46 | */ |
| 47 | |
| 48 | static void ran_dirichlet_small (const gsl_rng * r, const size_t K, const double alpha[], double theta[]); |
| 49 | |
| 50 | void |
| 51 | gsl_ran_dirichlet (const gsl_rng * r, const size_t K, |
| 52 | const double alpha[], double theta[]) |
| 53 | { |
| 54 | size_t i; |
| 55 | double norm = 0.0; |
| 56 | |
| 57 | for (i = 0; i < K; i++) |
| 58 | { |
| 59 | theta[i] = gsl_ran_gamma (r, alpha[i], 1.0); |
| 60 | } |
| 61 | |
| 62 | for (i = 0; i < K; i++) |
| 63 | { |
| 64 | norm += theta[i]; |
| 65 | } |
| 66 | |
| 67 | if (norm < GSL_SQRT_DBL_MIN) /* Handle underflow */ |
| 68 | { |
| 69 | ran_dirichlet_small (r, K, alpha, theta); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | for (i = 0; i < K; i++) |
| 74 | { |
| 75 | theta[i] /= norm; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /* When the values of alpha[] are small, scale the variates to avoid |
| 81 | underflow so that the result is not 0/0. Note that the Dirichlet |
| 82 | distribution is defined by a ratio of gamma functions so we can |
| 83 | take out an arbitrary factor to keep the values in the range of |
| 84 | double precision. */ |
| 85 | |
| 86 | static void |
| 87 | ran_dirichlet_small (const gsl_rng * r, const size_t K, |
| 88 | const double alpha[], double theta[]) |
| 89 | { |
| 90 | size_t i; |
| 91 | double norm = 0.0, umax = 0; |
| 92 | |
| 93 | for (i = 0; i < K; i++) |
| 94 | { |
| 95 | double u = log(gsl_rng_uniform_pos (r)) / alpha[i]; |
| 96 | |
| 97 | theta[i] = u; |
| 98 | |
| 99 | if (u > umax || i == 0) { |
| 100 | umax = u; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | for (i = 0; i < K; i++) |
| 105 | { |
| 106 | theta[i] = exp(theta[i] - umax); |
| 107 | } |
| 108 | |
| 109 | for (i = 0; i < K; i++) |
| 110 | { |
| 111 | theta[i] = theta[i] * gsl_ran_gamma (r, alpha[i] + 1.0, 1.0); |
| 112 | } |
| 113 | |
| 114 | for (i = 0; i < K; i++) |
| 115 | { |
| 116 | norm += theta[i]; |
| 117 | } |
| 118 | |
| 119 | for (i = 0; i < K; i++) |
| 120 | { |
| 121 | theta[i] /= norm; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | double |
| 130 | gsl_ran_dirichlet_pdf (const size_t K, |
| 131 | const double alpha[], const double theta[]) |
| 132 | { |
| 133 | return exp (gsl_ran_dirichlet_lnpdf (K, alpha, theta)); |
| 134 | } |
| 135 | |
| 136 | double |
| 137 | gsl_ran_dirichlet_lnpdf (const size_t K, |
| 138 | const double alpha[], const double theta[]) |
| 139 | { |
| 140 | /*We calculate the log of the pdf to minimize the possibility of overflow */ |
| 141 | size_t i; |
| 142 | double log_p = 0.0; |
| 143 | double sum_alpha = 0.0; |
| 144 | |
| 145 | for (i = 0; i < K; i++) |
| 146 | { |
| 147 | log_p += (alpha[i] - 1.0) * log (theta[i]); |
| 148 | } |
| 149 | |
| 150 | for (i = 0; i < K; i++) |
| 151 | { |
| 152 | sum_alpha += alpha[i]; |
| 153 | } |
| 154 | |
| 155 | log_p += gsl_sf_lngamma (sum_alpha); |
| 156 | |
| 157 | for (i = 0; i < K; i++) |
| 158 | { |
| 159 | log_p -= gsl_sf_lngamma (alpha[i]); |
| 160 | } |
| 161 | |
| 162 | return log_p; |
| 163 | } |