The GRIM test
An implementation of the GRIM test, in python
Introduction
This package is based on the GRIM (Granularity-Related Inconsistency of Means) test first highlighted by Heathers & Brown in their 2016 paper.
The test makes use of a simple numerical property to identify if the mean of integer values has been correctly calculated.
You don't need the original integer values. You just need the mean and the number (n) of items.
What about rounding?
Often the mean you are testing has previously been rounded. You can check if the mean is consistent with a particular rounding type by including that as an argument.
This implementation supports all the rounding types found in the Python decimal implementation (at least between versions 3.8 and 3.11).
(They are: ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, ROUND_05UP)
If no rounding type is included then the test assumes ROUND_HALF_UP.
These examples are available as a Google Colab Notebook
How do I install it?
On the command line:
pip install grim
In a google Colab/iPython/Jupyter notebook:
!pip install grim
Example: Is this mean, n and rounding type consistent?
from grim import mean_tester
import decimal
# mean is 11.09 and n is 21
print(mean_tester.consistency_check('11.09', '21', decimal.ROUND_HALF_UP))
This will return False as the mean could not be correct given a list of 21 integers (and using ROUND_HALF_UP rounding.)
Example: Is this mean & n consistent using any rounding type?
from grim import mean_tester
import decimal
# mean is 11.09 and n is 21
print(mean_tester.summary_consistency_check('11.09', '21'))
This will return:
{'ROUND_CEILING': False, 'ROUND_DOWN': True, 'ROUND_FLOOR': True, 'ROUND_HALF_DOWN': False, 'ROUND_HALF_EVEN': False, 'ROUND_HALF_UP': False, 'ROUND_UP': False, 'ROUND_05UP': True}
As you can see, a given mean and n might be consistent using one form of rounding but not others.
You can pass in the numbers as Strings or Decimals, this avoids floating point accuracy issues that are more likely to occur when using a 'float'.
How do I see some logging about how the possible matches the algorithm has considered?
Add an extra argument, log_status=True.
print(mean_tester.summary_consistency_check('11.09', '21', log_status=True))
The output would look this:
Tue, 18 Apr 2023 18:02:00 +0000 : Target Mean: 11.09, Decimal places: 2, Lower match: 11.00, Middle match: 11.05, Upper match: 11.10, Match status: False, Rounding method: ROUND_CEILING
Tue, 18 Apr 2023 18:02:00 +0000 : Target Mean: 11.09, Decimal places: 2, Lower match: 11.00, Middle match: 11.04, Upper match: 11.09, Match status: True, Rounding method: ROUND_DOWN
Tue, 18 Apr 2023 18:02:00 +0000 : Target Mean: 11.09, Decimal places: 2, Lower match: 11.00, Middle match: 11.04, Upper match: 11.09, Match status: True, Rounding method: ROUND_FLOOR
Tue, 18 Apr 2023 18:02:00 +0000 : Target Mean: 11.09, Decimal places: 2, Lower match: 11.00, Middle match: 11.05, Upper match: 11.10, Match status: False, Rounding method: ROUND_HALF_DOWN
Tue, 18 Apr 2023 18:02:00 +0000 : Target Mean: 11.09, Decimal places: 2, Lower match: 11.00, Middle match: 11.05, Upper match: 11.10, Match status: False, Rounding method: ROUND_HALF_EVEN
Tue, 18 Apr 2023 18:02:00 +0000 : Target Mean: 11.09, Decimal places: 2, Lower match: 11.00, Middle match: 11.05, Upper match: 11.10, Match status: False, Rounding method: ROUND_HALF_UP
Tue, 18 Apr 2023 18:02:00 +0000 : Target Mean: 11.09, Decimal places: 2, Lower match: 11.00, Middle match: 11.05, Upper match: 11.10, Match status: False, Rounding method: ROUND_UP
Tue, 18 Apr 2023 18:02:00 +0000 : Target Mean: 11.09, Decimal places: 2, Lower match: 11.00, Middle match: 11.04, Upper match: 11.09, Match status: True, Rounding method: ROUND_05UP
{'ROUND_CEILING': False, 'ROUND_DOWN': True, 'ROUND_FLOOR': True, 'ROUND_HALF_DOWN': False, 'ROUND_HALF_EVEN': False, 'ROUND_HALF_UP': False, 'ROUND_UP': False, 'ROUND_05UP': True}
A warning about floating point numbers & computers:
Beware of creating Decimals from floating point numbers as these may have floating point inaccuracies.
e.g.:
import decimal
print(decimal.Decimal(1.1))
1.100000000000000088817841970012523233890533447265625
Notice how the inaccurate representation of 1.1 from the floating point number has been preserved in the Decimal. Its better to create a decimal from a String E.g.:
import decimal
print(decimal.Decimal('1.1'))
1.1
Many tools can be configured to read in text [that might be a number] as a string with out parsing. Some tools, such as Webdriver, only return a string (Which is useful!)
For more information on the origins of these issues in modern computer languages read this.
How can I find out more about the GRIM test?
James Heathers has published articles that explain how the technique works and how he used it to expose inconsistencies in scientific papers.
Citation file
There is a citation file included in the code repo.
Metadata
Release files for grim 0.1.10
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 | |
|---|---|---|---|---|
| grim-0.1.10.tar.gz | 5.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| grim-0.1.10-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.8 kB
Release files / grim-0.1.10.tar.gz
| Download URL | grim-0.1.10.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ca662ed6b6aff510daff19ffb425a135e010b87f056a5584f5f1c633be4c6504
|
|
BLAKE2b-256 checksum How to use checksums |
48b8f14554256fd08af7010733cefbf7f0113b9bad8fe83a4fad5e37851b42f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.2
|
Release files / grim-0.1.10-py3-none-any.whl
| Download URL | grim-0.1.10-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5c7b69abb43229e3f2b57293790d22e805f14409119f288b8fcd1e96817fdcab
|
|
BLAKE2b-256 checksum How to use checksums |
377d73e4b23e04004f0320e6d7229a0637c2cf6497e3b23449bd9024e8afa613
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.2
|
Security for grim 0.1.10
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.