TMB Documentation  v1.9.11
pbeta.cpp
1 /*
2  * Mathlib : A C Library of Special Functions
3  * Copyright (C) 2006 The R Core Team
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 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU 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, a copy is available at
17  * https://www.R-project.org/Licenses/
18  *
19  * SYNOPSIS
20  *
21  * #include <Rmath.h>
22  *
23  * double pbeta_raw(double x, double a, double b, int lower_tail, int log_p)
24  * double pbeta (double x, double a, double b, int lower_tail, int log_p)
25  *
26  * DESCRIPTION
27  *
28  * Returns distribution function of the beta distribution.
29  * ( = The incomplete beta ratio I_x(p,q) ).
30  *
31  * NOTES
32  *
33  * As from R 2.3.0, a wrapper for TOMS708
34  * as from R 2.6.0, 'log_p' partially improved over log(p..)
35  */
36 
37 #include "nmath.h"
38 #include "dpq.h"
39 
40 template<class Float>
41 attribute_hidden
42 Float pbeta_raw(Float x, Float a, Float b, int lower_tail, int log_p)
43 {
44  // treat limit cases correctly here:
45  if(a == 0 || b == 0 || !R_FINITE(a) || !R_FINITE(b)) {
46  // NB: 0 < x < 1 :
47  if(a == 0 && b == 0) // point mass 1/2 at each of {0,1} :
48  return (log_p ? -M_LN2 : 0.5);
49  if (a == 0 || a/b == 0) // point mass 1 at 0 ==> P(X <= x) = 1, all x > 0
50  return R_DT_1;
51  if (b == 0 || b/a == 0) // point mass 1 at 1 ==> P(X <= x) = 0, all x < 1
52  return R_DT_0;
53  // else, remaining case: a = b = Inf : point mass 1 at 1/2
54  if (x < 0.5) return R_DT_0; else return R_DT_1;
55  }
56  // Now: 0 < a < Inf; 0 < b < Inf
57 
58  Float x1 = 0.5 - x + 0.5, w, wc;
59  int ierr;
60  //====
61  bratio(a, b, x, x1, &w, &wc, &ierr, log_p); /* -> ./toms708.c */
62  //====
63  // ierr in {10,14} <==> bgrat() error code ierr-10 in 1:4; for 1 and 4, warned *there*
64  if(ierr && ierr != 11 && ierr != 14)
65  MATHLIB_WARNING4(_("pbeta_raw(%g, a=%g, b=%g, ..) -> bratio() gave error code %d"),
66  x, a,b, ierr);
67  return lower_tail ? w : wc;
68 } /* pbeta_raw() */
69 
70 template<class Float>
71 Float pbeta(Float x, Float a, Float b, int lower_tail, int log_p)
72 {
73 #ifdef IEEE_754
74  if (ISNAN(x) || ISNAN(a) || ISNAN(b)) return x + a + b;
75 #endif
76 
77  if (a < 0 || b < 0) ML_ERR_return_NAN;
78  // allowing a==0 and b==0 <==> treat as one- or two-point mass
79 
80  if (x <= 0)
81  return R_DT_0;
82  if (x >= 1)
83  return R_DT_1;
84 
85  return pbeta_raw(x, a, b, lower_tail, log_p);
86 }
Type pbeta(Type q, Type shape1, Type shape2)
Distribution function of the beta distribution (following R argument convention). ...
License: GPL v2