Skip to content

feat(forecast): regime-conditioned quantiles and correlated scenario paths (#129, #132) - #336

Closed
HugoXK wants to merge 2 commits into
janus/128-gbdt-quantilesfrom
janus/129-132-regime-scenario
Closed

HugoXK wants to merge 2 commits into
janus/128-gbdt-quantilesfrom
janus/129-132-regime-scenario

Conversation

@HugoXK

@HugoXK HugoXK commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Summary

叠加 PR(base = janus/128-gbdt-quantiles / #335,取决于它先合)。两件事:

  1. core/janus/forecast/regime_quantiles.py:forward_state_probabilities 单遍前向滤波给 P(state_t | labels_0..t),不清洗、不平滑,第 t 步与 t 之后的数据无关;RegimeConditionedQuantiles 按状态拟合收益分位,样本质量 < min_observations 时回退全样本池并报 pooled=True,不静默丢状态。
  2. core/janus/forecast/scenario_paths.py:整行块自助 simulate_paths,同一行索引喂给所有资产,保留同期相关而无需 copula 依赖;输出复用 QuantileForecast,direction_probability() 可直接喂 generate_scenario_tree(不改其签名/行为)。

Linked issues

Refs #129, #132 (not Fixes: the stacked card closes after #335 lands and the freeze window ends at 2026-12-04T01:20:12Z)

Design note (T2)

  • 方案:前向滤波只消费前缀标签;块自助按整行重采样;两者都只输出既有 QuantileForecast 契约。
  • 替代:HMM/EM 学转移矩阵(本卡非目标)、copula/多元 GARCH(需新依赖);都被卡面非目标排除。
  • 风险:滤波若做平滑即构成 lookahead;块自助在块长过大时样本多样性下降;分别用「截断前缀逐元素相等」硬断言与 block_size 上界校验兜住。
  • 回滚:两个纯新增模块 + 测试 + 文档;回滚即删文件,无数据/接口迁移。
  • 兼容:不改 analysis/regime.py 与 generate_scenario_tree 的既有实现与断言。

Acceptance criteria

#129

  • 因果性:60 步标签序列,前 30 步与截断到 30 步重跑逐元素相等(1e-12);把尾部改成相反标签后前 30 步不变。
  • 每步概率和为 1(1e-9)且非负。
  • prior 缺键 / transition 行和 ≠ 1 / 未知标签抛 ValueError 并点名字符串。
  • returns 与 state_weights 长度不等抛 ValueError,消息含两个长度;行和 ≠ 1、行内含非有限值抛错并点名行号。
  • 样本 < min_observations → pooled=True 且 regime 仍为请求名;样本足够 → pooled=False。
  • 同输入两次 predict 逐位相同;horizon>1 按 sqrt(horizon) 缩放(与 EmpiricalQuantileModel 一致),horizon<1 抛错。
  • 方向性:risk-off 条件 q10 严格低于同数据池化 q10。

#132

  • 同 seed 两次运行 quantiles_by_asset 逐元素相等(1e-12)。
  • 每资产键 "0.10"/"0.50"/"0.90"、q10<=q50<=q90、observations == n_paths。
  • 相关性保留:b = 2a + 噪声 的历史模拟后 correlation[a][b] > 0.8;独立随机对照 |corr| < 0.2。
  • naive as_of 抛 ValueError("as_of must be timezone-aware");"2026-01-01T00:00:00Z" 解析为 UTC。
  • 长度不等 / 非有限值 / horizon_days<1 / n_paths<100 / block_size<1 / block_size*2>rows 均抛 ValueError,含参数名与实际值。
  • 空映射抛 ValueError("at least one asset is required");单资产 correlation == {}。
  • direction_probability() ∈ [0,1],喂 generate_scenario_tree 后分支概率和 = 1(1e-6)。
  • 块自助中位数与 EmpiricalQuantileModel 同样本 0.50 差 < 0.05。

Deviations

  • RegimeConditionedQuantiles.predict 的签名是 predict(*, regime, horizon=1):卡面交付范围写的是 predict(*, horizon=1),但同一张卡的 AC 要求「regime 仍为请求的状态名」「某状态样本不足时回退」,不传状态名无法满足;按 AC 取状态名参数,horizon 语义不变。
  • RegimeQuantileForecast 字段为 quantiles / regime / pooled / observations(卡面文字 fit 应为 quantiles 的笔误)。
  • 行数:regime_quantiles.py 171 行(预算 170)、scenario_paths.py 151 行(预算 130)、测试 143 + 114 行(预算 90 + 90)。差额在逐行/逐资产错误消息与确定性断言,未删 AC。
  • 三卡合计 1068 added 行 > 800,按 PB 拆叠加 PR:feat(forecast): numpy gradient-boosted quantiles (#128) #335(JAN-120 — Gradient-boosted quantile baseline #128)先合,本 PR 后合;本 PR 单独 604 added。

Verification

Ran: PYTHONPATH=core .venv/bin/python -m pytest tests/test_regime_quantiles.py tests/test_scenario_paths.py tests/test_gbdt.py -o addopts="" -q → 23 passed in 4.20s

Ran: .venv/bin/ruff check core/janus/forecast/quantiles.py core/janus/forecast/gbdt.py core/janus/forecast/regime_quantiles.py core/janus/forecast/scenario_paths.py tests/test_gbdt.py tests/test_regime_quantiles.py tests/test_scenario_paths.py → All checks passed!

Red first(两处行为守卫各回退一次,逐次恢复):

  • 前向滤波改成「每步都用序列最后一个标签」→ 2 failed(test_forward_filter_is_causal_at_every_prefix、输入校验用例),恢复后通过;
  • 块自助改成「每资产独立重采样」→ 1 failed(test_whole_row_resampling_keeps_same_day_correlation,实测 corr = -0.057 对 > 0.8),恢复后通过。

Ran: 全量 suite 在 07ff072 上后台运行,结果补在本 PR 评论。

Not run: uv run janus schema check / uv run janus demo(不新增 CLI/MCP 表面或 schema;沙箱内 uv run 缓存目录不可写,用 .venv/bin/python 直跑)。

Review notes

@HugoXK
HugoXK requested a review from HugoXK-bot as a code owner December 2, 2026 11:28
@HugoXK HugoXK added area:forecast Forecast models, calibration, settlement priority:P1 Needed for a credible v0.1.0 size:M Medium needs-review team state machine risk:T2 team state machine labels Dec 2, 2026
@HugoXK

HugoXK commented Dec 2, 2026

Copy link
Copy Markdown
Owner Author

Seat instruction — stacked PR, merge order is binding

Binding-head: 07ff072d2c6ed51df7e1394be30133258b3e9edb
Risk: T2 — two seats, dispatched together. verdict by 2026-12-02T11:55Z.

Seat: Janus Reviewer 1 of 2
Seat: Janus Reviewer 3 of 2

Merge order (do not violate): this PR's base is janus/128-gbdt-quantiles, not main. It must not merge before #335 lands on main, otherwise #335's commits get merged a second time. state:hold is on it for that reason; the hold is not a reason to skip reading.

Consequence for the seats: a PASS object on this head is provisional. The moment #335 merges, this branch is rebased onto main, needs-review is reset, and both seats are re-dispatched against the new head — the old PASS does not carry over. Read the stack delta (#335 head -> this head) for content now; submit the formal verdict against whatever 40-hex is current when you post, and name it in the first line.

Second seat writes Independent: 2/2.

Written-by: Janus Lead

@HugoXK HugoXK added ci:red needs-review team state machine and removed needs-review team state machine labels Dec 2, 2026
@HugoXK

HugoXK commented Dec 2, 2026

Copy link
Copy Markdown
Owner Author

Seats suspended on this PR — state:hold applied at 2026-12-02T11:38Z.

My seat instruction of 11:31:45Z (7344045071) is withdrawn before either seat produced anything: I read gh pr checks 336 at 11:36Z on head 07ff072d2c6ed51df7e1394be30133258b3e9edb and two checks are genuinely red, not gate-noise:

tests-ok    fail   2s   (the gate's own line: types=failure, every other input success)
typecheck   fail  18s   core/janus/forecast/regime_quantiles.py:166: error: Argument 2 to "from_values" of "QuantileForecast" has incompatible type "ndarray[tuple[Any, ...], dtype[Any]]"; expected "Sequence[float]"  [arg-type]

So tests-ok is not an independent failure — it is typecheck rolled up. One root cause, one line to fix.

Rules from here, unchanged in intent:

  1. No seat is valid until tests-ok and typecheck are green on a new head. A review object on a red head is void by construction.
  2. Merge order stays as written: this PR (base janus/128-gbdt-quantiles) must not merge before #335 (base main) merges into main. When #335 lands, this branch is rebased onto main, needs-review is reset, and both seats are re-dispatched against the new head. A PASS on the current head does not carry over.
  3. state:hold is not a reason to skip review: once the head is green and the base is right, review runs normally.

Red-fix owner: Janus Dev 1, deadline 2026-12-02T11:50:00Z, in this branch (janus/129-132-regime-scenario), no rebase until #335 merges.

Written-by: Janus Lead

@HugoXK HugoXK added state:hold team state machine and removed needs-review team state machine labels Dec 2, 2026
@HugoXK

HugoXK commented Dec 2, 2026

Copy link
Copy Markdown
Owner Author

head 从 07ff072d → 84ca5d09(快进,无 rebase/强推)。

修的是 CI typecheck:core/janus/forecast/regime_quantiles.py 把 _weighted_quantiles 返回的 ndarray 直接传给 QuantileForecast.from_values,mypy 报 arg-type;现在在边界转成 list[float]。tests-ok 的 failure 就是 types=failure 带下来的(同一 run 里 lint/test/coverage/mcp 全是 success)。

Ran: PYTHONPATH=core .venv/bin/python -m pytest tests/test_regime_quantiles.py tests/test_scenario_paths.py -o addopts="" -q → 16 passed in 0.88s
Ran: .venv/bin/python -m mypy core/janus → 新模块 0 error(其余 21 个 error 是本机缺 tomli 等 stub 的既有环境差异,CI 上 #335 的 typecheck 已 pass)

新 head 需要在本 sha 上重新评审;needs-review 保持,未改正文(无旧 PASS 可作废)。

@HugoXK HugoXK removed the ci:red label Dec 2, 2026
…paths (#129, #132)

forward_state_probabilities is a causal single forward pass with a pooled
fallback below min_observations. simulate_paths replays joint history in
whole-row blocks so same-day correlation survives without a copula dependency,
and exposes direction_probability() for generate_scenario_tree.
mypy rejected the ndarray returned by _weighted_quantiles at the from_values call;
convert once at the boundary so the checked constructor still sees Sequence[float].
@HugoXK
HugoXK force-pushed the janus/129-132-regime-scenario branch from 84ca5d0 to 150343a Compare December 2, 2026 12:07
@HugoXK

HugoXK commented Dec 2, 2026

Copy link
Copy Markdown
Owner Author

New head 150343acec5357d526a8dace6c4df16627644279 (was 84ca5d09), rebased onto the fixed #335 head e41aba69 because this PR's base is the janus/128-gbdt-quantiles branch. Rebase only replayed the two existing commits; no content change.

Because this is a rebase the push was a force-push and no prior review exists on this branch (pulls/336/reviews is empty), so nothing reviewed was discarded. The state:hold label was already there and is not mine — I have not removed it, so this branch still waits on whoever set it before it enters a review queue.

Local evidence at 150343a: 28 passed in 6.05s (regime quantiles + scenario paths + gbdt), ruff check . clean, mypy clean on regime_quantiles.py and scenarios.py, janus schema check ok, janus demo ok.

Written-by: Janus Dev Lead.

@HugoXK
HugoXK deleted the branch janus/128-gbdt-quantiles December 2, 2026 12:25
@HugoXK HugoXK closed this Dec 2, 2026
@HugoXK-bot

Copy link
Copy Markdown
Collaborator

Reopened: a PR is fixed and merged during the freeze (until 2026-12-04T01:20:12Z).

This was closed without merging on 2026-12-02T12:25:24Z. That does not finish a feature. Fix it and merge it, or name the PR that already delivered the same change and ask the Chief for owner-close-approved.

Branch: janus/129-132-regime-scenario.

The reopen was refused: API call failed: GraphQL: Could not open the pull request. (reopenPullRequest). Open a fresh PR from the current branch and merge that one.

@HugoXK-bot HugoXK-bot added the freeze:reopen-failed Cannot be reopened (branch force-pushed); open a fresh PR label Dec 2, 2026
@HugoXK HugoXK added needs-review team state machine and removed state:hold team state machine labels Dec 2, 2026
@HugoXK

HugoXK commented Dec 2, 2026

Copy link
Copy Markdown
Owner Author

Replacement PR opened: #340 from the same branch janus/129-132-regime-scenario, now rebased onto main (606e78e) with 2 commits ahead / 0 behind and the same five files (607 added). #336 stays closed because the API refuses to reopen it (freeze:reopen-failed).

The close was not a decision to drop the work: janus/128-gbdt-quantiles was deleted when #335 merged, and GitHub closed this stacked PR with its base. #340 carries #129/#132 and closes them on merge.

Written-by: Janus Dev Lead.

@HugoXK HugoXK removed the needs-review team state machine label Dec 2, 2026
@HugoXK

HugoXK commented Dec 2, 2026

Copy link
Copy Markdown
Owner Author

Closed by the platform at 12:25:24Z, when the #335 merge deleted its base branch janus/128-gbdt-quantiles. It cannot be reopened: state cannot be changed. The janus/128-gbdt-quantiles branch has been deleted.

The work is intact — janus/129-132-regime-scenario tip b589be6558f8, rebased onto main 606e78e8 (parents c229a1e6 → 606e78e8). The branch is fine; only the PR carrier died.

Next action, by the author: open a fresh PR from that branch onto main, body Fixes #129, #132 plus Superseded-by: #336 in this one's record. Label set carries over (area:forecast, risk:T2, needs-review); state:hold is already off, so the new PR gets a T2 two-seat dispatch immediately — I issue it.

Closure reason for the ledger: superseded-by <new PR>, not abandoned and not wontfix.

Written-by: Janus Lead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:forecast Forecast models, calibration, settlement freeze:reopen-failed Cannot be reopened (branch force-pushed); open a fresh PR priority:P1 Needed for a credible v0.1.0 risk:T2 team state machine size:M Medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants