From c3ee838dcad10b965bf71ad2ed731eff8e18b129 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sun, 6 Dec 2026 04:17:32 +0530 Subject: [PATCH 1/2] multivariateAnomaly: carry the persistence run across short unscored gaps --- lib/src/onehz/wellness/anomaly.dart | 9 +++++---- test/onehz/wellness_test.dart | 13 +++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/src/onehz/wellness/anomaly.dart b/lib/src/onehz/wellness/anomaly.dart index 5a18270e..41e29853 100644 --- a/lib/src/onehz/wellness/anomaly.dart +++ b/lib/src/onehz/wellness/anomaly.dart @@ -88,7 +88,9 @@ const _featQuantum = [1.0, 0.0, 1.0, 0.0]; /// trailing window for the covariance cloud; [minBaseline] min valid nights /// before any distance is computed; [chiSqGate] threshold on Mahalanobis² /// (see below — no fixed default); [persistDays] consecutive candidate nights -/// required to flag; [ridge] covariance regularizer fraction. +/// required to flag; [ridge] covariance regularizer fraction. The run carries +/// across up to [maxGapNights] unscored nights (illness is when sleep goes +/// unscored), like illnessCusum. /// /// [chiSqGate] OPTIONAL fixed Mahalanobis² threshold. When null (default) the /// gate is DIMENSION- AND SAMPLE-AWARE: χ²_{0.999, dof} for the number of @@ -105,6 +107,7 @@ List multivariateAnomaly( double? chiSqGate, int persistDays = 2, double ridge = 0.1, + int maxGapNights = 2, }) { final n = feats.length; final out = []; @@ -147,7 +150,6 @@ List multivariateAnomaly( need = needBaselineNote(have: bestHave, need: minBaseline); } out.add(AnomalyDay(dates[i], null, false, false, const [], need: need)); - run = 0; continue; } // Robust center (median) + scale (MAD, ordinary SD as the coarser fallback) @@ -187,7 +189,6 @@ List multivariateAnomaly( if (keep.length < 2) { out.add(AnomalyDay(dates[i], null, false, false, const [], need: 'degenerate_baseline:no_dispersion')); - run = 0; continue; } // Standardized current vector. @@ -251,7 +252,7 @@ List multivariateAnomaly( .sort((x, y) => y.contribution.abs().compareTo(x.contribution.abs())); // "N nights running" means CONSECUTIVE NIGHTS, not consecutive rows. - if (day[i] - lastScoredDay > 1) run = 0; + if (day[i] - lastScoredDay > 1 + maxGapNights) run = 0; lastScoredDay = day[i]; // Smallest surviving per-feature baseline drives the gate: the noisiest diff --git a/test/onehz/wellness_test.dart b/test/onehz/wellness_test.dart index 279ebe11..b44fbd0a 100644 --- a/test/onehz/wellness_test.dart +++ b/test/onehz/wellness_test.dart @@ -444,6 +444,19 @@ void main() { expect(out[21].drivers.first.label, 'RHR'); }); + test('persistence run carries across an unscored night', () { + final feats = [ + for (var i = 0; i < 20; i++) normalNight(), + AnomalyFeatures(rhr: 75, hrv: 4.0, temp: 2000, resp: 14), + AnomalyFeatures(rhr: 55), // one feature only, abstains + AnomalyFeatures(rhr: 76, hrv: 4.0, temp: 2000, resp: 14), + ]; + final out = multivariateAnomaly(dates(feats.length), feats, + baselineDays: 28, minBaseline: 10, persistDays: 2); + expect(out[20].candidate, isTrue); + expect(out[22].flagged, isTrue); + }); + test('does NOT cry wolf on a normal series', () { final feats = [for (var i = 0; i < 25; i++) normalNight()]; final out = multivariateAnomaly(dates(feats.length), feats, From a0113847d2a38e96f32f6e51354d2d62d6d3cf66 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Sun, 6 Dec 2026 04:18:37 +0530 Subject: [PATCH 2/2] pin the long-gap side of the mv anomaly run carry --- test/onehz/wellness_test.dart | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/onehz/wellness_test.dart b/test/onehz/wellness_test.dart index b44fbd0a..a685ad0d 100644 --- a/test/onehz/wellness_test.dart +++ b/test/onehz/wellness_test.dart @@ -457,6 +457,20 @@ void main() { expect(out[22].flagged, isTrue); }); + test('a long unscored gap still ends the persistence run', () { + final feats = [ + for (var i = 0; i < 20; i++) normalNight(), + AnomalyFeatures(rhr: 75, hrv: 4.0, temp: 2000, resp: 14), + for (var i = 0; i < 3; i++) AnomalyFeatures(rhr: 55), + AnomalyFeatures(rhr: 76, hrv: 4.0, temp: 2000, resp: 14), + ]; + final out = multivariateAnomaly(dates(feats.length), feats, + baselineDays: 28, minBaseline: 10, persistDays: 2); + expect(out[20].candidate, isTrue); + expect(out[24].candidate, isTrue); + expect(out[24].flagged, isFalse); + }); + test('does NOT cry wolf on a normal series', () { final feats = [for (var i = 0; i < 25; i++) normalNight()]; final out = multivariateAnomaly(dates(feats.length), feats,