Plotting¶
An extension beyond the paper: time-series plots of the diagnostics.
viz
¶
Visualization utilities for diagnostic plots.
This module plots time series of HPD overlap, KL divergence, and predictive p-values, with optional shading of flagged periods. It is an extension beyond the paper, whose figures plot one marker per event; matplotlib is imported only when a plot is made.
Functions:¶
plot_diagnostics
¶
plot_diagnostics(time: ArrayLike, overlap: ArrayLike, kl: ArrayLike, pvals: ArrayLike, flags: ArrayLike | None = None, *, overlap_threshold: float = 0.4, kl_z_threshold: float = 3.0, pvalue_threshold: float = 0.05) -> Figure
Plot HPD overlap, KL divergence (with its robust z-score), and p-values.
Creates a three-panel figure:
- HPD overlap, with a line at
overlap_threshold; - KL divergence, with its robust z-score on a secondary axis and a line
at
kl_z_threshold(the rule of :func:~statespacecheck.periods.flag_extreme_kl); infinite values (disjoint supports) are marked with triangles at the top; - predictive p-values, with a line at
pvalue_threshold. Small p-values indicate misfit; p-values near 1 indicate a typical observation.
Optionally shades flagged periods across all panels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
(array_like, shape(n_time))
|
Time values for the x-axis. |
required |
overlap
|
(array_like, shape(n_time))
|
HPD overlap values. |
required |
kl
|
(array_like, shape(n_time))
|
KL divergence values. |
required |
pvals
|
(array_like, shape(n_time))
|
Predictive p-values. |
required |
flags
|
array_like of bool, shape (n_time,)
|
Time points to shade, for example from
:func: |
None
|
overlap_threshold
|
float
|
Where to draw the HPD overlap threshold. Default is 0.4. |
0.4
|
kl_z_threshold
|
float
|
Where to draw the robust z-score threshold for KL divergence. Default is 3.0. |
3.0
|
pvalue_threshold
|
float
|
Where to draw the p-value cutoff. Default is 0.05. |
0.05
|
Returns:
| Name | Type | Description |
|---|---|---|
fig |
Figure
|
Figure containing the diagnostic plots. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a metric or |
Examples:
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from statespacecheck.viz import plot_diagnostics
>>> rng = np.random.default_rng(0)
>>> time = np.arange(100)
>>> overlap = rng.uniform(0.3, 0.9, 100)
>>> kl = rng.uniform(0.1, 2.0, 100)
>>> pvals = rng.uniform(0.1, 0.9, 100)
>>> fig = plot_diagnostics(time, overlap, kl, pvals)
>>> plt.close(fig)
Source code in src/statespacecheck/viz.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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 176 177 178 179 180 181 182 183 184 185 | |