Diagnostic Plots¶
viz
¶
Rendering for the diagnostics, using matplotlib.
The rest of the library returns coordinates rather than figures, which is what keeps matplotlib out of the requirements. That is the right default, but it means every user writes the same twenty lines of matplotlib to look at a Hill plot, and the documentation carries that boilerplate on several pages.
This module draws them. It needs the optional plot extra::
pip install "heavytails[plot]"
Nothing here is imported by :mod:heavytails.plotting, which stays free of
third-party imports, so the library itself is unaffected whether matplotlib is
installed or not.
Every function takes an optional ax and returns the axes it drew on, so
plots compose into a larger figure::
fig, axes = plt.subplots(1, 2)
plot_tail(data, ax=axes[0])
plot_hill(data, ax=axes[1])
plot_hill
¶
Hill plot: the tail index estimate against the number of order statistics.
Read the estimate off a stable plateau. Small k is noisy, large k
drifts as observations from the body enter. If there is no plateau, the
data does not support a tail index estimate, and the plot is telling you
that rather than failing to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[float]
|
Sample values. |
required |
ax
|
Any
|
Axes to draw on. A new figure is created when omitted. |
None
|
ks
|
Sequence[int] | None
|
Values of k. Defaults to the logarithmic sweep of
:func: |
None
|
true_gamma
|
float | None
|
Draws a horizontal reference line, useful on simulated data where the answer is known. |
None
|
**kwargs
|
Any
|
Passed to the plotting call. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The axes drawn on. |
Source code in heavytails/viz.py
plot_mean_residual_life
¶
Mean residual life plot, with a confidence band.
Linear above a valid threshold, so look for where the curve straightens. The band widens towards the right as exceedances run out; judge linearity from the region where it is still narrow, not from the noisy end.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[float]
|
Sample values. |
required |
ax
|
Any
|
Axes to draw on. A new figure is created when omitted. |
None
|
thresholds
|
Sequence[float] | None
|
Candidate thresholds. |
None
|
level
|
float
|
Confidence level for the band. |
0.95
|
**kwargs
|
Any
|
Passed to the plotting call. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The axes drawn on. |
Source code in heavytails/viz.py
plot_parameter_stability
¶
Parameter stability plot across candidate thresholds.
Both the shape and the modified scale are constant above a valid
threshold, so look for a plateau. The raw scale is not constant and grows
with the threshold, which is why modified_scale is the more useful of
the two to plot.
The right-hand end degrades badly, where two parameters are fitted to a few dozen points. That is the variance half of the trade-off, not a defect.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[float]
|
Sample values. |
required |
ax
|
Any
|
Axes to draw on. A new figure is created when omitted. |
None
|
thresholds
|
Sequence[float] | None
|
Candidate thresholds. |
None
|
parameter
|
str
|
|
'xi'
|
**kwargs
|
Any
|
Passed to the plotting call. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The axes drawn on. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in heavytails/viz.py
plot_qq
¶
QQ plot of the sample against Pareto quantiles.
Linear if the sample is Pareto-tailed. Departures show as systematic curvature: points above the line at the right mean a heavier tail than the reference, below means lighter.
More sensitive than the log-log plot to what happens in the body of the distribution, so the two complement each other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[float]
|
Sample values. |
required |
ax
|
Any
|
Axes to draw on. A new figure is created when omitted. |
None
|
**kwargs
|
Any
|
Passed to the plotting call. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The axes drawn on. |
Source code in heavytails/viz.py
plot_tail
¶
Log-log plot of the empirical survival function.
A power-law tail is a straight line here, with slope -alpha. This is
the single most useful heavy-tail diagnostic, and the first thing to look
at before fitting anything.
Pass fitted to overlay a distribution's own survival curve on the same
axes. That comparison is the one a goodness-of-fit statistic cannot make
for you: it shows where the model and the data part company.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[float]
|
Sample values. |
required |
ax
|
Any
|
Axes to draw on. A new figure is created when omitted. |
None
|
fitted
|
Any
|
Optional distribution whose survival curve is overlaid, drawn from a sample of the same size so the two are comparable. |
None
|
label
|
str
|
Legend label for the empirical curve. |
'empirical'
|
**kwargs
|
Any
|
Passed to the empirical scatter call. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The axes drawn on. |
Raises:
| Type | Description |
|---|---|
ModuleNotFoundError
|
If matplotlib is not installed and no |
Examples:
>>> import matplotlib
>>> matplotlib.use("Agg")
>>> from heavytails import Pareto
>>> from heavytails.viz import plot_tail
>>> data = Pareto(alpha=2.0, xm=1.0).rvs(1000, seed=1)
>>> ax = plot_tail(data)
>>> ax.get_xlabel()
'log x'
Source code in heavytails/viz.py
plot_trimmed_hill
¶
Trimmed Hill plot: the estimate against the number of observations trimmed.
The estimate moves while r is below the number of contaminated
observations and flattens once they are gone, so the elbow says how much
contamination is present. A plot flat from r = 0 says there is none.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[float]
|
Sample values. |
required |
k
|
int
|
Number of top order statistics to use. |
required |
ax
|
Any
|
Axes to draw on. A new figure is created when omitted. |
None
|
max_trim
|
int | None
|
Largest |
None
|
**kwargs
|
Any
|
Passed to the plotting call. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The axes drawn on. |