/src/r-source/src/nmath/qnorm.c
Line | Count | Source |
1 | | /* |
2 | | * Mathlib : A C Library of Special Functions |
3 | | * Copyright (C) 2000--2023 The R Core Team |
4 | | * Copyright (C) 1998 Ross Ihaka |
5 | | * based on AS 241 (C) 1988 Royal Statistical Society |
6 | | * |
7 | | * This program is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * This program is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, a copy is available at |
19 | | * https://www.R-project.org/Licenses/ |
20 | | * |
21 | | * SYNOPSIS |
22 | | * |
23 | | * double qnorm5(double p, double mu, double sigma, |
24 | | * int lower_tail, int log_p) |
25 | | * {qnorm (..) is synonymous and preferred inside R} |
26 | | * |
27 | | * DESCRIPTION |
28 | | * |
29 | | * Compute the quantile function for the normal distribution. |
30 | | * |
31 | | * The algorithm AS 241 of Wichura is used, |
32 | | * and has been improved for the very extreme tail (and log_p=TRUE) |
33 | | * |
34 | | * REFERENCE |
35 | | * |
36 | | * Wichura, M.J. (1988). |
37 | | * Algorithm AS 241: The Percentage Points of the Normal Distribution. |
38 | | * Applied Statistics, 37, 477-484. |
39 | | * |
40 | | * Maechler, M. (2022). Asymptotic tail formulas for gaussian quantiles; |
41 | | * https://CRAN.R-project.org/package=DPQ/vignettes/qnorm-asymp.pdf |
42 | | */ |
43 | | |
44 | | #include "nmath.h" |
45 | | #include "dpq.h" |
46 | | |
47 | | double qnorm5(double p, double mu, double sigma, int lower_tail, int log_p) |
48 | 9.68k | { |
49 | 9.68k | double p_, q, r, val; |
50 | | |
51 | 9.68k | #ifdef IEEE_754 |
52 | 9.68k | if (ISNAN(p) || ISNAN(mu) || ISNAN(sigma)) |
53 | 0 | return p + mu + sigma; |
54 | 9.68k | #endif |
55 | 9.68k | R_Q_P01_boundaries(p, ML_NEGINF, ML_POSINF); |
56 | | |
57 | 9.06k | if(sigma < 0) ML_WARN_return_NAN; |
58 | 8.91k | if(sigma == 0) return mu; |
59 | | |
60 | 8.84k | p_ = R_DT_qIv(p);/* real lower_tail prob. p */ |
61 | 8.84k | q = p_ - 0.5; |
62 | | |
63 | | #ifdef DEBUG_qnorm |
64 | | REprintf("qnorm(p=%10.7g, m=%g, s=%g, l.t.= %d, log= %d): q = %g\n", |
65 | | p,mu,sigma, lower_tail, log_p, q); |
66 | | #endif |
67 | | |
68 | | |
69 | | /*-- use AS 241 --- */ |
70 | | /* double ppnd16_(double *p, long *ifault)*/ |
71 | | /* ALGORITHM AS241 APPL. STATIST. (1988) VOL. 37, NO. 3 |
72 | | |
73 | | Produces the normal deviate Z corresponding to a given lower |
74 | | tail area of P; Z is accurate to about 1 part in 10**16. |
75 | | |
76 | | (original fortran code used PARAMETER(..) for the coefficients |
77 | | and provided hash codes for checking them...) |
78 | | */ |
79 | 8.84k | if (fabs(q) <= .425) {/* |p~ - 0.5| <= .425 <==> 0.075 <= p~ <= 0.925 */ |
80 | 350 | r = .180625 - q * q; // = .425^2 - q^2 >= 0 |
81 | 350 | val = |
82 | 350 | q * (((((((r * 2509.0809287301226727 + |
83 | 350 | 33430.575583588128105) * r + 67265.770927008700853) * r + |
84 | 350 | 45921.953931549871457) * r + 13731.693765509461125) * r + |
85 | 350 | 1971.5909503065514427) * r + 133.14166789178437745) * r + |
86 | 350 | 3.387132872796366608) |
87 | 350 | / (((((((r * 5226.495278852854561 + |
88 | 350 | 28729.085735721942674) * r + 39307.89580009271061) * r + |
89 | 350 | 21213.794301586595867) * r + 5394.1960214247511077) * r + |
90 | 350 | 687.1870074920579083) * r + 42.313330701600911252) * r + 1.); |
91 | 350 | } |
92 | 8.49k | else { /* closer than 0.075 from {0,1} boundary : |
93 | | * r := log(p~); p~ = min(p, 1-p) < 0.075 : */ |
94 | 8.49k | double lp; |
95 | 8.49k | if(log_p && ((lower_tail && q <= 0) || (!lower_tail && q > 0))) { |
96 | 2.64k | lp = p; |
97 | 5.84k | } else { |
98 | 5.84k | lp = log( (q > 0) ? R_DT_CIv(p) /* 1-p */ : p_ /* = R_DT_Iv(p) ^= p */); |
99 | 5.84k | } |
100 | | // r = sqrt( - log(min(p,1-p)) ) <==> min(p, 1-p) = exp( - r^2 ) : |
101 | 8.49k | r = sqrt(-lp); |
102 | | #ifdef DEBUG_qnorm |
103 | | REprintf("\t close to 0 or 1: r = %7g\n", r); |
104 | | #endif |
105 | 8.49k | if (r <= 5.) { /* <==> min(p,1-p) >= exp(-25) ~= 1.3888e-11 */ |
106 | 595 | r += -1.6; |
107 | 595 | val = (((((((r * 7.7454501427834140764e-4 + |
108 | 595 | .0227238449892691845833) * r + .24178072517745061177) * |
109 | 595 | r + 1.27045825245236838258) * r + |
110 | 595 | 3.64784832476320460504) * r + 5.7694972214606914055) * |
111 | 595 | r + 4.6303378461565452959) * r + |
112 | 595 | 1.42343711074968357734) |
113 | 595 | / (((((((r * |
114 | 595 | 1.05075007164441684324e-9 + 5.475938084995344946e-4) * |
115 | 595 | r + .0151986665636164571966) * r + |
116 | 595 | .14810397642748007459) * r + .68976733498510000455) * |
117 | 595 | r + 1.6763848301838038494) * r + |
118 | 595 | 2.05319162663775882187) * r + 1.); |
119 | 595 | } |
120 | 7.89k | else if(r <= 27) { /* p is very close to 0 or 1: r in (5, 27] : |
121 | | * r > 5 <==> min(p,1-p) < exp(-25) = 1.3888..e-11 |
122 | | * r <= 27 <==> min(p,1-p) >= exp(-27^2) = exp(-729) ~= 2.507972e-317 |
123 | | * i.e., we are just barely in the range where min(p, 1-p) has not yet underflowed to zero. |
124 | | */ |
125 | | // Wichura, p.478: minimax rational approx R_3(t) is for 5 <= t <= 27 (t :== r) |
126 | 4.90k | r += -5.; |
127 | 4.90k | val = (((((((r * 2.01033439929228813265e-7 + |
128 | 4.90k | 2.71155556874348757815e-5) * r + |
129 | 4.90k | .0012426609473880784386) * r + .026532189526576123093) * |
130 | 4.90k | r + .29656057182850489123) * r + |
131 | 4.90k | 1.7848265399172913358) * r + 5.4637849111641143699) * |
132 | 4.90k | r + 6.6579046435011037772) |
133 | 4.90k | / (((((((r * |
134 | 4.90k | 2.04426310338993978564e-15 + 1.4215117583164458887e-7)* |
135 | 4.90k | r + 1.8463183175100546818e-5) * r + |
136 | 4.90k | 7.868691311456132591e-4) * r + .0148753612908506148525) |
137 | 4.90k | * r + .13692988092273580531) * r + |
138 | 4.90k | .59983220655588793769) * r + 1.); |
139 | 4.90k | } |
140 | 2.99k | else { // r > 27: p is *really* close to 0 or 1 .. practically only when log_p =TRUE |
141 | 2.99k | if(r >= 6.4e8) { // p is *very extremely* close to 0 or 1 |
142 | | // Using the asymptotical formula ("0-th order"): qn = sqrt(2*s) |
143 | 2.30k | val = r * M_SQRT2; |
144 | 2.30k | } else { |
145 | 689 | double s2 = -ldexp(lp, 1), // = -2*lp = 2s |
146 | 689 | x2 = s2 - log(M_2PI * s2); // = xs_1 |
147 | | // if(r >= 36000.) # <==> s >= 36000^2 use x2 = xs_1 above |
148 | 689 | if(r < 36000.) { |
149 | 593 | x2 = s2 - log(M_2PI * x2) - 2./(2. + x2); // == xs_2 |
150 | 593 | if(r < 840.) { // 27 < r < 840 |
151 | 457 | x2 = s2 - log(M_2PI * x2) + 2*log1p(- (1 - 1/(4 + x2))/(2. + x2)); // == xs_3 |
152 | 457 | if(r < 109.) { // 27 < r < 109 |
153 | 437 | x2 = s2 - log(M_2PI * x2) + |
154 | 437 | 2*log1p(- (1 - (1 - 5/(6 + x2))/(4. + x2))/(2. + x2)); // == xs_4 |
155 | 437 | if(r < 55.) { // 27 < r < 55 |
156 | 354 | x2 = s2 - log(M_2PI * x2) + |
157 | 354 | 2*log1p(- (1 - (1 - (5 - 9/(8. + x2))/(6. + x2))/(4. + x2))/(2. + x2)); // == xs_5 |
158 | 354 | } |
159 | 437 | } |
160 | 457 | } |
161 | 593 | } |
162 | 689 | val = sqrt(x2); |
163 | 689 | } |
164 | 2.99k | } |
165 | 8.49k | if(q < 0.0) |
166 | 3.61k | val = -val; |
167 | 8.49k | } |
168 | 8.84k | return mu + sigma * val; |
169 | 8.91k | } |