001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.stat.inference;
018
019import org.apache.commons.math3.distribution.NormalDistribution;
020import org.apache.commons.math3.exception.ConvergenceException;
021import org.apache.commons.math3.exception.DimensionMismatchException;
022import org.apache.commons.math3.exception.MaxCountExceededException;
023import org.apache.commons.math3.exception.NoDataException;
024import org.apache.commons.math3.exception.NullArgumentException;
025import org.apache.commons.math3.exception.NumberIsTooLargeException;
026import org.apache.commons.math3.stat.ranking.NaNStrategy;
027import org.apache.commons.math3.stat.ranking.NaturalRanking;
028import org.apache.commons.math3.stat.ranking.TiesStrategy;
029import org.apache.commons.math3.util.FastMath;
030
031/**
032 * An implementation of the Wilcoxon signed-rank test.
033 *
034 */
035public class WilcoxonSignedRankTest {
036
037    /** Ranking algorithm. */
038    private NaturalRanking naturalRanking;
039
040    /**
041     * Create a test instance where NaN's are left in place and ties get
042     * the average of applicable ranks. Use this unless you are very sure
043     * of what you are doing.
044     */
045    public WilcoxonSignedRankTest() {
046        naturalRanking = new NaturalRanking(NaNStrategy.FIXED,
047                TiesStrategy.AVERAGE);
048    }
049
050    /**
051     * Create a test instance using the given strategies for NaN's and ties.
052     * Only use this if you are sure of what you are doing.
053     *
054     * @param nanStrategy
055     *            specifies the strategy that should be used for Double.NaN's
056     * @param tiesStrategy
057     *            specifies the strategy that should be used for ties
058     */
059    public WilcoxonSignedRankTest(final NaNStrategy nanStrategy,
060                                  final TiesStrategy tiesStrategy) {
061        naturalRanking = new NaturalRanking(nanStrategy, tiesStrategy);
062    }
063
064    /**
065     * Ensures that the provided arrays fulfills the assumptions.
066     *
067     * @param x first sample
068     * @param y second sample
069     * @throws NullArgumentException if {@code x} or {@code y} are {@code null}.
070     * @throws NoDataException if {@code x} or {@code y} are zero-length.
071     * @throws DimensionMismatchException if {@code x} and {@code y} do not
072     * have the same length.
073     */
074    private void ensureDataConformance(final double[] x, final double[] y)
075        throws NullArgumentException, NoDataException, DimensionMismatchException {
076
077        if (x == null ||
078            y == null) {
079                throw new NullArgumentException();
080        }
081        if (x.length == 0 ||
082            y.length == 0) {
083            throw new NoDataException();
084        }
085        if (y.length != x.length) {
086            throw new DimensionMismatchException(y.length, x.length);
087        }
088    }
089
090    /**
091     * Calculates y[i] - x[i] for all i
092     *
093     * @param x first sample
094     * @param y second sample
095     * @return z = y - x
096     */
097    private double[] calculateDifferences(final double[] x, final double[] y) {
098
099        final double[] z = new double[x.length];
100
101        for (int i = 0; i < x.length; ++i) {
102            z[i] = y[i] - x[i];
103        }
104
105        return z;
106    }
107
108    /**
109     * Calculates |z[i]| for all i
110     *
111     * @param z sample
112     * @return |z|
113     * @throws NullArgumentException if {@code z} is {@code null}
114     * @throws NoDataException if {@code z} is zero-length.
115     */
116    private double[] calculateAbsoluteDifferences(final double[] z)
117        throws NullArgumentException, NoDataException {
118
119        if (z == null) {
120            throw new NullArgumentException();
121        }
122
123        if (z.length == 0) {
124            throw new NoDataException();
125        }
126
127        final double[] zAbs = new double[z.length];
128
129        for (int i = 0; i < z.length; ++i) {
130            zAbs[i] = FastMath.abs(z[i]);
131        }
132
133        return zAbs;
134    }
135
136    /**
137     * Computes the <a
138     * href="http://en.wikipedia.org/wiki/Wilcoxon_signed-rank_test">
139     * Wilcoxon signed ranked statistic</a> comparing mean for two related
140     * samples or repeated measurements on a single sample.
141     * <p>
142     * This statistic can be used to perform a Wilcoxon signed ranked test
143     * evaluating the null hypothesis that the two related samples or repeated
144     * measurements on a single sample has equal mean.
145     * </p>
146     * <p>
147     * Let X<sub>i</sub> denote the i'th individual of the first sample and
148     * Y<sub>i</sub> the related i'th individual in the second sample. Let
149     * Z<sub>i</sub> = Y<sub>i</sub> - X<sub>i</sub>.
150     * </p>
151     * <p>
152     * <strong>Preconditions</strong>:
153     * <ul>
154     * <li>The differences Z<sub>i</sub> must be independent.</li>
155     * <li>Each Z<sub>i</sub> comes from a continuous population (they must be
156     * identical) and is symmetric about a common median.</li>
157     * <li>The values that X<sub>i</sub> and Y<sub>i</sub> represent are
158     * ordered, so the comparisons greater than, less than, and equal to are
159     * meaningful.</li>
160     * </ul>
161     * </p>
162     *
163     * @param x the first sample
164     * @param y the second sample
165     * @return wilcoxonSignedRank statistic (the larger of W+ and W-)
166     * @throws NullArgumentException if {@code x} or {@code y} are {@code null}.
167     * @throws NoDataException if {@code x} or {@code y} are zero-length.
168     * @throws DimensionMismatchException if {@code x} and {@code y} do not
169     * have the same length.
170     */
171    public double wilcoxonSignedRank(final double[] x, final double[] y)
172        throws NullArgumentException, NoDataException, DimensionMismatchException {
173
174        ensureDataConformance(x, y);
175
176        // throws IllegalArgumentException if x and y are not correctly
177        // specified
178        final double[] z = calculateDifferences(x, y);
179        final double[] zAbs = calculateAbsoluteDifferences(z);
180
181        final double[] ranks = naturalRanking.rank(zAbs);
182
183        double Wplus = 0;
184
185        for (int i = 0; i < z.length; ++i) {
186            if (z[i] > 0) {
187                Wplus += ranks[i];
188            }
189        }
190
191        final int N = x.length;
192        final double Wminus = (((double) (N * (N + 1))) / 2.0) - Wplus;
193
194        return FastMath.max(Wplus, Wminus);
195    }
196
197    /**
198     * Algorithm inspired by
199     * http://www.fon.hum.uva.nl/Service/Statistics/Signed_Rank_Algorihms.html#C
200     * by Rob van Son, Institute of Phonetic Sciences & IFOTT,
201     * University of Amsterdam
202     *
203     * @param Wmax largest Wilcoxon signed rank value
204     * @param N number of subjects (corresponding to x.length)
205     * @return two-sided exact p-value
206     */
207    private double calculateExactPValue(final double Wmax, final int N) {
208
209        // Total number of outcomes (equal to 2^N but a lot faster)
210        final int m = 1 << N;
211
212        int largerRankSums = 0;
213
214        for (int i = 0; i < m; ++i) {
215            int rankSum = 0;
216
217            // Generate all possible rank sums
218            for (int j = 0; j < N; ++j) {
219
220                // (i >> j) & 1 extract i's j-th bit from the right
221                if (((i >> j) & 1) == 1) {
222                    rankSum += j + 1;
223                }
224            }
225
226            if (rankSum >= Wmax) {
227                ++largerRankSums;
228            }
229        }
230
231        /*
232         * largerRankSums / m gives the one-sided p-value, so it's multiplied
233         * with 2 to get the two-sided p-value
234         */
235        return 2 * ((double) largerRankSums) / ((double) m);
236    }
237
238    /**
239     * @param Wmin smallest Wilcoxon signed rank value
240     * @param N number of subjects (corresponding to x.length)
241     * @return two-sided asymptotic p-value
242     */
243    private double calculateAsymptoticPValue(final double Wmin, final int N) {
244
245        final double ES = (double) (N * (N + 1)) / 4.0;
246
247        /* Same as (but saves computations):
248         * final double VarW = ((double) (N * (N + 1) * (2*N + 1))) / 24;
249         */
250        final double VarS = ES * ((double) (2 * N + 1) / 6.0);
251
252        // - 0.5 is a continuity correction
253        final double z = (Wmin - ES - 0.5) / FastMath.sqrt(VarS);
254
255        // No try-catch or advertised exception because args are valid
256        // pass a null rng to avoid unneeded overhead as we will not sample from this distribution
257        final NormalDistribution standardNormal = new NormalDistribution(null, 0, 1);
258
259        return 2*standardNormal.cumulativeProbability(z);
260    }
261
262    /**
263     * Returns the <i>observed significance level</i>, or <a href=
264     * "http://www.cas.lancs.ac.uk/glossary_v1.1/hyptest.html#pvalue">
265     * p-value</a>, associated with a <a
266     * href="http://en.wikipedia.org/wiki/Wilcoxon_signed-rank_test">
267     * Wilcoxon signed ranked statistic</a> comparing mean for two related
268     * samples or repeated measurements on a single sample.
269     * <p>
270     * Let X<sub>i</sub> denote the i'th individual of the first sample and
271     * Y<sub>i</sub> the related i'th individual in the second sample. Let
272     * Z<sub>i</sub> = Y<sub>i</sub> - X<sub>i</sub>.
273     * </p>
274     * <p>
275     * <strong>Preconditions</strong>:
276     * <ul>
277     * <li>The differences Z<sub>i</sub> must be independent.</li>
278     * <li>Each Z<sub>i</sub> comes from a continuous population (they must be
279     * identical) and is symmetric about a common median.</li>
280     * <li>The values that X<sub>i</sub> and Y<sub>i</sub> represent are
281     * ordered, so the comparisons greater than, less than, and equal to are
282     * meaningful.</li>
283     * </ul>
284     * </p>
285     *
286     * @param x the first sample
287     * @param y the second sample
288     * @param exactPValue
289     *            if the exact p-value is wanted (only works for x.length <= 30,
290     *            if true and x.length > 30, this is ignored because
291     *            calculations may take too long)
292     * @return p-value
293     * @throws NullArgumentException if {@code x} or {@code y} are {@code null}.
294     * @throws NoDataException if {@code x} or {@code y} are zero-length.
295     * @throws DimensionMismatchException if {@code x} and {@code y} do not
296     * have the same length.
297     * @throws NumberIsTooLargeException if {@code exactPValue} is {@code true}
298     * and {@code x.length} > 30
299     * @throws ConvergenceException if the p-value can not be computed due to
300     * a convergence error
301     * @throws MaxCountExceededException if the maximum number of iterations
302     * is exceeded
303     */
304    public double wilcoxonSignedRankTest(final double[] x, final double[] y,
305                                         final boolean exactPValue)
306        throws NullArgumentException, NoDataException, DimensionMismatchException,
307        NumberIsTooLargeException, ConvergenceException, MaxCountExceededException {
308
309        ensureDataConformance(x, y);
310
311        final int N = x.length;
312        final double Wmax = wilcoxonSignedRank(x, y);
313
314        if (exactPValue && N > 30) {
315            throw new NumberIsTooLargeException(N, 30, true);
316        }
317
318        if (exactPValue) {
319            return calculateExactPValue(Wmax, N);
320        } else {
321            final double Wmin = ( (double)(N*(N+1)) / 2.0 ) - Wmax;
322            return calculateAsymptoticPValue(Wmin, N);
323        }
324    }
325}