Validation¶
validation
¶
Validation and quality assurance module for HeavyTails library.
This module provides comprehensive mathematical validation, numerical accuracy testing, and property-based testing for all distributions.
GoodnessOfFitTests
¶
Statistical tests for distribution goodness-of-fit.
Both tests answer a question that AIC and BIC cannot. Information criteria rank candidate models against each other, so the best of a bad set still ranks first. A goodness-of-fit test asks whether the winner is compatible with the data at all.
For heavy tails the Anderson-Darling test is the more informative of the two, because it weights the tails of the distribution. The Kolmogorov-Smirnov statistic is driven by the centre, which is exactly where these families agree with each other.
Examples¶
from heavytails import Pareto data = Pareto(alpha=2.5, xm=1.0).rvs(500, seed=42) tests = GoodnessOfFitTests() result = tests.kolmogorov_smirnov_test( ... data, "pareto", alpha=2.5, xm=1.0 ... ) result["reject"] False
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha_level
|
float
|
Significance level for the |
0.05
|
Source code in heavytails/validation.py
anderson_darling_test
¶
Anderson-Darling test against a named distribution.
The statistic is
A^2 = -n - (1/n) sum_i (2i-1) [ln F(x_i) + ln(1 - F(x_{n+1-i}))].
Unlike the Kolmogorov-Smirnov statistic this weights the tails, which is what makes it the more useful of the two here: heavy-tailed families differ from one another in the tail and agree in the middle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
Sample values. |
required |
distribution
|
str
|
Distribution name, as accepted by the fitting helpers. |
required |
parameters_estimated
|
bool
|
Set when the parameters came from this same
sample; see :meth: |
False
|
**params
|
Any
|
Distribution parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with the same shape as |
dict[str, Any]
|
meth: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the sample is empty or the distribution is unknown. |
Source code in heavytails/validation.py
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 | |
kolmogorov_smirnov_test
¶
Kolmogorov-Smirnov test against a named distribution.
The statistic is the largest vertical distance between the empirical
distribution function and the fitted one,
D = max_i max(i/n - F(x_i), F(x_i) - (i-1)/n).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[float]
|
Sample values. |
required |
distribution
|
str
|
Distribution name, as accepted by the fitting helpers. |
required |
parameters_estimated
|
bool
|
Set when the parameters came from this same
sample. The reported p-value is then conservative, because the
fitted distribution is closer to the data than the null
assumes, and the result carries a |
False
|
**params
|
Any
|
Distribution parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with |
dict[str, Any]
|
|
dict[str, Any]
|
when the p-value should not be read at face value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the sample is empty or the distribution is unknown. |
Source code in heavytails/validation.py
NumericalValidation
¶
Comprehensive numerical validation against scipy and known results.
Validates accuracy of PDF, CDF, PPF, and sampling for all distributions against scipy implementations where available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tolerance
|
float
|
Maximum allowed relative error (default: 1e-10) |
1e-10
|
Source code in heavytails/validation.py
validate_against_scipy
¶
Compare distribution against SciPy implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Distribution name |
required |
params
|
dict[str, float] | None
|
Optional specific parameters to test (uses defaults if None) |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with validation results including errors and pass/fail |
Examples:
>>> validator = NumericalValidation()
>>> if SCIPY_AVAILABLE:
... result = validator.validate_against_scipy("pareto", {"alpha": 2.5, "xm": 1.0})
... result["pass"] or result["max_error"] < 0.01
... else:
... True # Skip if scipy not available
True
Source code in heavytails/validation.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
PropertyBasedTests
¶
Property-based testing for mathematical correctness using Hypothesis.
Tests fundamental mathematical properties that all distributions should satisfy: - PDF non-negativity - CDF monotonicity - PPF/CDF inverse relationship - Probability axioms
Source code in heavytails/validation.py
test_cdf_monotonicity
¶
Test that CDF is monotonically increasing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Distribution name to test |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with test results |
Source code in heavytails/validation.py
test_pdf_nonnegativity
¶
Test that PDF is non-negative for all valid inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Distribution name to test |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with test results |
Examples:
>>> tester = PropertyBasedTests()
>>> result = tester.test_pdf_nonnegativity("pareto")
>>> result["property"]
'pdf_nonnegativity'
Source code in heavytails/validation.py
test_ppf_cdf_inverse
¶
Test that PPF and CDF are inverse functions: CDF(PPF(u)) ≈ u.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Distribution name to test |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with test results |
Source code in heavytails/validation.py
convergence_validation
¶
Validate convergence of numerical algorithms.
Tests convergence properties of iterative algorithms used in the library, such as PPF computation via bisection/Newton-Raphson.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Distribution name to test |
required |
method
|
str
|
Method to test ("ppf", "cdf", or "pdf") |
'ppf'
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with convergence diagnostics |
Examples:
Source code in heavytails/validation.py
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 | |
parameter_stability_check
¶
Check parameter combinations for numerical stability with automatic fixes.
Analyzes parameters for potential numerical issues and provides specific warnings and suggested fixes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
distribution
|
str
|
Distribution name |
required |
**params
|
Any
|
Distribution parameters to check |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with warnings, suggested fixes, and stability assessment |
Examples:
>>> result = parameter_stability_check("pareto", alpha=1e-8, xm=1.0)
>>> len(result["warnings"]) > 0
True
>>> result["stable"]
False
Source code in heavytails/validation.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | |
ppf_edge_case_handler
¶
Handle edge cases in quantile function calculation.
Problematic cases: - u very close to 0 or 1 - Parameters at boundary values - Distributions with bounded support - Numerical overflow/underflow
Should provide graceful degradation and informative errors.