Tail Estimators¶
tail_index
¶
Estimators of the extreme-value index.
Two conventions are in circulation and they are reciprocals of each other:
- the tail index
alpha, from the regular variation formP(X > x) ~ L(x) * x**-alpha, common in economics and network science; - the extreme-value index
xi, also writtengamma, the convention of the extreme value theory literature, withgamma = 1 / alpha.
Every estimator in this module returns gamma, not alpha. Larger gamma means a heavier tail. Invert it to recover alpha:
gamma = hill_estimator(data, k=100)
alpha = 1 / gamma
The module name refers to the quantity being estimated, not to the
parameterisation of the return value. :func:moment_estimator is the one
exception in shape, returning the pair (gamma, alpha) for convenience, and
:func:tail_index_confidence_interval reports both.
adaptive_trim_selection
¶
Choose the trimming parameter for the trimmed Hill estimator from the data.
:func:trimmed_hill_estimator needs r to exceed the number of
contaminated observations, and in practice nobody knows that number. This
finds it.
The normalised log-spacings are iid exponential under a Pareto tail, and contamination among the largest observations inflates one of them. The rule is to trim past the deepest anomalous spacing, not the first:
Several outliers of similar size sit close together, so the gaps between them are small and only the gap below the last one is large. Stopping at the first ordinary-looking spacing would therefore find nothing at all when there is more than one outlier, and report the sample as clean.
Every spacing from max_trim down to the first is tested at
level / max_trim, a Bonferroni correction for the scan, so on clean data
the estimator over-trims with probability close to level -- measured at
0.009, 0.052 and 0.094 for nominal 0.01, 0.05 and 0.10.
Detection is not certain, and how likely it is depends on how extreme the
outliers are. With three of them among 10,000 Pareto(2) draws and
k = 300, the trimming is found in 100% of samples at five times the true
sample maximum, 95% at three times, 64% at twice and 46% at one and a half
times. An outlier only half again the size of the largest genuine
observation is not reliably distinguishable from the tail itself, and no
procedure could make it so.
Parameters¶
data : sequence of floats
Sample values. The top k + 1 are read and must be positive.
k : int
Number of top order statistics to use, with 1 < k < n.
max_trim : int, optional
Largest trimming considered. Defaults to k // 4, which is generous:
the scan is cheap and a limit that is too low is the one way this
procedure fails badly.
level : float, optional
Family-wise probability of over-trimming clean data.
Returns¶
dict
trim: the chosen r. gamma: the trimmed Hill estimate at that
r. p_values: the test for each spacing from 0 to max_trim-1.
saturated: whether an anomaly was found below the scanned range,
meaning max_trim is too small and trim is not to be trusted.
deepest_anomaly: where that anomaly was, or None.
Raises¶
ValueError If k, max_trim or level is out of range, or the data is not positive.
References¶
Bhattacharya, S., Kallitsis, M., & Stoev, S. (2019). Trimming the Hill estimator: robustness, optimality and adaptivity. arXiv:1705.03088. The trimmed estimator is theirs; the selection rule here is a sequential exact test on the log-spacings rather than their procedure.
Examples¶
from heavytails import Pareto data = sorted(Pareto(alpha=2.0, xm=1.0).rvs(10000, seed=1), reverse=True) for j in range(3): ... data[j] = 1e6 * (j + 1) result = adaptive_trim_selection(sorted(data, reverse=True), k=300) result["trim"] 3 round(result["gamma"], 3) 0.479 clean = Pareto(alpha=2.0, xm=1.0).rvs(10000, seed=1) round(hill_estimator(clean, k=300), 3) # what the outliers destroyed 0.479
Source code in heavytails/tail_index.py
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 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 | |
adaptive_trimmed_hill_estimator
¶
Trimmed Hill with the trimming chosen from the data.
Fixed trimming forces a choice nobody can make well: too little leaves the
contamination in, too much throws away good observations. This picks r
by testing the log-spacings, and on simulated contamination it picks the
right number -- the median choice equals the number of planted outliers at
1, 2, 3, 5 and 8 of them, and equals zero when there are none.
What that buys, on 10,000 Pareto(2) draws with k = 300:
============================ ========= =========== Sample Adaptive Plain Hill ============================ ========= =========== clean 0.5004 0.5007 3 contaminated 0.5007 0.6004 8 contaminated 0.5007 0.7971 ============================ ========= ===========
and on clean data it costs almost nothing: the standard deviation is 0.0295 against 0.0292 for the plain estimator, a 1% loss. The robustness is close to free because trimming is applied only when the data asks for it.
Parameters¶
data : sequence of floats
Sample values. The top k + 1 are read and must be positive.
k : int
Number of top order statistics to use, with 1 < k < n.
max_trim : int, optional
Largest trimming considered, defaulting to k // 4.
level : float, optional
Family-wise probability of over-trimming clean data.
Returns¶
float
The extreme-value index estimate gamma, equal to 1 / alpha.
Raises¶
ValueError
If the arguments are out of range, or if contamination is found deeper
than max_trim reaches. That case is an error rather than a number
because the estimate would be indistinguishable from a clean one: with
30 outliers and max_trim = 20 the scan sees only the gaps between
them, finds nothing, and reports 1.79 for a true 0.5.
See Also¶
adaptive_trim_selection : The chosen r and the tests behind it.
trimmed_hill_estimator : Fixed trimming, when r is known.
References¶
Bhattacharya, S., Kallitsis, M., & Stoev, S. (2019). Trimming the Hill estimator: robustness, optimality and adaptivity. arXiv:1705.03088.
Examples¶
from heavytails import Pareto data = sorted(Pareto(alpha=2.0, xm=1.0).rvs(10000, seed=1), reverse=True) for j in range(3): ... data[j] = 1e6 * (j + 1) round(adaptive_trimmed_hill_estimator(sorted(data, reverse=True), k=300), 3) 0.479 round(hill_estimator(data, k=300), 3) # the same sample, untrimmed 0.581
Source code in heavytails/tail_index.py
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 | |
bias_reduced_hill_estimator
¶
Bias-reduced Hill estimator of Caeiro, Gomes and Pestana (2005).
The Hill estimator trades variance at small k against bias at large k, and that bias is systematic rather than random: it comes from the second-order behaviour of the tail, so it can be estimated and subtracted.
gamma = hill(k) * (1 - beta / (1 - rho) * (n/k) ** rho)
Measured over forty samples at n = 20000 with rho supplied, the bias of
the Hill estimator falls by a factor of four to twenty::
case k Hill corrected
Frechet(2) 2000 0.0162 0.0046
BurrXII(c=2, k=1) 2000 0.0298 0.0050
BurrXII(c=1, k=2) 2000 0.1422 0.0081
Note
Supply rho where you can. Estimating it works and the correction
still helps, taking the worst case above from 0.1422 to 0.0354, but
:func:second_order_rho is unstable and a poor estimate costs most of
the benefit. Practitioners commonly fix rho at a canonical value such
as -1 rather than estimate it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[float]
|
Sample values. The top |
required |
k
|
int
|
Number of top order statistics, with |
required |
rho
|
float | None
|
Second-order shape parameter, which must be negative. Estimated
with :func: |
None
|
beta
|
float | None
|
Second-order scale parameter. Estimated with
:func: |
None
|
Returns:
| Type | Description |
|---|---|
float
|
The extreme-value index estimate gamma, equal to |
Raises:
| Type | Description |
|---|---|
ValueError
|
If k is out of range, rho is not negative, or either second-order parameter cannot be estimated. |
References
Caeiro, F., Gomes, M. I., & Pestana, D. (2005). Direct reduction of bias of the classical Hill estimator. Revstat, 3(2), 113-136.
Examples:
>>> from heavytails import Frechet
>>> data = Frechet(alpha=2.0, s=1.0, m=0.0).rvs(20000, seed=1)
>>> round(bias_reduced_hill_estimator(data, k=2000, rho=-1.0), 1)
0.5
Source code in heavytails/tail_index.py
1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 | |
fit_generalized_pareto
¶
Fit a generalized Pareto distribution to exceedances by maximum likelihood.
Uses the reduction of Grimshaw (1993), which turns the two-parameter
problem into a one-dimensional search over theta = xi / sigma. The
profile is scanned coarsely to bracket the maximum and then refined by
golden-section search, which needs no derivatives and no third-party
optimiser.
Valid for any sign of xi, so unlike the Hill family this does not
assume the tail is heavy.
Parameters¶
exceedances : sequence of floats Amounts by which observations exceed a threshold. All must be strictly positive. scan : int, optional Number of points in the initial bracketing scan. The profile can be flat far from its maximum, so a scan is more reliable than starting the search from an arbitrary point. iterations : int, optional Golden-section refinement steps. Sixty reduces the bracket by a factor of about 1e-13, which is past the precision of the data.
Returns¶
dict
xi (shape), sigma (scale), n and log_likelihood.
Raises¶
ValueError If fewer than two exceedances are given, or any is not positive.
References¶
Grimshaw, S. D. (1993). Computing maximum likelihood estimates for the generalized Pareto distribution. Technometrics, 35(2), 185-191.
Examples¶
from heavytails import GeneralizedPareto y = GeneralizedPareto(xi=0.5, sigma=1.0, mu=0.0).rvs(5000, seed=7) round(fit_generalized_pareto(y)["xi"], 1) 0.5
Source code in heavytails/tail_index.py
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 | |
generalized_hill_estimator
¶
Generalized Hill (UH) estimator of the extreme-value index.
The Hill estimator is only valid for gamma > 0. The generalized Hill
estimator applies the same log-excess averaging to the UH statistics
UH_j = X_(j+1) * H_j, where H_j is the Hill estimator on the top j
order statistics, and is consistent for every gamma in the reals:
gamma_hat = (1/k) * sum_{j=1}^{k} log(UH_j) - log(UH_{k+1})
Use it when you are not certain the tail is heavy. Where Hill applies, the two agree closely and Hill is slightly more efficient.
Parameters¶
data : sequence of floats
Sample values. The top k + 1 are read, and those must be
positive; values below the threshold are never touched.
k : int
Number of top order statistics to use, with 1 < k < n - 1. One more
order statistic is needed than for the Hill estimator, because the
reference term is UH_{k+1}.
Returns¶
float The extreme-value index estimate gamma.
Raises¶
ValueError If k is out of range, the data is not positive, or the UH statistics are not positive (which happens only for degenerate samples).
References¶
Beirlant, J., Vynckier, P., & Teugels, J. L. (1996). Excess functions and estimation of the extreme-value index. Bernoulli, 2(4), 293-318.
Examples¶
from heavytails import Pareto data = Pareto(alpha=2.0, xm=1.0).rvs(20000, seed=1) round(generalized_hill_estimator(data, k=2000), 1) 0.5
Source code in heavytails/tail_index.py
gpd_mle_estimator
¶
Peaks-over-threshold estimator: fit a GPD to the exceedances above X_(k+1).
The parametric alternative to the semiparametric estimators in this module. Rather than averaging a functional of the upper order statistics, it fits a two-parameter model to the exceedances and estimates shape and scale jointly, which is what the Pickands-Balkema-de Haan theorem licenses.
Valid for any sign of the index, and noticeably slower than the closed-form
estimators because it optimises. That matters mainly for bootstrapping, so
reduce n_bootstrap accordingly.
Parameters¶
data : sequence of floats
Sample values.
k : int
Number of exceedances to use, with 1 < k < n. The threshold is the
(k+1)-th largest observation.
Returns¶
float The extreme-value index estimate gamma, which is the GPD shape parameter.
Raises¶
ValueError If k is out of range or the exceedances are degenerate.
Examples¶
from heavytails import Pareto data = Pareto(alpha=2.0, xm=1.0).rvs(20000, seed=3) round(gpd_mle_estimator(data, k=2000), 1) 0.5
Source code in heavytails/tail_index.py
harmonic_moment_estimator
¶
Harmonic moment estimator of the extreme-value index.
Where the Hill estimator averages log(X_(i) / u) for a threshold
u = X_(k+1), this averages the powers of the reciprocal ratios
R_i = u / X_(i), which lie in (0, 1]. Under an exact Pareto tail
those ratios are Beta(alpha, 1) distributed, so
E[R**beta] = alpha / (alpha + beta) and
alpha_hat = beta * H / (1 - H), H = mean(R_i ** beta)
The estimate returned is gamma = 1 / alpha_hat.
The point of the reciprocal form is bounded influence. Hill's contributions
are unbounded above, so one sufficiently extreme observation moves the
estimate arbitrarily far. Here a contaminated observation sent to infinity
contributes R_i -> 0, and its influence is bounded. Sending a single
observation of ten thousand from 1e2 to 1e30 moves the Hill
estimate from 0.502 to 0.631, and moves this one not at all.
beta trades robustness against efficiency. Larger values weight the
observations nearest the threshold more heavily and the extreme ones less,
which is more robust and less efficient. As beta tends to zero the
estimator tends to the Hill estimator.
Like Hill this assumes gamma > 0; see
:func:generalized_hill_estimator if the sign is in doubt.
Parameters¶
data : sequence of floats
Sample values. The top k + 1 are read, and those must be
positive; values below the threshold are never touched.
k : int
Number of top order statistics to use, with 1 < k < n.
beta : float, optional
Robustness parameter, strictly positive. beta = 1 is the t-Hill
estimator, available separately as :func:t_hill_estimator.
Returns¶
float
The extreme-value index estimate gamma, equal to 1 / alpha.
Raises¶
ValueError
If k or beta is out of range, the data is not positive, or the sample
is degenerate enough that H reaches 1.
References¶
Beran, J., Schell, D., & Stehlik, M. (2014). The harmonic moment tail index estimator. Annals of the Institute of Statistical Mathematics, 66.
Examples¶
from heavytails import Pareto data = Pareto(alpha=2.0, xm=1.0).rvs(50000, seed=11) round(harmonic_moment_estimator(data, k=2500, beta=1.0), 1) 0.5
Source code in heavytails/tail_index.py
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 | |
hill_estimator
¶
Hill estimator for the tail index gamma (where gamma = 1/alpha for Pareto).
Parameters¶
data : sequence of floats k : int number of top order statistics (1 < k < n)
Returns¶
gamma : float The tail index estimate (gamma = 1/alpha for Pareto distributions)
Source code in heavytails/tail_index.py
hill_plot
¶
Hill estimates across a range of k, for the Hill plot.
Every tail index estimator depends on how many upper order statistics it uses, and there is no universally correct choice: small k means low bias and high variance, large k the reverse. The standard practice is to plot the estimate against k and read it off a stable plateau. If there is no plateau, the data does not support a tail index estimate and forcing one produces a confident wrong answer.
Parameters¶
data : sequence of floats
Sample values.
ks : sequence of ints, optional
Values of k to evaluate. Defaults to a logarithmically spaced sweep
from 5 to n // 2, which keeps the plot readable for large samples.
Returns¶
list of (int, float)
(k, gamma_hat) pairs, ordered by k.
Examples¶
from heavytails import Pareto data = Pareto(alpha=2.0, xm=1.0).rvs(1000, seed=1) points = hill_plot(data) all(k > 0 for k, _ in points) True
Source code in heavytails/tail_index.py
moment_estimator
¶
Dekkers-Einmahl-de Haan moment estimator for tail index.
Returns (gamma_hat, alpha_hat) where alpha = 1/gamma.
Source code in heavytails/tail_index.py
orthogonalized_bias_reduced_hill_estimator
¶
orthogonalized_bias_reduced_hill_estimator(
data,
k,
r=0,
rho=-1.0,
*,
adaptive_trim=False,
max_trim=None,
level=None,
)
Trimmed log-spacing estimator with explicit second-order orthogonalization.
This is the local reduced-bias building block, not a novelty claim. Weighted
log-spacing and exponential-regression estimators with bias-cancelling
constraints are part of the reduced-bias tail-index literature. The useful
role of this function is to expose a transparent (r, k) candidate that
can be combined with adaptive trimming, threshold selection and
cross-fitting.
The ordinary Hill estimator is the mean of the normalised upper
log-spacings. Under a second-order tail approximation those spacings have
mean gamma + b * (j / (k + 1)) ** (-rho). This estimator fits that
one-covariate exponential-regression mean model and returns the intercept,
which is equivalent to a weighted sum whose weights satisfy both::
sum(w_j) = 1
sum(w_j * (j / (k + 1)) ** (-rho)) = 0
The first identity targets gamma. The second removes the leading
second-order bias term. Setting r > 0 discards the largest r
spacings before the regression; setting adaptive_trim=True chooses that
trimming level with :func:adaptive_trim_selection.
The variance is intentionally higher than Hill's on an exact Pareto tail. This is a bias-variance trade: use it when second-order bias or extreme contamination is more damaging than that extra variance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[float]
|
Sample values. The top |
required |
k
|
int
|
Number of top order statistics, with |
required |
r
|
int
|
Number of largest spacings to discard when |
0
|
rho
|
float | None
|
Negative second-order shape parameter. Defaults to |
-1.0
|
adaptive_trim
|
bool
|
If true, choose |
False
|
max_trim
|
int | None
|
Largest trimming considered by the adaptive selector. |
None
|
level
|
float | None
|
Family-wise probability of over-trimming clean data. Defaults to a conservative sequence that tends to zero with the sample size. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
The extreme-value index estimate gamma, equal to |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the arguments are out of range, the data is not positive,
rho is non-negative, or adaptive trimming finds contamination below
|
Examples:
>>> from heavytails import Frechet
>>> data = Frechet(alpha=2.0, s=1.0, m=0.0).rvs(20000, seed=1)
>>> round(orthogonalized_bias_reduced_hill_estimator(data, k=2000), 1)
0.5
Source code in heavytails/tail_index.py
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 | |
pickands_estimator
¶
Pickands tail index estimator (extreme-value index gamma).
gammâ = (1 / log(m)) * log( (X_k - X_{2k}) / (X_{mk} - X_{2mk}) )
Source code in heavytails/tail_index.py
recommended_rho_k
¶
Recommended number of order statistics for estimating rho.
Estimating the second-order parameter needs far more of the sample than
estimating gamma does, because it describes how the tail approaches its
limit rather than the limit itself. The usual recommendation is
min(n - 1, floor(2n / log log n)), roughly 85% of the sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Sample size, at least 16 so that |
required |
Returns:
| Type | Description |
|---|---|
int
|
The recommended k. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If n is below 16. |
Source code in heavytails/tail_index.py
second_order_beta
¶
Estimate the second-order scale parameter beta, given rho.
Implements the estimator of Gomes and Martins (2002), built from the
normalised log-spacings weighted by powers of i / k.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[float]
|
Sample values. |
required |
k
|
int
|
Number of order statistics, matching the k used for gamma. |
required |
rho
|
float
|
The second-order shape parameter, which must be negative. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The estimate of beta. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If rho is not negative, k is out of range, or the estimator is degenerate. |
References
Gomes, M. I., & Martins, M. J. (2002). Asymptotically unbiased estimators of the tail index based on external estimation of the second order parameter. Extremes, 5(1), 5-31.
Source code in heavytails/tail_index.py
second_order_rho
¶
Estimate the second-order parameter rho of a regularly varying tail.
Under second-order regular variation the tail behaves like
C x**(-1/gamma) [1 + D x**(rho/gamma) + ...] with rho < 0. The
parameter controls how fast the tail approaches its Pareto limit, and so
how quickly the Hill estimator's bias grows with k.
Implements the estimator of Fraga Alves, Gomes and de Haan (2003), built from the first three moments of the log-excesses.
Warning
This estimator is unstable, which is a property of the estimator
rather than of this implementation. Sweeping k on a Frechet sample
whose true rho is -1 gives estimates ranging from -0.07 to -20.5, the
latter at a pole where the denominator crosses zero. Prefer supplying
a known or assumed rho to :func:bias_reduced_hill_estimator, and
plot the estimate against k before trusting any single value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[float]
|
Sample values. The top |
required |
k
|
int | None
|
Number of order statistics. Defaults to :func: |
None
|
tau
|
float
|
Tuning parameter. |
0.0
|
Returns:
| Type | Description |
|---|---|
float
|
The estimate of rho, always negative. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If k is out of range, tau is negative, or the moments are degenerate. |
References
Fraga Alves, M. I., Gomes, M. I., & de Haan, L. (2003). A new class of semi-parametric estimators of the second order parameter. Portugaliae Mathematica, 60(2), 193-213.
Examples:
>>> from heavytails import Frechet
>>> data = Frechet(alpha=2.0, s=1.0, m=0.0).rvs(20000, seed=11)
>>> second_order_rho(data) < 0 # true value is -1
True
Source code in heavytails/tail_index.py
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 | |
smoothed_hill_estimator
¶
Resnick-Starica smoothed Hill estimator of the extreme-value index.
The ordinary Hill estimate varies substantially with k, which is the practical difficulty the Hill plot exists to work around. This averages it over a range of k instead:
gamma_hat(k, u) = 1/((u-1)k) * sum_{j=k+1}^{floor(u*k)} hill(j)
The asymptotic variance falls from gamma**2 to
gamma**2 * 2*(u - 1 - log(u)) / (u - 1)**2: about 0.61 times at
u = 2 and 0.45 times at u = 3. The cost is bias, because a larger
u averages over a wider range of k and so reaches further into the body
of the distribution. Values between 2 and 3 are the usual compromise.
Like the Hill estimator it assumes gamma > 0; see
:func:generalized_hill_estimator if the sign is in doubt.
Parameters¶
data : sequence of floats
Sample values. The top k + 1 are read, and those must be
positive; values below the threshold are never touched.
k : int
Lower end of the averaging range.
u : float, optional
Smoothing parameter, strictly greater than 1. The average runs over
j in (k, u*k], so u*k must be less than the sample size.
Returns¶
float The extreme-value index estimate gamma.
Raises¶
ValueError
If u <= 1, the averaging range is empty, or it runs past the end of
the sample.
References¶
Resnick, S., & Starica, C. (1997). Smoothing the Hill estimator. Advances in Applied Probability, 29(1), 271-293.
Examples¶
from heavytails import Pareto data = Pareto(alpha=2.0, xm=1.0).rvs(20000, seed=11) round(smoothed_hill_estimator(data, k=1000, u=2.0), 1) 0.5
Source code in heavytails/tail_index.py
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 | |
smoothed_hill_variance_ratio
¶
Asymptotic variance of the smoothed Hill estimator relative to Hill.
Returns 2*(u - 1 - log(u)) / (u - 1)**2, the factor by which
:func:smoothed_hill_estimator reduces the asymptotic variance of the
ordinary Hill estimator. It tends to 1 as u tends to 1 from above, and
decreases as u grows.
This describes variance only. Larger u also increases bias, so it is
not a quantity to minimise blindly.
Parameters¶
u : float Smoothing parameter, strictly greater than 1.
Returns¶
float The variance ratio, between 0 and 1.
Examples¶
round(smoothed_hill_variance_ratio(2.0), 4) 0.6137 round(smoothed_hill_variance_ratio(3.0), 4) 0.4507
Source code in heavytails/tail_index.py
t_hill_estimator
¶
t-Hill estimator of the extreme-value index.
The harmonic moment estimator at beta = 1, given its own name because
the literature treats it as a distinct estimator with its own results. See
:func:harmonic_moment_estimator for the derivation and for the meaning of
beta.
It replaces Hill's unbounded logarithmic contributions with bounded reciprocal ratios, which is what makes it insensitive to how extreme a contaminated observation is.
Parameters¶
data : sequence of floats
Sample values. The top k + 1 are read, and those must be
positive; values below the threshold are never touched.
k : int
Number of top order statistics to use, with 1 < k < n.
Returns¶
float
The extreme-value index estimate gamma, equal to 1 / alpha.
References¶
Fabian, Z. (2001). Induced cores and their use in robust parametric estimation. Communications in Statistics.
Jordanova, P., Stehlik, M., et al. (2016). Weak properties and robustness of t-Hill estimators. Extremes, 19(4).
Examples¶
from heavytails import Pareto data = Pareto(alpha=2.0, xm=1.0).rvs(50000, seed=11) round(t_hill_estimator(data, k=2500), 1) 0.5
Source code in heavytails/tail_index.py
tail_index_confidence_interval
¶
tail_index_confidence_interval(
data,
k,
*,
estimator="hill",
level=0.95,
method="asymptotic",
n_bootstrap=500,
seed=None,
estimator_kwargs=None,
)
Point estimate and confidence interval for the extreme-value index.
A tail index without an interval is not usable: the estimate depends on a choice of k that no rule fixes, and the sampling variability at realistic sample sizes is large.
Two methods are offered:
asymptotic
Only available for the Hill estimator, whose limiting distribution is
sqrt(k) (gamma_hat - gamma) -> N(0, gamma^2), giving the interval
gamma_hat * (1 +/- z / sqrt(k)). It assumes k is in the range where
the estimator is unbiased, so it understates the true uncertainty when
the threshold is chosen badly.
bootstrap
Resamples the data with replacement and takes percentiles of the
resulting estimates. Available for every estimator and free of
distributional assumptions, at the cost of n_bootstrap refits. Note
that resampling does not capture the bias from the choice of k, only
the variance.
Parameters¶
data : sequence of floats
Sample values.
k : int
Number of top order statistics.
estimator : str, optional
One of hill, generalized_hill, moment or pickands.
level : float, optional
Confidence level in (0, 1).
method : str, optional
asymptotic or bootstrap.
n_bootstrap : int, optional
Number of bootstrap resamples.
seed : int, optional
Seed for reproducible bootstrap resampling.
estimator_kwargs : dict, optional
Extra keyword arguments for the estimator, such as {"r": 5} for the
trimmed Hill estimator or {"u": 3.0} for the smoothed one. Without
this the estimators run at their defaults, which for trimmed_hill
means no trimming at all and therefore no robustness.
Returns¶
dict
gamma, alpha, lower, upper, level, method,
estimator and k. alpha is 1/gamma, or None when the
estimate is not positive and the reciprocal is meaningless.
Raises¶
ValueError For an unknown estimator or method, or an out-of-range level.
Examples¶
from heavytails import Pareto data = Pareto(alpha=2.0, xm=1.0).rvs(2000, seed=7) result = tail_index_confidence_interval(data, k=200) result["lower"] < result["gamma"] < result["upper"] True
Source code in heavytails/tail_index.py
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 | |
threshold_averaged_orthogonalized_hill_estimator
¶
threshold_averaged_orthogonalized_hill_estimator(
data,
k,
*,
min_k=None,
grid_size=12,
rho=-1.0,
adaptive_trim=True,
max_trim=None,
level=None,
critical=None,
convex_weights=True,
crossfit=True,
seed=None,
)
Threshold-averaged orthogonalized tail-index estimator.
By default this uses two-fold cross-fitting: threshold sets and rho are
learned on one half of the sample and evaluated on the other, then the roles
are swapped. The trimming count and covariance weights are recomputed on the
evaluation half, because a random split need not contain the same number of
contaminated extremes as the training half. Set crossfit=False to return
the full-sample diagnostic estimate from
:func:threshold_averaged_orthogonalized_hill_selection.
Source code in heavytails/tail_index.py
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 | |
threshold_averaged_orthogonalized_hill_selection
¶
threshold_averaged_orthogonalized_hill_selection(
data,
k,
*,
min_k=None,
grid_size=12,
rho=-1.0,
adaptive_trim=True,
max_trim=None,
level=None,
critical=None,
convex_weights=True,
)
Choose and average compatible orthogonalized (r, k) candidates.
This is the threshold-aggregation layer around
:func:orthogonalized_bias_reduced_hill_estimator. A logarithmic grid of
thresholds from min_k to k is evaluated from the most extreme data
toward the body. A new threshold is admitted while its estimate remains
compatible with the estimates already admitted, using the exact
log-spacing covariance approximation under a Pareto tail. The admitted
estimates are then averaged with minimum-variance weights based on the same
covariance approximation.
The returned dictionary exposes the candidate pairs and threshold decisions. Use
:func:threshold_averaged_orthogonalized_hill_estimator when only the point
estimate is needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[float]
|
Sample values. The top |
required |
k
|
int
|
Largest threshold in the grid, with |
required |
min_k
|
int | None
|
Smallest threshold in the grid. Defaults to |
None
|
grid_size
|
int
|
Number of log-spaced thresholds to propose. |
12
|
rho
|
float | None
|
Negative second-order shape parameter. If |
-1.0
|
adaptive_trim
|
bool
|
Whether to choose trimming separately at each threshold. |
True
|
max_trim
|
int | None
|
Largest trimming considered by the adaptive selector. |
None
|
level
|
float | None
|
Family-wise probability of over-trimming clean data. Defaults to a conservative sequence that tends to zero with the sample size. |
None
|
critical
|
float | None
|
Compatibility cutoff, in approximate standard errors.
Defaults to a grid-size penalty, |
None
|
convex_weights
|
bool
|
If true, constrain the threshold-averaging weights to be non-negative. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary with |
dict[str, Any]
|
|
dict[str, Any]
|
threshold-averaging |
dict[str, Any]
|
so under an exact Pareto spacing approximation the candidate variance |
dict[str, Any]
|
is approximately |
Source code in heavytails/tail_index.py
1023 1024 1025 1026 1027 1028 1029 1030 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 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 | |
trimmed_hill_estimator
¶
Trimmed Hill estimator: Hill with the r largest observations discarded.
The Hill estimator gives enormous leverage to the largest order statistics, which enter through unbounded logarithms of ratios, so a handful of contaminated observations is enough to destroy it. Replacing the three largest of ten thousand Pareto(2) draws with outliers moves the ordinary Hill estimate from 0.50 to 0.66; trimming five recovers 0.50.
On clean data the cost is small. For the same sample, the standard
deviation rises only from 0.0296 at r = 0 to 0.0302 at r = 10.
r must exceed the number of contaminated observations. Trimming two
when three are contaminated leaves the estimate essentially as bad as
trimming none, because the third still enters through a spacing.
Like the Hill estimator this assumes gamma > 0; see
:func:generalized_hill_estimator if the sign is in doubt.
Parameters¶
data : sequence of floats
Sample values. The top k + 1 are read, and those must be
positive; values below the threshold are never touched.
k : int
Number of top order statistics to use, with 1 < k < n.
r : int, optional
Number of largest observations to discard, with 0 <= r < k.
r = 0 reproduces the ordinary Hill estimator exactly.
Returns¶
float
The extreme-value index estimate gamma, equal to 1 / alpha.
Raises¶
ValueError If k or r is out of range, or the data is not positive.
References¶
Bhattacharya, S., Kallitsis, M., & Stoev, S. (2019). Trimming the Hill estimator: robustness, optimality and adaptivity. arXiv:1705.03088.
Examples¶
from heavytails import Pareto data = sorted(Pareto(alpha=2.0, xm=1.0).rvs(10000, seed=1), reverse=True) data[0] = 1e9 # one contaminated observation round(trimmed_hill_estimator(data, k=300, r=5), 1) 0.5
Source code in heavytails/tail_index.py
trimmed_hill_plot
¶
Trimmed Hill estimates across a range of r, for choosing the trimming level.
Read it the way you read a Hill plot. The estimate typically moves sharply
while r is below the number of contaminated observations and then flattens
once they have all been discarded, so the elbow indicates how much
contamination is present. A plot that is flat from r = 0 suggests there
is none.
Parameters¶
data : sequence of floats
Sample values.
k : int
Number of top order statistics to use.
max_trim : int, optional
Largest r to evaluate. Defaults to k // 10, which is enough to
reveal an elbow without spending the whole sample on trimming.
Returns¶
list of (int, float)
(r, gamma_hat) pairs, ordered by r.
Examples¶
from heavytails import Pareto data = Pareto(alpha=2.0, xm=1.0).rvs(5000, seed=1) points = trimmed_hill_plot(data, k=250) first_r, first_gamma = points[0] first_r 0