Top Description Fields Constructors Methods
org.apache.commons.math3.stat.inference

public Class WilcoxonSignedRankTest

extends Object
Class Inheritance
Imports
org.apache.commons.math3.distribution.NormalDistribution, org.apache.commons.math3.exception.ConvergenceException, .DimensionMismatchException, .MaxCountExceededException, .NoDataException, .NullArgumentException, .NumberIsTooLargeException, org.apache.commons.math3.stat.ranking.NaNStrategy, .NaturalRanking, .TiesStrategy, org.apache.commons.math3.util.FastMath

An implementation of the Wilcoxon signed-rank test.

Field Summary

Modifier and TypeField and Description
private NaturalRanking
naturalRanking

Ranking algorithm.

Constructor Summary

AccessConstructor and Description
public
WilcoxonSignedRankTest()

Create a test instance where NaN's are left in place and ties get the average of applicable ranks.

public
WilcoxonSignedRankTest(final NaNStrategy
specifies the strategy that should be used for Double.NaN's
nanStrategy
,
final TiesStrategy
specifies the strategy that should be used for ties
tiesStrategy
)

Create a test instance using the given strategies for NaN's and ties.

Method Summary

Modifier and TypeMethod and Description
private double[]

Returns:

|z|
calculateAbsoluteDifferences
(final double[]
sample
z
)

Calculates |z[i]| for all i

private double

Returns:

two-sided asymptotic p-value
calculateAsymptoticPValue
(final double
smallest Wilcoxon signed rank value
Wmin
,
final int
number of subjects (corresponding to x.length)
N
)

private double[]

Returns:

z = y - x
calculateDifferences
(final double[]
first sample
x
,
final double[]
second sample
y
)

Calculates y[i] - x[i] for all i

private double

Returns:

two-sided exact p-value
calculateExactPValue
(final double
largest Wilcoxon signed rank value
Wmax
,
final int
number of subjects (corresponding to x.length)
N
)

Algorithm inspired by http://www.fon.hum.uva.nl/Service/Statistics/Signed_Rank_Algorihms.html#C by Rob van Son, Institute of Phonetic Sciences & IFOTT, University of Amsterdam

private void
ensureDataConformance(final double[]
first sample
x
,
final double[]
second sample
y
)

Ensures that the provided arrays fulfills the assumptions.

public double

Returns:

wilcoxonSignedRank statistic (the larger of W+ and W-)
wilcoxonSignedRank
(final double[]
the first sample
x
,
final double[]
the second sample
y
)

Computes the Wilcoxon signed ranked statistic comparing mean for two related samples or repeated measurements on a single sample.

public double

Returns:

p-value
wilcoxonSignedRankTest
(final double[]
the first sample
x
,
final double[]
the second sample
y
,
final boolean
if the exact p-value is wanted (only works for x.length <= 30, if true and x.length > 30, this is ignored because calculations may take too long)
exactPValue
)

Returns the observed significance level, or p-value, associated with a Wilcoxon signed ranked statistic comparing mean for two related samples or repeated measurements on a single sample.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

naturalRankingback to summary
private NaturalRanking naturalRanking

Ranking algorithm.

Constructor Detail

WilcoxonSignedRankTestback to summary
public WilcoxonSignedRankTest()

Create a test instance where NaN's are left in place and ties get the average of applicable ranks. Use this unless you are very sure of what you are doing.

WilcoxonSignedRankTestback to summary
public WilcoxonSignedRankTest(final NaNStrategy nanStrategy, final TiesStrategy tiesStrategy)

Create a test instance using the given strategies for NaN's and ties. Only use this if you are sure of what you are doing.

Parameters
nanStrategy:NaNStrategy

specifies the strategy that should be used for Double.NaN's

tiesStrategy:TiesStrategy

specifies the strategy that should be used for ties

Method Detail

calculateAbsoluteDifferencesback to summary
private double[] calculateAbsoluteDifferences(final double[] z) throws NullArgumentException, NoDataException

Calculates |z[i]| for all i

Parameters
z:double[]

sample

Returns:double[]

|z|

Exceptions
NullArgumentException:
if z is null
NoDataException:
if z is zero-length.
calculateAsymptoticPValueback to summary
private double calculateAsymptoticPValue(final double Wmin, final int N)
Parameters
Wmin:double

smallest Wilcoxon signed rank value

N:int

number of subjects (corresponding to x.length)

Returns:double

two-sided asymptotic p-value

calculateDifferencesback to summary
private double[] calculateDifferences(final double[] x, final double[] y)

Calculates y[i] - x[i] for all i

Parameters
x:double[]

first sample

y:double[]

second sample

Returns:double[]

z = y - x

calculateExactPValueback to summary
private double calculateExactPValue(final double Wmax, final int N)

Algorithm inspired by http://www.fon.hum.uva.nl/Service/Statistics/Signed_Rank_Algorihms.html#C by Rob van Son, Institute of Phonetic Sciences & IFOTT, University of Amsterdam

Parameters
Wmax:double

largest Wilcoxon signed rank value

N:int

number of subjects (corresponding to x.length)

Returns:double

two-sided exact p-value

ensureDataConformanceback to summary
private void ensureDataConformance(final double[] x, final double[] y) throws NullArgumentException, NoDataException, DimensionMismatchException

Ensures that the provided arrays fulfills the assumptions.

Parameters
x:double[]

first sample

y:double[]

second sample

Exceptions
NullArgumentException:
if x or y are null.
NoDataException:
if x or y are zero-length.
DimensionMismatchException:
if x and y do not have the same length.
wilcoxonSignedRankback to summary
public double wilcoxonSignedRank(final double[] x, final double[] y) throws NullArgumentException, NoDataException, DimensionMismatchException

Computes the Wilcoxon signed ranked statistic comparing mean for two related samples or repeated measurements on a single sample.

This statistic can be used to perform a Wilcoxon signed ranked test evaluating the null hypothesis that the two related samples or repeated measurements on a single sample has equal mean.

Let Xi denote the i'th individual of the first sample and Yi the related i'th individual in the second sample. Let Zi = Yi - Xi.

Preconditions:

  • The differences Zi must be independent.
  • Each Zi comes from a continuous population (they must be identical) and is symmetric about a common median.
  • The values that Xi and Yi represent are ordered, so the comparisons greater than, less than, and equal to are meaningful.

Parameters
x:double[]

the first sample

y:double[]

the second sample

Returns:double

wilcoxonSignedRank statistic (the larger of W+ and W-)

Exceptions
NullArgumentException:
if x or y are null.
NoDataException:
if x or y are zero-length.
DimensionMismatchException:
if x and y do not have the same length.
wilcoxonSignedRankTestback to summary
public double wilcoxonSignedRankTest(final double[] x, final double[] y, final boolean exactPValue) throws NullArgumentException, NoDataException, DimensionMismatchException, NumberIsTooLargeException, ConvergenceException, MaxCountExceededException

Returns the observed significance level, or p-value, associated with a Wilcoxon signed ranked statistic comparing mean for two related samples or repeated measurements on a single sample.

Let Xi denote the i'th individual of the first sample and Yi the related i'th individual in the second sample. Let Zi = Yi - Xi.

Preconditions:

  • The differences Zi must be independent.
  • Each Zi comes from a continuous population (they must be identical) and is symmetric about a common median.
  • The values that Xi and Yi represent are ordered, so the comparisons greater than, less than, and equal to are meaningful.

Parameters
x:double[]

the first sample

y:double[]

the second sample

exactPValue:boolean

if the exact p-value is wanted (only works for x.length <= 30, if true and x.length > 30, this is ignored because calculations may take too long)

Returns:double

p-value

Exceptions
NullArgumentException:
if x or y are null.
NoDataException:
if x or y are zero-length.
DimensionMismatchException:
if x and y do not have the same length.
NumberIsTooLargeException:
if exactPValue is true and x.length > 30
ConvergenceException:
if the p-value can not be computed due to a convergence error
MaxCountExceededException:
if the maximum number of iterations is exceeded