gsl_randist__multinomial.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* randist/multinomial.c
  2. *
  3. * Copyright (C) 2002 Gavin E. Crooks <gec@compbio.berkeley.edu>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include <math.h>
  21. #include "gsl_rng.h"
  22. #include "gsl_randist.h"
  23. #include "gsl_sf_gamma.h"
  24. /* The multinomial distribution has the form
  25. N! n_1 n_2 n_K
  26. prob(n_1, n_2, ... n_K) = -------------------- p_1 p_2 ... p_K
  27. (n_1! n_2! ... n_K!)
  28. where n_1, n_2, ... n_K are nonnegative integers, sum_{k=1,K} n_k = N,
  29. and p = (p_1, p_2, ..., p_K) is a probability distribution.
  30. Random variates are generated using the conditional binomial method.
  31. This scales well with N and does not require a setup step.
  32. Ref:
  33. C.S. David, The computer generation of multinomial random variates,
  34. Comp. Stat. Data Anal. 16 (1993) 205-217
  35. */
  36. void
  37. gsl_ran_multinomial (const gsl_rng * r, const size_t K,
  38. const unsigned int N, const double p[], unsigned int n[])
  39. {
  40. size_t k;
  41. double norm = 0.0;
  42. double sum_p = 0.0;
  43. unsigned int sum_n = 0;
  44. /* p[k] may contain non-negative weights that do not sum to 1.0.
  45. * Even a probability distribution will not exactly sum to 1.0
  46. * due to rounding errors.
  47. */
  48. for (k = 0; k < K; k++)
  49. {
  50. norm += p[k];
  51. }
  52. for (k = 0; k < K; k++)
  53. {
  54. if (p[k] > 0.0)
  55. {
  56. n[k] = gsl_ran_binomial (r, p[k] / (norm - sum_p), N - sum_n);
  57. }
  58. else
  59. {
  60. n[k] = 0;
  61. }
  62. sum_p += p[k];
  63. sum_n += n[k];
  64. }
  65. }
  66. double
  67. gsl_ran_multinomial_pdf (const size_t K,
  68. const double p[], const unsigned int n[])
  69. {
  70. return exp (gsl_ran_multinomial_lnpdf (K, p, n));
  71. }
  72. double
  73. gsl_ran_multinomial_lnpdf (const size_t K,
  74. const double p[], const unsigned int n[])
  75. {
  76. size_t k;
  77. unsigned int N = 0;
  78. double log_pdf = 0.0;
  79. double norm = 0.0;
  80. for (k = 0; k < K; k++)
  81. {
  82. N += n[k];
  83. }
  84. for (k = 0; k < K; k++)
  85. {
  86. norm += p[k];
  87. }
  88. log_pdf = gsl_sf_lnfact (N);
  89. for (k = 0; k < K; k++)
  90. {
  91. log_pdf -= gsl_sf_lnfact (n[k]);
  92. }
  93. for (k = 0; k < K; k++)
  94. {
  95. log_pdf += log (p[k] / norm) * n[k];
  96. }
  97. return log_pdf;
  98. }