Build a median and percentile band from completed periods while keeping record counts, eligibility and interval meaning visible.
Historical percentile ribbons: show the spread of observations over time
Define what each vertical slice contains
A historical percentile ribbon connects selected quantiles of observations at each completed time period. Its center might be the median; the shaded area might span the 25th to 75th percentiles. That band describes the spread of observed units within a period. It is not a confidence interval for the median and not a prediction interval for next week. Name the observation unit and eligibility rule. Daily response times across requests tell a different story from daily medians across customers, because a heavy user receives more weight in the first view. A change in which units qualify can move every quantile without any change in the behavior of continuing units. Grain and denominator and cohort filters should be settled before computing a ribbon.
Freeze the quantile rule and period watermark
Quantile software has several interpolation conventions. This lesson uses linear interpolation between adjacent sorted observations at position p times N minus one, where p is the fraction from zero to one. State that convention when numerical values are compared across tools. Keep the same percentile levels, units and scale through the chart. The latest partial period often looks artificially narrow because late observations have not arrived; stop at a declared last-complete-period watermark or clearly mark the partial region. A missing period should break the ribbon, not be joined by an unexplained bridge. The code rejects duplicate unit-period pairs and omits records after the watermark, but real event pipelines also need an ingestion-completeness check. Period completeness and export metadata make that rule reproducible.
Keep sample support beside the envelope
A wide ribbon can reflect genuine diversity, a changed unit mix, an outlier or fewer observations. Show the count at each period, and use a threshold or a distinct mark when support becomes too thin for the selected quantiles. The 25th and 75th percentiles enclose the central half of the observed values under the chosen sample convention; they do not promise that half of a future population will land there. For small N, interpolation creates numbers that may never have been measured. That is acceptable when disclosed, but do not present extra decimal places as physical precision. If paired entities recur across periods, a ribbon of marginal distributions does not show individual trajectories. Use a separate paired view when the question is who improved. Paired-change charts and small-sample displays address those cases.
Use the ribbon where a line would hide the population
A single median line may remain flat while the upper tail grows sharply; the shaded interval makes that difference visible. Conversely, a rising median with a stable width can indicate a broad shift rather than merely more extreme cases. Do not stack opaque bands until they conceal each other. If several groups must be compared, use shared-scale panels or thin boundaries with explicit labels, then include an accessible table of median, endpoints and counts. A plotted outer range can be useful, but it may be dominated by a handful of measurement errors; reconcile impossible values before charting. Small multiples and text alternatives preserve the comparison. Forecast fan charts are a different claim about unseen future outcomes.
Worked contract
from collections import defaultdict
from math import floor, isfinite
def interpolated_quantile(sorted_values: list[float], fraction: float) -> float:
position = fraction * (len(sorted_values) - 1)
lower = floor(position)
weight = position - lower
return sorted_values[lower] * (1 - weight) + sorted_values[min(lower + 1, len(sorted_values) - 1)] * weight
def latency_ribbon(records: list[tuple[int, str, float]],
last_complete_period: int):
if not isinstance(last_complete_period, int):
raise ValueError("watermark must be a complete integer period")
grouped = defaultdict(list)
seen = set()
excluded = 0
for period, request_id, latency_ms in records:
if (not isinstance(period, int) or not request_id or
not isfinite(latency_ms) or latency_ms < 0 or
(period, request_id) in seen):
raise ValueError("records need unique unit-period IDs and finite latency")
seen.add((period, request_id))
if period > last_complete_period:
excluded += 1
continue
grouped[period].append(latency_ms)
if not grouped:
raise ValueError("no completed observations")
rows = []
for period in sorted(grouped):
values = sorted(grouped[period])
rows.append((period, len(values),
interpolated_quantile(values, 0.25),
interpolated_quantile(values, 0.50),
interpolated_quantile(values, 0.75)))
return rows, excluded
samples = [(47, "request-47", 47), (47, "request-59", 59),
(47, "request-83", 83), (47, "request-97", 97),
(48, "request-131", 59), (48, "request-149", 83),
(48, "request-173", 97), (48, "request-211", 131),
(49, "request-227", 47)]
period_rows, provisional_count = latency_ribbon(samples, 48)
assert period_rows == [(47, 4, 56.0, 71.0, 86.5),
(48, 4, 77.0, 90.0, 105.5)]
assert provisional_count == 1Performance and operating cost
For N records split across P completed periods, validation and grouping take O(N) expected time; sorting within groups costs O(sum nₚ log nₚ), bounded by O(N log N), and sorting period keys costs O(P log P). The ledger, grouped values and duplicate-key set use O(N + P) space. The worked sample yields a first-period central band from 56 to 86.5 milliseconds, median 71 and support four; the later incomplete record is counted as excluded. Four observations are an arithmetic check, not enough support for a stable production ribbon. Publish a minimum-count policy before interpreting thin periods.
Common Mistakes
- Labeling a within-period percentile band as uncertainty in the median.
- Joining the last completed period to an incomplete current period without a provisional mark.
- Comparing quantiles after changing unit eligibility or interpolation convention.
- Hiding period counts and inferring individual movement from marginal distributions.
Validation drill
Build a weekly latency ribbon from 83 completed weeks of synthetic requests. Reconcile eligible request IDs, display sample count and the 25th, 50th and 75th percentiles for each week, and show which week is excluded by the watermark.
