Skip to content

JAN-121 — Regime-switching forecaster #129

Todo

Description

@HugoXK

解读

analysis/regime.py:25 的 classify_regime 文档里自己写着「A learned HMM/change-point model can replace this later; the interface is the same」(:28-30)——今天 main 上仍然只有这条规则基线,没有任何状态概率、没有转移矩阵、也没有「按 regime 条件化的分位」。所以同一档波动在 risk-on 与 risk-off 下拿到一样的分位,这正是 docs/FORECASTING.md 里「Condition on the regime」那条原则没落地的地方。本卡补**因果(只前向、无平滑)**的状态概率滤波 + 按状态条件化的分位预测,接口与 classify_regime 同构,规则版与学习版可互换。

现状(按 main 0521399f 复核)

  • core/janus/analysis/regime.py 106 行:realized_volatility :10、trend_strength :19、classify_regime :25(标签固定为 high-vol/risk-on/risk-off/range,非有限输入返回 "unknown" :32-33)、regime_features :43、volatility_percentile :71、latest_regime :79。
  • core/janus/forecast/quantiles.py:23 的 EmpiricalQuantileModel 不接收任何状态;:40 fit 只吃收益序列。
  • 没有转移矩阵、没有前向滤波、没有按状态分桶的最小样本量规则(全树无 forward/transition 于 forecast 目录)。

交付范围(一个 PR,与本批 #128、#132 共用文件面)

  • 新增 core/janus/forecast/regime_quantiles.py(约 170 行):
    • forward_state_probabilities(labels, *, prior, transition):单遍前向滤波,P(s_t | labels_0..t),不做平滑,因此第 t 步的输出与 t 之后的数据无关(可 replay);返回 list[dict[str, float]],每步和为 1。
    • RegimeConditionedQuantiles(quantiles=(0.1,0.5,0.9), min_observations=20):fit(returns, state_weights),predict(*, horizon=1) 返回 RegimeQuantileForecast(fit、regime、pooled、observations 四个字段)。
    • 某状态样本 < min_observations 时回退到全样本池(pooled=True),不静默丢弃该状态。
  • 复用 forecast/quantiles.py 的 QuantileForecast 与本次新增的 QuantileModel Protocol / from_values 校验构造器(同批 #128 落地,+34 行)。
  • docs/FORECASTING.md 补「Regime-conditioned quantiles」小节:前向滤波的因果性、回退阈值、与规则版的关系(约 20 行)。
  • 行数预算:模块 170 + 测试 90 + 文档 20 ≈ 280 行。

为什么这三张能同 PR:三张卡都只通过 forecast/quantiles.py 的 QuantileForecast 输出契约对外,并共用同一套分位校验、as_of 语义与 seed 确定性规则;各自只新增一个模型模块 + 一组测试,无跨卡编辑冲突,一个 ≤800 行 PR 可一次 Fixes 三张。

验收标准

  • 因果性(硬性):对长度 60 的标签序列,前 30 步滤波概率与「截断到 30 步」重跑同一前缀的结果逐元素相等到 1e-12;把尾部改成相反标签不会改变前 30 步输出。
  • 每一步的状态概率和为 1(容差 1e-9),且每个概率非负。
  • prior 缺状态键、transition 行和不为 1(容差 1e-9)、出现未知状态标签时抛 ValueError,消息点名字符串。
  • fit 的 returns 与 state_weights 长度不等时抛 ValueError,消息含两个长度;state_weights 某行和不为 1 时抛 ValueError。
  • 某状态样本 < min_observations 时 predict 返回 pooled=True 且 regime 仍为请求的状态名;样本足够时 pooled=False。
  • 同一输入两次 predict 完全一致(本模型无 RNG,必须逐位相同)。
  • 在合成数据上:risk-off 状态的条件分位下边界严格低于同数据的池化下边界(方向性断言,容差 1e-12)。
  • horizon < 1 抛 ValueError;horizon>1 时按 sqrt(horizon) 缩放,与 quantiles.py:53 的既有约定一致。

失败路径与输入校验

输入 行为
未知状态标签 ValueError,点名标签
prior 缺键 / 含未知键 ValueError
transition 行和 ≠ 1 ValueError,含该行状态名与和
returns 与 state_weights 长度不等 ValueError,含两个长度
state_weights 含非有限值 ValueError,点名行号
horizon < 1 ValueError("horizon must be >= 1")
空序列 ValueError(不返回空分位)

约束:不得 TODO/FIXME 占位;不得用 pytest.skip 跳过因果性用例;不得修改 classify_regime 的既有标签集合(analysis/regime.py:34-40)或 tests/test_regime.py 的断言。

测试要求

新增 tests/test_regime_quantiles.py(≥90 行):因果截断对比、概率归一、四类输入校验、池化回退阈值边界(恰好 min_observations-1 与 min_observations)、方向性断言、horizon 缩放、replay 一致性。测试固定输入、无网络、不读系统时钟。

非目标

  • 不引入 HMM 学习/EM 训练;本卡只做前向滤波 + 条件分位,转移矩阵由调用方给。
  • 不改 analysis/regime.py 的规则阈值与标签。
  • 不接 CLI/MCP 表面;库函数 + 测试即可(表面在 #134 一并暴露)。
  • 不做状态平滑(那是 lookahead,违反本卡因果性)。

假设

  • 状态标签来自 classify_regime(规则版)或未来学习版的同一标签集合。
  • 转移矩阵与先验由调用方提供并在文档中记录,不由本模型估计。
  • 池化阈值 min_observations=20 是构造参数,不是常量。

依赖与排期

  • 依赖同批 #128 落在 quantiles.py 的共用件;两卡同一 PR,顺序是 quantiles.py → gbdt.py → regime_quantiles.py。
  • 体量:约 280 行;与 #128、#132 合计约 784 行,仍 ≤800;若实测超标,先砍 #128 的直方图优化(-30 行),不改任何 AC。

Owner original (verbatim)

Part of the Janus V2 roadmap (JAN-101–JAN-150). Builds on the v0.1.x primitives; all point-in-time, provenance, and no-silent-overwrite rules apply.

Context

HMM/change-point state probabilities condition direction and volatility forecasts. State inference is causal and replayable.

Tasks

  • Implement the capability behind the existing service/module boundaries, not as a UI-only layer.
  • Add explicit failure states, deterministic ordering, and an as_of/version story.
  • Add tests for the happy path, missing data, restatement/drift, and reproducibility.
  • Document the contract, limitations, and operational cost.

Acceptance

  • The stated behavioral fixture is reproducible.
  • A missing or stale input is an explicit error, not an empty value.
  • The feature is exposed through the appropriate CLI/MCP surface and keeps provenance.

Dependencies

JAN-024, JAN-028

Notes

  • Priority: P1
  • V2 ID: JAN-121
  • This is research tooling, not investment advice; no execution path.

Activity

  1. added
    area:forecast
    type:feature
    priority:P1
    state:triaged
    state:backlog
    on Nov 27, 2026
  2. HugoXK-bot commented on Nov 28, 2026

    @HugoXK-bot
    Collaborator

    Issue-review: PASS at issue #129 body fd6f3c72c18a

    Scope: no regime-switching forecaster on main; forecast/engine.py and forecast/calibration.py provide the walk-forward loop and scoring it must reproduce.

    Acceptance criteria: present (## Acceptance requires a reproducible fixture and explicit failure/as-of behaviour); scope stays behind existing module boundaries.
    Reviewer: Janus Review Lead (HugoXK-bot)

  3. added
    state:ready
    and removed
    state:triaged
    on Nov 28, 2026
  4. removed
    state:ready
    on Nov 28, 2026
  5. HugoXK commented on Dec 2, 2026

    @HugoXK
    OwnerAuthor

    Issue-review: PASS at issue #129 body 3e98b8cdd348

    Body-digest (sha256): 3e98b8cdd3481f196ef1d50ce80393823dd1efa1ec5afd402caaf6fe105f585c
    Algorithm: sha256 of raw .body bytes as returned by the API
    Size: 4652 characters / 6940 bytes
    Updated-at: 2026-12-02T10:48:55Z
    Read-at: 2026-12-02T10:49:20Z
    Command: gh api repos/HugoXK/janus/issues/129 | jq -j .body | sha256sum

    Batch-1 (forecast trio, one PR): #128 + #129 + #132 share only core/janus/forecast/quantiles.py's QuantileForecast contract, quantile validation, as_of rule and seed determinism; each adds one model module + one test file. Budget ≈784 lines.

    PASS binds the digest above. Any later body edit voids it; a superseding PASS must be posted for the new body. State labels and assignee are the Lead's to write.

  6. HugoXK commented on Dec 2, 2026

    @HugoXK
    OwnerAuthor

    Handoff: #129 从模板卡改成可派单规格(Owner 原文逐字保留)。

    改了什么:

    • 新增 ## 解读:analysis/regime.py:28-30 的 docstring 自己写着「A learned HMM/change-point model can replace this later; the interface is the same」——今天 main 上只有规则基线,没有状态概率、没有转移矩阵、也没有按 regime 条件化的分位。
    • 新增 ## 现状(按 main 0521399f 复核):regime.py 106 行(:10/:19/:25/:43/:71/:79,标签集合与 "unknown" 回退在 :32-40);quantiles.py:23 不接收状态;forecast 目录无 transition/forward。
    • 新增 AC 8 条(含硬性因果截断断言)、失败路径表 7 行、测试要求、非目标 4 条、假设 3 条、行数预算(约 280 行)。
    • 原卡正文原样放在 ## Owner original (verbatim),逐字节可比(cmp 通过,仅多一个尾换行)。

    为什么是这一批:与 #128、#132 同 area、同文件面;本卡复用 #128 在 quantiles.py 落的 QuantileModel/from_values(+34 行已在 #128 预算内),自己不重复计。

    关键取舍:只做前向滤波(无平滑),因为平滑要看未来、违反因果性;某状态样本 < min_observations=20 时回退全样本池并置 pooled=True,不静默丢状态;horizon 缩放沿用 quantiles.py:53 的 sqrt(horizon)。不改 classify_regime 的标签与阈值,也不动 tests/test_regime.py。

    Written-by: Janus Product

  7. added
    state:ready
    and removed
    state:backlog
    on Dec 2, 2026
  8. self-assigned this
    on Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:forecastpriority:P1state:readytype:feature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions