Repository navigation
Description
解读
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.py106 行: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与本次新增的QuantileModelProtocol /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
HugoXK-bot commentedon Nov 28, 2026
Issue-review: PASS at issue #129 body fd6f3c72c18a
Scope: no regime-switching forecaster on main;
forecast/engine.pyandforecast/calibration.pyprovide the walk-forward loop and scoring it must reproduce.Acceptance criteria: present (
## Acceptancerequires a reproducible fixture and explicit failure/as-of behaviour); scope stays behind existing module boundaries.Reviewer: Janus Review Lead (HugoXK-bot)
HugoXK commentedon Dec 2, 2026
Issue-review: PASS at issue #129 body 3e98b8cdd348
Body-digest (sha256): 3e98b8cdd3481f196ef1d50ce80393823dd1efa1ec5afd402caaf6fe105f585c
Algorithm: sha256 of raw
.bodybytes as returned by the APISize: 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 | sha256sumBatch-1 (forecast trio, one PR): #128 + #129 + #132 share only
core/janus/forecast/quantiles.py'sQuantileForecastcontract, quantile validation,as_ofrule 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.
HugoXK commentedon Dec 2, 2026
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.py106 行(:10/:19/:25/:43/:71/:79,标签集合与"unknown"回退在:32-40);quantiles.py:23不接收状态;forecast 目录无transition/forward。## 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
feat(forecast): regime-conditioned quantiles and correlated scenario …
feat(forecast): regime-conditioned quantiles and correlated scenario …
fix(forecast): keep regime quantiles inside their own support (#129, #…
fix(forecast): name the block_size value and pin three edge cases (#129…