Verifiable Logic

The mathematics behind staying Ahead.

Safety software must never be a mysterious black box. Here is the exact architectural and mathematical model Ahead uses to filter sensor jitter, compute kinetic velocity, and project dangerous trajectories.

Layer 01 · Rate of Change

Instantaneous Slope vs. 2-Interval Smoothed Reversal

CGM sensors produce microscopic signal noise. If you calculate slope on raw noise, you get false alarm chatter. If you over-smooth, you miss real crashes. Ahead solves this with a dual-pipeline approach.

1

On-Device Instantaneous Slope

Given two deduplicated consecutive points \(P_1 = (t_1, G_1)\) and \(P_2 = (t_2, G_2)\) with elapsed fractional minutes \(\Delta m = (t_2 - t_1) / 60{,}000\):

$$R = \frac{G_2 - G_1}{\Delta m} \quad \text{[mg/dL/min]}$$

If \(\Delta m \le 0\) or points are missing, \(R\) strictly evaluates to null (never defaulting to 0 / flat).

2

2-Interval Smoothed Span with Reversal Override

To prevent false alerts from single-packet jitter, the server spans two 5-minute cycles (\(P_0, P_1, P_2\)). However, if you chug juice and reverse course:

$$R_{\text{final}} = \begin{cases} R_{\text{curr}}, & \text{if } \operatorname{sgn}(R_{\text{curr}}) \ne \operatorname{sgn}(R_{\text{prior}}) \land |R_{\text{curr}}| \ge 0.5 \\ R_{\text{span}}, & \text{otherwise (smoothed span)} \end{cases}$$
ahead-rate-math Canonical Engine

Kotlin Implementation

fun calculateRate(
  p1: GlucosePoint,
  p2: GlucosePoint
): Double? {
  val deltaMs = p2.epochMs - p1.epochMs
  if (deltaMs <= 0) return null
  val deltaMinutes = deltaMs / 60000.0
  return (p2.mgdl - p1.mgdl) / deltaMinutes
}

Shared across ahead-android, ahead-lite-android, and ahead-backend.

Layer 02 · Invariant Safety

The 90-Second Monotonic Deduplication Invariant

When multiple background services or Health Connect sync routines write overlapping records, duplicate timestamps cause \(\Delta m = 0\), turning slopes into division-by-zero crashes.

Canonical Collapse Algorithm

Because Dexcom G7 hardware operates on a strict 300-second (5-minute) sampling interval, two physiological readings can never legitimately occur within 90 seconds.

// Invariant: If duplicate write occurs within 90,000ms with identical value:
if (current.mgdl == last.mgdl && abs(current.epochMs - last.epochMs) <= 90_000L) {
  // Replace last with fresher arrival timestamp
  collapsedList[lastIndex] = current
} else {
  collapsedList.add(current)
}

Layer 03 · Trajectory Prediction

Decaying Momentum Projections

Human biology does not travel in infinite straight lines. A drop of -3.0 mg/dL/min will naturally decelerate as counter-regulatory hormones and liver glycogen kick in. Ahead models parabolic decay rather than naive linear extrapolation.

Linear Extrapolation (Naive)

Assumes constant rate forever. Leads to intense alarm fatigue because small downward movements get projected to 0 mg/dL.

Ahead Kinetic Decay Model

Applies friction coefficients across 15, 30, and 45-minute windows based on multi-interval acceleration, calculating realistic intercepts.