Coverage Report

Created: 2026-11-30 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/r-source/src/nmath/rmultinom.c
Line
Count
Source
1
/*
2
 *  Mathlib : A C Library of Special Functions
3
 *  Copyright (C) 2003-2026 The R Core Team
4
 *  Copyright (C) 2003-2007 The R Foundation
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 2, or (at your option)
9
 *  any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU 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, a copy is available at
18
 *  https://www.R-project.org/Licenses/
19
 *
20
 *
21
 *  SYNOPSIS
22
 *
23
 *  #include <Rmath.h>
24
 *  void rmultinom(int n, double* prob, int K, int* rN);
25
 *
26
 *  DESCRIPTION
27
 *
28
 *  Random Vector from the multinomial distribution.
29
 *             ~~~~~~
30
 *  NOTE
31
 *  Because we generate random _vectors_ this doesn't fit easily
32
 *  into the do_random[1-4](.) framework setup in ../main/random.c
33
 *  as that is used only for the univariate random generators.
34
 *      Multivariate distributions typically have too complex parameter spaces
35
 *  to be treated uniformly.
36
 *  => Hence also can have  int arguments.
37
 */
38
39
#include "nmath.h"
40
#include <stdlib.h>
41
42
#ifdef MATHLIB_STANDALONE
43
#define ML_WARN_ret_NAN(_k_) {ML_WARNING(ME_DOMAIN, "rmultinom"); rN[_k_]=-1; return;}
44
#else
45
0
#define ML_WARN_ret_NAN(_k_) {ML_WARNING(ME_DOMAIN, "rmultinom"); rN[_k_]=NA_INTEGER; return;}
46
#endif
47
48
void rmultinom(int n, double* prob, int K, int* rN)
49
/* `Return' vector  rN[1:K] {K := length(prob)}
50
 *  where rN[j] ~ Bin(n, prob[j]) ,  sum_j rN[j] == n,  sum_j prob[j] == 1,
51
 */
52
0
{
53
0
    int k;
54
0
    double pp;
55
    /* Using Kahan compensated summation for platform-independent accuracy
56
       avoids relying on LDOUBLE (long double) which varies across
57
       platforms (e.g., 80-bit on x86 vs 64-bit on ARM).    See PR#18693. */
58
0
    double p_tot = 0.,
59
0
     p_comp = 0.; /* Kahan compensation term */
60
61
#ifdef MATHLIB_STANDALONE
62
    if (K < 1) { ML_WARNING(ME_DOMAIN, "rmultinom"); return;}
63
    if (n < 0)  ML_WARN_ret_NAN(0);
64
#else
65
0
    if (K == NA_INTEGER || K < 1) { ML_WARNING(ME_DOMAIN, "rmultinom"); return;}
66
0
    if (n == NA_INTEGER || n < 0)  ML_WARN_ret_NAN(0);
67
0
#endif
68
69
    /* Note: prob[K] is only used here for checking  sum_k prob[k] = 1 ;
70
     *       Could make loop one shorter and drop that check !
71
     */
72
0
    for(k = 0; k < K; k++) {
73
0
  pp = prob[k];
74
0
  if (!R_FINITE(pp) || pp < 0. || pp > 1.) ML_WARN_ret_NAN(k);
75
  /* Kahan summation: p_tot += pp with compensation */
76
0
  double y = pp - p_comp,
77
0
      t = p_tot + y;
78
0
  p_comp = (t - p_tot) - y;
79
0
  p_tot = t;
80
0
  rN[k] = 0;
81
0
    }
82
0
    if(fabs(p_tot - 1.) > 1e-7)
83
0
  MATHLIB_ERROR(_("rbinom: probability sum should be 1, but is %g"), p_tot);
84
0
    if(n == 0 ||
85
0
       (K == 1 && p_tot == 0.)) /* trivial border case: do as rbinom */
86
0
  return;
87
88
    /* Generate the first K-1 obs. via binomials */
89
90
0
    for(k = 0; k < K-1; k++) { /* (p_tot, n) are for "remaining binomial" */
91
0
  if(prob[k] != 0.) {
92
0
      pp = prob[k] / p_tot;
93
      /* printf("[%d] %.17f\n", k+1, pp); */
94
0
      rN[k] = ((pp < 1.) ? (int) rbinom((double) n,  pp) :
95
         /*>= 1; > 1 happens because of rounding */
96
0
         n);
97
0
      n -= rN[k];
98
0
  }
99
0
  else rN[k] = 0;
100
0
  if(n <= 0) /* we have all*/ return;
101
  /* Kahan subtraction: p_tot -= prob[k] with compensation */
102
0
  double y = -prob[k] - p_comp,
103
0
      t = p_tot + y;
104
0
  p_comp = (t - p_tot) - y;
105
0
  p_tot = t;
106
0
    }
107
0
    rN[K-1] = n;
108
0
    return;
109
0
}
110
#undef ML_WARN_ret_NAN