Coverage Report

Created: 2026-11-30 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/r-source/src/nmath/signrank.c
Line
Count
Source
1
/*
2
 *  Mathlib : A C Library of Special Functions
3
 *  Copyright (C) 1999-2024  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
 *    double dsignrank(double x, double n, int give_log)
23
 *    double psignrank(double x, double n, int lower_tail, int log_p)
24
 *    double qsignrank(double x, double n, int lower_tail, int log_p)
25
 *    double rsignrank(double n)
26
 *
27
 *  DESCRIPTION
28
 *
29
 *    dsignrank    The density of the Wilcoxon Signed Rank distribution.
30
 *    psignrank    The distribution function of the Wilcoxon Signed Rank
31
 *       distribution.
32
 *    qsignrank    The quantile function of the Wilcoxon Signed Rank
33
 *       distribution.
34
 *    rsignrank    Random variates from the Wilcoxon Signed Rank
35
 *       distribution.
36
 */
37
38
#include "nmath.h"
39
#include "dpq.h"
40
41
static double *w;
42
static int allocated_n;
43
44
static void
45
w_free(void)
46
272
{
47
272
    if (!w) return;
48
49
185
    free((void *) w);
50
185
    w = 0;
51
185
    allocated_n = 0;
52
185
}
53
54
void signrank_free(void)
55
272
{
56
272
    w_free();
57
272
}
58
59
static void
60
w_init_maybe(int n)
61
1.14k
{
62
1.14k
    int u, c;
63
64
1.14k
    u = n * (n + 1) / 2;
65
1.14k
    c = (u / 2);
66
67
1.14k
    if (w) {
68
957
        if(n != allocated_n) {
69
0
      w_free();
70
0
  }
71
957
  else return;
72
957
    }
73
74
185
    if(!w) {
75
185
  w = (double *) calloc((size_t) c + 1, sizeof(double));
76
#ifdef MATHLIB_STANDALONE
77
  if (!w) MATHLIB_ERROR("%s", _("signrank allocation error"));
78
#endif
79
185
  allocated_n = n;
80
185
    }
81
185
}
82
83
static double
84
csignrank(int k, int n)
85
647k
{
86
647k
    int c, u, j;
87
88
647k
#ifndef MATHLIB_STANDALONE
89
647k
    R_CheckUserInterrupt();
90
647k
#endif
91
92
647k
    u = n * (n + 1) / 2;
93
647k
    c = (u / 2);
94
95
647k
    if (k < 0 || k > u)
96
0
  return 0;
97
647k
    if (k > c)
98
16
  k = u - k;
99
100
647k
    if (n == 1)
101
341
        return 1.;
102
647k
    if (w[0] == 1.)
103
647k
        return w[k];
104
105
149
    w[0] = w[1] = 1.;
106
8.43k
    for(j = 2; j < n+1; ++j) {
107
8.28k
        int i, end = imin2(j*(j+1)/2, c);
108
380M
  for(i = end; i >= j; --i)
109
379M
      w[i] += w[i-j];
110
8.28k
    }
111
112
149
    return w[k];
113
647k
}
114
115
double dsignrank(double x, double n, int give_log)
116
716
{
117
716
    double d;
118
119
716
#ifdef IEEE_754
120
    /* NaNs propagated correctly */
121
716
    if (ISNAN(x) || ISNAN(n)) return(x + n);
122
716
#endif
123
716
    n = R_forceint(n);
124
716
    if (n <= 0)
125
591
  ML_WARN_return_NAN;
126
127
591
    if (R_nonint(x))
128
140
  return(R_D__0);
129
451
    x = R_forceint(x);
130
451
    if ((x < 0) || (x > (n * (n + 1) / 2)))
131
211
  return(R_D__0);
132
133
240
    int nn = (int) n;
134
240
    w_init_maybe(nn);
135
240
    d = R_D_exp(log(csignrank((int) x, nn)) - n * M_LN2);
136
137
240
    return(d);
138
451
}
139
140
double psignrank(double x, double n, int lower_tail, int log_p)
141
1.07k
{
142
1.07k
    int i;
143
1.07k
    double f, p;
144
145
1.07k
#ifdef IEEE_754
146
1.07k
    if (ISNAN(x) || ISNAN(n))
147
0
    return(x + n);
148
1.07k
#endif
149
1.07k
    if (!R_FINITE(n)) ML_WARN_return_NAN;
150
1.07k
    n = R_forceint(n);
151
1.07k
    if (n <= 0) ML_WARN_return_NAN;
152
153
977
    x = floor(x + 1e-7);
154
977
    if (x < 0.0)
155
237
  return(R_DT_0);
156
740
    if (x >= n * (n + 1) / 2)
157
227
  return(R_DT_1);
158
159
513
    int nn = (int) n;
160
513
    w_init_maybe(nn);
161
513
    f = exp(- n * M_LN2);
162
513
    p = 0;
163
513
    if (x <= (n * (n + 1) / 4)) {
164
1.40k
  for (i = 0; i <= x; i++)
165
940
      p += csignrank(i, nn) * f;
166
467
    }
167
46
    else {
168
46
  x = n * (n + 1) / 2 - x;
169
1.14k
  for (i = 0; i < x; i++)
170
1.09k
      p += csignrank(i, nn) * f;
171
46
  lower_tail = !lower_tail; /* p = 1 - p; */
172
46
    }
173
174
513
    return(R_DT_val(p));
175
740
} /* psignrank() */
176
177
double qsignrank(double x, double n, int lower_tail, int log_p)
178
1.02k
{
179
1.02k
    double f, p;
180
181
1.02k
#ifdef IEEE_754
182
1.02k
    if (ISNAN(x) || ISNAN(n))
183
0
  return(x + n);
184
1.02k
#endif
185
1.02k
    if (!R_FINITE(x) || !R_FINITE(n))
186
1.01k
  ML_WARN_return_NAN;
187
1.01k
    R_Q_P01_check(x);
188
189
698
    n = R_forceint(n);
190
698
    if (n <= 0)
191
565
  ML_WARN_return_NAN;
192
193
565
    if (x == R_DT_0)
194
80
  return(0);
195
485
    if (x == R_DT_1)
196
96
  return(n * (n + 1) / 2);
197
198
389
    if(log_p || !lower_tail)
199
292
  x = R_DT_qIv(x); /* lower_tail,non-log "p" */
200
201
389
    int nn = (int) n;
202
389
    w_init_maybe(nn);
203
389
    f = exp(- n * M_LN2);
204
389
    p = 0;
205
389
    int q = 0;
206
389
    if (x <= 0.5) {
207
200
  x = x - 10 * DBL_EPSILON;
208
247k
  for (;;) {
209
247k
      p += csignrank(q, nn) * f;
210
247k
      if (p >= x)
211
200
    break;
212
246k
      q++;
213
246k
  }
214
200
    }
215
189
    else {
216
189
  x = 1 - x + 10 * DBL_EPSILON;
217
398k
  for (;;) {
218
398k
      p += csignrank(q, nn) * f;
219
398k
      if (p > x) {
220
189
    q = (int)(n * (n + 1) / 2 - q);
221
189
    break;
222
189
      }
223
397k
      q++;
224
397k
  }
225
189
    }
226
227
389
    return(q);
228
485
}
229
230
double rsignrank(double n)
231
0
{
232
0
    int i, k;
233
0
    double r;
234
235
0
#ifdef IEEE_754
236
    /* NaNs propagated correctly */
237
0
    if (ISNAN(n)) return(n);
238
0
#endif
239
0
    n = R_forceint(n);
240
0
    if (n < 0) ML_WARN_return_NAN;
241
242
0
    if (n == 0)
243
0
  return(0);
244
0
    r = 0.0;
245
0
    k = (int) n;
246
0
    for (i = 0; i < k; ) {
247
0
  r += (++i) * floor(unif_rand() + 0.5);
248
0
    }
249
0
    return(r);
250
0
}