File: wilcox.java

package info (click to toggle)
libdistlib-java 1.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 664 kB
  • sloc: java: 4,420; sh: 533; xml: 38; makefile: 3
file content (214 lines) | stat: -rwxr-xr-x 6,068 bytes parent folder | download | duplicates (4)
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
/*
 *  DistLib : A C Library of Special Functions
 *  Copyright (C) 1998 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * 
 * data translated from C using perl script translate.pl
 * script version 0.00
 */

package DistLib;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Wrapper of functions for Wilcoxon distribution.
 * <p>
 * This actually the Mann-Whitney Ux statistic.
 */

public class wilcox {
  private static Log log = LogFactory.getLog(wilcox.class);

  public static final int  WILCOX_MMAX = 50; 
  public static final int  WILCOX_NMAX = 50; 

  /**
   * check values for too large and log complaint
   */
  private static boolean checkSizesLarge(final double m, final double n) {
    if (m >= WILCOX_MMAX) {
      log.info("m should be less than %d\n"+ WILCOX_MMAX);
      return false;
    }
    if (n >= WILCOX_NMAX) {
      log.info("n should be less than %d\n"+ WILCOX_NMAX);
      return false;
    }
    return true;    
  }
  
  /**
   * round sizes to integer
   */
  private static void roundSizes(double m, double n) {
    m = Math.floor(m + 0.5);
    n = Math.floor(n + 0.5);
  }

  // table of exact cumulative probabilities
  static private double w[][][] = new double[WILCOX_MMAX][WILCOX_NMAX][];

  /**
   *    The density of the Wilcoxon distribution.
   */
  static private double cwilcox(int k, int m, int n) {
    int u = m * n;
    int c = (int)(u / 2);

    if ((k < 0) || (k > u)) return(0);
    if (k > c) k = u - k;
    int i = m;
    int j = n;
    if (m >= n) {
      i = n;
      j = m;
    }
    if (w[i][j] == null) {
      w[i][j] = new double[c + 1];
      for (int l = 0; l <= c; l++)
        w[i][j][l] = -1;
    }
    if (w[i][j][k] < 0) {
      if ((i == 0) || (j == 0))
        w[i][j][k] = (k == 0)?1.0:0.0;
      else
        w[i][j][k] = cwilcox(k - n, m - 1, n) + cwilcox(k, m, n - 1);
    }
    return(w[i][j][k]);
  }

  /**
   * density function
   * @param x
   * @param m
   * @param n
   * @return density
   */
  public static double density(double x, double m, double n) {
    /*!* #ifdef IEEE_754 /*4!*/
    /* NaNs propagated correctly */
    if (Double.isNaN(x) || Double.isNaN(m) || Double.isNaN(n)) return x + m + n;
    /*!* #endif /*4!*/
    roundSizes(m,n);
    if (m <= 0 || n <= 0) {
      throw new java.lang.ArithmeticException("Math Error: DOMAIN");
      //        return Double.NaN;
    }
    if (!checkSizesLarge(m,n)) return Double.NaN;
    
    /*!*   x = floor(x + 0.5); *!*/
    x = java.lang.Math.floor(x + 0.5);
    if ((x < 0) || (x > m * n))
      return 0;
    /*!*   return(cwilcox(x, m, n) / choose(m + n, n)); *!*/
    return(cwilcox((int) x, (int) m, (int) n) / misc.choose(m + n, n));
  }

  /**
   * Cumulative distribution function of the Wilcoxon distribution.
   */
  public static double cumulative(double x, double m, double n) {

    /*!* #ifdef IEEE_754 /*4!*/
    if (Double.isNaN(x) || Double.isNaN(m) || Double.isNaN(n))
      return x + m + n;
    if (Double.isInfinite(m) || Double.isInfinite(n)) {
      throw new java.lang.ArithmeticException("Math Error: DOMAIN");
      //        return Double.NaN;
    }
    /*!* #endif /*4!*/
    roundSizes(m,n);
    if (m <= 0 || n <= 0) {
      throw new java.lang.ArithmeticException("Math Error: DOMAIN");
      //        return Double.NaN;
    }
    if (!checkSizesLarge(m,n)) return Double.NaN;
    /*!*   x = floor(x + 0.5); *!*/
    x = java.lang.Math.floor(x + 0.5);
    if (x < 0.0) return 0;
    if (x >= m * n) return 1;
    double p = 0.0;
    for (int i = 0; i <= x; i++)
      p += density(i, m, n);
    return(p);
  }

  /**
   * The quantile function of the Wilcoxon distribution.
   */
  public static double  quantile(double x, double m, double n) {

    /*!* #ifdef IEEE_754 /*4!*/
    if (Double.isNaN(x) || Double.isNaN(m) || Double.isNaN(n))
      return x + m + n;
    if(Double.isInfinite(x) || Double.isInfinite(m) || Double.isInfinite(n)) {
      throw new java.lang.ArithmeticException("Math Error: DOMAIN");
      //    	return Double.NaN;
    }
    /*!* #endif /*4!*/

    roundSizes(m,n);
    if (x < 0 || x > 1 || m <= 0 || n <= 0) {
      throw new java.lang.ArithmeticException("Math Error: DOMAIN");
      //    	return Double.NaN;
    };
    if (!checkSizesLarge(m,n)) return Double.NaN;

    if (x == 0) return(0.0);
    if (x == 1) return(m * n);
    double p = 0.0;
    double q = 0.0;
    for (;;) {
      /* Don't call cumulative() for efficiency */
      p += density(q, m, n);
      if (p >= x)
        return(q);
      q++;
    }
  }

  /**
   *    Random variates from the Wilcoxon distribution.
   */
  public static double random(double m, double n) {

    /*!* #ifdef IEEE_754 /*4!*/
    /* NaNs propagated correctly */
    if (Double.isNaN(m) || Double.isNaN(n)) return(m + n);
    /*!* #endif /*4!*/
    roundSizes(m,n);
    if ((m < 0) || (n < 0)) {
      throw new java.lang.ArithmeticException("Math Error: DOMAIN");
      //        return Double.NaN;
    }
    if ((m == 0) || (n == 0))
      return(0);
    double r = 0.0;
    int k = (int) (m + n);
    int[] x = new int[k];
    for (int i = 0; i < k; i++)
      x[i] = i;
    for (int i = 0; i < n; i++) {
      /*!*     j = floor(k * sunif()); *!*/
      int j = (int) java.lang.Math.floor(k * uniform.random());
      r += x[j];
      x[j] = x[--k];
    }
    return(r - n * (n - 1) / 2);
  }
}