TrinomialTest
The trinomial test is a non-parametric statistical test for consistent differences in medians or if a difference between the number of positive or negative differences between two groups. This test focuses on the direction (positive or negative) of the differences and the number of ties.
First one determines the number of positive, negative, and tied samples using the first set of equations. Note, that these describe the two-sample case. In the one sample case, the median can be used to find the number of positives, negatives, and tied samples. The test statistic, D, is then calculated by taking the absolute value of the difference between the number of positives and negatives. The number of ties, t, and the total number of trials, N, is used to calculate the probability of a tie. Following this, the multinomial distribution can be used to determine the probability associated with D or more ties, P(D).
In this package, one is also able to set a region of practical equivalence (ROPE). This increases the number of ties by setting differences to zero if the absolute value of the difference is smaller than the ROPE. This will inflate the number of ties but can be useful if one suspects that differences between groups or between each sample and the median may be random. The default value of the ROPE is 0.
$$I_{pos}$$, $$I_{neg}$$, and $$I_{tie}$$ are indicator functions that return 1 if the input to the function is positive, negative, or zero.
Calculation of the Number of Positive, Negative, and Tied Samples in the Two-Sample Case:
$$n_{+} = \sum_{i=1}^{N}I_{pos}\left(x_{i} - y_{i} > 0\right)$$
$$n_{-} = \sum_{i=1}^{N}I_{neg}\left(x_{i} - y_{i} < 0\right)$$
$$t = \sum_{i=1}^{N}I_{tie}\left(x_{i} - y_{i} = 0\right)$$
Calculation of the Number of Positive, Negative, and Tied Samples in the One-Sample Case:
$$n_{+} = \sum_{i=1}^{N}I_{pos}\left(x_{i} - median(X) > 0\right)$$
$$n_{-} = \sum_{i=1}^{N}I_{neg}\left(x_{i} - median(X) < 0\right)$$
$$t = \sum_{i=1}^{N}I_{tie}\left(x_{i} = median(X)\right)$$
Calculation of the Test Statistic:
$$D = |n_{+} - n_{-}|$$
Calculate the Probability of a Tie:
$$p_{tie} = \frac{t}{N}$$
Calculate the p-value:
$$P\left(D\right) = \sum_{z=D}^N\sum_{k=0}^{\frac{N-z}{2}}\frac{N!}{(n-z-2k)!(k+z)!k!}p_{tie}^{n-z-2k}\left(\frac{1-p_{tie}}{2}\right)^{z+2k}$$
Install
From PyPI:
pip install TrinomialTest
From source:
git clone https://github.com/jrudar/TrinomialTest.git
cd TrinomialTest
pip install .
# or create a virtual environment
python -m venv venv
source venv/bin/activate
pip install .
Usage
import numpy as np
from TrinomialTest import TrinomialTest
X = np.asarray([1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3])
Y = np.asarray([2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3])
result = TrinomialTest(X, Y, alternative = "two-sided")
# p-value should be 0.0772262851453612
print(result.pvalue)
Citations
Rudar, J., & Lung, O. (2025). TrinomialTest. Zenodo. https://doi.org/10.5281/zenodo.15353378
Bian, Guorui & McAleer, Michael & Wong, Wing-Keung, 2011. "A trinomial test for paired data when there are many ties," Mathematics and Computers in Simulation (MATCOM), Elsevier, vol. 81(6), pages 1153-1160.
Whitaker, D., Drew, B., & Barss, J. (2021) GridItemTools: Grid item tools. R package version 0.0.12. https://github.com/douglaswhitaker/GridItemTools
Pauli Virtanen, Ralf Gommers, Travis E. Oliphant, Matt Haberland, Tyler Reddy, David Cournapeau, Evgeni Burovski, Pearu Peterson, Warren Weckesser, Jonathan Bright, Stéfan J. van der Walt, Matthew Brett, Joshua Wilson, K. Jarrod Millman, Nikolay Mayorov, Andrew R. J. Nelson, Eric Jones, Robert Kern, Eric Larson, CJ Carey, İlhan Polat, Yu Feng, Eric W. Moore, Jake VanderPlas, Denis Laxalde, Josef Perktold, Robert Cimrman, Ian Henriksen, E.A. Quintero, Charles R Harris, Anne M. Archibald, Antônio H. Ribeiro, Fabian Pedregosa, Paul van Mulbregt, and SciPy 1.0 Contributors. (2020) SciPy 1.0: Fundamental Algorithms for Scientific Computing in Python. Nature Methods, 17(3), 261-272. DOI: 10.1038/s41592-019-0686-2.
Harris, C.R., Millman, K.J., van der Walt, S.J. et al. Array programming with NumPy. Nature 585, 357–362 (2020). DOI: 10.1038/s41586-020-2649-2.
McKinney W, others. Data structures for statistical computing in python. In: Proceedings of the 9th Python in Science Conference. 2010. p. 51–6.
Seabold S, Perktold J. statsmodels: Econometric and statistical modeling with python. In: 9th Python in Science Conference. 2010.
Metadata
Release files for TrinomialTest 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| trinomialtest-1.0.4.tar.gz | 8.5 kB | Without provenance | Details for trinomialtest-1.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| trinomialtest-1.0.4-py3-none-any.whl | Python 3 | none | any | Details for trinomialtest-1.0.4-py3-none-any.whl |
Total release size: 16.8 kB
Release files / trinomialtest-1.0.4.tar.gz
| Download URL | trinomialtest-1.0.4.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8fd5bf2e134477e25cdfde54bb7d979cc624d10b855d41daa7e69523dcb9df15
|
|
BLAKE2b-256 checksum How to use checksums |
56eafe250e913d3dd2b97ea88043cabb298a57eb2c2ec0c4321bbcdda979b991
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.22
|
Release files / trinomialtest-1.0.4-py3-none-any.whl
| Download URL | trinomialtest-1.0.4-py3-none-any.whl |
|---|---|
| Size | 8.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
137591631d52d1f75d24a88476769dc5ee72f1eb6b9cfa9dcf8ccffecb695aeb
|
|
BLAKE2b-256 checksum How to use checksums |
dd51085146c769cf10ce66baa793065c0920d03a1df1623e5a16913618a497a0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.22
|
Security for TrinomialTest 1.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.