/src/r-source/src/nmath/wilcox.c
Line | Count | Source |
1 | | /* |
2 | | Mathlib : A C Library of Special Functions |
3 | | Copyright (C) 1999-2026 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 (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, a copy is available at |
17 | | https://www.R-project.org/Licenses/ |
18 | | |
19 | | SYNOPSIS |
20 | | |
21 | | #include <Rmath.h> |
22 | | double dwilcox(double x, double m, double n, int give_log) |
23 | | double pwilcox(double x, double m, double n, int lower_tail, int log_p) |
24 | | double qwilcox(double x, double m, double n, int lower_tail, int log_p); |
25 | | double rwilcox(double m, double n) |
26 | | |
27 | | DESCRIPTION |
28 | | |
29 | | dwilcox The density of the Wilcoxon distribution. |
30 | | pwilcox The distribution function of the Wilcoxon distribution. |
31 | | qwilcox The quantile function of the Wilcoxon distribution. |
32 | | rwilcox Random variates from the Wilcoxon distribution. |
33 | | |
34 | | */ |
35 | | |
36 | | /* |
37 | | Note: the checks here for R_CheckUserInterrupt also do stack checking. |
38 | | |
39 | | calloc/free are remapped for use in R, so allocation checks are done there. |
40 | | freeing is completed by an on.exit action in the R wrappers. |
41 | | |
42 | | The Wilcoxon distribution is calculated using work from Andreas Loeffler |
43 | | https://upload.wikimedia.org/wikipedia/commons/f/f5/LoefflerWilcoxonMannWhitneyTest.pdf |
44 | | https://upload.wikimedia.org/wikipedia/de/1/19/MannWhitney_151102.pdf |
45 | | */ |
46 | | |
47 | | #include <limits.h> |
48 | | |
49 | | #include "nmath.h" |
50 | | #include "dpq.h" |
51 | | |
52 | | #ifndef MATHLIB_STANDALONE |
53 | | #include <R_ext/Utils.h> |
54 | | #endif |
55 | | |
56 | | static double *w; /* to store (one half of) the Wilcoxon distribution */ |
57 | | static int *sigma; |
58 | | static int allocated_m, allocated_n, max_k; |
59 | | |
60 | | static int |
61 | 39.0k | cwilcox_sigma(int k, int m, int n) { |
62 | | /* this is used in w_fill_to_k to calculate w */ |
63 | 39.0k | int s=0, d, iter1, iter2; |
64 | | |
65 | | /* the factors of k must be at most k */ |
66 | 39.0k | iter1 = m < k ? m : k; |
67 | 39.0k | iter2 = m+n < k ? m+n : k; |
68 | 4.10M | for (d = 1; d <= iter1; d++) s += (k%d==0)*d; |
69 | 3.94M | for (d = n+1; d <= iter2; d++) s -= (k%d==0)*d; |
70 | 39.0k | return s; |
71 | 39.0k | } |
72 | | |
73 | | static void |
74 | | w_fill_to_k(int m, int n, int new_k) |
75 | 62.9k | { |
76 | | /* |
77 | | * lazily fill in the distribution up to new_k |
78 | | * store the last final index evaluated globally |
79 | | */ |
80 | 62.9k | if(new_k < max_k) return; |
81 | | |
82 | 38.4k | int i, k; |
83 | 38.4k | double s; |
84 | | |
85 | | /* fill in the values for sigma */ |
86 | 77.4k | for(i=max_k+1; i<=new_k; i++) |
87 | 39.0k | sigma[i] = cwilcox_sigma(i, m, n); |
88 | | |
89 | | /* fill in the values for the distribution */ |
90 | 77.4k | for (k=max_k+1; k<=new_k; k++) { |
91 | 39.0k | if (k==0){ |
92 | 124 | w[0]=1; /* by definition 0 has only 1 partition */ |
93 | 38.9k | } else { |
94 | 38.9k | s = 0; |
95 | 127M | for (i = 0; i<k; i++){ |
96 | | /* recursion formula */ |
97 | 127M | s += w[i]*sigma[k-i]; |
98 | 127M | } |
99 | 38.9k | w[k] = s/k; |
100 | 38.9k | } |
101 | 39.0k | } |
102 | 38.4k | max_k = new_k; |
103 | 38.4k | return; |
104 | 62.9k | } |
105 | | |
106 | | static void |
107 | | w_init_maybe(int m, int n) |
108 | 124 | { |
109 | | /* if the size is 0 just return, no need to recall cwilcox_sigma */ |
110 | 124 | if (m==0 || n==0) return; |
111 | | |
112 | | /* if we need to resize, first free the existing array */ |
113 | 124 | if ((w || sigma) && (m != allocated_m || n != allocated_n)) |
114 | 0 | wilcox_free(); |
115 | | |
116 | 124 | if (!w || !sigma) { /* initialize w[] */ |
117 | 124 | w = (double *) calloc(((size_t) m*n)/2+1, sizeof(double)); |
118 | 124 | sigma = (int *) calloc(((size_t) m*n)/2+1, sizeof(int)); |
119 | | |
120 | | #ifdef MATHLIB_STANDALONE |
121 | | if (!w || !sigma) MATHLIB_ERROR(_("wilcox allocation error %d"), 1); |
122 | | #endif |
123 | | |
124 | 124 | allocated_m = m; allocated_n = n; |
125 | 124 | max_k = -1; |
126 | 124 | } |
127 | 124 | } |
128 | | |
129 | | #ifndef MATHLIB_STANDALONE |
130 | | static int ic = 99999; |
131 | | #endif |
132 | | /* This counts the number of choices with statistic = k */ |
133 | | static double |
134 | | cwilcox(int k, int m, int n) |
135 | 64.4k | { |
136 | 64.4k | int c, i, j, |
137 | 64.4k | u = m * n; |
138 | 64.4k | #ifndef MATHLIB_STANDALONE |
139 | 64.4k | if (!ic--) { |
140 | 0 | R_CheckUserInterrupt(); |
141 | 0 | ic = 99999; |
142 | 0 | } |
143 | 64.4k | #endif |
144 | 64.4k | if (k < 0 || k > u) |
145 | 0 | return(0); |
146 | 64.4k | c = (int)(u / 2); |
147 | 64.4k | if (k > c) |
148 | 22 | k = u - k; /* hence k < floor(u / 2) */ |
149 | 64.4k | if (m < n) { |
150 | 12.4k | i = m; j = n; |
151 | 52.0k | } else { |
152 | 52.0k | i = n; j = m; |
153 | 52.0k | } /* hence i <= j */ |
154 | | |
155 | | /* if any of these values are 0 we return k==0 */ |
156 | 64.4k | if (i == 0 || j == 0 || k == 0) return (k == 0); |
157 | | |
158 | | /* |
159 | | * previous iterations called cwilcox(k, i, k) here if j>0 and k<j |
160 | | * however, I'm intentionally not doing that since `sigma` and `w` are |
161 | | * cached for fixed values of m,n. Swapping m,n will force a realloc |
162 | | * and lots of additional w_fill_to_k() calls. |
163 | | */ |
164 | | |
165 | | /* initialize space for the distribution if it doesn't yet exist */ |
166 | 62.9k | if(!w || !sigma || i != allocated_m || j != allocated_n) |
167 | 124 | w_init_maybe(i,j); |
168 | | |
169 | | /* fill in values up to k (caching results) */ |
170 | 62.9k | w_fill_to_k(i,j,k); |
171 | | |
172 | 62.9k | return w[k]; |
173 | 64.4k | } |
174 | | |
175 | | double dwilcox(double x, double m, double n, int give_log) |
176 | 882 | { |
177 | 882 | #ifdef IEEE_754 |
178 | | /* NaNs propagated correctly */ |
179 | 882 | if (ISNAN(x) || ISNAN(m) || ISNAN(n)) |
180 | 0 | return(x + m + n); |
181 | 882 | #endif |
182 | 882 | m = R_forceint(m); |
183 | 882 | n = R_forceint(n); |
184 | 882 | if (m <= 0 || n <= 0) |
185 | 750 | ML_WARN_return_NAN; |
186 | | |
187 | 750 | if (R_nonint(x)) |
188 | 169 | return(R_D__0); |
189 | 581 | x = R_forceint(x); |
190 | 581 | if ((x < 0) || (x > m * n)) |
191 | 259 | return(R_D__0); |
192 | | |
193 | 322 | if (m > INT_MAX) |
194 | 322 | MATHLIB_ERROR("m(%g) > INT_MAX", m); |
195 | 322 | if (n > INT_MAX) |
196 | 322 | MATHLIB_ERROR("n(%g) > INT_MAX", n); |
197 | 322 | if (x > INT_MAX) |
198 | 322 | MATHLIB_ERROR("x(%g) > INT_MAX", x); |
199 | | |
200 | 322 | int mm = (int) m, nn = (int) n, xx = (int) x; |
201 | 322 | double d = give_log ? |
202 | 96 | log(cwilcox(xx, mm, nn)) - lchoose(m + n, n) : |
203 | 322 | cwilcox(xx, mm, nn) / choose(m + n, n); |
204 | | |
205 | 322 | return(d); |
206 | 581 | } |
207 | | |
208 | | /* args have the same meaning as R function pwilcox */ |
209 | | double pwilcox(double q, double m, double n, int lower_tail, int log_p) |
210 | 1.28k | { |
211 | 1.28k | #ifdef IEEE_754 |
212 | 1.28k | if (ISNAN(q) || ISNAN(m) || ISNAN(n)) |
213 | 0 | return(q + m + n); |
214 | 1.28k | #endif |
215 | 1.28k | if (!R_FINITE(m) || !R_FINITE(n)) |
216 | 1.28k | ML_WARN_return_NAN; |
217 | 1.28k | m = R_forceint(m); |
218 | 1.28k | n = R_forceint(n); |
219 | 1.28k | if (m <= 0 || n <= 0) |
220 | 1.15k | ML_WARN_return_NAN; |
221 | | |
222 | 1.15k | q = floor(q + 1e-7); |
223 | | |
224 | 1.15k | if (q < 0.0) |
225 | 258 | return(R_DT_0); |
226 | 892 | if (q >= m * n) |
227 | 247 | return(R_DT_1); |
228 | | |
229 | 645 | if (m > INT_MAX) |
230 | 645 | MATHLIB_ERROR("m(%g) > INT_MAX", m); |
231 | 645 | if (n > INT_MAX) |
232 | 645 | MATHLIB_ERROR("n(%g) > INT_MAX", n); |
233 | 645 | int mm = (int) m, nn = (int) n; |
234 | 645 | double c = choose(m + n, n), |
235 | 645 | p = 0; |
236 | | /* Use summation of probs over the shorter range */ |
237 | 645 | if (q <= (m * n / 2)) { |
238 | 5.28k | for (int i = 0; i <= q; i++) |
239 | 4.71k | p += cwilcox(i, mm, nn) / c; |
240 | 568 | } |
241 | 77 | else { |
242 | 77 | q = m * n - q; |
243 | 2.55k | for (int i = 0; i < q; i++) |
244 | 2.47k | p += cwilcox(i, mm, nn) / c; |
245 | 77 | lower_tail = !lower_tail; /* p = 1 - p; */ |
246 | 77 | } |
247 | | |
248 | 645 | return(R_DT_val(p)); |
249 | 892 | } /* pwilcox */ |
250 | | |
251 | | /* x is 'p' in R function qwilcox */ |
252 | | |
253 | | double qwilcox(double x, double m, double n, int lower_tail, int log_p) |
254 | 1.44k | { |
255 | 1.44k | #ifdef IEEE_754 |
256 | 1.44k | if (ISNAN(x) || ISNAN(m) || ISNAN(n)) |
257 | 0 | return(x + m + n); |
258 | 1.44k | #endif |
259 | 1.44k | if(!R_FINITE(x) || !R_FINITE(m) || !R_FINITE(n)) |
260 | 1.43k | ML_WARN_return_NAN; |
261 | 1.43k | R_Q_P01_check(x); |
262 | | |
263 | 1.02k | m = R_forceint(m); |
264 | 1.02k | n = R_forceint(n); |
265 | 1.02k | if (m <= 0 || n <= 0) |
266 | 817 | ML_WARN_return_NAN; |
267 | | |
268 | 817 | if (x == R_DT_0) |
269 | 145 | return(0); |
270 | 672 | if (x == R_DT_1) |
271 | 113 | return(m * n); |
272 | | |
273 | 559 | if(log_p || !lower_tail) |
274 | 357 | x = R_DT_qIv(x); /* lower_tail,non-log "p" */ |
275 | | |
276 | 559 | if (m > INT_MAX) |
277 | 559 | MATHLIB_ERROR("m(%g) > INT_MAX", m); |
278 | 559 | if (n > INT_MAX) |
279 | 559 | MATHLIB_ERROR("n(%g) > INT_MAX", n); |
280 | 559 | int mm = (int) m, nn = (int) n; |
281 | 559 | double c = choose(m + n, n), |
282 | 559 | p = 0.; |
283 | 559 | int q = 0; |
284 | 559 | if (x <= 0.5) { |
285 | 339 | x = x - 10 * DBL_EPSILON; |
286 | 28.5k | for (;;) { |
287 | 28.5k | p += cwilcox(q, mm, nn) / c; |
288 | 28.5k | if (p >= x) |
289 | 339 | break; |
290 | 28.2k | q++; |
291 | 28.2k | } |
292 | 339 | } |
293 | 220 | else { |
294 | 220 | x = 1 - x + 10 * DBL_EPSILON; |
295 | 28.3k | for (;;) { |
296 | 28.3k | p += cwilcox(q, mm, nn) / c; |
297 | 28.3k | if (p > x) { |
298 | 220 | q = (int) (m * n - q); |
299 | 220 | break; |
300 | 220 | } |
301 | 28.1k | q++; |
302 | 28.1k | } |
303 | 220 | } |
304 | | |
305 | 559 | return(q); |
306 | 672 | } |
307 | | |
308 | | double rwilcox(double m, double n) |
309 | 0 | { |
310 | 0 | int i, j, k, *x; |
311 | 0 | double r; |
312 | |
|
313 | 0 | #ifdef IEEE_754 |
314 | | /* NaNs propagated correctly */ |
315 | 0 | if (ISNAN(m) || ISNAN(n)) |
316 | 0 | return(m + n); |
317 | 0 | #endif |
318 | 0 | m = R_forceint(m); |
319 | 0 | n = R_forceint(n); |
320 | 0 | if ((m < 0) || (n < 0)) |
321 | 0 | ML_WARN_return_NAN; |
322 | |
|
323 | 0 | if ((m == 0) || (n == 0)) |
324 | 0 | return(0); |
325 | | |
326 | 0 | r = 0.0; |
327 | 0 | if ((m + n) > INT_MAX) |
328 | 0 | MATHLIB_ERROR("m+n(%g) > INT_MAX", m + n); |
329 | 0 | k = (int) (m + n); |
330 | 0 | x = (int *) calloc((size_t) k, sizeof(int)); |
331 | | #ifdef MATHLIB_STANDALONE |
332 | | if (!x) MATHLIB_ERROR(_("wilcox allocation error %d"), 4); |
333 | | #endif |
334 | 0 | for (i = 0; i < k; i++) |
335 | 0 | x[i] = i; |
336 | 0 | for (i = 0; i < n; i++) { |
337 | 0 | j = (int) R_unif_index(k); |
338 | 0 | r += x[j]; |
339 | 0 | x[j] = x[--k]; |
340 | 0 | } |
341 | 0 | free(x); |
342 | 0 | return(r - n * (n - 1) / 2); |
343 | 0 | } |
344 | | |
345 | | void wilcox_free(void) |
346 | 325 | { |
347 | 325 | free(w); |
348 | 325 | free(sigma); |
349 | 325 | w = NULL; sigma = NULL; |
350 | 325 | allocated_m = allocated_n = 0; |
351 | 325 | max_k = -1; |
352 | 325 | } |