home | career | drupal | java | mac | mysql | perl | scala | uml | unix

# Java example source code file (WilcoxonSignedRankTestTest.java)

This example Java source code file (WilcoxonSignedRankTestTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

## Java - Java tags/keywords

dimensionmismatchexception, more, nodataexception, nullargumentexception, numberistoolargeexception, test, wilcoxonsignedranktest, wilcoxonsignedranktesttest

## The WilcoxonSignedRankTestTest.java Java example source code

```/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*/
package org.apache.commons.math3.stat.inference;

import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.NoDataException;
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.junit.Assert;
import org.junit.Test;

/**
* Test cases for the WilcoxonSignedRangTest class.
*
*/

public class WilcoxonSignedRankTestTest {

protected WilcoxonSignedRankTest testStatistic = new WilcoxonSignedRankTest();

@Test
public void testWilcoxonSignedRankSimple() {
/* Target values computed using R version 2.11.1
* x <- c(1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30)
* y <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29)
*/
final double x[] = {1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30};
final double y[] = {0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29};

/* EXACT:
* wilcox.test(x, y, alternative = "two.sided", mu = 0, paired = TRUE, exact = TRUE, correct = FALSE)
* V = 40, p-value = 0.03906
*
* Corresponds to the value obtained in R.
*/
Assert.assertEquals(40, testStatistic.wilcoxonSignedRank(x, y), 1e-10);
Assert.assertEquals(0.03906, testStatistic.wilcoxonSignedRankTest(x, y, true), 1e-5);

/* ASYMPTOTIC:
* wilcox.test(x, y, alternative = "two.sided", mu = 0, paired = TRUE, exact = FALSE, correct = FALSE)
* V = 40, p-value = 0.03815
*
* This is not entirely the same due to different corrects,
* e.g. http://mlsc.lboro.ac.uk/resources/statistics/wsrt.pdf
* and src/library/stats/R/wilcox.test.R in the R source
*/
Assert.assertEquals(40, testStatistic.wilcoxonSignedRank(x, y), 1e-10);
Assert.assertEquals(0.0329693812, testStatistic.wilcoxonSignedRankTest(x, y, false), 1e-10);
}

@Test
public void testWilcoxonSignedRankInputValidation() {
/*
* Exact only for sample size <= 30
*/
final double[] x1 = new double;
final double[] x2 = new double;
final double[] y1 = new double;
final double[] y2 = new double;
for (int i = 0; i < 30; ++i) {
x1[i] = x2[i] = y1[i] = y2[i] = i;
}

// Exactly 30 is okay
//testStatistic.wilcoxonSignedRankTest(x1, y1, true);

try {
testStatistic.wilcoxonSignedRankTest(x2, y2, true);
Assert.fail("More than 30 samples and exact chosen, NumberIsTooLargeException expected");
} catch (NumberIsTooLargeException ex) {
// expected
}

/* Samples must be present, i.e. length > 0
*/
try {
testStatistic.wilcoxonSignedRankTest(new double[] { }, new double[] { 1.0 }, true);
Assert.fail("x does not contain samples (exact), NoDataException expected");
} catch (NoDataException ex) {
// expected
}

try {
testStatistic.wilcoxonSignedRankTest(new double[] { }, new double[] { 1.0 }, false);
Assert.fail("x does not contain samples (asymptotic), NoDataException expected");
} catch (NoDataException ex) {
// expected
}

try {
testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, new double[] { }, true);
Assert.fail("y does not contain samples (exact), NoDataException expected");
} catch (NoDataException ex) {
// expected
}

try {
testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, new double[] { }, false);
Assert.fail("y does not contain samples (asymptotic), NoDataException expected");
} catch (NoDataException ex) {
// expected
}

/* Samples not same size, i.e. cannot be pairred
*/
try {
testStatistic.wilcoxonSignedRankTest(new double[] { 1.0, 2.0 }, new double[] { 3.0 }, true);
Assert.fail("x and y not same size (exact), DimensionMismatchException expected");
} catch (DimensionMismatchException ex) {
// expected
}

try {
testStatistic.wilcoxonSignedRankTest(new double[] { 1.0, 2.0 }, new double[] { 3.0 }, false);
Assert.fail("x and y not same size (asymptotic), DimensionMismatchException expected");
} catch (DimensionMismatchException ex) {
// expected
}

/*
* x and y is null
*/
try {
testStatistic.wilcoxonSignedRankTest(null, null, true);
Assert.fail("x and y is null (exact), NullArgumentException expected");
} catch (NullArgumentException ex) {
// expected
}

try {
testStatistic.wilcoxonSignedRankTest(null, null, false);
Assert.fail("x and y is null (asymptotic), NullArgumentException expected");
} catch (NullArgumentException ex) {
// expected
}

/*
* x or y is null
*/
try {
testStatistic.wilcoxonSignedRankTest(null, new double[] { 1.0 }, true);
Assert.fail("x is null (exact), NullArgumentException expected");
} catch (NullArgumentException ex) {
// expected
}

try {
testStatistic.wilcoxonSignedRankTest(null, new double[] { 1.0 }, false);
Assert.fail("x is null (asymptotic), NullArgumentException expected");
} catch (NullArgumentException ex) {
// expected
}

try {
testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, null, true);
Assert.fail("y is null (exact), NullArgumentException expected");
} catch (NullArgumentException ex) {
// expected
}

try {
testStatistic.wilcoxonSignedRankTest(new double[] { 1.0 }, null, false);
Assert.fail("y is null (asymptotic), NullArgumentException expected");
} catch (NullArgumentException ex) {
// expected
}
}
}
```

## Other Java examples (source code examples)

Here is a short list of links related to this Java WilcoxonSignedRankTestTest.java source code file:
{"ft_lang_label":"__label__en","ft_lang_prob":0.5249655,"math_prob":0.6064144,"size":6279,"snap":"2021-04-2021-17","text_gpt3_token_len":1751,"char_repetition_ratio":0.19155379,"word_repetition_ratio":0.3442029,"special_character_ratio":0.30132186,"punctuation_ratio":0.27249357,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95083505,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-01-18T04:56:29Z\",\"WARC-Record-ID\":\"<urn:uuid:ee135918-e656-4d21-9437-93466df330f2>\",\"Content-Length\":\"14459\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1b2d5a13-c9db-4cab-9c56-20267a0d8df0>\",\"WARC-Concurrent-To\":\"<urn:uuid:cd9c722f-7903-4f8c-87dc-12fc5fbd123d>\",\"WARC-IP-Address\":\"173.230.141.151\",\"WARC-Target-URI\":\"http://alvinalexander.com/java/jwarehouse/commons-math3-3.6.1/src/test/java/org/apache/commons/math3/stat/inference/WilcoxonSignedRankTestTest.java.shtml\",\"WARC-Payload-Digest\":\"sha1:F2MZ52DYUMGHG6EHBOTVCG2466TTMLBT\",\"WARC-Block-Digest\":\"sha1:XW2R6MUFVDAN6WVPCP44Q3OPOCXXGPTR\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-04/CC-MAIN-2021-04_segments_1610703514121.8_warc_CC-MAIN-20210118030549-20210118060549-00322.warc.gz\"}"}