Skip to content

fix(pvmodel): drop RLS intercept so daytime predictions don't carry a constant offset - #136

Merged
frahlg merged 1 commit into
masterfrom
134-pvmodel-drop-intercept
Apr 19, 2026
Merged

frahlg merged 1 commit into
masterfrom
134-pvmodel-drop-intercept

Conversation

@frahlg

@frahlg frahlg commented Apr 19, 2026

Copy link
Copy Markdown
Member

Closes #134. Follow-up to #135 (#133 night-gate).

Summary

  • `Features()[0]` pinned to `0.0` (was `1.0`). PV physics pass through the origin; an intercept in the RLS regression has no physical basis.
  • `Update()` zeroes `Beta[0]` at end of each tick so off-diagonal covariance nudges can't revive the dead coefficient.
  • `NewService()` zeroes `Beta[0]` on load so drifted pre-PV twin intercept drifts, biasing daytime predictions #134 persisted models self-heal on deploy.
  • Kept `NFeat = 7` (slot 0 dead) instead of dropping to 6 — preserves on-disk Beta persistence.

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:

  • Loaded Beta[0] × Features[0]=0 = 0 in predictions.
  • `NewService()` explicitly zeroes Beta[0] on load (belt and braces).
  • First `Update()` tick also re-zeroes it.

Operators who want a clean retrain can still POST `/api/pvmodel/reset`, but it's optional.

Test plan

  • `TestInterceptDoesNotAffectDaytimePredict` — vary `Beta[0]` across a wide range, assert zero delta in `Predict` output (proves decoupling).
  • `TestUpdateZeroesIntercept` — pre-drifted `Beta[0]=1100` → 0 after one `Update`.
  • `TestLearnsSyntheticSystem` still converges (synthetic data has no intercept, so the new constraint is strictly aligned).
  • `make test` + `make e2e` — green.
  • Smoke on Pi: compare planner PV column against actual over a daylight hour; gap should narrow over subsequent replans as RLS re-fits with `Beta[0]=0`.

🤖 Generated with Claude Code

… 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]>
@frahlg
frahlg merged commit d4da244 into master Apr 19, 2026
1 check passed
@frahlg
frahlg deleted the 134-pvmodel-drop-intercept branch April 19, 2026 18:13

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

frahlg added a commit that referenced this pull request Apr 20, 2026
## 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]>
frahlg added a commit that referenced this pull request Apr 20, 2026
## 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PV twin intercept drifts, biasing daytime predictions

1 participant