Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/src/onehz/wellness/anomaly.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -105,6 +107,7 @@ List<AnomalyDay> multivariateAnomaly(
double? chiSqGate,
int persistDays = 2,
double ridge = 0.1,
int maxGapNights = 2,
}) {
final n = feats.length;
final out = <AnomalyDay>[];
Expand Down Expand Up @@ -147,7 +150,6 @@ List<AnomalyDay> 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)
Expand Down Expand Up @@ -187,7 +189,6 @@ List<AnomalyDay> 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.
Expand Down Expand Up @@ -251,7 +252,7 @@ List<AnomalyDay> 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
Expand Down
27 changes: 27 additions & 0 deletions test/onehz/wellness_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,33 @@ void main() {
expect(out[21].drivers.first.label, 'RHR');
});

test('persistence run carries across an unscored night', () {
final feats = <AnomalyFeatures>[
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 = <AnomalyFeatures>[
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,
Expand Down
Loading