mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
The target is the day's own pleasantness; the inputs are the other four self-ratings, the numbers and closed vocabularies of the day's context, and the figures the sleep, activity and recovery modules already hold. Nothing is imputed. A day missing one of the selected inputs is a day the fit does not see, and a feature nobody recorded on most days never becomes a column in the first place — the coverage floor is what keeps the complete-case rule from quietly emptying the matrix. A day with no context row contributes no work-status column at all, rather than a zero that would read as "worked a regular day". Columns are standardised against the account's own window, so a person whose stress never passes six does not have six read as the maximum. Candidates are screened with the machinery the rest of the product already runs — Pearson at the shared paired floor, Benjamini-Hochberg at the shared FDR, the shared effect-size floor on the shrunk estimate — because a sweep over sixty features at raw p < 0.05 finds three things that are not there. The fit is a ridge solved in closed form, validated on expanding windows in date order. The training range ends exactly where the validation range starts, asserted on the fold indices and on the day keys, not on the resulting error: a leaking split passes an error check whenever the leak is small, which is when nobody looks. The band is the central 80% of the validation residuals, so it widens when the model is bad instead of staying confidently narrow. The window read is the linked resolver's own engine, extended to answer many days in one query rather than growing a second opinion about what a Tuesday looked like.
99 lines
3.6 KiB
TypeScript
99 lines
3.6 KiB
TypeScript
/**
|
|
* The names the model's inputs are known by, everywhere.
|
|
*
|
|
* One string per feature, stored in `MoodPrediction.features`, read back by
|
|
* the surface that names what the model weighted, and compared against by the
|
|
* tests. It is therefore a wire format in all but name: renaming a key
|
|
* silently orphans every stored row that used it, which is why the encoding is
|
|
* a function here rather than a template literal at four call sites.
|
|
*
|
|
* The shape is `<family>:<rest>`, and the four families are the four places an
|
|
* input can come from:
|
|
*
|
|
* * `dimension:a2` — a level-A self-rating (A1 is the target)
|
|
* * `context:workLoad` — a numeric field of the day's context
|
|
* * `context:workStatus=off` — one value of a closed context vocabulary
|
|
* * `linked:steps` — a figure read from the module that owns it
|
|
*
|
|
* Nothing here resolves a label. The label is a locale lookup and this module
|
|
* is pure and bundle-safe, so the resolver lives beside the component that
|
|
* renders it and takes a parsed key from here.
|
|
*/
|
|
import type { MoodDimensionKey } from "@/lib/mood/dimensions";
|
|
|
|
/** The linked figures the model may read, and the day-figure each names. */
|
|
export const LINKED_FEATURE_METRICS = [
|
|
"sleepAsleep",
|
|
"steps",
|
|
"activeEnergy",
|
|
"restingHeartRate",
|
|
"heartRateVariability",
|
|
] as const;
|
|
|
|
export type LinkedFeatureMetric = (typeof LINKED_FEATURE_METRICS)[number];
|
|
|
|
export type ParsedFeatureKey =
|
|
| { family: "dimension"; dimension: MoodDimensionKey }
|
|
| { family: "context"; field: string; value: null }
|
|
| { family: "context"; field: string; value: string }
|
|
| { family: "linked"; metric: LinkedFeatureMetric };
|
|
|
|
/** `dimension:a2` */
|
|
export function dimensionFeatureKey(dimension: MoodDimensionKey): string {
|
|
return `dimension:${dimension}`;
|
|
}
|
|
|
|
/** `context:workLoad` — a numeric field. */
|
|
export function contextNumericFeatureKey(field: string): string {
|
|
return `context:${field}`;
|
|
}
|
|
|
|
/** `context:workStatus=off` — one value of a closed vocabulary. */
|
|
export function contextValueFeatureKey(field: string, value: string): string {
|
|
return `context:${field}=${value}`;
|
|
}
|
|
|
|
/** `linked:steps` */
|
|
export function linkedFeatureKey(metric: LinkedFeatureMetric): string {
|
|
return `linked:${metric}`;
|
|
}
|
|
|
|
const DIMENSION_KEYS = new Set(["a1", "a2", "a3", "a4", "a5"]);
|
|
const LINKED_KEYS = new Set<string>(LINKED_FEATURE_METRICS);
|
|
|
|
/**
|
|
* Read a stored key back.
|
|
*
|
|
* `null` for anything this version does not recognise, and that case is real
|
|
* rather than defensive: a row written by an older `modelVersion` can name a
|
|
* feature the current build has dropped, and the surface has to skip it rather
|
|
* than render `undefined` at somebody.
|
|
*/
|
|
export function parseFeatureKey(key: string): ParsedFeatureKey | null {
|
|
const separator = key.indexOf(":");
|
|
if (separator < 0) return null;
|
|
const family = key.slice(0, separator);
|
|
const rest = key.slice(separator + 1);
|
|
if (rest.length === 0) return null;
|
|
|
|
if (family === "dimension") {
|
|
return DIMENSION_KEYS.has(rest)
|
|
? { family: "dimension", dimension: rest as MoodDimensionKey }
|
|
: null;
|
|
}
|
|
if (family === "linked") {
|
|
return LINKED_KEYS.has(rest)
|
|
? { family: "linked", metric: rest as LinkedFeatureMetric }
|
|
: null;
|
|
}
|
|
if (family === "context") {
|
|
const equals = rest.indexOf("=");
|
|
if (equals < 0) return { family: "context", field: rest, value: null };
|
|
const field = rest.slice(0, equals);
|
|
const value = rest.slice(equals + 1);
|
|
if (field.length === 0 || value.length === 0) return null;
|
|
return { family: "context", field, value };
|
|
}
|
|
return null;
|
|
}
|