File: wilcox.cpp

package info (click to toggle)
mothur 1.48.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,692 kB
  • sloc: cpp: 161,866; makefile: 122; sh: 31
file content (233 lines) | stat: -rwxr-xr-x 7,483 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//
//  wilcox.cpp
//  Mothur
//
//  Created by SarahsWork on 8/6/13.
//  Copyright (c) 2013 Schloss Lab. All rights reserved.
//
//  Modified to avoid using Rmath.h 

#include "mothur.h"

/*
 Mathlib : A C Library of Special Functions
 Copyright (C) 1999-2012  The R Core Team
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at
 your option) any later version.
 
 This program is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the GNU
 General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, a copy is available at
 http://www.r-project.org/Licenses/
 
 SYNOPSIS
 
 #include <Rmath.h>
 double dwilcox(double x, double m, double n, int give_log)
 double pwilcox(double x, double m, double n, int lower_tail, int log_p)
 double qwilcox(double x, double m, double n, int lower_tail, int log_p);
 double rwilcox(double m, double n)
 
 DESCRIPTION
 
 dwilcox	The density of the Wilcoxon distribution.
 pwilcox	The distribution function of the Wilcoxon distribution.
 qwilcox	The quantile function of the Wilcoxon distribution.
 rwilcox	Random variates from the Wilcoxon distribution.
 
 */

/*
 Note: the checks here for R_CheckInterrupt also do stack checking.
 
 calloc/free are remapped for use in R, so allocation checks are done there.
 freeing is completed by an on.exit action in the R wrappers.
 */
#include "wilcox.h"

#define WILCOX_MAX 50
#define give_log log_p
#define R_D__0	(log_p ? 1 : 0.)		/* 0 */
#define R_D__1	(log_p ? 0. : 1.)			/* 1 */
#define R_DT_0	(lower_tail ? R_D__0 : R_D__1)		/* 0 */
#define R_DT_1	(lower_tail ? R_D__1 : R_D__0)		/* 1 */
#define R_D_val(x)	(log_p	? log(x) : (x))		/*  x  in pF(x,..) */
#define R_D_Clog(p)	(log_p	? log1p(-(p)) : (0.5 - (p) + 0.5)) /* [log](1-p) */
#define R_DT_val(x)	(lower_tail ? R_D_val(x)  : R_D_Clog(x))

/*********************************************************************************************************************************/
//Numerical Recipes pg. 219
double PWilcox::gammln(const double xx) {
    try {
        int j;
        double x,y,tmp,ser;
        static const double cof[6]={76.18009172947146,-86.50532032941677,24.01409824083091,
            -1.231739572450155,0.120858003e-2,-0.536382e-5};
        
        y=x=xx;
        tmp=x+5.5;
        tmp -= (x+0.5)*log(tmp);
        ser=1.0;
        for (j=0;j<6;j++) {
            ser += cof[j]/++y;
        }
        return -tmp+log(2.5066282746310005*ser/x);
    }
    catch(exception& e) {
        mout->errorOut(e, "PWilcox", "gammln");
        exit(1);
    }
}
/*********************************************************************************************************************************/
double PWilcox::choose(double n, double k){
    try {
        n = floor(n + 0.5);
        k = floor(k + 0.5);
        
        double lchoose = gammln(n + 1.0) - gammln(k + 1.0) - gammln(n - k + 1.0);
        
        return (floor(exp(lchoose) + 0.5));
    }
    catch(exception& e) {
        mout->errorOut(e, "PWilcox", "choose");
        exit(1);
    }
}
/*********************************************************************************************************************************/

/* This counts the number of choices with statistic = k */
double PWilcox::cwilcox(int k, int m, int n, double*** w) {
    try {
        int c, u, i, j, l;
        
        if (mout->getControl_pressed()) { return 0; }
        
        u = m * n;
        if (k < 0 || k > u)
            return(0);
        c = (int)(u / 2);
        if (k > c)
            k = u - k; /* hence  k <= floor(u / 2) */
        if (m < n) {
            i = m; j = n;
        } else {
            i = n; j = m;
        } /* hence  i <= j */
        
        if (j == 0) /* and hence i == 0 */
            return (k == 0);
        
        
        /* We can simplify things if k is small.  Consider the Mann-Whitney
         definition, and sort y.  Then if the statistic is k, no more
         than k of the y's can be <= any x[i], and since they are sorted
         these can only be in the first k.  So the count is the same as
         if there were just k y's.
         */
        if (j > 0 && k < j) return cwilcox(k, i, k, w);
        Utils util;
        if (w[i][j] == nullptr) {
            w[i][j] = (double *) calloc((size_t) c + 1, sizeof(double));

            for (l = 0; l <= c; l++)
                w[i][j][l] = -1;
        }
        if (w[i][j][k] < 0) {
            if (j == 0) /* and hence i == 0 */
                w[i][j][k] = (k == 0);
            else
                w[i][j][k] = cwilcox(k - j, i - 1, j, w) + cwilcox(k, i, j - 1, w);
            
        }
        return(w[i][j][k]);
    }
    catch(exception& e) {
        mout->errorOut(e, "PWilcox", "cwilcox");
        exit(1);
    }
}
/*********************************************************************************************************************************/

/* args have the same meaning as R function pwilcox */
double PWilcox::pwilcox(double q, double m, double n, bool lower_tail){
    try {
        int i;
        double c, p;
        bool log_p = false;
        double*** w; w = nullptr;
        

        if (isnan(m) || isnan(n))
        {return 0;}
        m = floor(m + 0.5);
        n = floor(n + 0.5);
        if (m <= 0 || n <= 0) {return 0;}
        
        q = floor(q + 1e-7);
        
        if (q < 0.0)
            return(R_DT_0);
        if (q >= m * n)
            return(R_DT_1);
        
        int mm = (int) m, nn = (int) n;
        
        if (mout->getControl_pressed()) { return 0; }
        //w_init_maybe(mm, nn);
        /********************************************/
        int thisi;
        if (mm > nn) { thisi = nn; nn = mm; mm = thisi; }
        
        mm = max(mm, 50);
        nn = max(nn, 50);
        w = (double ***) calloc((size_t) mm + 1, sizeof(double **));
            
        for (thisi = 0; thisi <= mm; thisi++) {
            w[thisi] = (double **) calloc((size_t) nn + 1, sizeof(double *));
        }
        allocated_m = m; allocated_n = n;
        /********************************************/    
        
        c = choose(m + n, n);
        p = 0;
        /* Use summation of probs over the shorter range */
        if (q <= (m * n / 2)) {
            for (i = 0; i <= q; i++)
                p += cwilcox(i, m, n, w) / c;
        }
        else {
            q = m * n - q;
            for (i = 0; i < q; i++) {
                p += cwilcox(i, m, n, w) / c;
            }
            lower_tail = !lower_tail; /* p = 1 - p; */
        }
        Utils util;
        //free w
        /********************************************/
        for (int i = allocated_m; i >= 0; i--) {
            for (int j = allocated_n; j >= 0; j--) {
                if (w[i][j] != nullptr)
                    free((void *) w[i][j]);
            }
            free((void *) w[i]);
        }
        free((void *) w);
        w = 0; allocated_m = allocated_n = 0;
        /********************************************/
        
        return(R_DT_val(p));
    }
    catch(exception& e) {
        mout->errorOut(e, "PWilcox", "pwilcox");
        exit(1);
    }
} /* pwilcox */