mtit-gof 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mtit/__init__.py +4 -0
- mtit/core.py +100 -0
- mtit_gof-0.1.0.dist-info/METADATA +76 -0
- mtit_gof-0.1.0.dist-info/RECORD +6 -0
- mtit_gof-0.1.0.dist-info/WHEEL +5 -0
- mtit_gof-0.1.0.dist-info/top_level.txt +1 -0
mtit/__init__.py
ADDED
mtit/core.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"""Maximum Tail Information Test for continuous distributions."""
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from typing import Callable
|
|
4
|
+
import numpy as np
|
|
5
|
+
from scipy.stats import norm
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True)
|
|
9
|
+
class MTITResult:
|
|
10
|
+
statistic: float
|
|
11
|
+
pvalue: float
|
|
12
|
+
lower: float
|
|
13
|
+
upper: float
|
|
14
|
+
method: str
|
|
15
|
+
n: int
|
|
16
|
+
simulations: int
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _sample(x):
|
|
20
|
+
x = np.asarray(x, dtype=float)
|
|
21
|
+
if x.ndim != 1 or len(x) < 2 or not np.all(np.isfinite(x)):
|
|
22
|
+
raise ValueError("x must contain at least two finite observations in a one-dimensional array")
|
|
23
|
+
return x
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _count(B):
|
|
27
|
+
if isinstance(B, (bool, np.bool_)) or not isinstance(B, (int, np.integer)) or B < 1:
|
|
28
|
+
raise ValueError("B must be a positive integer")
|
|
29
|
+
return int(B)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _stat(u):
|
|
33
|
+
u = np.asarray(u, dtype=float)
|
|
34
|
+
if not np.all(np.isfinite(u)) or np.any((u < 0) | (u > 1)):
|
|
35
|
+
raise ValueError("CDF must return probabilities between 0 and 1")
|
|
36
|
+
# Preserve true zero/one probabilities, yielding infinite tail information.
|
|
37
|
+
with np.errstate(divide="ignore", invalid="ignore"):
|
|
38
|
+
lo = float(-2 * np.log(u).sum())
|
|
39
|
+
hi = float(-2 * np.log1p(-u).sum())
|
|
40
|
+
return max(lo, hi), lo, hi
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _pval(obs, stats):
|
|
44
|
+
return float((1 + np.count_nonzero(stats >= obs)) / (len(stats) + 1))
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def mtit(x, cdf: Callable, *args, B: int = 10000, random_state=None, **kwargs) -> MTITResult:
|
|
48
|
+
"""Test a fully specified continuous CDF using PIT Monte Carlo calibration.
|
|
49
|
+
|
|
50
|
+
``cdf(x, *args, **kwargs)`` must return values in [0, 1]. All parameters
|
|
51
|
+
must be specified *before* observing x, not estimated from x.
|
|
52
|
+
"""
|
|
53
|
+
x, B = _sample(x), _count(B)
|
|
54
|
+
if not callable(cdf):
|
|
55
|
+
raise TypeError("cdf must be callable")
|
|
56
|
+
u = np.asarray(cdf(x, *args, **kwargs), dtype=float)
|
|
57
|
+
if u.shape != x.shape:
|
|
58
|
+
raise ValueError("cdf must return one probability per observation")
|
|
59
|
+
observed, lower, upper = _stat(u)
|
|
60
|
+
rng = np.random.default_rng(random_state)
|
|
61
|
+
stats = np.empty(B)
|
|
62
|
+
for b in range(B):
|
|
63
|
+
stats[b] = _stat(rng.uniform(size=x.size))[0]
|
|
64
|
+
return MTITResult(observed, _pval(observed, stats), lower, upper,
|
|
65
|
+
"MTIT (fully specified continuous null; PIT Monte Carlo)", len(x), B)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def mtit_normal(x, B: int = 10000, random_state=None) -> MTITResult:
|
|
69
|
+
"""Composite normality test; bootstrap re-estimates mean and sample SD."""
|
|
70
|
+
x, B = _sample(x), _count(B)
|
|
71
|
+
mu, sd = float(np.mean(x)), float(np.std(x, ddof=1))
|
|
72
|
+
if sd <= 0:
|
|
73
|
+
raise ValueError("sample standard deviation must be positive")
|
|
74
|
+
observed, lower, upper = _stat(norm.cdf(x, loc=mu, scale=sd))
|
|
75
|
+
rng = np.random.default_rng(random_state)
|
|
76
|
+
stats = np.empty(B)
|
|
77
|
+
for b in range(B):
|
|
78
|
+
xb = rng.normal(mu, sd, len(x))
|
|
79
|
+
mub, sdb = np.mean(xb), np.std(xb, ddof=1)
|
|
80
|
+
stats[b] = _stat(norm.cdf(xb, loc=mub, scale=sdb))[0]
|
|
81
|
+
return MTITResult(observed, _pval(observed, stats), lower, upper,
|
|
82
|
+
"MTIT normality test (parametric bootstrap)", len(x), B)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def mtit_exponential(x, B: int = 10000, random_state=None) -> MTITResult:
|
|
86
|
+
"""Composite exponential goodness-of-fit test with estimated rate."""
|
|
87
|
+
x, B = _sample(x), _count(B)
|
|
88
|
+
if np.any(x < 0) or np.mean(x) <= 0:
|
|
89
|
+
raise ValueError("exponential data must be nonnegative with a positive mean")
|
|
90
|
+
rate = 1.0 / np.mean(x)
|
|
91
|
+
u = -np.expm1(-rate*x)
|
|
92
|
+
observed, lower, upper = _stat(u)
|
|
93
|
+
rng = np.random.default_rng(random_state)
|
|
94
|
+
stats = np.empty(B)
|
|
95
|
+
for b in range(B):
|
|
96
|
+
xb = rng.exponential(scale=1/rate, size=len(x))
|
|
97
|
+
rateb = 1/np.mean(xb)
|
|
98
|
+
stats[b] = _stat(-np.expm1(-rateb*xb))[0]
|
|
99
|
+
return MTITResult(observed, _pval(observed, stats), lower, upper,
|
|
100
|
+
"MTIT exponential test (parametric bootstrap)", len(x), B)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mtit-gof
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Maximum Tail Information Test for goodness-of-fit of continuous distributions
|
|
5
|
+
Author: Said Al Hadhrami, Amer Al Omari
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
9
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
10
|
+
Requires-Python: >=3.10
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
Requires-Dist: numpy>=1.24
|
|
13
|
+
Requires-Dist: scipy>=1.10
|
|
14
|
+
Provides-Extra: test
|
|
15
|
+
Requires-Dist: pytest>=7; extra == "test"
|
|
16
|
+
|
|
17
|
+
# MTIT for Python (development release)
|
|
18
|
+
|
|
19
|
+
Python implementation of the **Maximum Tail Information Test (MTIT)** developed for goodness-of-fit of continuous distributions. This is an independently packaged Python version; it has **not been published on PyPI**.
|
|
20
|
+
|
|
21
|
+
## Install locally
|
|
22
|
+
|
|
23
|
+
Open a terminal in this folder and run:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
python -m pip install .
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Examples
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
import numpy as np
|
|
33
|
+
from scipy.stats import norm, gamma
|
|
34
|
+
from mtit import mtit, mtit_normal, mtit_exponential
|
|
35
|
+
|
|
36
|
+
rng = np.random.default_rng(123)
|
|
37
|
+
x = rng.normal(size=50)
|
|
38
|
+
|
|
39
|
+
# Specified N(0,1) null
|
|
40
|
+
print(mtit(x, norm.cdf, loc=0, scale=1, B=999, random_state=123))
|
|
41
|
+
|
|
42
|
+
# Normality with estimated mean and standard deviation
|
|
43
|
+
print(mtit_normal(x, B=999, random_state=123))
|
|
44
|
+
|
|
45
|
+
# Specified Gamma(shape=3, rate=2); scipy scale = 1/rate
|
|
46
|
+
z = rng.gamma(shape=3, scale=0.5, size=50)
|
|
47
|
+
print(mtit(z, gamma.cdf, a=3, scale=0.5, B=999, random_state=123))
|
|
48
|
+
|
|
49
|
+
# Exponential with unknown rate
|
|
50
|
+
w = rng.exponential(scale=2, size=50)
|
|
51
|
+
print(mtit_exponential(w, B=999, random_state=123))
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The MTIT statistic is the maximum of lower- and upper-tail information:
|
|
55
|
+
`T_L=-2 sum(log(F(x_i)))`, `T_U=-2 sum(log(1-F(x_i)))`.
|
|
56
|
+
The general test simulates the null distribution from i.i.d. uniform values via the probability integral transform; it does **not** fit parameters. The composite normal and exponential tests re-estimate the unknown parameters in each bootstrap sample.
|
|
57
|
+
|
|
58
|
+
Results use a Monte Carlo p-value `(1 + number of bootstrap statistics >= observed) / (B + 1)`.
|
|
59
|
+
For composite-normality testing, sample SD (`ddof=1`) is used. The implementation must undergo further cross-language numerical comparison and statistical validation before public publication. The original upper-tail rejection rule may have low power against some bounded alternatives.
|
|
60
|
+
|
|
61
|
+
## Run tests
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
python -m pip install '.[test]'
|
|
65
|
+
python -m pytest -q
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Build (not yet publish)
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
python -m pip install build twine
|
|
72
|
+
python -m build
|
|
73
|
+
python -m twine check dist/*
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
PyPI project name `mtit-gof` is **provisional** until ownership and availability are confirmed. Before release, add the verified citation to the accepted MTIT paper and check the code and its numerical equivalence to the final R release. The local package version is `0.1.0` and does not imply PyPI publication.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
mtit/__init__.py,sha256=QU8CJkJhmTsMxx3vD2rMFckvd8QQMKJ2x1RzWSzneMM,229
|
|
2
|
+
mtit/core.py,sha256=mzOLcvLpE8sLZkRHx7iGu0Drc1U7i5o3CwHuribCPG8,3809
|
|
3
|
+
mtit_gof-0.1.0.dist-info/METADATA,sha256=f2q2ioI-BDgyMpyezhwqIij_aiCbxjajup3Ryxd1twU,2872
|
|
4
|
+
mtit_gof-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
5
|
+
mtit_gof-0.1.0.dist-info/top_level.txt,sha256=RaCQTOcj8Tk7g3j2MnQRMEvMtMq3fRZOJCx9xQ1GsVA,5
|
|
6
|
+
mtit_gof-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mtit
|