Coverage Report

Created: 2026-11-13 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/r-source/src/nmath/pnbinom.c
Line
Count
Source
1
/*
2
 *  Mathlib : A C Library of Special Functions
3
 *  Copyright (C) 1998 Ross Ihaka
4
 *  Copyright (C) 2000-2016 The R Core Team
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 of the License, or
9
 *  (at your option) 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
 *  DESCRIPTION
21
 *
22
 *  The distribution function of the negative binomial distribution.
23
 *
24
 *  NOTES
25
 *
26
 *  x = the number of failures before the n-th success
27
 */
28
29
#include "nmath.h"
30
#include "dpq.h"
31
32
double pnbinom(double x, double size, double prob, int lower_tail, int log_p)
33
0
{
34
0
#ifdef IEEE_754
35
0
    if (ISNAN(x) || ISNAN(size) || ISNAN(prob))
36
0
  return x + size + prob;
37
0
    if(!R_FINITE(size) || !R_FINITE(prob)) ML_WARN_return_NAN;
38
0
#endif
39
0
    if (size < 0 || prob <= 0 || prob > 1) ML_WARN_return_NAN;
40
41
    /* limiting case: point mass at zero */
42
0
    if (size == 0)
43
0
        return (x >= 0) ? R_DT_1 : R_DT_0;
44
45
0
    if (x < 0) return R_DT_0;
46
0
    if (!R_FINITE(x)) return R_DT_1;
47
0
    x = floor(x + 1e-7);
48
0
    return pbeta(prob, size, x + 1, lower_tail, log_p);
49
0
}
50
51
double pnbinom_mu(double x, double size, double mu, int lower_tail, int log_p)
52
0
{
53
0
#ifdef IEEE_754
54
0
    if (ISNAN(x) || ISNAN(size) || ISNAN(mu))
55
0
  return x + size + mu;
56
0
    if(!R_FINITE(mu)) ML_WARN_return_NAN;
57
0
#endif
58
0
    if (size < 0 || mu < 0) ML_WARN_return_NAN;
59
60
    /* limiting case: point mass at zero */
61
0
    if (size == 0)
62
0
        return (x >= 0) ? R_DT_1 : R_DT_0;
63
64
0
    if (x < 0) return R_DT_0;
65
0
    if (!R_FINITE(x)) return R_DT_1;
66
0
    if (!R_FINITE(size)) // limit case: Poisson
67
0
  return(ppois(x, mu, lower_tail, log_p));
68
69
0
    x = floor(x + 1e-7);
70
    /* return
71
     * pbeta(pr, size, x + 1, lower_tail, log_p);  pr = size/(size + mu), 1-pr = mu/(size+mu)
72
     *
73
     *= pbeta_raw(pr, size, x + 1, lower_tail, log_p)
74
     *            x.  pin   qin
75
     *=  bratio (pin,  qin, x., 1-x., &w, &wc, &ierr, log_p),  and return w or wc ..
76
     *=  bratio (size, x+1, pr, 1-pr, &w, &wc, &ierr, log_p) */
77
0
    {
78
0
  int ierr;
79
0
  double w, wc;
80
0
  bratio(size, x+1, size/(size+mu), mu/(size+mu), &w, &wc, &ierr, log_p);
81
0
  if(ierr)
82
0
      MATHLIB_WARNING(_("pnbinom_mu() -> bratio() gave error code %d"), ierr);
83
0
  return lower_tail ? w : wc;
84
0
    }
85
0
}