diff --git a/lib/src/onehz/wellness/anomaly.dart b/lib/src/onehz/wellness/anomaly.dart index 5a18270..41e2985 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 279ebe1..a685ad0 100644 --- a/test/onehz/wellness_test.dart +++ b/test/onehz/wellness_test.dart @@ -444,6 +444,33 @@ 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('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,