5 // Created by SarahsWork on 8/6/13.
6 // Copyright (c) 2013 Schloss Lab. All rights reserved.
8 // Modified to avoid using Rmath.h
13 Mathlib : A C Library of Special Functions
14 Copyright (C) 1999-2012 The R Core Team
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or (at
19 your option) any later version.
21 This program is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, a copy is available at
28 http://www.r-project.org/Licenses/
33 double dwilcox(double x, double m, double n, int give_log)
34 double pwilcox(double x, double m, double n, int lower_tail, int log_p)
35 double qwilcox(double x, double m, double n, int lower_tail, int log_p);
36 double rwilcox(double m, double n)
40 dwilcox The density of the Wilcoxon distribution.
41 pwilcox The distribution function of the Wilcoxon distribution.
42 qwilcox The quantile function of the Wilcoxon distribution.
43 rwilcox Random variates from the Wilcoxon distribution.
48 Note: the checks here for R_CheckInterrupt also do stack checking.
50 calloc/free are remapped for use in R, so allocation checks are done there.
51 freeing is completed by an on.exit action in the R wrappers.
56 #define give_log log_p
57 #define R_D__0 (log_p ? 1 : 0.) /* 0 */
58 #define R_D__1 (log_p ? 0. : 1.) /* 1 */
59 #define R_DT_0 (lower_tail ? R_D__0 : R_D__1) /* 0 */
60 #define R_DT_1 (lower_tail ? R_D__1 : R_D__0) /* 1 */
61 #define R_D_val(x) (log_p ? log(x) : (x)) /* x in pF(x,..) */
62 #define R_D_Clog(p) (log_p ? log1p(-(p)) : (0.5 - (p) + 0.5)) /* [log](1-p) */
63 #define R_DT_val(x) (lower_tail ? R_D_val(x) : R_D_Clog(x))
65 /*********************************************************************************************************************************/
66 //Numerical Recipes pg. 219
67 double PWilcox::gammln(const double xx) {
71 static const double cof[6]={76.18009172947146,-86.50532032941677,24.01409824083091,
72 -1.231739572450155,0.120858003e-2,-0.536382e-5};
76 tmp -= (x+0.5)*log(tmp);
81 return -tmp+log(2.5066282746310005*ser/x);
84 mout->errorOut(e, "PWilcox", "gammln");
88 /*********************************************************************************************************************************/
89 double PWilcox::choose(double n, double k){
94 double lchoose = gammln(n + 1.0) - gammln(k + 1.0) - gammln(n - k + 1.0);
96 return (floor(exp(lchoose) + 0.5));
99 mout->errorOut(e, "PWilcox", "choose");
103 /*********************************************************************************************************************************/
105 /* This counts the number of choices with statistic = k */
106 double PWilcox::cwilcox(int k, int m, int n, double*** w) {
110 if (mout->control_pressed) { return 0; }
117 k = u - k; /* hence k <= floor(u / 2) */
124 if (j == 0) /* and hence i == 0 */
128 /* We can simplify things if k is small. Consider the Mann-Whitney
129 definition, and sort y. Then if the statistic is k, no more
130 than k of the y's can be <= any x[i], and since they are sorted
131 these can only be in the first k. So the count is the same as
132 if there were just k y's.
134 if (j > 0 && k < j) return cwilcox(k, i, k, w);
137 w[i][j] = (double *) calloc((size_t) c + 1, sizeof(double));
139 for (l = 0; l <= c; l++)
142 if (w[i][j][k] < 0) {
143 if (j == 0) /* and hence i == 0 */
144 w[i][j][k] = (k == 0);
146 w[i][j][k] = cwilcox(k - j, i - 1, j, w) + cwilcox(k, i, j - 1, w);
151 catch(exception& e) {
152 mout->errorOut(e, "PWilcox", "cwilcox");
156 /*********************************************************************************************************************************/
158 /* args have the same meaning as R function pwilcox */
159 double PWilcox::pwilcox(double q, double m, double n, bool lower_tail){
167 if (isnan(m) || isnan(n))
171 if (m <= 0 || n <= 0) {return 0;}
180 int mm = (int) m, nn = (int) n;
182 if (mout->control_pressed) { return 0; }
183 //w_init_maybe(mm, nn);
184 /********************************************/
186 if (mm > nn) { thisi = nn; nn = mm; mm = thisi; }
190 w = (double ***) calloc((size_t) mm + 1, sizeof(double **));
192 for (thisi = 0; thisi <= mm; thisi++) {
193 w[thisi] = (double **) calloc((size_t) nn + 1, sizeof(double *));
195 allocated_m = m; allocated_n = n;
196 /********************************************/
198 c = choose(m + n, n);
200 /* Use summation of probs over the shorter range */
201 if (q <= (m * n / 2)) {
202 for (i = 0; i <= q; i++)
203 p += cwilcox(i, m, n, w) / c;
207 for (i = 0; i < q; i++) {
208 p += cwilcox(i, m, n, w) / c;
210 lower_tail = !lower_tail; /* p = 1 - p; */
214 /********************************************/
215 for (int i = allocated_m; i >= 0; i--) {
216 for (int j = allocated_n; j >= 0; j--) {
218 free((void *) w[i][j]);
223 w = 0; allocated_m = allocated_n = 0;
224 /********************************************/
228 catch(exception& e) {
229 mout->errorOut(e, "PWilcox", "pwilcox");