Skip to content

Commit 794b343

Browse files
author
Tonny@Home
committed
Refactor: decouple ensemble analysis and restore comprehensive visualizations
- Create standalone [analyze_ensembles.py] to decouple IS brute force from OOS validation via JSON metadata. - Restore visual artifacts: risk-return scatters, cluster dendrogram, and model attribution plots. - Generate detailed textual assessment reports for IS predictions and OOS verification. - Add granular subset extraction filters (`--top-n-*`) and model pattern matching (`--training-mode`). - Optimize OOS backtest performance with memory-efficient model loading and multiprocessing (`--max-workers`). - Update documentation and walkthrough guides to reflect new analyzer usage and outputs.
1 parent 2194d1d commit 794b343

14 files changed

Lines changed: 1029 additions & 1687 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ An advanced, production-ready quantitative trading system built on top of [Micro
1717
* **Multi-Workspace Isolation**: Spin up independent "Pits" for different markets (e.g., CSI300, CSI500) or configurations without duplicating code.
1818
* **Component-Based Pipeline**:
1919
- **Train & Predict**: Support for both full and incremental training on multiple models (LSTM, GRU, Transformers, LightGBM, GATs).
20-
- **Brute Force & Ensemble**: High-performance (CuPy accelerated) brute force combination finding and intelligent signal fusion.
20+
- **Brute Force & Ensemble**: High-performance (CuPy accelerated) brute force combination finding, multidimensional Out-Of-Sample (OOS) pool filtering, and intelligent signal fusion.
2121
- **Orders & Execution**: Generate actionable buy/sell signals with TopK/DropN logic and analyze micro-friction (slippage, delay costs).
2222
- **Extensible Broker Adapters**: Decoupled settlement parser supporting arbitrary broker terminal formats (e.g., Guotai Junan).
2323
* **Rich Observability**: Two interactive `streamlit` dashboards for macro portfolio performance and micro rolling health monitoring.

README_zh.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* **多工作区(Workspace)级隔离**:能够为不同的市场(如沪深300、中证500)或不同的策略配置拉起独立的“交易控制台”,无需复制系统底层核心代码。
1818
* **组件化流水线**
1919
- **训练与预测**:完全支持多模型的全量训练与增量训练更新(包含 LSTM, GRU, Transformers, LightGBM, GATs 等)。
20-
- **暴力回测与融合框架**:内置基于 CuPy 显存加速演算的高性能组合暴力穷举寻优,以及智能化的信号融合架构。
20+
- **暴力回测与融合框架**:内置基于 CuPy 显存加速演算的高性能组合暴力穷举寻优、多维度 Out-Of-Sample (OOS) OOS 候选池分析,以及智能化的信号融合架构。
2121
- **订单与实盘执行**:利用系统内置的 TopK/DropN 原生逻辑自动生成可操作的买卖订单,并在事前和事后全面分析微观执行摩擦损耗(包含价差滑点、持仓延时成本等)。
2222
- **高拓展券商适配器**:通过统一 Schema 实现交割单解析解耦,支持任意券商终端导出格式无缝接入(默认预装国泰君安)。
2323
* **全息化监测面板**:系统内置两大原生交互式 `streamlit` 数据看板,分别用于追踪“宏观资产组合表现”与“微观滚动策略健康状态监控”。

docs/00_SYSTEM_OVERVIEW.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ python quantpits/scripts/ensemble_fusion.py \
282282

283283
- **推荐工作流**:先用快速版粗筛所有组合,再用原版精确验证 Top 候选
284284
- 输出模型归因分析、风险收益散点图、层次聚类等
285-
- 支持 `--resume` 断点续跑和 `--analysis-only` 仅分析
285+
- 支持 `--resume` 断点续跑
286286

287287
### ④ 融合预测模块
288288

docs/02_BRUTE_FORCE_GUIDE.md

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ python quantpits/scripts/brute_force_ensemble.py --max-combo-size 3
1313
# 完整穷举(10 个模型 = 1023 个组合,耗时较长)
1414
python quantpits/scripts/brute_force_ensemble.py
1515

16-
# 仅分析已有结果(不重新跑回测)
17-
python quantpits/scripts/brute_force_ensemble.py --analysis-only
1816

1917
# 从上次中断处继续(Ctrl+C 中断或崩溃后均可)
2018
python quantpits/scripts/brute_force_ensemble.py --resume
@@ -43,13 +41,12 @@ python quantpits/scripts/brute_force_ensemble.py --use-groups --group-config con
4341
- 提取指标:年化收益、最大回撤、Calmar、超额收益等
4442
- 支持 `--resume` 从已有 CSV 继续
4543

46-
### Stage 4 — 结果分析
47-
- **Top 组合展示**:按年化超额排序 Top 20、最稳健 Top 10
48-
- **模型归因**:统计 Top/Bottom N 中各模型出现频率,计算净胜分
49-
- **相关性 vs 绩效**:计算多样性红利,找低相关性高 Calmar 的"黄金组合"
50-
- **层次聚类**:基于超额收益相关性做 Ward 聚类
51-
- **权重优化**:对 Top 10 单模型做 Max Sharpe / Risk Parity 优化对比
52-
- **综合报告**:自动输出最佳组合、MVP 核心模型
44+
### Stage 4 — 元数据导出
45+
- 穷举结束后,将生成包含回测环境、日期切分设置的 `run_metadata_{date}.json`,供独立分析脚本消费。
46+
47+
### Stage 5 — 独立多维分析与 OOS 验证
48+
- 穷举本质上是 In-Sample (IS) 寻优。我们将所有分析、挑选和 Out-Of-Sample (OOS) 验证逻辑解耦到了独立的 `analyze_ensembles.py` 脚本中。
49+
- 通过运行 `python quantpits/scripts/analyze_ensembles.py --metadata output/brute_force_fast/run_metadata_<date>.json`,系统将基于 Yield, Robustness, MVP 等多维度构建候选池,并在 OOS 数据上自动且无偏地打分排名。
5350

5451
> [!NOTE]
5552
> **关于单模型表现与融合回测的评测差异说明**
@@ -71,8 +68,6 @@ python quantpits/scripts/brute_force_ensemble.py --use-groups --group-config con
7168
| `--top-n` | `50` | 分析时 Top/Bottom N |
7269
| `--output-dir` | `output/brute_force` | 输出目录 |
7370
| `--resume` | - | 从已有 CSV 继续(支持崩溃/中断后恢复) |
74-
| `--skip-analysis` | - | 跳过分析阶段 |
75-
| `--analysis-only` | - | 仅分析已有结果 |
7671
| `--n-jobs` | `4` | 并发回测线程数 |
7772
| `--batch-size` | `50` | 每批处理组合数(影响 checkpoint 粒度和内存) |
7873
| `--use-groups` | - | 启用分组穷举模式(每组只选一个模型) |
@@ -120,11 +115,6 @@ python quantpits/scripts/brute_force_ensemble.py --min-combo-size 4 --max-combo-
120115
python quantpits/scripts/brute_force_ensemble.py --freq day
121116
```
122117

123-
### 更细粒度的归因分析
124-
```bash
125-
# Top/Bottom 100 的归因
126-
python quantpits/scripts/brute_force_ensemble.py --analysis-only --top-n 100
127-
```
128118

129119
## 断点续跑与安全中断
130120

@@ -236,13 +226,14 @@ python quantpits/scripts/brute_force_fast.py
236226
# 在除最后 1 年外的所有数据上寻找最佳组合 (In-Sample)
237227
# 并在最后 1 年的数据上自动验证前 10 名组合 (Out-Of-Sample)
238228
# ================================
239-
python quantpits/scripts/brute_force_fast.py --exclude-last-years 1 --auto-test-top 10
229+
python quantpits/scripts/brute_force_fast.py --exclude-last-years 1
230+
231+
# 2. 调用独立的分析脚本进行多维筛选和自动 OOS 验证
232+
python quantpits/scripts/analyze_ensembles.py --metadata output/brute_force_fast/run_metadata_<上次运行时间>.json
240233

241234
# 使用 GPU 加速
242235
python quantpits/scripts/brute_force_fast.py --use-gpu
243236

244-
# 仅分析已有结果
245-
python quantpits/scripts/brute_force_fast.py --analysis-only
246237

247238
# 从上次中断处继续
248239
python quantpits/scripts/brute_force_fast.py --resume
@@ -278,7 +269,7 @@ python quantpits/scripts/brute_force_fast.py --use-groups --group-config config/
278269
| `--no-gpu` | - | 强制禁用 GPU |
279270
| `--cost-rate` | `0.002` | 单次换手交易费用率 (双边 0.2%) |
280271

281-
> 其他参数(`--max-combo-size`, `--resume`, `--analysis-only`, `--use-groups`, `--group-config`, `--exclude-last-years`, `--auto-test-top` 等)与原版相同。
272+
> 其他参数(`--max-combo-size`, `--resume`, `--use-groups`, `--group-config`, `--exclude-last-years` 等)与原版相同。
282273
283274
---
284275

@@ -293,19 +284,40 @@ python quantpits/scripts/brute_force_fast.py --use-groups --group-config config/
293284
```bash
294285
# 1. 寻找组合并立即在 OOS 数据上验证
295286
# --exclude-last-years 1: 把最近的 1 年数据剔除,仅使用前 2 年作为 In-Sample 寻找组合
296-
# --auto-test-top 5: 在被剔除的最近 1 年数据上,验证选出的 Top 5 组合
297-
python quantpits/scripts/brute_force_fast.py --exclude-last-years 1 --auto-test-top 5
287+
python quantpits/scripts/brute_force_fast.py --exclude-last-years 1
288+
289+
# 2. 调用独立的分析脚本,在被剔除的最近 1 年 OOS 数据上自动验证模型池
290+
python quantpits/scripts/analyze_ensembles.py --metadata output/brute_force_fast/run_metadata_<上次运行时间>.json
298291
```
299292

300-
执行后,除了生成正常的 IS 报告外,还会在终端输出 `Stage 5: 自动 Out-Of-Sample (OOS) 验证 (Top 5)`,展示这几个最佳组合在从未见过的新数据上的真实表现。
293+
执行后,穷举结束后,通过调用 `analyze_ensembles.py` 喂入刚才生成的元数据 JSON,系统将自动构建多维候选池(Yield, Robustness 等),并在被剔除的 OOS 数据上展开真实回测,生成散点图与 OOS 评价报告。
294+
295+
### 分析与评估产出物 (Analysis Artifacts)
296+
297+
运行上述分析脚本后,系统将在 `output/brute_force_fast/` 或对应目录下生成详尽的评估文件:
298+
- **综合评估报告 (`analysis_report_{date}.txt` / `oos_report_{date}.txt`)**:包含各策略候选池(Yield, MVP, Diversity)在 IS 和 OOS 的年化收益、最大回撤、Calmar等表现。
299+
- **风险收益全景散点图 (`risk_return_scatter_{date}.png`)**:展示 IS 阶段的所有组合风险-收益表现二维全景以及相关性拟合。
300+
- **内部聚合特征树状图 (`cluster_dendrogram_{date}.png`)**:基于模型间预测相关性绘制的 Ward 聚类距离分析,识别模型间的同质化。
301+
- **模型归因重要度 (`model_attribution_{date}.png`)**:基于最优与最差多模型组合进行归因频率统计,发现最有价值的基础模型。
302+
- **OOS 验证散点图 (`oos_risk_return_{date}.png`)**:多维候选池在 OOS 独立集中的真实盲测结果绘图追踪。
303+
304+
### 分析脚本参数说明 (Analyzer Parameters)
305+
306+
针对独立运行的 `analyze_ensembles.py` 脚本,你还可以追加下列高级指令控制候选池颗粒度:
307+
308+
| 参数 | 说明 |
309+
|------|------|
310+
| `--top-n N` | 设定每个维度(Yield, MVP, Defensive 等)默认提取 Top N 组合进入 OOS (默认: 5) |
311+
| `--top-n-yield`, `--top-n-robust`, 等 | 单独覆盖某一特定派系的 Top N 提取数量。支持的覆盖项包括: `-yield`, `-robust`, `-defensive`, `-mvp`, `-diversity`|
312+
| `--training-mode MODE` | 在评估 OOS 之前,硬性过滤所有组合,仅保留所有成员均匹配给定触发模式(如 `static``incremental`)的组合进行打分。 |
313+
| `--max-workers N` | OOS 回测时的并发线程数,默认为 4。如果候选池极大,可适当调高加速验证过程。 |
301314

302315
### 日期参数说明
303316

304317
| 参数 | 说明 |
305318
|------|------|
306319
| `--exclude-last-years N` | 在寻找组合时,剔除最近 N 年的数据作为 OOS 集。 |
307320
| `--exclude-last-months N` | 同上,剔除最近 N 个月的数据。 |
308-
| `--auto-test-top N` | 选择 Top N 在 OOS 集上执行真实回测并生成评估报告 (`oos_validation_{date}.csv`)。|
309321
| `--start-date YYYY-MM-DD` | 绝对日期过滤:强行指定 IS 阶段的最早开始日期。 |
310322
| `--end-date YYYY-MM-DD` | 绝对日期过滤:强行截断,即不使用该日期之后的数据。 |
311323

@@ -342,9 +354,8 @@ pip install cupy-cuda11x
342354
output/brute_force_fast/
343355
├── correlation_matrix_{date}.csv # 预测值相关性矩阵
344356
├── brute_force_fast_results_{date}.csv # 回测结果(核心文件)
345-
├── model_attribution_{date}.csv # 模型归因表
346-
├── model_attribution_{date}.png # 归因条形图
347-
├── risk_return_scatter_{date}.png # 风险-收益散点图
348-
├── optimization_weights_{date}.csv # 优化权重
349-
└── analysis_report_fast_{date}.txt # 综合分析报告
357+
├── run_metadata_{date}.json # 运行配置及参数(供独立分析脚本使用)
358+
├── oos_multi_analysis_{date}.csv # 【分析脚本生成】多维候选池 OOS 评测结果
359+
├── oos_risk_return_{date}.png # 【分析脚本生成】多维候选池 OOS 风险-收益散点图
360+
└── oos_report_{date}.txt # 【分析脚本生成】综合 OOS 分析报告
350361
```

docs/70_WALKTHROUGH.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,19 @@ python quantpits/scripts/brute_force_fast.py --max-combo-size 3
303303
python quantpits/scripts/brute_force_fast.py
304304

305305
# 带防过拟合的 OOS 验证(推荐!)
306-
python quantpits/scripts/brute_force_fast.py --exclude-last-years 1 --auto-test-top 10
306+
python quantpits/scripts/brute_force_fast.py --exclude-last-years 1
307+
308+
### 6.3 组合 OOS 分析与优选
309+
310+
# 根据上一步生成的 metadata 文件,让分析脚本自动构建候选池并进行 OOS 回测
311+
python quantpits/scripts/analyze_ensembles.py --metadata output/brute_force_fast/run_metadata_<上次运行时间>.json
312+
313+
# 运行后可在 output 目录下查看多种验证报告与图形:
314+
# - analysis_report_<date>.txt (IS 阶段综合评估报告)
315+
# - risk_return_scatter_<date>.png (IS 风险收益及相关性拟合图)
316+
# - cluster_dendrogram_<date>.png (模型聚类树状图)
317+
# - model_attribution_<date>.png (前列与落后组合的模型归因重要度频次)
318+
# - oos_risk_return_<date>.png / oos_report_<date>.txt (OOS 样本外独立测试成绩)
307319

308320
# 使用分组穷举
309321
python quantpits/scripts/brute_force_fast.py --use-groups
@@ -575,7 +587,10 @@ python quantpits/scripts/order_gen.py
575587
python quantpits/scripts/static_train.py --predict-only --all-enabled
576588
577589
# ② 快速穷举(带 OOS 验证)
578-
python quantpits/scripts/brute_force_fast.py --exclude-last-years 1 --auto-test-top 10
590+
python quantpits/scripts/brute_force_fast.py --exclude-last-years 1
591+
592+
# ③ 评估并验证 Top 候选
593+
python quantpits/scripts/analyze_ensembles.py --metadata output/brute_force_fast/run_metadata_<上次运行时间>.json
579594
580595
# ③ 精确验证 Top 候选
581596
python quantpits/scripts/brute_force_ensemble.py --min-combo-size 3 --max-combo-size 3

docs/en/00_SYSTEM_OVERVIEW.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ python quantpits/scripts/ensemble_fusion.py \
278278

279279
- **Recommended Workflow**: Use the fast script to rough-screen all combos, then use the original script to accurately verify top candidates
280280
- Outputs model attribution analysis, risk-return scatter plots, hierarchical clustering, etc.
281-
- Supports `--resume` to continue running and `--analysis-only` for running analysis only
281+
- Supports `--resume` to continue running
282282

283283
### ④ Fusion Prediction Module
284284

0 commit comments

Comments
 (0)