Exact Range-Count Envelopes
Over Ordered Time Series
Under Total-Variation Drift Budgets

Zijia Wang & Christofer Toumazou

Centre for Bio-Inspired Technology, Department of Electrical and Electronic Engineering
Imperial College London, London, United Kingdom

One time series. One total-variation drift budget. Exact count envelopes.

Each of 96 public BIG001 glucose readings fills one well. As recorded, 84 lie in the TITR band [70,140] mg/dL. Over every integer offset path in −30…30 with total variation at most 4, at least 63 and at most 92 lie in the band, exactly. The film subtracts offsets (gᵢ − xᵢ), as the browser tool does; on this symmetric domain that is the paper’s gᵢ + δᵢ model with xᵢ = −δᵢ. Skin, sensor and plate are schematic; the trace, the offset paths and every count shown are computed from the public readings.

One shared budget

Σ |δᵢ − δᵢ₋₁| ≤ V

Choose budget V

Integer offsets δᵢ ∈ {−30,…,30}
Perturbed reading: gᵢ + δᵢ

Exact count in [70,140] mg/dL

[63,92]/ 96

minimum and maximum attainable counts

Exact count extrema: 63 and 92 out of 9604896

Extrema over admissible offset paths,
not confidence intervals. Interior counts
need not all be attainable.

At V = 4, the exact TITR envelope is [63,92] out of 96.

The observations stay fixed. Shifting them by one integer offset path with total variation at most V changes how many readings lie in the band; choosing every row’s offset independently would give [33,96]. Choose another budget to see its exact envelope. See the complete real-data example.

Public record BIG001 · PhysioNet v1.1.3 · envelopes computed by the repository solver. Offset radius and budget are user-specified query parameters; the envelopes are exact extrema, not confidence intervals.

Try an exact query on your own data

How much can your range count change under one shared offset budget? Paste a sequence, choose a range, and compute the exact minimum and maximum. A JavaScript port of the LocalWindows solver runs in your browser.

Start with:

One value per line, comma-separated values, or CSV with a glucose/value column and optional weight column (0 or 1). Rows stay in input order; a weight of 0 masks the count, without resetting the budget.

Count corrected values inside
Allowed integer offsets

Comma-separated integers, 0–64. Budget 0 allows any constant offset. Corrected value = reading − offset.

Your imported data stays in this browser. No server upload, account or API key.

Your query result

Ready

Choose a preset or paste your readings, then compute.

[ minimum, maximum ]

Check how temporal coupling changes a range report.
Try the two-reading example: budget 0 → [1,1]; budget 1 → [0,2].

Interactive edition: up to 2,000 rows, 401 offset states and budget 64; unit counts with optional zero-weight masks. Larger jobs and rational weights: demo contract / Python & C++.

Teaching example. Range endpoints and offsets use the same units as your readings. No clinical interpretation is implied.

Download the public 96-reading example · JSON ↓ · Dataset license & attribution

Using the browser tool

Actual browser sessions: enter data, compare exact range counts under shared budgets, and export a reproducible query.

Load 96 public readings, change the allowed offsets from ±30 to ±10, recompute and export. At budget 4, the bounds change from [63,92] to [82,89] while the nominal count stays 84. See which assumptions drive the range report. Actual browser recording; idle pauses trimmed and reading holds added.

MP4 ↓GIF ↓Session steps & sourcesOpen the application

Abstract

Threshold-count and range-proportion queries over ordered sensor time series underpin clinical decision support, industrial telemetry, and service-level monitoring, where domain standards map raw measurements into multi-band occupancy summaries such as time in range (TIR) and time in tight range (TITR). In deployed sensing pipelines, however, calibration bias and electrochemical drift perturb observations smoothly over time rather than independently at each sample. Treating per-sample error bounds independently permits implausible step-to-step jumps and produces vacuous count intervals, whereas enforcing a total-variation (TV) coupling budget across consecutive offsets turns a linear threshold scan into a global sequence-optimization problem. Existing dynamic programming (DP) operators evaluate transitions across the full discrete offset domain, incurring substantial state-update overhead when queries probe small or moderate drift budgets across multi-threshold report banks.

We propose LocalWindows, an exact query operator that computes deterministic minimum and maximum threshold-count envelopes across a bank of TV budgets. First, we establish an extrema-preserving run reduction that contracts fixed always-hit and always-miss samples into scalar constants and merges adjacent identical offset intervals into positive-weight runs without altering any budget-constrained count extremum. Second, we exploit the range-diameter bound of TV-constrained trajectories to cover all feasible offset paths with overlapping 2V-state local windows, proving that each compressed run is active as a boundary transition in at most four windows (≤ 4r global run-window incidences) while covered runs fold into an O(1) static depth baseline. Third, we introduce a two-pass linear-time state-line transition kernel per window alongside an O(r + m) boundary-prefix fast path for unit drift (V = 1). Across 6,804 benchmark query cells spanning 211 public continuous glucose monitoring (CGM) series, LocalWindows achieves exact bit-for-bit agreement with three exact baselines while reducing prepared-operator runtime at V ∈ {1, 4} (median ratios 0.488–0.889, p ≤ 5.7×10−37) and reaching up to 10.2× summed-time speedup on 1,152-sample horizons.

Worked Examples

Subtract an offset from each reading, then count the corrected glucose readings in [70,140] mg/dL, including both edges. These invented examples use just two allowed offsets: 0 or 1. The budget limits their total change along the sequence. Subtracting xᵢ is the browser tool’s convention; the paper adds δᵢ = −xᵢ, which is the same model with the offset domain reflected.

Illustrative values · exact answers

Two readings, tied together.

The first reading, 70, is in the band at offset 0. The second, 141, enters the band at offset 1. Can we count both?

At budget 0, the offsets must stay constant. Choosing [0,0] counts only the first reading; choosing [1,1] counts only the second. The exact pair is [1,1].

Exact minimum, maximum[1,1]among 2 counted readings

Giving each row its own free choice would count both. A shared budget of 0 does not allow the offset path [0,1].

Two paths that attain the exact bounds
In temporal order12
Observed reading70141
Count this row? weight11
Minimum: 1 hit movement used: 0 / 0
Offset00
Corrected reading − offset70141
Counted hit10
Maximum: 1 hit movement used: 0 / 0
Offset11
Corrected reading − offset69140
Counted hit01

Movement adds the absolute offset changes between consecutive rows. The first and last offsets are free.

The same question scales to a real sequence. Try the fixed public BIG001 case below

Real-data Example

The paper’s running example is public record BIG001: 96 glucose samples, the closed TITR band [70,140] mg/dL, and integer offsets from −30 to 30. As recorded, 84 readings are in range. Choosing every row’s offset independently gives the vacuous interval [33,96]; a total-variation budget V = 4 narrows the envelope by 54.0% to [63,92]. The figure shows the exact envelopes at budgets 0, 1, 4, 16 and 64, computed by the repository solver.

BIG001 glucose observations, closed TIR and TITR bands, selected hit-offset intervals, and exact envelopes at budgets 0, 1, 4, 16 and 64 computed by the repository solver. TITR envelopes are [63,91], [63,91], [63,92], [63,96] and [51,96].
BIG001

Observed data, exact envelopes. Computed by this repository’s solver in its reading-minus-offset form, which gives the same envelopes on this symmetric domain. At budget zero, every constant offset in D is allowed. TIR stays at [66,96] for these five budgets. Different metrics may attain their extrema on different paths.

SVG source

Offset radius and budget are user-specified query parameters; the envelopes are exact extrema, not confidence intervals. Data: Cho, Kim, Bent & Dunn, BIG IDEAs Lab Glycemic Variability and Wearable Device Data, v1.1.3. ODC Attribution notice

Method

Each reading gᵢ is perturbed by an unknown integer offset δᵢ ∈ D = {−R,…,R}, and consecutive offsets share one total-variation budget V. Against a threshold band [L,U], every reading compiles to a contiguous interval Iᵢ ⊆ D of offsets for which gᵢ + δᵢ is a hit.

LocalWindows returns the exact minimum and maximum hit counts [lo(v), hi(v)] for every budget v in an ordered bank, with the matching range-proportion intervals. The code and browser tool test the equivalent form gᵢ − xᵢ with xᵢ = −δᵢ, on any consecutive integer domain and with optional row weights.

2Voverlapping state windows cover
every path with TV ≤ V (Lemma 2)
≤ 4ractive run–window
incidences across the cover (Lemma 3)
O(r+m)boundary-prefix fast path
for unit drift V = 1 (Proposition 2)
Σi=2n |δᵢ − δᵢ₋₁| ≤ VInteger offsets δᵢ ∈ D = {−R,…,R}
Minimize or maximize Σᵢ 1{gᵢ + δᵢ ∈ [L,U]}
Formulation & query contract
Mechanism example: four weighted interval runs, five overlapping local windows, twelve active run–window incidences, covered-run baseline restoration, and reduction to the exact global envelope [0,8].
Mechanism

The exact localization, end to end. A mathematical example computed with the repository solver: D = {0,…,23} and V = 4, so windows have at most 2V = 8 states, and the twelve active run–window incidences stay within 4r = 16. The witness (6,7,7,10) has variation four and score eight. Restoring covered-run baselines and reducing all local and static extrema gives [0,8].

SVG source
01

Contract and merge runs

Always-hit and always-miss rows contract into constants (Lemma 1, fixed-row contraction), and adjacent identical offset intervals merge into r positive-weight runs (Theorem 1). No count extremum changes.

02

Cover with 2V-state windows

A path with total variation at most V spans at most V offset units, so overlapping windows built from adjacent blocks of the offset grid contain every feasible path (Lemma 2).

03

Solve only boundary runs

Each run is active in at most four windows (≤ 4r incidences, Lemma 3); covered runs fold into a static-depth baseline. Active windows use a two-pass linear-time sweep, and V = 1 an O(r + m) boundary-prefix scan.

Exactness and complexity · Theorem 2 · V ≥ 2

≤ 4r(V + 1)(2V + 1) inner-loop state updates

That is O(rV² + m) time and O(V² + m) working memory when V ≪ m, and O(rVm) in the worst case. Run compression and the static V = 0 sweep take O(n + m); V = 1 takes O(r + m) time and O(m) space. Here m = 2R + 1 is the offset-grid size. As 2V + 1 approaches m (V ≥ 16 on m = 61), the windows span nearly the whole grid and global DP regains the advantage. Formulation, implementation and limits

Results

Prepared-operator runtime reported in the paper

Across 211 public CGM series, LocalWindows agrees bit for bit with three exact DP baselines and is faster than all of them at budgets 1 and 4. From V = 16, where the windows approach the full offset grid, the global baselines regain the advantage.

On the 3,798 non-static cells the median speedup over SparseGlobal is 3.03× at V = 1 and 1.72× at V = 4; on 1,152-sample horizons the summed-time speedup reaches 10.2×.

211public CGM series
6,804query cells per request mode
0 / 68,040output mismatches
LocalWindows / baseline runtime ratio Participant-level median geometric-mean ratio, range over replications R1–R2, with the summed-time ratio T beneath
Requested budgetSparseGlobalGapComponentsRunTailFullGRegime
10.838–0.846T 0.562–0.5650.867–0.882T 0.679–0.6860.488–0.499T 0.054–0.062Unit drift · faster than all baselines
40.856–0.861T 0.456–0.4710.880–0.889T 0.543–0.5570.614–0.620T 0.092–0.104Moderate drift · faster than all baselines
161.069–1.086T 0.830–0.8801.040–1.058T 0.883–0.9810.890–0.898T 0.298–0.309Transition · medians favor SparseGlobal and GapComponents; T favors LocalWindows
641.291–1.293T 2.194–2.2561.276–1.283T 2.169–2.2031.411–1.416T 1.387–1.574Dense · baselines faster
Bank 0, 1, 4, 16, 641.305–1.319T 2.095–2.1851.269–1.275T 2.102–2.1181.381–1.424T 1.298–1.604Dense · baselines faster

Below one favors LocalWindows. Ranges span the two replications’ point estimates and are not confidence intervals. Paired Wilcoxon p ≤ 5.7×10−37 at V = 1 and ≤ 3.2×10−36 at V = 4. Every value with its 95% bootstrap interval is in Table II.

Figure 3 of the paper: participant-median LocalWindows/baseline runtime ratios with 95% bootstrap intervals and summed-time ratios against SparseGlobal, GapComponents and RunTailFullG in replications R1 and R2, favoring LocalWindows at budgets 1 and 4 and the baselines at budget 64 and on the bank; absolute prepared-operator latency medians and 95th percentiles.
Paper Fig. 3

Primary prepared-operator runtime in replications R1 and R2. Circles and bars: participant-median ratio with 95% bootstrap interval. Diamonds: summed-time ratio T. Lower panels: absolute latency medians and 95th percentiles.

Paper PDF
Longer horizons Summed-time ratio LocalWindows / SparseGlobal, R1 / R2 (paper Table IV)
Budgetn = 96 · 1 dayn = 288 · 3 daysn = 576 · 6 daysn = 1,152 · 12 days
V = 10.310 / 0.3170.172 / 0.1750.130 / 0.1310.101 / 0.096
V = 40.332 / 0.3400.203 / 0.2240.181 / 0.1800.163 / 0.177
V = 160.827 / 0.8040.626 / 0.6220.586 / 0.5830.546 / 0.574

At V = 1 the summed-time speedup grows from 3.19× to 10.15× as records lengthen. At V = 16 the share of cells where LocalWindows is slower than SparseGlobal falls from 35.4% to 10.0%.

Scope of these measurements

Prepared-operator timings of the paper’s C++20 implementations (g++ -O3), which share parsing, interval compilation and 64-bit integer arithmetic; every suite ran in two independent replications. In a cold, uncached source-to-report pipeline, file I/O and parsing (30–75 ms per 18-cell report) dominate the prepared operator (0.05–2.5 ms), and most 95% intervals on end-to-end time span 1.0. These are not timings of this repository’s Python package, native driver or browser tool.

Code

Python 3.10+ · standard library

A portable Python implementation of LocalWindows with exact global and gap-split plans and a small literal-state correctness oracle, plus a native C++17 driver that also exposes the paper’s SparseGlobal, GapComponents and RunTailFullG baselines.

Browse the repository
Quick start bash
git clone https://github.com/OliverDOU776/Exact-Temporal-Range-Queries-through-Local-State-Windows.git
cd Exact-Temporal-Range-Queries-through-Local-State-Windows
python -m pip install -e .
python -m lsw_range examples/synthetic.json
The mechanism-example query python
from lsw_range import solve_intervals

answer = solve_intervals(
    [(2, 6, 2), (7, 13, 1), (4, 19, 3), (10, 17, 2)],
    domain=(0, 23),
    budgets=[4],
)
print(answer.bounds)  # ((0, 8),)
Prefer the native C++17 path?

The native driver accepts unit-count rows. The synthetic input expands the mechanism example’s integer weights into repeated rows.

mkdir -p build
c++ -std=c++17 -O3 native/lsw_range.cpp -o build/lsw-native
build/lsw-native < examples/synthetic_native.txt

The JSON and native demos return [[0,6], [0,6], [0,8]] for budgets [0,1,4]. Use method="global" or method="gap" to compare exact plans, and method="reference" for small-case checks.

Citation & Resources

The paper, code, figure sources and the full query contract are available in the repository.

BibTeX
@misc{wang2026rangecount,
  title  = {Exact Range-Count Envelopes Over Ordered Time Series Under Total-Variation Drift Budgets},
  author = {Wang, Zijia and Toumazou, Christofer},
  year   = {2026},
  url    = {https://github.com/OliverDOU776/Exact-Temporal-Range-Queries-through-Local-State-Windows}
}

Data and figure attribution. The BIG001 observations use Cho et al., BIG IDEAs Lab data v1.1.3. The paper’s evaluation also uses Zhao et al., Shanghai diabetes datasets v5. Clinical thresholds follow the international consensus on time in range (Battelino et al., Diabetes Care, 2019).

The results figure is Figure 3 of the paper; the mechanism and BIG001 figures are repository illustrations computed with the repository solver. See figure details and provenance and third-party notices for attribution and applicable terms.