Repository navigation
fix(pvmodel): drop RLS intercept so daytime predictions don't carry a constant offset - #136
Conversation
… constant offset Closes #134. Features[] held an intercept term (x[0]=1.0) that had no physical basis — PV output is proportional to clear-sky irradiance and passes through the origin. Nothing pinned Beta[0] to 0, so during daytime training the RLS would drift it wherever minimised the residual, biasing every prediction by a constant offset and (before #133's night-gate) leaking into night slots. The planner then consumed that drifted prediction as the sole PV input whenever met.no's forecast was also below its fallback threshold (always at night). Fix: pin Features()[0] = 0. Slot stays in the array so on-disk Beta persistence keeps working — loaded Beta[0] becomes a dead coefficient multiplied by zero. Update() zeroes Beta[0] at the end of each RLS tick so off-diagonal covariance nudges can't revive it, and NewService() zeroes Beta[0] on load so a drifted pre-#134 value doesn't linger until the first post-deploy Update runs. No schema migration needed; existing persisted models load cleanly and self-heal. Operators can still POST /api/pvmodel/reset for a clean slate, but it's not required. Tests: - TestInterceptDoesNotAffectDaytimePredict — vary Beta[0] across a wide range, assert zero delta in Predict output (proves decoupling) - TestUpdateZeroesIntercept — Update() self-heal migration - Existing TestLearnsSyntheticSystem still converges (synthetic data has no intercept anyway, so the new constraint is strictly aligned) Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0d4eb61c01
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| h := 2 * math.Pi * hour / 24.0 | ||
| return [NFeat]float64{ | ||
| 1.0, | ||
| 0.0, // dead slot — see doc comment above |
There was a problem hiding this comment.
Stabilize covariance for the dead intercept feature
With Features()[0] now hardcoded to 0.0, the RLS update never observes feature 0, but Update() still applies the forgetting step to its covariance entry (P = ... / λ). That makes P[0][0] grow by 1/0.995 on every successful update (~140k updates to overflow), after which the Px computation hits Inf*0 and produces NaN, poisoning denom, K, and eventually all coefficients/predictions. This will surface in long-running deployments (months at 1-minute daytime training cadence), so the dead slot also needs its covariance row/column frozen or excluded from the RLS matrix update.
Useful? React with 👍 / 👎.
## control — slot-energy bookkeeping reset on planner_self (Codex P2 on #131) The new planner_self branch bypasses the energy-allocation path but previously left state.currentDirective / slotDelivered / lastTickTs untouched. A mode hop cheap → self → cheap within the same 15-minute slot would find SlotStart still matching on the return, skip its own rollover-reset, and compute remainingWh off a stale slotDelivered — over-commanding battery power for the rest of the slot. Clear the three fields whenever the planner_self branch fires so a later energy-path cycle always takes its intended rollover-reset branch. Regression test: TestPlannerSelfResetsEnergyBookkeepingOnEntry in control_test.go exercises the three-step mode hop and asserts the state is clean after the self cycle. ## pvmodel — freeze row/column 0 of covariance matrix (Codex P1 on #136) With Features[0] pinned to 0.0 (dead slot since #134), the standard RLS covariance update still divided P[0][0] by λ=0.995 every tick. Growth compounds at ~1.005× per sample, so after ~140k successful Updates P[0][0] overflows to +Inf; the next tick computes Px[0] = sum_k P[0][k] * x[k] = Inf * 0 = NaN and the poison cascades to K, β, and every downstream prediction. At the 1-minute daytime cadence that's months — long enough to bite in production but easy to miss in CI. Skip i=0 and j=0 in the covariance update loop. The two dead arrays naturally decay to zero (the newP zero value), which matches the desired invariant (a dead feature's covariance is zero). No growth, no NaN, no coupling into the live coefficients. Regression test: TestUpdatePKeepsDeadInterceptFinite runs 2000 Updates and asserts every P cell + every β coefficient stays finite; also asserts row 0 and column 0 stay at zero so a future refactor can't silently re-introduce the growth path. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
## control — slot-energy bookkeeping reset on planner_self (Codex P2 on #131) The new planner_self branch bypasses the energy-allocation path but previously left state.currentDirective / slotDelivered / lastTickTs untouched. A mode hop cheap → self → cheap within the same 15-minute slot would find SlotStart still matching on the return, skip its own rollover-reset, and compute remainingWh off a stale slotDelivered — over-commanding battery power for the rest of the slot. Clear the three fields whenever the planner_self branch fires so a later energy-path cycle always takes its intended rollover-reset branch. Regression test: TestPlannerSelfResetsEnergyBookkeepingOnEntry in control_test.go exercises the three-step mode hop and asserts the state is clean after the self cycle. ## pvmodel — freeze row/column 0 of covariance matrix (Codex P1 on #136) With Features[0] pinned to 0.0 (dead slot since #134), the standard RLS covariance update still divided P[0][0] by λ=0.995 every tick. Growth compounds at ~1.005× per sample, so after ~140k successful Updates P[0][0] overflows to +Inf; the next tick computes Px[0] = sum_k P[0][k] * x[k] = Inf * 0 = NaN and the poison cascades to K, β, and every downstream prediction. At the 1-minute daytime cadence that's months — long enough to bite in production but easy to miss in CI. Skip i=0 and j=0 in the covariance update loop. The two dead arrays naturally decay to zero (the newP zero value), which matches the desired invariant (a dead feature's covariance is zero). No growth, no NaN, no coupling into the live coefficients. Regression test: TestUpdatePKeepsDeadInterceptFinite runs 2000 Updates and asserts every P cell + every β coefficient stays finite; also asserts row 0 and column 0 stay at zero so a future refactor can't silently re-introduce the growth path. Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Closes #134. Follow-up to #135 (#133 night-gate).
Summary
Why
The twin's `Beta[0]` was free to drift during daytime training. Before #135's night-gate, that drifted intercept produced phantom PV generation at night (operator report 2026-04-19). The night-gate suppresses the symptom but the intercept still biases every daytime prediction by a constant offset — typically 5-15 % on a 10 kW system, enough to erode planner trust.
Pinning the intercept to 0 is the principled fix: the model now has exactly the six active DOF it needs to express orientation + shading + cloud-response, and nothing else.
Migration
None required. Existing persisted Beta[0] values become inert the moment this ships:
Operators who want a clean retrain can still POST `/api/pvmodel/reset`, but it's optional.
Test plan
🤖 Generated with Claude Code