Nonparametric Test Statistics
Project description
PyNonpar
Why PyNonpar?
Many statistical analyses require alternatives to classical parametric methods. While Python provides excellent general statistical functionality through scipy.stats, some advanced nonparametric procedures are not currently available.
PyNonpar implements methods based on modern rank statistics, including pseudo-rank based inference. Test statistics based on ranks may lead to paradoxical results. A solution are so-called pseudo-ranks. This package provides a function to calculate pseudo-ranks as well as nonparametric, (pseudo)-rank statistics. For a definition and discussion of pseudo-ranks, see for example [1].
To install the package from PyPI, simply type
pip install PyNonpar
What's new in 0.3.0
- All functions now accept array-like input — Python lists, tuples,
numpy.ndarray, andpandas.Series— matching the calling convention ofscipy.stats. Existing code that passes plain lists keeps working exactly as before. - Stricter, more consistent input validation across all functions: invalid options now raise a
clear
TypeError/ValueErrorinstead of silently producing an incorrect result. - Type hints and updated docstrings throughout.
import numpy as np
import pandas as pd
import PyNonpar
from PyNonpar import*
# array-like input is now accepted everywhere - not just plain lists
x = np.array([8, 4, 10, 4, 9, 1, 3, 3, 4, 8])
y = pd.Series([10, 5, 11, 6, 11, 2, 4, 5, 5, 10])
PyNonpar.twosample.brunner_munzel_test(x, y, alternative="less", quantile="t")
Table of Contents
Two-Sample Tests
Paired Two-Sample Tests
Multi-Sample Tests
Repeated-Measures Tests
Pseudo-Ranks
If there are ties (i.e., observations with the same value) in the data, then the pseudo-ranks have to be adjusted. There are the options 'minimum', 'maximum' and 'average'. It is recommended to use 'average' as for this adjusmtent, normalized empirical distribution functions are used. See the example for details on the usage of the function 'psrank'.
import PyNonpar
from PyNonpar import*
# some artificial data
x = [1, 1, 1, 1, 2, 3, 4, 5, 6]
group = ['C', 'C', 'B', 'B', 'B', 'A', 'C', 'A', 'C']
PyNonpar.pseudorank.psrank(x, group, ties_method = "average")
Nonparametric Test Statistics
Two-Sample Tests
- Wilcoxon-Mann-Whitney test: wilcoxon_mann_whitney_test()
- Brunner-Munzel test (Generalized Wilcoxon test): brunner_munzel_test()
The Hodges-Lehmann estimator can be calculated in a location shift model: hodges_lehmann(). The confidence interval for this estimator is only asymptotic and assumes continuous distributions.
1. Wilcoxon-Mann-Whitney test
For large sample sizes is the asymptotic Wilcoxon test recommended (method = "asymptotic"). For small sample sizes, we recommend the exact Wilcoxon test. Note that the Wilcoxon test assumes the null hypothesis of equal distributions H0: F1 = F2.
import PyNonpar
from PyNonpar import*
x = [8,4,10,4,9,1,3,3,4,8]
y = [10,5,11,6,11,2,4,5,5,10]
PyNonpar.twosample.wilcoxon_mann_whitney_test(x, y, alternative="less", method = "asymptotic", alpha = 0.05)
PyNonpar.twosample.wilcoxon_mann_whitney_test(x, y, alternative="less", method = "exact", alpha = 0.05)
Wilcoxon-Mann-Whitney Sample Size Planning
To calculate the sample size which is needed to detect a specific relative effect p with probability beta and type-I error alpha, the function'wilcoxon_mann_whitney_ssp' can be used. Here, prior information for one group is needed. The artificial data for the second group can be created by some interpretable effect, e.g. a location shift effect. For more information, see [1] or [3].
import PyNonpar
from PyNonpar import*
# pior information
x_ssp = [315, 375, 356, 374, 412, 418, 445, 403, 431, 410, 391, 475, 379]
# y_ssp = x_ssp - 20
y_ssp = [295, 355, 336, 354, 392, 398, 425, 383, 411, 390, 371, 455, 359]
PyNonpar.twosample_paired.paired_ranks_ssp(x_ssp, y_ssp, 0.8, 0.05, 1/2)
2. Brunner-Munzel test
The Brunner-Munzel test extends the Wilcoxon test to the null hypothesis H0: p = 1/2.
import PyNonpar
from PyNonpar import*
x = [8,4,10,4,9,1,3,3,4,8]
y = [10,5,11,6,11,2,4,5,5,10]
PyNonpar.twosample.brunner_munzel_test(x, y, alternative="less", quantile = "t")
PyNonpar.twosample.brunner_munzel_test(x, y, alternative="less", quantile = "normal")
Paired Two-Sample Tests
1. Paired ranks test
The paired ranks test compares the marginal distributions F1 and F2. The Null hypothesis is H0: F1 = F2 (var_equal = True) or H0: p = 1/2 (var_equal = False). The two sided alternative is for both cases p != 1/2.
p = Probability(X_i < Y_j) + 1/2 * Probability(X_i = Y_j) for i != j where (X_i, Y_i), (X_j, Y_j) are paired observations.
import PyNonpar
from PyNonpar import*
x = [1, 2, 3, 4, 5, 7, 1, 1, 1]
y = [4, 6, 8, 7, 6, 5, 9, 1, 1]
PyNonpar.twosample_paired.paired_ranks_test(x, y, alternative="two.sided", var_equal=False, quantile="normal")
Multi-Sample Tests
- The Hettmansperger-Norton Test for Patterned Alternatives: hettmansperger_norton_test()
- Kruskal-Wallis test: kruskal_wallis_test()
1. The Hettmansperger-Norton Test for Patterned Alternatives
This package provides a function to calculate the Hettmansperger-Norton test for patterned alternatives using pseudo-ranks. Originally, this test was developed for ranks but this version was adapted to pseudo-ranks.
For the alternative, it is possible to use 'increasing' (i.e., trend = [1, 2, 3, ..., g]), 'decreasing' (i.e., trend = [g, g-1, g-2, ..., 1]) or 'custom' where the trend has to be specified manually. Note, that the trend is a list of length g where g is the number of groups.
import PyNonpar
from PyNonpar import*
# some artificial data
x = [1, 1, 1, 1, 2, 3, 4, 5, 6]
group = ['C', 'C', 'B', 'B', 'B', 'A', 'C', 'A', 'C']
PyNonpar.hettmansperger.hettmansperger_norton_test(x, group, alternative = "custom", trend = [1,3,2])
2. Kruskal-Wallis Test
import PyNonpar
from PyNonpar import*
# some artificial data
x = [1, 1, 1, 1, 2, 3, 4, 5, 6]
group = ['C', 'C', 'B', 'B', 'B', 'A', 'C', 'A', 'C']
# Using pseudo-ranks
PyNonpar.multisample.kruskal_wallis_test(x, group, pseudoranks = True)
# Using ranks
PyNonpar.multisample.kruskal_wallis_test(x, group, pseudoranks = False)
Repeated-Measures Tests
- The Paired-Ranks Test: paired_ranks_test()
- The Kepner-Robinson Test Test: kepner_robinson_test()
1. Paired ranks test
See Section ''Paired Twosample Tests''.
2. Kepner-Robinson Test
For the Kepner-Robinson Test we have several dependent observations per subject (subplot factor). Let us denote with F_k the cdf for the k-th observation. The null hypothesis for this test is H_0: F_1 = ... F_d where d is the number of observations per subject. This test assumes for the dependence structure a compound symmetry, that is, all variances are the same and all covariances are the same. In other words, the observations on one subject can basically be interchanged. For more information, we refer to [2].
import PyNonpar
from PyNonpar import*
# some artificial data
data = [1, 0, -2, -1, -2, 1, 0, 0, 0, -2]
time = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
subject = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
PyNonpar.repeated_measures.kepner_robinson_test(data, time, subject, distribution="F")
From version 0.2.1 and higher PyNonpar uses AI tools for code optimization. The mathematical and statistical part of PyNonpar are handled by a statistician.
References
[1] Brunner, E., Bathke A. C. and Konietschke, F: Rank- and Pseudo-Rank Procedures in Factorial Designs - Using R and SAS, Springer Verlag, to appear.
[2] Kepner, J. L., & Robinson, D. H. (1988). Nonparametric methods for detecting treatment effects in repeated-measures designs. Journal of the American Statistical Association, 83(402), 456-461.
[3] Happ, M., Bathke, A. C., & Brunner, E. (2019). Optimal sample size planning for the Wilcoxon‐Mann‐Whitney test. Statistics in medicine, 38(3), 363-375.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pynonpar-0.3.0.tar.gz.
File metadata
- Download URL: pynonpar-0.3.0.tar.gz
- Upload date:
- Size: 30.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3e617fc735ed7989a82492a0ae59de2bf15b50a45eab60eb9e62e18f2790821
|
|
| MD5 |
e2e9c111fd41129df9af8de43b326de4
|
|
| BLAKE2b-256 |
452b9b8cde06d853ffd7957d9f3bc324dd9d8f4f581e4416dff64e71413801c3
|
Provenance
The following attestation bundles were made for pynonpar-0.3.0.tar.gz:
Publisher:
publish.yml on happma/PyNonpar
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pynonpar-0.3.0.tar.gz -
Subject digest:
d3e617fc735ed7989a82492a0ae59de2bf15b50a45eab60eb9e62e18f2790821 - Sigstore transparency entry: 2163280709
- Sigstore integration time:
-
Permalink:
happma/PyNonpar@0c5a40b5266c66447b1985756a3edf86808e0bde -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/happma
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0c5a40b5266c66447b1985756a3edf86808e0bde -
Trigger Event:
release
-
Statement type:
File details
Details for the file pynonpar-0.3.0-py3-none-any.whl.
File metadata
- Download URL: pynonpar-0.3.0-py3-none-any.whl
- Upload date:
- Size: 28.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dfc267918adb59d6aedf6f652c01b8f4f87449b0a4755a083fe050f578001206
|
|
| MD5 |
9e8a2d56b3c9d53d9470ef393fa6f1fd
|
|
| BLAKE2b-256 |
18ee844e2ac3f7b172602374d356694cca3aa9cfabf5cf95dce6760751a56f85
|
Provenance
The following attestation bundles were made for pynonpar-0.3.0-py3-none-any.whl:
Publisher:
publish.yml on happma/PyNonpar
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pynonpar-0.3.0-py3-none-any.whl -
Subject digest:
dfc267918adb59d6aedf6f652c01b8f4f87449b0a4755a083fe050f578001206 - Sigstore transparency entry: 2163280775
- Sigstore integration time:
-
Permalink:
happma/PyNonpar@0c5a40b5266c66447b1985756a3edf86808e0bde -
Branch / Tag:
refs/tags/0.3.0 - Owner: https://github.com/happma
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@0c5a40b5266c66447b1985756a3edf86808e0bde -
Trigger Event:
release
-
Statement type: