完成宏观价量网格首轮实验
13
DEVLOG.md
|
|
@ -2516,3 +2516,16 @@ QMT仍有序号缺口,16:23确认1441965、最高1442003、暂存32条。16:24
|
|||
数据质量是主要边界:选股截止前已入库的完整20日观察池只有24日;要求历史逐日及时入库仅4日;忽略入库时间的含回填诊断71日。9月7日缺数使其后20日无完整窗口。固定热度0.80、历史至少5次、H5平均每批净收益−1.42%,60bp−2.01%;所有H1至10均负。主口径1240个达标格中38格30bp正收益(含16个重复K0对照),60bp正收益0;各阶段样本不足,未选上线参数。
|
||||
|
||||
开发机12项断网隔离测试、2400个原始窗口、36项实际买卖日指数比较、三种逐笔账本独立重建、72账户现金守恒及36账户样本内截断前缀通过;原H1/5/10热度事件基线和H5现金基线精确复现。四指数阶段加总、费用顺序、缺值、43个本地链接和源摘要通过;本机Chrome10项验收,外部请求0,六图目视核验。研究代码不属于业务运行镜像,无受影响容器。接续见`docs/热度反复活跃矩阵研究接续_2026-10-29.md`。原有未提交日志、台账、文档及研究目录保持原样。
|
||||
|
||||
|
||||
## 2026-10-29 宏观价量网格与三因素首轮实验
|
||||
|
||||
核验时间:2026-10-29 11:50,北京时间。用户要求先固定口径,用既有数据比较同步形成、进入方向和价格推进效率,并交付热力图、各风格结果及当前区间。协议11时10分冻结并提交2877c59;新增研究目录research/macro_grid_20261029,报告和接续均已保存。行情截至10月23日,没有新取数。
|
||||
|
||||
五完整年训练、下一年检验,2024至2026年六个主要或扩展目标各657个日期。四个主目标的三项合用Brier误差分别增加8.48%、9.77%、13.05%、9.60%,28项主比较没有正增量通过综合标准。原有信息基线优于当前网格表示。同步高位停滞21日实为5段相连行情,保留衰减条件线索;不据负结果修改协议或重选参数。
|
||||
|
||||
当前是10月23日快照:沪深300所在层4016至4370,创业板2553至3128,中证2000为2521至2944;上方同步候选分别4576至4661、无(3128至3324仅几何参考)、2944至3042。三个风格均在最低层,没有完整下方相邻格;层下沿不称验证支撑,双侧首达有效分母为零。模型概率校准表现不佳,微盘50.7%原始上涨分数不当可信胜率。下一轮建议减少逐格交互、区分同步形成与停滞,并增加尾部固定ATR细格。
|
||||
|
||||
开发机12项配置隔离、断网测试通过;独立复核600个特征窗口、18次QRS、3942条未来收益与分类、39420行概率、18个训练边界、3285条路径风险、393条首达记录,14个截断对比、36次模型重放及12次当前概率重放通过。八幅PNG已目视检查,链接与摘要校验通过。研究计算不导入业务配置,不访问服务器;只在准备本地环境时安装研究依赖。未改应用、运行参数、数据库、持仓或容器,无受影响业务镜像,155和188均未操作。
|
||||
|
||||
接续见docs/宏观网格研究接续_2026-10-29.md。现有未提交日志、台账、旧研究材料、探针与其他工作树保留;仅提交本轮新增内容。
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
# 宏观网格研究接续
|
||||
|
||||
核验时间为2026年10月29日北京时间11时50分。用户要求固定口径、使用现有数据完成首轮,比较同步形成、进入方向和价格推进效率,交付热力图、风格结果与当前区间,暂不改线上策略。
|
||||
|
||||
研究位于[macro_grid_20261029](../research/macro_grid_20261029/README.md),主要入口为[首轮报告](../research/macro_grid_20261029/宏观网格首轮研究报告.md)。协议于11时10分固定,提交2877c59;行情沿用旧研究缓存,截至10月23日,没有补取服务器行情。
|
||||
|
||||
## 已完成结果
|
||||
|
||||
五个完整年训练、下一年检验,三个检验年度为2024、2025、2026。全市场共识、沪深300、创业板、中证2000以及扩展中证500、中证1000,各657个可兑现日。科创50的自身特征起于2022年7月21日,不满足五完整训练年,只保留描述结果。
|
||||
|
||||
三因素单独加入当前同步模型时,四个主对象总体Brier改善均为负。三项合用误差分别增加8.48%、9.77%、13.05%、9.60%,没有主比较达到预设综合标准。原有信息基线优于网格模型,下一轮应首先检验减少逐格交互和邻格共享估计。当前完整模型456列输入,训练窗约1190个重叠日标签。
|
||||
|
||||
同步形成较强的134日与高同步不再增强的21日路径存在差别,后者在三个风格的20日收益中位数分别为负3.77%、负9.08%、负12.87%。结果后集中度诊断发现21日未来窗口连为5段,不能将其当21次独立发现;保留为趋势衰减研究线索。
|
||||
|
||||
## 数据末日区间
|
||||
|
||||
全部为10月23日收盘快照。三条宽基平均价格相关性0.925,增量0.022,低于强同步形成门槛0.075。三个风格成交活跃度都低于常态参照。
|
||||
|
||||
| 风格 | 收盘 | 当前价格层 | 上方区域 |
|
||||
| --- | ---: | --- | --- |
|
||||
| 沪深300 | 4105.33 | 4016至4370 | 4576至4661,同步增强候选 |
|
||||
| 创业板 | 2684.10 | 2553至3128 | 3128至3324,几何参考 |
|
||||
| 中证2000 | 2856.27 | 2521至2944 | 2944至3042,同步增强候选 |
|
||||
|
||||
全市场方向未集中,小微盘模型原始倾向略偏多;模型概率校准表现不佳,原始分数不当可信胜率。三个风格都位于最低价格层,没有完整下方相邻格,当前双侧首达统计有效分母为零。层下沿仅作观察边界,不称已验证支撑。下一版可预先固定尾部ATR细格,再检验支撑反应。
|
||||
|
||||
## 验证与交付
|
||||
|
||||
开发机12项配置隔离、禁止联网测试通过。[独立审计](../research/macro_grid_20261029/results/audit.json)复核600个原始特征窗口、18次QRS对拍、3942条未来收益及分类、39420行概率、18个训练边界、3285条路径风险、393条首次到达记录;14个特征截断对比、36次模型重放和12次当前概率重放通过。原始缓存与协议摘要匹配。
|
||||
|
||||
八幅PNG覆盖区域同步与收益、网格覆盖、单维分层、三因素总比较、逐年表现、当前区间、事件路径和概率校准,已目视检查标注和单位。完整特征与预测为压缩JSON,报告有复现入口。原始缓存未加入Git。
|
||||
|
||||
只修改本地研究与说明文档,没有变更应用代码、运行参数、数据库、持仓或容器。155和188均未操作;研究代码不进入业务镜像,无受影响业务容器。原有未提交研究、台账、日志内容和其他工作树保留。
|
||||
|
||||
未完成的是可信多空信号与支撑有效性验证,而不是本轮计算。没有重跑新参数来替换负结果。继续研究时另立版本,优先减小模型复杂度、按行情段区分同步形成与停滞,并补尾部价格结构。
|
||||
|
|
@ -9,6 +9,7 @@
|
|||
| 当前微盘接入、参数和运行待办 | [阶段接续与实验意见](微盘轮动阶段接续_2026-10-28.md);需要部署证据再读[50股发布记录](微盘轮动50股参数发布_2026-10-28.md) |
|
||||
| 全市场热度、涨跌幅与持有期标定 | [热度策略研究接续](全市场热度四维标定接续_2026-10-28.md),含回填限制、参数结果和复现入口;[热度反复活跃矩阵接续](热度反复活跃矩阵研究接续_2026-10-29.md),含前20日次数、持有期、数据可用性与阶段指数对照 |
|
||||
| 综合分与聪明资金分阈值及市场阶段比较 | [评分策略研究接续](综合分与聪明资金分标定接续_2026-10-28.md),含810组结果、历史版本限制和复现入口;[反复活跃矩阵接续](反复活跃矩阵研究接续_2026-10-29.md),含20日次数、1至10日持有期与指数隔离 |
|
||||
| 宏观价量网格、同步形成与日线趋势 | [宏观网格研究接续](宏观网格研究接续_2026-10-29.md),含三因素增量、八幅研究图、各风格与数据末日区间判断 |
|
||||
| PMS通用迁移与既有修复 | [开发接续](开发接续_2026-10-12.md),按主题查对应节;旧时点未完成项可能已有后续结论 |
|
||||
| 架构与接口 | [README模块地图](../README.md)、[总体设计](../POSITION_MGMT_DESIGN.md);改接口再读[QMT协议](../QMT_WS_PROTOCOL.md)、[bionic接口](../BIONIC_PMS_INTERFACE.md)或[计划接口](../UPSTREAM_PLAN_API.md) |
|
||||
| 后端、前端、测试、研究 | [后端规则](../app/AGENTS.md)、[页面规则](../app/web/AGENTS.md)、[脚本规则](../scripts/AGENTS.md)、[研究规则](../research/AGENTS.md),仅选择所涉及目录 |
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
# 宏观价格成交网格实验
|
||||
|
||||
本实验使用既有指数日线,检验同步形成、进入方向和价格推进效率对未来二十日趋势概率的增量价值。主协议在计算结果前于2026年10月29日北京时间11时10分固定,提交为2877c59。协议与原始缓存均检查SHA256,不覆盖此前宏观研究。
|
||||
|
||||
研究入口为[首轮报告](宏观网格首轮研究报告.md),完整参数在[protocol.json](protocol.json)。结果只进入本目录,不导入PMS配置或业务服务,不访问服务器。研究代码不属于业务镜像,没有需要重建的业务容器。
|
||||
|
||||
## 数据和实现
|
||||
|
||||
数据复用`../macro_timing_20261026/raw/export.json`和`../macro_timing_20261026/raw/csi2000.json`,截止2026年10月23日。原始文件未加入Git;复现需要取得同一摘要的源缓存,不能用汇总结果代替行情。
|
||||
|
||||
`engine.py`负责当时可用特征、标签和固定模型。`regions.py`负责冻结区域边界和首次到达。`run.py`执行五年训练下一年检验、消融比较与当前快照。`audit.py`独立重算源窗口与标签,检查分年隔离和截断一致性。`render.py`仅从结果绘图和写报告,不重选参数。
|
||||
|
||||
网格为价格五层乘成交五层。三项因素各自加入静态同步模型,再从完整模型中逐项删除。分类使用固定L2多项逻辑回归,连续收益使用固定岭回归。所有模型对同一目标使用相同日期,标准化只在训练期拟合。
|
||||
|
||||
## 本地复现
|
||||
|
||||
从仓库根目录运行。研究虚拟环境使用Python 3.12及requirements.txt中的版本;本次环境位于`/tmp/pms-macro-grid-20261029-venv`,该临时路径不保证跨重启保留。首次建立环境可以安装依赖,统计、测试和复核阶段禁止联网。
|
||||
|
||||
```bash
|
||||
/tmp/pms-macro-grid-20261029-venv/bin/python scripts/unit_test_guard.py research/macro_grid_20261029/test_engine.py
|
||||
/tmp/pms-macro-grid-20261029-venv/bin/python research/macro_grid_20261029/run.py
|
||||
/tmp/pms-macro-grid-20261029-venv/bin/python research/macro_grid_20261029/audit.py
|
||||
/tmp/pms-macro-grid-20261029-venv/bin/python research/macro_grid_20261029/render.py
|
||||
git diff --check
|
||||
```
|
||||
|
||||
主计算与审计安装套接字审计钩子,模型计算固定为一个本地计算线程。测试另外经过仓库护栏,清除业务环境变量,在无`.env`临时目录执行。
|
||||
|
||||
## 解释范围
|
||||
|
||||
报告的“当前”是数据末日快照。全市场标签按三条宽基的方向多数判断,风格收益分别计算。中证2000训练包含发布前回溯,科创50不足五完整年时不缩短训练期。日线在当前行情版本上重算,不能复原供应商过去所有修订版本。
|
||||
|
||||
区域统计的首次到达与触区反应是训练内类比;候选区域没有逐日外推胜率的称谓。预测误差的增量比较才来自逐年外推。未来部署、参数与真实交易判收不属于本轮。
|
||||
|
|
@ -0,0 +1,182 @@
|
|||
#!/usr/bin/env python3
|
||||
"""从源缓存独立核验日期、标签、历史窗口和保存的预测汇总。"""
|
||||
from datetime import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
import json
|
||||
import sys
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from threadpoolctl import threadpool_limits
|
||||
from engine import *
|
||||
|
||||
OUT=HERE/'results'
|
||||
|
||||
|
||||
def independent_qrs(d,t):
|
||||
bs=[];rs=[]
|
||||
for end in range(t-599,t+1):
|
||||
lo=d.low.iloc[end-17:end+1].to_numpy()
|
||||
hi=d.high.iloc[end-17:end+1].to_numpy()
|
||||
lc,hc=lo-lo.mean(),hi-hi.mean()
|
||||
beta=(lc@hc)/(lc@lc)
|
||||
r2=(lc@hc)**2/((lc@lc)*(hc@hc))
|
||||
bs.append(beta);rs.append(r2)
|
||||
return (bs[-1]-np.mean(bs))/np.std(bs)*rs[-1]/np.mean(rs)
|
||||
|
||||
|
||||
def main():
|
||||
sys.addaudithook(no_network)
|
||||
p=protocol();data,macro,quality=load_data(p)
|
||||
f=make_features(data,macro);lab=labels(data,f)
|
||||
results=json.loads((OUT/'results.json').read_text());comps=json.loads((OUT/'comparisons.json').read_text())
|
||||
counts={'direct_feature_rows':0,'qrs_reference_rows':0,'forward_returns':0,'class_labels':0,
|
||||
'prediction_probability_rows':0,'fold_boundaries':0,'grid_partitions':0,'prefix_features':0,
|
||||
'prediction_replays':0,'current_region_boundaries':0,'metrics':0,'path_risk_rows':0,
|
||||
'historical_first_passage_rows':0,'current_probability_replays':0}
|
||||
rng=np.random.default_rng(20261029)
|
||||
for s in p['primary_targets']+p['secondary_targets']:
|
||||
ff=f[s]
|
||||
if s!='market':
|
||||
d=data[s]
|
||||
candidates=np.flatnonzero(ff.price_rank.notna() & ff.efficiency.notna())
|
||||
for t in rng.choice(candidates,min(100,len(candidates)),replace=False):
|
||||
c=d.close.to_numpy();h=d.high.to_numpy();lo=d.low.to_numpy();amt=d.amount.to_numpy()
|
||||
atr=np.mean([max(h[k]-lo[k],abs(h[k]-c[k-1]),abs(lo[k]-c[k-1])) for k in range(t-20,t)])
|
||||
z=(c[t]-np.mean(c[t-60:t]))/atr
|
||||
act=np.mean(amt[t-4:t+1])/np.median(amt[t-60:t])
|
||||
eff=abs(c[t]-c[t-5])/np.sum(np.abs(np.diff(c[t-5:t+1])))
|
||||
np.testing.assert_allclose([ff.atr.iloc[t],ff.price_z.iloc[t],ff.activity.iloc[t],ff.efficiency.iloc[t]],
|
||||
[atr,z,act,eff],rtol=1e-9,atol=1e-9)
|
||||
history=ff.price_z.iloc[t-252:t].to_numpy()
|
||||
rank=(np.sum(history<z)+.5*np.sum(history==z))/252
|
||||
# 浮点运算次序最多影响一个极近相等值的中点排名。
|
||||
assert abs(rank-ff.price_rank.iloc[t])<=1/252+1e-12
|
||||
counts['direct_feature_rows']+=1
|
||||
for date in ['2024-01-15','2025-06-30','2026-10-23']:
|
||||
date=pd.Timestamp(date)
|
||||
if date in d.index and np.isfinite(ff.loc[date,'qrs']):
|
||||
t=d.index.get_loc(date)
|
||||
np.testing.assert_allclose(ff.qrs.iloc[t],independent_qrs(d,t),rtol=1e-6,atol=1e-6)
|
||||
counts['qrs_reference_rows']+=1
|
||||
predpath=OUT/f'predictions_{s}.json.gz'
|
||||
if not predpath.exists():
|
||||
assert results[s]['n']==0
|
||||
continue
|
||||
pred=pd.read_json(predpath,orient='table')
|
||||
assert len(pred)==results[s]['n'] and not pred.index.duplicated().any()
|
||||
for date,row in pred.iterrows():
|
||||
t=ff.index.get_loc(date)
|
||||
assert row.entry_date==ff.index[t+1] and row.exit_date==ff.index[t+21]
|
||||
syms=CORE if s=='market' else [s]
|
||||
rets=[];scales=[];cls=[]
|
||||
for symbol in syms:
|
||||
d=data[symbol]
|
||||
r=d.open.iloc[t+21]/d.open.iloc[t+1]-1
|
||||
sigma=np.std(np.diff(np.log(d.close.iloc[t-20:t+1].to_numpy())),ddof=1)
|
||||
scale=sigma*np.sqrt(20)
|
||||
c=2 if r/scale>.5 else (0 if r/scale<-.5 else 1)
|
||||
rets.append(r);scales.append(scale);cls.append(c)
|
||||
expected_cls=(2 if cls.count(2)>=2 else (0 if cls.count(0)>=2 else 1)) if s=='market' else cls[0]
|
||||
np.testing.assert_allclose([row['return'],row.scale],[np.mean(rets),np.mean(scales)],rtol=1e-9,atol=1e-12)
|
||||
assert row['class']==expected_cls
|
||||
counts['forward_returns']+=1;counts['class_labels']+=1
|
||||
if s!='market':
|
||||
d=data[s];entry=d.open.iloc[t+1]
|
||||
pts=[entry]+d.close.iloc[t+1:t+21].tolist()+[d.open.iloc[t+21]]
|
||||
maximum=pts[0];drawdown=0.
|
||||
for value in pts:
|
||||
maximum=max(maximum,value);drawdown=min(drawdown,value/maximum-1)
|
||||
np.testing.assert_allclose(row.mdd,drawdown,atol=1e-12)
|
||||
np.testing.assert_allclose(row.mae,min(d.low.iloc[t+1:t+21].min(),d.open.iloc[t+21])/entry-1,atol=1e-12)
|
||||
counts['path_risk_rows']+=1
|
||||
for name in VARIANTS:
|
||||
probs=pred[[f'{name}_p{i}' for i in range(3)]].to_numpy()
|
||||
assert (probs>=0).all() and (probs<=1).all()
|
||||
np.testing.assert_allclose(probs.sum(axis=1),1,atol=1e-12)
|
||||
direct=np.sum((probs-np.eye(3)[pred['class'].astype(int)])**2,axis=1)
|
||||
np.testing.assert_allclose(pred[f'{name}_brier'],direct,atol=1e-12)
|
||||
np.testing.assert_allclose(results[s]['metrics'][name]['brier'],direct.mean(),atol=1e-12)
|
||||
counts['prediction_probability_rows']+=len(pred);counts['metrics']+=1
|
||||
assert sum(x['n'] for x in results[s]['grid'])==len(pred)
|
||||
counts['grid_partitions']+=1
|
||||
for fold in results[s]['folds']:
|
||||
year=fold['year'];boundary=ff.index[ff.index.year==year][0]
|
||||
assert pd.Timestamp(fold['train_exit_max'])<boundary
|
||||
train,test=split_fold(ff,lab[s],year)
|
||||
assert len(train)==fold['train_n'] and len(test)==fold['test_n']
|
||||
counts['fold_boundaries']+=1
|
||||
# 固定选择该折中点,重新拟合完整模型和当前同步对照。
|
||||
sample=test[[len(test)//2]]
|
||||
for name in ['static','full']:
|
||||
x=design(ff,name)
|
||||
probs,z,_=fit_models(x.loc[train],lab[s].loc[train,'class'],lab[s].loc[train,'z'],x.loc[sample])
|
||||
np.testing.assert_allclose(probs,pred.loc[sample,[f'{name}_p{i}' for i in range(3)]].to_numpy(),atol=1e-10)
|
||||
np.testing.assert_allclose(z,pred.loc[sample,f'{name}_zhat'].to_numpy(),atol=1e-10)
|
||||
counts['prediction_replays']+=1
|
||||
cur=results[s]['current']
|
||||
if cur['status']=='ok':
|
||||
train,_=split_fold(ff,lab[s],ff.index[-1].year)
|
||||
for name in ['static','full']:
|
||||
x=design(ff,name)
|
||||
probs,z,_=fit_models(x.loc[train],lab[s].loc[train,'class'],lab[s].loc[train,'z'],x.iloc[[-1]])
|
||||
np.testing.assert_allclose(probs[0],cur['models'][name]['probabilities'],atol=1e-10)
|
||||
counts['current_probability_replays']+=1
|
||||
if s!='market' and cur['status']=='ok':
|
||||
row=ff.iloc[-1]
|
||||
for side in ['lower','upper']:
|
||||
zone=cur['regions'][side]
|
||||
if not zone:continue
|
||||
b=zone['price_bin'];expected=[row.anchor+row.atr*row[f'edge{j}'] for j in [b,b+1]]
|
||||
np.testing.assert_allclose([zone['low'],zone['high']],expected,atol=1e-9)
|
||||
assert zone['high']<cur['close'] if side=='lower' else zone['low']>cur['close']
|
||||
counts['current_region_boundaries']+=1
|
||||
h=cur['historical_first_passage'];d=data[s]
|
||||
outcomes=[]
|
||||
for record in h['records']:
|
||||
t=d.index.get_loc(pd.Timestamp(record['date']))
|
||||
low,high=record['lower'],record['upper']
|
||||
opening=d.open.iloc[t+1]
|
||||
day=None
|
||||
if low is None or high is None:
|
||||
expected='missing_side'
|
||||
elif opening<=low['high']:
|
||||
expected,day='gap_lower',0
|
||||
elif opening>=high['low']:
|
||||
expected,day='gap_upper',0
|
||||
else:
|
||||
expected='timeout'
|
||||
for k in range(1,21):
|
||||
value=d.close.iloc[t+k]
|
||||
if value<=low['high']:
|
||||
expected,day='lower',k;break
|
||||
if value>=high['low']:
|
||||
expected,day='upper',k;break
|
||||
assert expected==record['outcome'] and day==record['day']
|
||||
outcomes.append(expected);counts['historical_first_passage_rows']+=1
|
||||
for outcome in set(outcomes):assert outcomes.count(outcome)==h['counts'][outcome]
|
||||
denominator=sum(outcomes.count(x) for x in ['lower','upper','timeout'])
|
||||
assert denominator==h['valid_first_passage_days']
|
||||
if denominator:
|
||||
for outcome in ['lower','upper','timeout']:
|
||||
np.testing.assert_allclose(h['probabilities'][outcome],outcomes.count(outcome)/denominator,atol=1e-12)
|
||||
for date in ['2024-12-31','2025-12-31']:
|
||||
subset={s:d.loc[:date] for s,d in data.items()}
|
||||
prefix=make_features(subset,macro)
|
||||
for s in f:
|
||||
pd.testing.assert_frame_equal(prefix[s],f[s].loc[:date],check_exact=False,atol=1e-9,rtol=1e-9)
|
||||
counts['prefix_features']+=1
|
||||
for targets in [p['primary_targets'],p['secondary_targets']]:
|
||||
sub=[c for c in comps if c['target'] in targets]
|
||||
np.testing.assert_allclose(holm([c['p'] for c in sub]),[c['p_holm'] for c in sub],atol=1e-12)
|
||||
manifest=json.loads((OUT/'manifest.json').read_text())
|
||||
for name,digest in manifest['code'].items():assert sha(HERE/name)==digest
|
||||
result={'status':'passed','verified_at_beijing':datetime.now(ZoneInfo('Asia/Shanghai')).isoformat(timespec='seconds'),
|
||||
'counts':counts,'protocol_sha256':sha(HERE/'protocol.json'),'raw_digests':'全部匹配',
|
||||
'network':'经审计钩子禁止联网;只读本地缓存','primary_comparison_count':28,
|
||||
'secondary_comparison_count':21,'limitations':'复核研究计算,不替代原始供应商行情逐日历史版本或真实成交验收。'}
|
||||
dump(OUT/'audit.json',result)
|
||||
print(json.dumps(result,ensure_ascii=False,indent=2))
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
with threadpool_limits(limits=1):main()
|
||||
|
|
@ -0,0 +1,337 @@
|
|||
"""宏观网格研究的纯计算。无PMS依赖;缺数不跨接,所有特征只看当时。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import itertools
|
||||
import json
|
||||
import math
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
HERE = Path(__file__).resolve().parent
|
||||
ROOT = HERE.parents[1]
|
||||
CORE = ['000300.SH', '000905.SH', '000852.SH']
|
||||
FOUR = CORE + ['399006.SZ']
|
||||
NAMES = {'market': '全市场共识', '000300.SH': '沪深300', '000905.SH': '中证500',
|
||||
'000852.SH': '中证1000', '399006.SZ': '创业板', '932000.CSI': '中证2000',
|
||||
'000688.SH': '科创50', '000001.SH': '上证指数'}
|
||||
BASE = ['r5', 'r20', 'sigma', 'qrs', 'qrs_mean', 'breadth', 'hedge']
|
||||
STATIC = ['price_sync', 'volume_sync', 'qrs_sync', 'qrs_dispersion']
|
||||
FORMATION = ['price_form', 'volume_form', 'qrs_form', 'breadth_change']
|
||||
DIRECTION = ['entry_direction', 'entry_bin_direction', 'dwell', 'new_entry']
|
||||
EFFICIENCY = ['efficiency', 'signed_efficiency', 'eff_volume', 'eff_sync', 'eff_position']
|
||||
VARIANTS = {'baseline': [], 'grid': [], 'static': [], 'formation': ['F'],
|
||||
'direction': ['D'], 'efficiency': ['E'], 'full': ['F', 'D', 'E'],
|
||||
'without_formation': ['D', 'E'], 'without_direction': ['F', 'E'],
|
||||
'without_efficiency': ['F', 'D']}
|
||||
GROUPS = {'F': FORMATION, 'D': DIRECTION, 'E': EFFICIENCY}
|
||||
|
||||
|
||||
def no_network(event, args):
|
||||
if event in {'socket.connect', 'socket.getaddrinfo', 'socket.sendto', 'socket.sendmsg'}:
|
||||
raise RuntimeError('离线研究禁止联网')
|
||||
|
||||
|
||||
def clean(x):
|
||||
if isinstance(x, dict):
|
||||
return {str(k): clean(v) for k, v in x.items()}
|
||||
if isinstance(x, (list, tuple, np.ndarray)):
|
||||
return [clean(v) for v in x]
|
||||
if isinstance(x, (pd.Timestamp,)):
|
||||
return x.isoformat()
|
||||
if isinstance(x, (np.integer,)):
|
||||
return int(x)
|
||||
if isinstance(x, (np.bool_,)):
|
||||
return bool(x)
|
||||
if isinstance(x, (float, np.floating)):
|
||||
return float(x) if np.isfinite(x) else None
|
||||
return x
|
||||
|
||||
|
||||
def dump(path, obj):
|
||||
Path(path).write_text(json.dumps(clean(obj), ensure_ascii=False, indent=2, allow_nan=False) + '\n')
|
||||
|
||||
|
||||
def sha(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def protocol():
|
||||
digest = sha(HERE / 'protocol.json')
|
||||
assert digest == (HERE / 'protocol.sha256').read_text().split()[0], '固定协议摘要不符'
|
||||
p = json.loads((HERE / 'protocol.json').read_text())
|
||||
for path, expected in p['raw_files'].items():
|
||||
assert sha(ROOT / path) == expected, '原始缓存摘要不符'
|
||||
return p
|
||||
|
||||
|
||||
def load_data(p):
|
||||
raw = json.loads((ROOT / list(p['raw_files'])[0]).read_text())['data']
|
||||
micro = json.loads((ROOT / list(p['raw_files'])[1]).read_text())['data']
|
||||
rows = {k: v['rows'] for k, v in raw['index_daily'].items()}
|
||||
rows['932000.CSI'] = micro['daily']['rows']
|
||||
data, quality = {}, {}
|
||||
for symbol, records in rows.items():
|
||||
d = pd.DataFrame(records)
|
||||
d.index = pd.to_datetime(d.pop('timestamp')).dt.normalize()
|
||||
d = d.sort_index()
|
||||
assert not d.index.duplicated().any(), '重复日期'
|
||||
assert d.index.max() <= pd.Timestamp(p['data_cutoff'])
|
||||
cols = ['open', 'high', 'low', 'close', 'amount', 'volume']
|
||||
d = d[cols].apply(pd.to_numeric, errors='coerce')
|
||||
bad = d[['open', 'high', 'low', 'close']].isna().any(axis=1)
|
||||
bad |= (d[['open', 'high', 'low', 'close']] <= 0).any(axis=1)
|
||||
bad |= d.high < d[['open', 'low', 'close']].max(axis=1) - 1e-6
|
||||
bad |= d.low > d[['open', 'high', 'close']].min(axis=1) + 1e-6
|
||||
quality[symbol] = {'rows': len(d), 'first': str(d.index.min().date()),
|
||||
'last': str(d.index.max().date()), 'invalid_ohlc': int(bad.sum()),
|
||||
'invalid_amount': int((~np.isfinite(d.amount) | (d.amount <= 0)).sum())}
|
||||
d.loc[bad, ['open', 'high', 'low', 'close']] = np.nan
|
||||
d.loc[~np.isfinite(d.amount) | (d.amount <= 0), 'amount'] = np.nan
|
||||
data[symbol] = d
|
||||
calendar = data['000001.SH'].index
|
||||
data = {s: d.reindex(calendar) for s, d in data.items()}
|
||||
return data, raw['macro_inputs'], quality
|
||||
|
||||
|
||||
def qrs(d):
|
||||
# 与既有上游公式一致;连续窗口使真正缺口后的暖机重新开始。
|
||||
low, high = d.low, d.high
|
||||
cov = low.rolling(18).cov(high, ddof=0)
|
||||
lv, hv = low.rolling(18).var(ddof=0), high.rolling(18).var(ddof=0)
|
||||
beta = (cov / lv).where((lv > 0) & (hv > 0))
|
||||
r2 = ((cov * cov) / (lv * hv)).clip(0, 1).where(beta.notna())
|
||||
sd = beta.rolling(600).std(ddof=0)
|
||||
z = ((beta - beta.rolling(600).mean()) / sd).mask(sd == 0, 0)
|
||||
mean_r2 = r2.rolling(600).mean()
|
||||
result = (z * r2 / mean_r2).mask(mean_r2 == 0, 0)
|
||||
valid_bars = d.close.notna().astype(int).rolling(618).sum()
|
||||
return result.where(valid_bars == 618)
|
||||
|
||||
|
||||
def past_rank(s, window=252):
|
||||
# 严格过去分布,平值取中点;窗口内任何缺失都不出值。
|
||||
a = s.to_numpy(float)
|
||||
out = np.full(len(a), np.nan)
|
||||
for t in range(window, len(a)):
|
||||
w = a[t-window:t]
|
||||
if np.isfinite(a[t]) and np.isfinite(w).all():
|
||||
out[t] = ((w < a[t]).sum() + .5 * (w == a[t]).sum()) / window
|
||||
return pd.Series(out, index=s.index)
|
||||
|
||||
|
||||
def bins(rank, n=5):
|
||||
return np.floor(rank * n).clip(upper=n-1)
|
||||
|
||||
|
||||
def entry_path(price_bin, actual_change):
|
||||
out = np.full((len(price_bin), 4), np.nan)
|
||||
last = None
|
||||
direction = bin_direction = np.nan
|
||||
b, r = price_bin.to_numpy(float), actual_change.to_numpy(float)
|
||||
for t in range(len(b)):
|
||||
if not np.isfinite(b[t]) or not np.isfinite(r[t]):
|
||||
last = None
|
||||
continue
|
||||
if t == 0 or not np.isfinite(b[t-1]):
|
||||
last = None
|
||||
continue
|
||||
new = b[t] != b[t-1]
|
||||
if new:
|
||||
last = t
|
||||
direction, bin_direction = np.sign(r[t]), np.sign(b[t] - b[t-1])
|
||||
if last is not None:
|
||||
out[t] = [direction, bin_direction, min(t-last, 20)/20, int(new)]
|
||||
return pd.DataFrame(out, index=price_bin.index, columns=DIRECTION)
|
||||
|
||||
|
||||
def own_features(d):
|
||||
f = pd.DataFrame(index=d.index)
|
||||
prev = d.close.shift(1)
|
||||
tr = pd.concat([d.high-d.low, (d.high-prev).abs(), (d.low-prev).abs()], axis=1).max(axis=1, skipna=False)
|
||||
f['anchor'] = d.close.shift(1).rolling(60).mean()
|
||||
f['atr'] = tr.shift(1).rolling(20).mean()
|
||||
f['price_z'] = (d.close - f.anchor) / f.atr
|
||||
f['activity'] = d.amount.rolling(5).mean() / d.amount.shift(1).rolling(60).median()
|
||||
f['price_rank'] = past_rank(f.price_z)
|
||||
f['volume_rank'] = past_rank(f.activity)
|
||||
f['price_bin'], f['volume_bin'] = bins(f.price_rank), bins(f.volume_rank)
|
||||
f['cell'] = f.price_bin * 5 + f.volume_bin
|
||||
f['r5'] = d.close / d.close.shift(5) - 1
|
||||
f['r20'] = d.close / d.close.shift(20) - 1
|
||||
f['sigma'] = np.log(d.close / prev).rolling(20).std(ddof=1)
|
||||
f['qrs'] = qrs(d)
|
||||
travel = d.close.diff().abs().rolling(5).sum()
|
||||
f['efficiency'] = ((d.close - d.close.shift(5)).abs() / travel).mask(travel == 0, 0)
|
||||
f['signed_efficiency'] = ((d.close - d.close.shift(5)) / travel).mask(travel == 0, 0)
|
||||
for j in range(6):
|
||||
f[f'edge{j}'] = f.price_z.shift(1).rolling(252).quantile(j/5)
|
||||
return pd.concat([f, entry_path(f.price_bin, d.close.diff())], axis=1)
|
||||
|
||||
|
||||
def mean_pair_corr(series, window):
|
||||
pairs = [series[a].rolling(window).corr(series[b]) for a, b in itertools.combinations(CORE, 2)]
|
||||
return pd.concat(pairs, axis=1).mean(axis=1, skipna=False).clip(-1, 1)
|
||||
|
||||
|
||||
def hedge(calendar, close, raw):
|
||||
vals, ages = {}, {}
|
||||
for key, col, lag in [('fx', 'bid_close', 1), ('shibor', 'rate_1m', 0)]:
|
||||
d = pd.DataFrame(raw[key]['rows'])
|
||||
dates = pd.to_datetime(d.trade_date) + pd.Timedelta(days=lag)
|
||||
s = pd.Series(d[col].to_numpy(float), index=dates).sort_index()
|
||||
assert not s.index.duplicated().any()
|
||||
vals[key] = s.reindex(calendar, method='ffill')
|
||||
obs = pd.Series(s.index, index=s.index).reindex(calendar, method='ffill')
|
||||
ages[key] = (pd.Series(calendar, index=calendar)-obs).dt.days
|
||||
adjusted = np.log(close/close.shift(20)) + np.log(vals['fx']/vals['fx'].shift(20)) - .02*vals['shibor'].diff(20)
|
||||
z = (adjusted-adjusted.rolling(40).mean())/adjusted.rolling(40).std(ddof=1)*10
|
||||
return z.where((ages['fx'] <= 6) & (ages['shibor'] <= 6)).round(4)
|
||||
|
||||
|
||||
def make_features(data, macro):
|
||||
own = {s: own_features(d) for s, d in data.items() if s != '000001.SH'}
|
||||
calendar = next(iter(data.values())).index
|
||||
shared = pd.DataFrame(index=calendar)
|
||||
inputs = {'price': {s: np.log(data[s].close/data[s].close.shift(1)) for s in CORE},
|
||||
'volume': {s: np.log(data[s].amount).diff() for s in CORE},
|
||||
'qrs': {s: own[s].qrs.diff() for s in CORE}}
|
||||
for kind, series in inputs.items():
|
||||
shared[f'{kind}_sync'] = mean_pair_corr(series, 20)
|
||||
shared[f'{kind}_form'] = shared[f'{kind}_sync'] - mean_pair_corr(series, 60).shift(20)
|
||||
qs = pd.concat([own[s].qrs for s in FOUR], axis=1)
|
||||
r5 = pd.concat([own[s].r5 for s in FOUR], axis=1)
|
||||
shared['qrs_mean'] = qs.mean(axis=1, skipna=False)
|
||||
shared['qrs_dispersion'] = qs.std(axis=1, ddof=0, skipna=False)
|
||||
shared['breadth'] = (r5 > 0).mean(axis=1).where(r5.notna().all(axis=1))
|
||||
shared['breadth_change'] = shared.breadth.diff(5)
|
||||
shared['hedge'] = hedge(calendar, data['000001.SH'].close, macro)
|
||||
market = pd.DataFrame(index=calendar)
|
||||
for col in ['price_rank', 'volume_rank']:
|
||||
market[col] = pd.concat([own[s][col] for s in CORE], axis=1).median(axis=1, skipna=False)
|
||||
for col in ['r5', 'r20', 'sigma', 'qrs', 'efficiency', 'signed_efficiency', 'activity', 'price_z']:
|
||||
market[col] = pd.concat([own[s][col] for s in CORE], axis=1).mean(axis=1, skipna=False)
|
||||
market['price_bin'], market['volume_bin'] = bins(market.price_rank), bins(market.volume_rank)
|
||||
market['cell'] = market.price_bin*5 + market.volume_bin
|
||||
market_return = pd.concat([data[s].close.pct_change(fill_method=None) for s in CORE], axis=1).mean(axis=1, skipna=False)
|
||||
own['market'] = pd.concat([market, entry_path(market.price_bin, market_return)], axis=1)
|
||||
for s, f in own.items():
|
||||
f = pd.concat([f, shared], axis=1)
|
||||
f['eff_volume'] = f.efficiency * np.log(f.activity)
|
||||
f['eff_sync'] = f.efficiency * f.price_sync
|
||||
f['eff_position'] = f.efficiency * f.price_rank
|
||||
own[s] = f.replace([np.inf, -np.inf], np.nan)
|
||||
return own
|
||||
|
||||
|
||||
def label_class(z):
|
||||
return pd.Series(np.where(z > .5, 2, np.where(z < -.5, 0, 1)), index=z.index).where(z.notna())
|
||||
|
||||
|
||||
def labels(data, features, horizon=20):
|
||||
lab = {}
|
||||
calendar = next(iter(data.values())).index
|
||||
exits = pd.Series(calendar, index=calendar).shift(-(horizon+1))
|
||||
entries = pd.Series(calendar, index=calendar).shift(-1)
|
||||
for s in features:
|
||||
if s == 'market':
|
||||
continue
|
||||
d, f = data[s], features[s]
|
||||
r = d.open.shift(-(horizon+1))/d.open.shift(-1)-1
|
||||
# 持有区间有任何缺失价格不产生可兑现标签。
|
||||
future_ok = d[['open', 'high', 'low', 'close']].notna().all(axis=1).astype(int).rolling(horizon+1).sum().shift(-(horizon+1)) == horizon+1
|
||||
r = r.where(future_ok)
|
||||
scale = f.sigma * np.sqrt(horizon)
|
||||
z = r / scale
|
||||
lab[s] = pd.DataFrame({'return': r, 'scale': scale, 'z': z, 'class': label_class(z),
|
||||
'entry_date': entries, 'exit_date': exits})
|
||||
market_r = pd.concat([lab[s]['return'] for s in CORE], axis=1).mean(axis=1, skipna=False)
|
||||
scale = pd.concat([lab[s]['scale'] for s in CORE], axis=1).mean(axis=1, skipna=False)
|
||||
classes = pd.concat([lab[s]['class'] for s in CORE], axis=1)
|
||||
mc = pd.Series(np.where((classes == 2).sum(axis=1) >= 2, 2,
|
||||
np.where((classes == 0).sum(axis=1) >= 2, 0, 1)), index=calendar).where(classes.notna().all(axis=1))
|
||||
lab['market'] = pd.DataFrame({'return': market_r, 'scale': scale, 'z': market_r/scale,
|
||||
'class': mc, 'entry_date': entries, 'exit_date': exits})
|
||||
return lab
|
||||
|
||||
|
||||
def design(f, name):
|
||||
cols = BASE.copy()
|
||||
if name not in ['baseline', 'grid']:
|
||||
cols += STATIC
|
||||
for group in VARIANTS[name]:
|
||||
cols += GROUPS[group]
|
||||
parts = [f[cols]]
|
||||
if name != 'baseline':
|
||||
cells = pd.DataFrame({f'cell_{j}': (f.cell == j).astype(float).where(f.cell.notna()) for j in range(1, 25)}, index=f.index)
|
||||
parts.append(cells)
|
||||
for col in cols[len(BASE):]:
|
||||
parts.append(cells.mul(f[col], axis=0).rename(columns=lambda x: f'{col}:{x}'))
|
||||
return pd.concat(parts, axis=1)
|
||||
|
||||
|
||||
def split_fold(f, l, year, all_features=None):
|
||||
x = design(f, 'full') if all_features is None else all_features
|
||||
valid = x.notna().all(axis=1)
|
||||
first_test = f.index[f.index.year == year]
|
||||
if not len(first_test):
|
||||
return None
|
||||
for y in range(year-5, year):
|
||||
cy = f.index[f.index.year == y]
|
||||
good = f.index[(f.index.year == y) & valid]
|
||||
if not len(cy) or len(good) < 200 or good[0] != cy[0]:
|
||||
return None
|
||||
train = f.index[(f.index.year >= year-5) & (f.index.year < year) & valid &
|
||||
l['class'].notna() & (l.exit_date < first_test[0])]
|
||||
test = f.index[(f.index.year == year) & valid & l['class'].notna()]
|
||||
if len(train) < 900:
|
||||
return None
|
||||
return train, test
|
||||
|
||||
|
||||
def fit_models(xtrain, ytrain, ztrain, xtest):
|
||||
from sklearn.linear_model import LogisticRegression, Ridge
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
scaler = StandardScaler()
|
||||
a = scaler.fit_transform(xtrain)
|
||||
b = scaler.transform(xtest)
|
||||
model = LogisticRegression(C=.01, solver='lbfgs', max_iter=3000, tol=1e-7)
|
||||
model.fit(a, ytrain.astype(int))
|
||||
assert (model.n_iter_ < 3000).all(), '分类模型未收敛'
|
||||
probabilities = np.zeros((len(b), 3))
|
||||
probabilities[:, model.classes_] = model.predict_proba(b)
|
||||
reg = Ridge(alpha=100).fit(a, ztrain)
|
||||
return probabilities, reg.predict(b), {'iterations': int(model.n_iter_.max()), 'columns': a.shape[1]}
|
||||
|
||||
|
||||
def brier(p, cls):
|
||||
return np.square(p - np.eye(3)[np.asarray(cls, int)]).sum(axis=1)
|
||||
|
||||
|
||||
def holm(values):
|
||||
a = np.asarray(values, float)
|
||||
order = np.argsort(a)
|
||||
corrected = np.minimum(1, np.maximum.accumulate(a[order] * np.arange(len(a), 0, -1)))
|
||||
out = np.empty_like(a)
|
||||
out[order] = corrected
|
||||
return out
|
||||
|
||||
|
||||
def block_interval(values, calendar, repetitions=2000, block=60, seed=20261029):
|
||||
# 完整日历保留缺失,不能将缺失前后直接拼接成邻接日。
|
||||
a = values.reindex(calendar).to_numpy(float)
|
||||
observed = np.nanmean(a)
|
||||
if np.isfinite(a).sum() < 120:
|
||||
return {'mean': observed, 'ci95': [None, None], 'p': 1.0}
|
||||
rng = np.random.default_rng(seed)
|
||||
offsets = np.arange(block)
|
||||
draws = np.empty(repetitions)
|
||||
for k in range(repetitions):
|
||||
starts = rng.integers(0, len(a), size=math.ceil(len(a)/block))
|
||||
idx = ((starts[:, None] + offsets) % len(a)).ravel()[:len(a)]
|
||||
draws[k] = np.nanmean(a[idx])
|
||||
p = (1 + (np.abs(draws-observed) >= abs(observed)).sum()) / (repetitions+1)
|
||||
return {'mean': observed, 'ci95': np.quantile(draws, [.025, .975]), 'p': p}
|
||||
|
After Width: | Height: | Size: 215 KiB |
|
After Width: | Height: | Size: 258 KiB |
|
After Width: | Height: | Size: 184 KiB |
|
After Width: | Height: | Size: 546 KiB |
|
After Width: | Height: | Size: 145 KiB |
|
After Width: | Height: | Size: 264 KiB |
|
After Width: | Height: | Size: 355 KiB |
|
After Width: | Height: | Size: 215 KiB |
|
|
@ -0,0 +1,48 @@
|
|||
"""核验交付文件链接、摘要和数据边界,保存最终清单。"""
|
||||
from datetime import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
from pathlib import Path
|
||||
import json
|
||||
import re
|
||||
import sys
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import scipy
|
||||
import sklearn
|
||||
import matplotlib
|
||||
from engine import HERE, ROOT, sha, dump, protocol, no_network
|
||||
|
||||
|
||||
def main():
|
||||
sys.addaudithook(no_network)
|
||||
p=protocol();out=HERE/'results'
|
||||
audit=json.loads((out/'audit.json').read_text())
|
||||
assert audit['status']=='passed'
|
||||
assert 'Ran 12 tests' in (out/'unit_tests.txt').read_text() and '\nOK\n' in (out/'unit_tests.txt').read_text()
|
||||
run=json.loads((out/'manifest.json').read_text())
|
||||
for name,digest in run['code'].items():assert sha(HERE/name)==digest
|
||||
rendered=json.loads((out/'render_manifest.json').read_text())
|
||||
assert len(rendered['figures'])==8
|
||||
for name,digest in rendered['figures'].items():assert sha(HERE/'figures'/name)==digest
|
||||
assert sha(HERE/'render.py')==rendered['render_code']
|
||||
checked=[]
|
||||
docs=[HERE/'README.md',HERE/'宏观网格首轮研究报告.md',ROOT/'docs/宏观网格研究接续_2026-10-29.md']
|
||||
for doc in docs:
|
||||
for link in re.findall(r'!?\[[^\]]*\]\(([^)]+)\)',doc.read_text()):
|
||||
if '://' in link or link.startswith('#'):continue
|
||||
path=(doc.parent/link.split('#')[0]).resolve()
|
||||
assert path.exists(),f'失效链接 {doc.name}: {link}'
|
||||
checked.append(str(path.relative_to(ROOT)))
|
||||
artifacts=[x for x in HERE.rglob('*') if x.is_file() and '__pycache__' not in x.parts and x.name!='delivery_manifest.json']
|
||||
manifest={'verified_at_beijing':datetime.now(ZoneInfo('Asia/Shanghai')).isoformat(timespec='seconds'),
|
||||
'protocol_sha256':sha(HERE/'protocol.json'),'protocol_commit':'2877c59','local_links_checked':len(checked),
|
||||
'figures_visually_checked':8,'tests_passed':12,'audit':audit['counts'],
|
||||
'runtime':{'python':sys.version.split()[0],'numpy':np.__version__,'pandas':pd.__version__,
|
||||
'scipy':scipy.__version__,'sklearn':sklearn.__version__,'matplotlib':matplotlib.__version__},
|
||||
'files_sha256':{str(x.relative_to(HERE)):sha(x) for x in sorted(artifacts)},
|
||||
'online_changes':False,'source_data_cutoff':p['data_cutoff']}
|
||||
dump(out/'delivery_manifest.json',manifest)
|
||||
print(json.dumps({'status':'passed','links':len(checked),'figures':8,'files':len(artifacts)},ensure_ascii=False))
|
||||
|
||||
|
||||
if __name__=='__main__':main()
|
||||
|
|
@ -0,0 +1,124 @@
|
|||
"""冻结于信号日的候选区域与收盘首次到达路径。"""
|
||||
from __future__ import annotations
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from engine import BASE
|
||||
|
||||
|
||||
def visits(cell):
|
||||
return cell.notna() & cell.shift(1).notna() & cell.ne(cell.shift(1))
|
||||
|
||||
|
||||
def region_table(f, train):
|
||||
from sklearn.linear_model import Ridge
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
scale = StandardScaler()
|
||||
x = scale.fit_transform(f.loc[train, BASE])
|
||||
fitted = Ridge(alpha=100).fit(x, f.loc[train, 'price_sync']).predict(x)
|
||||
r = pd.DataFrame({'cell': f.loc[train, 'cell'], 'residual_sync': f.loc[train, 'price_sync']-fitted,
|
||||
'visit': visits(f.cell).loc[train], 'price_sync': f.loc[train, 'price_sync']})
|
||||
result = {}
|
||||
for c in range(25):
|
||||
sub = r[r.cell == c]
|
||||
result[c] = {'n': len(sub), 'visits': int(sub.visit.sum()),
|
||||
'residual_sync': sub.residual_sync.mean(), 'price_sync': sub.price_sync.mean(),
|
||||
'enhanced': bool(len(sub) >= 30 and sub.visit.sum() >= 5 and sub.residual_sync.mean() > 0)}
|
||||
return result
|
||||
|
||||
|
||||
def select_regions(row, close, table):
|
||||
edges = row.anchor + row.atr * np.array([row[f'edge{j}'] for j in range(6)])
|
||||
vb = int(row.volume_bin)
|
||||
lower = [i for i in range(5) if edges[i+1] < close and table[i*5+vb]['enhanced']]
|
||||
upper = [i for i in range(5) if edges[i] > close and table[i*5+vb]['enhanced']]
|
||||
result = {'edges': edges.tolist()}
|
||||
for side, candidates in [('lower', lower), ('upper', upper)]:
|
||||
candidates = sorted(candidates, reverse=side == 'lower')
|
||||
enhanced = bool(candidates)
|
||||
if not candidates:
|
||||
candidates = [i for i in range(5) if (edges[i+1] < close if side == 'lower' else edges[i] > close)]
|
||||
candidates = sorted(candidates, reverse=side == 'lower')
|
||||
if not candidates:
|
||||
result[side] = None
|
||||
continue
|
||||
i = candidates[0]
|
||||
result[side] = {'price_bin': i, 'cell': i*5+vb, 'low': edges[i], 'high': edges[i+1],
|
||||
'enhanced': enhanced, 'support': table[i*5+vb],
|
||||
'distance_pct': ((edges[i+1] if side == 'lower' else edges[i])/close-1)*100}
|
||||
return result
|
||||
|
||||
|
||||
def first_passage(opening, closes, lower, upper):
|
||||
if lower is None or upper is None:
|
||||
return 'missing_side', None
|
||||
if not np.isfinite(opening) or not np.isfinite(closes).all():
|
||||
return 'missing_data', None
|
||||
if opening <= lower:
|
||||
return 'gap_lower', 0
|
||||
if opening >= upper:
|
||||
return 'gap_upper', 0
|
||||
for t, value in enumerate(closes, 1):
|
||||
if value <= lower:
|
||||
return 'lower', t
|
||||
if value >= upper:
|
||||
return 'upper', t
|
||||
return 'timeout', None
|
||||
|
||||
|
||||
def response_path(closes, atr, zone, side):
|
||||
"""触区收盘及随后5个收盘;穿透区外0.5ATR优先于随后反弹。"""
|
||||
if len(closes) < 6 or not np.isfinite(closes).all():
|
||||
return 'incomplete'
|
||||
touched = closes[0]
|
||||
if side == 'lower':
|
||||
failure, goal = zone['low']-.5*atr, touched+atr
|
||||
if touched <= failure:
|
||||
return 'failed'
|
||||
for x in closes[1:]:
|
||||
if x <= failure: return 'failed'
|
||||
if x >= goal: return 'success'
|
||||
else:
|
||||
failure, goal = zone['high']+.5*atr, touched-atr
|
||||
if touched >= failure:
|
||||
return 'failed'
|
||||
for x in closes[1:]:
|
||||
if x >= failure: return 'failed'
|
||||
if x <= goal: return 'success'
|
||||
return 'timeout'
|
||||
|
||||
|
||||
def historical_region_stats(f, d, train, table, current_cell, boundary):
|
||||
same = train[f.loc[train, 'cell'] == current_cell]
|
||||
records = []
|
||||
calendar = f.index
|
||||
for date in same:
|
||||
t = calendar.get_loc(date)
|
||||
if t+21 >= len(calendar) or calendar[t+21] >= boundary:
|
||||
continue
|
||||
row = f.loc[date]
|
||||
regions = select_regions(row, d.loc[date, 'close'], table)
|
||||
lo, hi = regions['lower'], regions['upper']
|
||||
outcome, day = first_passage(d.open.iloc[t+1], d.close.iloc[t+1:t+21].to_numpy(),
|
||||
lo['high'] if lo else None, hi['low'] if hi else None)
|
||||
response = None
|
||||
if outcome in ['lower', 'upper'] and day is not None:
|
||||
touch = t+day
|
||||
if touch+5 < len(calendar) and calendar[touch+5] < boundary:
|
||||
response = response_path(d.close.iloc[touch:touch+6].to_numpy(), row.atr, lo if outcome == 'lower' else hi, outcome)
|
||||
else:
|
||||
response = 'incomplete'
|
||||
records.append({'date': str(date.date()), 'outcome': outcome, 'day': day, 'response': response,
|
||||
'lower': lo, 'upper': hi})
|
||||
counts = pd.Series([r['outcome'] for r in records], dtype=str).value_counts().to_dict()
|
||||
eligible = sum(counts.get(x, 0) for x in ['lower', 'upper', 'timeout'])
|
||||
responses = {}
|
||||
for side in ['lower', 'upper']:
|
||||
rc = pd.Series([r['response'] for r in records if r['outcome'] == side], dtype=str).value_counts().to_dict()
|
||||
denominator = sum(rc.get(x, 0) for x in ['success', 'failed', 'timeout'])
|
||||
responses[side] = {'counts': rc, 'n': denominator,
|
||||
'success_rate': rc.get('success', 0)/denominator if denominator else None}
|
||||
return {'same_cell_days': len(same), 'visits': int(visits(f.cell).loc[same].sum()), 'counts': counts,
|
||||
'valid_first_passage_days': eligible,
|
||||
'probabilities': {x: counts.get(x, 0)/eligible if eligible else None for x in ['lower', 'upper', 'timeout']},
|
||||
'responses': responses, 'records': records,
|
||||
'scope': '训练内同格历史类比;候选区域由整段训练估计,不是逐日外推支撑胜率。'}
|
||||
|
|
@ -0,0 +1,384 @@
|
|||
#!/usr/bin/env python3
|
||||
"""从固定结果生成中文研究图与报告,不重新选参。"""
|
||||
from pathlib import Path
|
||||
import json
|
||||
import sys
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import matplotlib
|
||||
matplotlib.use('Agg')
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib import font_manager
|
||||
from matplotlib.colors import TwoSlopeNorm
|
||||
from engine import HERE, NAMES, dump, sha, protocol, load_data, no_network
|
||||
|
||||
OUT = HERE/'results'
|
||||
FIG = HERE/'figures'
|
||||
STYLES = ['000300.SH', '399006.SZ', '932000.CSI']
|
||||
PRIMARY = ['market']+STYLES
|
||||
TITLE = {'formation': '同步形成', 'direction': '进入方向', 'efficiency': '推进效率', 'full': '三项合用',
|
||||
'without_formation': '移除同步形成', 'without_direction': '移除进入方向', 'without_efficiency': '移除推进效率',
|
||||
'static': '网格与当前同步', 'grid': '价格成交网格', 'baseline': '原有信息基线'}
|
||||
|
||||
|
||||
def style():
|
||||
for path in ['/System/Library/Fonts/STHeiti Light.ttc','/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc']:
|
||||
if Path(path).exists():
|
||||
font_manager.fontManager.addfont(path)
|
||||
plt.rcParams['font.family'] = font_manager.FontProperties(fname=path).get_name()
|
||||
break
|
||||
plt.rcParams.update({'axes.unicode_minus': False, 'font.size': 10, 'savefig.dpi': 160,
|
||||
'axes.spines.top': False, 'axes.spines.right': False})
|
||||
|
||||
|
||||
def p(x, digits=2):
|
||||
return '—' if x is None or not np.isfinite(x) else f'{x*100:.{digits}f}%'
|
||||
|
||||
|
||||
def n(x, digits=2):
|
||||
return '—' if x is None or not np.isfinite(x) else f'{x:.{digits}f}'
|
||||
|
||||
|
||||
def heat(ax, values, support=None, limits=None, cmap='RdBu_r', percentage=True):
|
||||
a = np.asarray(values, float).reshape(5,5)
|
||||
if limits is None:
|
||||
m = max(.01, np.nanmax(np.abs(a)))
|
||||
limits=(-m,m)
|
||||
im = ax.imshow(a, origin='lower', cmap=cmap, vmin=limits[0], vmax=limits[1], aspect='equal')
|
||||
if support is not None:
|
||||
good = np.asarray(support, bool).reshape(5,5)
|
||||
# 薄样本格覆盖半透明灰色,同时保留数值供探索。
|
||||
rgba = np.zeros((5,5,4))
|
||||
rgba[~good] = [.8,.82,.84,.78]
|
||||
ax.imshow(rgba, origin='lower', aspect='equal')
|
||||
else:
|
||||
good = np.ones((5,5),bool)
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
v=a[i,j]
|
||||
label='—' if not np.isfinite(v) else (f'{v*100:+.1f}' if percentage else f'{v:.2f}')
|
||||
if not good[i,j]: label+='*'
|
||||
rgba=im.cmap(im.norm(v)) if np.isfinite(v) else (1,1,1,1)
|
||||
dark=sum(rgba[k]*w for k,w in enumerate([.2126,.7152,.0722]))<.45
|
||||
ax.text(j,i,label,ha='center',va='center',fontsize=9,color='white' if dark and good[i,j] else '#172739')
|
||||
ticks=['0–20','20–40','40–60','60–80','80–100']
|
||||
ax.set_xticks(range(5),ticks,fontsize=8)
|
||||
ax.set_yticks(range(5),ticks,fontsize=8)
|
||||
ax.set_xlabel('成交活跃度历史百分位')
|
||||
ax.set_ylabel('价格位置历史百分位')
|
||||
return im
|
||||
|
||||
|
||||
def region_heatmaps(results):
|
||||
fig, axes=plt.subplots(3,3,figsize=(15,13),layout='constrained')
|
||||
for i,s in enumerate(STYLES):
|
||||
rows=results[s]['grid']
|
||||
support=[x['display_supported'] for x in rows]
|
||||
for j,(field,title,limit,percent) in enumerate([
|
||||
('price_sync','价格同步水平',(0.4,1),False),
|
||||
('residual_sync','控制波动与动量后的同步差',(-.1,.1),True),
|
||||
('median_return','随后20日收益中位数',(-.1,.1),True)]):
|
||||
im=heat(axes[i,j],[x.get(field) for x in rows],support,limit,'YlGnBu' if j==0 else 'RdBu_r',percent)
|
||||
axes[i,j].set_title(f'{NAMES[s]} · {title}')
|
||||
fig.colorbar(im,ax=axes[i,j],shrink=.75,label='相关系数' if j==0 else ('相关系数差,格内数字乘100' if j==1 else '收益比例,格内数字为百分数'))
|
||||
fig.suptitle('同一区域的同步程度与后续走势:2024至2026年历史滚动检验',fontsize=17)
|
||||
fig.supxlabel('灰格及*:少于30个日期或5次访问;所有网格均为探索展示,相关性增强与上涨方向分别检验。',fontsize=10)
|
||||
fig.savefig(FIG/'区域同步与收益热力图.png')
|
||||
plt.close(fig)
|
||||
|
||||
|
||||
def support_heatmaps(results):
|
||||
fig,axes=plt.subplots(1,3,figsize=(15,5.6),layout='constrained')
|
||||
for ax,s in zip(axes,STYLES):
|
||||
rows=results[s]['grid']
|
||||
a=np.array([r['n'] for r in rows]).reshape(5,5)
|
||||
im=ax.imshow(a,origin='lower',vmin=0,vmax=100,cmap='Blues')
|
||||
for i in range(5):
|
||||
for j in range(5):
|
||||
r=rows[i*5+j]
|
||||
ax.text(j,i,f"{r['n']}日\n{r['visits']}次",ha='center',va='center',fontsize=10,color='white' if r['n']>65 else '#172739')
|
||||
ax.set_xticks(range(5),['低','偏低','中','偏高','高'])
|
||||
ax.set_yticks(range(5),['低','偏低','中','偏高','高'])
|
||||
ax.set_xlabel('成交活跃度层级');ax.set_ylabel('价格层级');ax.set_title(NAMES[s])
|
||||
fig.colorbar(im,ax=axes,label='检验日期数',shrink=.75)
|
||||
fig.suptitle('网格覆盖:日期数与访问次数同时展示',fontsize=17)
|
||||
fig.supxlabel('一次连续停留算同一次访问;相邻访问和20日未来收益仍可能相关。',fontsize=10)
|
||||
fig.savefig(FIG/'网格样本覆盖.png');plt.close(fig)
|
||||
|
||||
|
||||
def incremental(comps):
|
||||
pairs=[('formation','static'),('direction','static'),('efficiency','static'),('full','static'),
|
||||
('full','without_formation'),('full','without_direction'),('full','without_efficiency')]
|
||||
cols=['加入同步形成','加入进入方向','加入推进效率','三项合用','形成的独有贡献','方向的独有贡献','效率的独有贡献']
|
||||
a=np.full((4,7),np.nan)
|
||||
lookup={(c['target'],c['candidate'],c['reference']):c for c in comps}
|
||||
for i,s in enumerate(PRIMARY):
|
||||
for j,(cand,ref) in enumerate(pairs):
|
||||
a[i,j]=lookup[(s,cand,ref)].get('relative_improvement_pct',np.nan)
|
||||
m=max(1,np.nanmax(np.abs(a)))
|
||||
fig,ax=plt.subplots(figsize=(15,5.6),layout='constrained')
|
||||
im=ax.imshow(a,cmap='RdBu',vmin=-m,vmax=m,aspect='auto')
|
||||
for i,s in enumerate(PRIMARY):
|
||||
for j,(cand,ref) in enumerate(pairs):
|
||||
c=lookup[(s,cand,ref)]
|
||||
label=f'{a[i,j]:+.2f}%\n校正p={c["p_holm"]:.2f}'
|
||||
ax.text(j,i,label,ha='center',va='center',fontsize=10,color='white' if abs(a[i,j])>m*.65 else '#172739')
|
||||
ax.set_xticks(range(7),cols,fontsize=10)
|
||||
ax.set_yticks(range(4),[NAMES[s] for s in PRIMARY])
|
||||
ax.set_title('概率预测的增量价值:正值表示三分类Brier误差下降',fontsize=17,pad=18)
|
||||
fig.colorbar(im,ax=ax,label='相对误差改善(%)',shrink=.85)
|
||||
fig.supxlabel('前四列相对“网格与当前同步”;后三列比较完整模型与逐项移除。28项统一Holm校正。',fontsize=10)
|
||||
fig.savefig(FIG/'三因素增量比较.png');plt.close(fig)
|
||||
fig,axes=plt.subplots(2,2,figsize=(14,9),layout='constrained',sharey=True)
|
||||
colors=['#2b6f99','#d18b22','#8a6bb1','#2c8b68']
|
||||
for ax,s in zip(axes.flat,PRIMARY):
|
||||
for color,(cand,ref) in zip(colors,pairs[:4]):
|
||||
c=lookup[(s,cand,ref)]
|
||||
years=list(c.get('yearly_improvement_pct',{}))
|
||||
ax.plot(years,[c['yearly_improvement_pct'][y] for y in years],marker='o',label=TITLE[cand],color=color)
|
||||
ax.axhline(0,color='#8d949e',linewidth=1)
|
||||
ax.set_title(NAMES[s]);ax.set_ylabel('Brier相对改善(%)');ax.legend(fontsize=9)
|
||||
fig.suptitle('同一因素能否跨年保留优势',fontsize=17)
|
||||
fig.supxlabel('2026年仅含截至9月16日、未来20日标签已完整兑现的信号;2026年并非全年。',fontsize=10)
|
||||
fig.savefig(FIG/'三因素逐年表现.png');plt.close(fig)
|
||||
|
||||
|
||||
def one_dimension(results):
|
||||
fig,axes=plt.subplots(2,3,figsize=(15,8),layout='constrained',sharey=True)
|
||||
all_values=[r['median_return']*100 for s in STYLES for dim in ['price_rank','volume_rank']
|
||||
for r in results[s]['univariate'][dim] if r['median_return'] is not None]
|
||||
low,high=min(0,min(all_values)),max(0,max(all_values));span=max(1,high-low)
|
||||
for j,s in enumerate(STYLES):
|
||||
for i,dim in enumerate(['price_rank','volume_rank']):
|
||||
rows=results[s]['univariate'][dim]
|
||||
ax=axes[i,j]
|
||||
ax.bar(range(10),[r['median_return']*100 if r['median_return'] is not None else np.nan for r in rows],color='#366b91',alpha=.85)
|
||||
ax.axhline(0,color='#777',linewidth=.8)
|
||||
ax.set_xticks(range(10),[f'{k*10}–{k*10+10}' for k in range(10)],rotation=45,fontsize=8)
|
||||
ax.set_ylabel('未来20日收益中位数(%)')
|
||||
ax.set_xlabel(('价格' if i==0 else '成交活跃度')+'历史百分位')
|
||||
ax.set_title(NAMES[s])
|
||||
ax.set_ylim(low-span*.06,high+span*.18)
|
||||
for k,r in enumerate(rows):
|
||||
ax.text(k,.97,str(r['n']),transform=ax.get_xaxis_transform(),ha='center',va='top',fontsize=8)
|
||||
fig.suptitle('单维分层结果:价格位置与成交活跃度',fontsize=17)
|
||||
fig.supxlabel('柱顶行数字为日期数;全部使用同一历史滚动检验区间,尚未加入同步筛选。',fontsize=10)
|
||||
fig.savefig(FIG/'单维价格与成交分层.png');plt.close(fig)
|
||||
|
||||
|
||||
def current_chart(results,data):
|
||||
fig,axes=plt.subplots(3,1,figsize=(14,11),layout='constrained')
|
||||
for ax,s in zip(axes,STYLES):
|
||||
c=results[s]['current'];d=data[s].iloc[-120:]
|
||||
ax.plot(d.index,d.close,color='#1c4264',lw=1.7,label='指数收盘')
|
||||
ax.scatter(d.index[-1],d.close.iloc[-1],color='#172739',zorder=5)
|
||||
edges=c['regions']['edges'];b=int(c['price_bin'])
|
||||
ax.axhspan(edges[b],edges[b+1],color='#929aa3',alpha=.10)
|
||||
ax.axhline(edges[b],color='#717983',ls=':',lw=1)
|
||||
label=[f'当前层 {edges[b]:.0f}–{edges[b+1]:.0f}']
|
||||
for side,color,word in [('lower','#387e70','下方'),('upper','#b57c33','上方')]:
|
||||
zone=c['regions'][side]
|
||||
if zone:
|
||||
ax.axhspan(zone['low'],zone['high'],color=color,alpha=.14)
|
||||
ax.axhline(zone['high'] if side=='lower' else zone['low'],color=color,ls='--',lw=1)
|
||||
label.append(f"{word} {zone['low']:.0f}–{zone['high']:.0f}({'同步增强候选' if zone['enhanced'] else '几何参考'})")
|
||||
else: label.append(word+'无完整相邻格')
|
||||
prob=c['models']['full']['probabilities']
|
||||
ax.set_title(f"{NAMES[s]} {c['close']:.2f}点 | 模型原始分数:上涨{prob[2]:.0%} 震荡{prob[1]:.0%} 下跌{prob[0]:.0%}")
|
||||
ax.set_ylabel('指数点位')
|
||||
ax.text(.01,.03,';'.join(label),transform=ax.transAxes,fontsize=10,bbox={'facecolor':'white','alpha':.85,'edgecolor':'none'})
|
||||
ax.margins(x=.04)
|
||||
fig.suptitle('截至2026年10月23日的区间快照',fontsize=18)
|
||||
fig.supxlabel('灰带为当前价格层,层下沿仅为观察边界;概率校准表现不佳,模型原始分数不作为可信胜率。所有边界按10月23日冻结。',fontsize=10)
|
||||
fig.savefig(FIG/'当前区间快照.png');plt.close(fig)
|
||||
|
||||
|
||||
def paths_chart(results,data):
|
||||
fig,axes=plt.subplots(1,3,figsize=(15,5.4),layout='constrained',sharey=True)
|
||||
stats={}
|
||||
for ax,s in zip(axes,STYLES):
|
||||
d=data[s];events=results[s]['supplementary']['events'];stats[s]={}
|
||||
for direction,color,label in [(1,'#b77434','偏多事件'),(-1,'#386e9a','偏空事件')]:
|
||||
curves=[]
|
||||
for event in events:
|
||||
if event['direction'] != direction:continue
|
||||
date=pd.Timestamp(event['date']);t=d.index.get_loc(date);entry=d.open.iloc[t+1]
|
||||
curves.append(np.r_[0,d.close.iloc[t+1:t+21].to_numpy()/entry-1,d.open.iloc[t+21]/entry-1])
|
||||
if curves:
|
||||
a=np.asarray(curves)
|
||||
ax.plot(range(22),np.median(a,axis=0)*100,color=color,label=f'{label}中位数 n={len(a)}')
|
||||
ax.plot(range(22),np.mean(a,axis=0)*100,color=color,ls='--',alpha=.7,label=f'{label}均值')
|
||||
stats[s][str(direction)]={'n':len(a),'mean_path':a.mean(axis=0),'median_path':np.median(a,axis=0)}
|
||||
ax.axhline(0,color='#999',lw=.8);ax.legend(fontsize=8);ax.set_title(NAMES[s])
|
||||
ax.set_xticks([0,5,10,15,21],['入场','5日收','10日收','15日收','20日后开盘'],rotation=20)
|
||||
ax.set_ylabel('指数相对入场开盘变化(%)')
|
||||
fig.suptitle('固定完整模型的方向事件路径',fontsize=17)
|
||||
fig.supxlabel('方向概率至少50%、比反方向高15个百分点;事件间隔至少20交易日。指数价格路径,不是账户收益。',fontsize=10)
|
||||
fig.savefig(FIG/'各风格事件路径.png');plt.close(fig)
|
||||
dump(OUT/'event_paths.json',stats)
|
||||
|
||||
|
||||
def calibration_chart(results):
|
||||
fig,axes=plt.subplots(1,3,figsize=(15,5),layout='constrained')
|
||||
for ax,s in zip(axes,STYLES):
|
||||
ax.plot([0,1],[0,1],ls='--',color='#929aa3',label='预测与实际一致')
|
||||
for name,color in [('static','#5e8eab'),('full','#bd793a')]:
|
||||
rows=[r for r in results[s]['metrics'][name]['calibration'] if r['class']==2 and r['n']>0]
|
||||
ax.plot([r['predicted'] for r in rows],[r['observed'] for r in rows],marker='o',color=color,label=TITLE[name])
|
||||
if name=='full':
|
||||
for r in rows:ax.annotate(str(r['n']), (r['predicted'],r['observed']), xytext=(3,5),textcoords='offset points',fontsize=8)
|
||||
ax.set_xlim(0,1);ax.set_ylim(0,1);ax.set_title(NAMES[s])
|
||||
ax.set_xlabel('模型预测的上涨概率');ax.set_ylabel('实际上涨达标比例');ax.legend(fontsize=8)
|
||||
fig.suptitle('概率校准:模型越确信上涨,实际达标比例没有相应提高',fontsize=17)
|
||||
fig.supxlabel('五个固定概率组;数字为完整模型的日期数。上涨达标指20日收益超过信号时半个预测期波动尺度。',fontsize=10)
|
||||
fig.savefig(FIG/'上涨概率校准.png');plt.close(fig)
|
||||
|
||||
|
||||
def diagnostics(results,data):
|
||||
calendar=data['000001.SH'].index
|
||||
records={}
|
||||
for s in STYLES:
|
||||
pred=pd.read_json(OUT/f'predictions_{s}.json.gz',orient='table')
|
||||
dates=pred.index[pred.static_high]
|
||||
clusters=[]
|
||||
end=-1
|
||||
for date in dates:
|
||||
pos=calendar.get_loc(date)
|
||||
if pos>end:
|
||||
clusters.append({'first':str(date.date()),'last':str(date.date()),'dates':1})
|
||||
else:
|
||||
clusters[-1]['last']=str(date.date());clusters[-1]['dates']+=1
|
||||
end=pos+21
|
||||
c=results[s]['current'];b=int(c['price_bin']);edges=c['regions']['edges']
|
||||
records[s]={'static_high_days':len(dates),'overlapping_label_clusters':clusters,
|
||||
'current_band':[edges[b],edges[b+1]],'lower_observation_edge':edges[b],
|
||||
'lower_edge_distance_pct':(edges[b]/c['close']-1)*100,
|
||||
'current_up_score':c['models']['full']['probabilities'][2],
|
||||
'calibration_group':next(r for r in results[s]['metrics']['full']['calibration']
|
||||
if r['class']==2 and r['bin']==min(4,int(c['models']['full']['probabilities'][2]*5)))}
|
||||
output={'scope':'结果后的集中度和展示诊断;未重选参数,未变更主比较、协议或统计家族。',
|
||||
'clustering':'按20日标签实际退出位置连通合并;相连窗口计同一段,不声称这些段相互独立。',
|
||||
'styles':records}
|
||||
dump(OUT/'diagnostics.json',output)
|
||||
return output
|
||||
|
||||
|
||||
def write_report(results,comps,pdata,diag):
|
||||
lines=['# 宏观价格成交网格与趋势增量实验', '',
|
||||
'本轮以2026年10月23日为行情末日,检验同步形成、进入方向和价格推进效率能否改善20日趋势判断。协议于2026年10月29日北京时间11时10分固定并提交,全部计算使用开发机既有缓存。', '',
|
||||
'主结果来自2024、2025、2026三个年度的历史滚动检验;每次只用此前五个完整年训练,并删除退出日期跨入检验年的标签。已有历史参与过假设形成,因此这属于历史外推研究。', '',
|
||||
'**本轮结论:三项因素在当前网格交互模型中均未取得总体正增量,完整模型误差增加8.48%至13.05%。** 原有信息基线的误差也低于网格模型,因此下一版应优先检验更简约的网格表示,而不是继续增加指标。', '',
|
||||
'值得保留的研究线索是同步形成与高同步停滞的状态差异,以及中证2000相对较强的低位修复倾向;两者目前都不能转成确定趋势信号。当前三个风格落在最低价格层,分位网格在尾部过粗,导致没有完整下方相邻格。这是区间设计需要改进的具体位置。', '',
|
||||
'## 三项因素的增量价值', '',
|
||||
'Brier分数是上涨、震荡、下跌三种概率的平方误差之和,越小越好。下表正值表示相较网格与当前同步模型的误差下降。四个目标各七项比较统一进行28项Holm校正。', '',
|
||||
'| 判断对象 | 检验日数 | 同步形成 | 进入方向 | 推进效率 | 三项合用 |',
|
||||
'| --- | ---: | ---: | ---: | ---: | ---: |']
|
||||
lookup={(c['target'],c['candidate'],c['reference']):c for c in comps}
|
||||
for s in PRIMARY:
|
||||
vals=[lookup[(s,k,'static')]['relative_improvement_pct'] for k in ['formation','direction','efficiency','full']]
|
||||
lines.append(f"| {NAMES[s]} | {results[s]['n']} | "+' | '.join(f'{x:+.2f}%' for x in vals)+' |')
|
||||
accepted=[c for c in comps if c['target'] in PRIMARY and c['accepted']]
|
||||
lines += ['', '达到预设综合标准的主比较有'+str(len(accepted))+'项。综合标准要求改善为正、区块区间不跨零、校正后p小于0.05,并在至少三个年度中的两个年度改善。', '',
|
||||
'', '',
|
||||
'', '',
|
||||
'| 对象 | 对比 | Brier绝对改善 | 95%区块区间 | 校正p | 收益预测MSE改善 |',
|
||||
'| --- | --- | ---: | --- | ---: | ---: |']
|
||||
for c in comps:
|
||||
if c['target'] not in PRIMARY:continue
|
||||
desc=TITLE[c['candidate']]+' 相对 '+TITLE[c['reference']]
|
||||
lo,hi=c['ci95']
|
||||
lines.append(f"| {NAMES[c['target']]} | {desc} | {n(c['mean'],4)} | {n(lo,4)} 至 {n(hi,4)} | {c['p_holm']:.3f} | {c['mse_improvement_pct']:+.2f}% |")
|
||||
lines += ['', '区块区间对已固定的外推预测误差按60交易日抽样2000次,保留时间相关性;不把每一天或每一网格当成独立试验,也不涵盖全部训练重估不确定性。', '',
|
||||
'## 各风格概率表现与覆盖', '',
|
||||
'| 对象 | 当前同步模型Brier | 完整模型Brier | 完整模型准确率 | 方向判断覆盖率 |',
|
||||
'| --- | ---: | ---: | ---: | ---: |']
|
||||
for s in PRIMARY:
|
||||
m=results[s]['metrics'];lines.append(f"| {NAMES[s]} | {m['static']['brier']:.4f} | {m['full']['brier']:.4f} | {p(m['full']['accuracy'],1)} | {p(m['full']['coverage'],1)} |")
|
||||
lines += ['', '| 对象 | 原有信息基线 | 加网格 | 加当前同步 | 再加三项因素 |',
|
||||
'| --- | ---: | ---: | ---: | ---: |']
|
||||
for s in PRIMARY:
|
||||
m=results[s]['metrics']
|
||||
lines.append('| '+NAMES[s]+' | '+' | '.join(f"{m[k]['brier']:.4f}" for k in ['baseline','grid','static','full'])+' |')
|
||||
lines += ['', '上表各列为同一日期集合的Brier,逐步扩展使用信息。它说明整套网格相较原有信息的变化;主检验的三因素增量仍以当前同步模型为对照。', '',
|
||||
'全市场标签要求沪深300、中证500、中证1000中至少两个出现同方向、超过各自半个预测期波动尺度的变化;它与三指数平均收益的正负并不等同。风格标签分别计算,创业板与宽基的重叠不增加全市场票数。', '',
|
||||
'中证2000的历史外推日期均在发布后,五年训练包含发布前回溯数据;它也不等同PMS微盘股票篮子。科创50因自身QRS预热后不足五个完整训练年,不生成同等级外推预测,描述结果保留在机器结果中。', '',
|
||||
'## 同步形成与同步高位的状态差异', '',
|
||||
'| 风格 | 状态 | 日期数 | 20日均值/中位数 | 相较静态模型预测的平均收益差 |',
|
||||
'| --- | --- | ---: | --- | ---: |']
|
||||
for s in STYLES:
|
||||
for key,label in [('forming','同步形成较强'),('static_high','同步较高且不再增强'),('weak_forming','同步形成较弱')]:
|
||||
r=results[s]['synchrony_states'][key]
|
||||
lines.append(f"| {NAMES[s]} | {label} | {r['n']} | {p(r['mean_return'])}/{p(r['median_return'])} | {p(r['mean_residual_return'])} |")
|
||||
lines += ['', '强弱分位阈值只在对应训练期估计。这张表展示状态差异,不控制三组网格构成完全相同;三因素的正式增量结论以同日期模型比较为准。', '',
|
||||
'## 价格与成交分层', '', '', '',
|
||||
'', '',
|
||||
'', '',
|
||||
'二维网格按各自此前252日价格位置与成交活跃度分成五层。价格位置以此前60日均价和20日ATR归一;活跃度使用含当日5日平均成交额除以此前60日成交额中位数。同步残差由每个训练期的基线特征岭回归去除动量、波动、QRS、普通涨跌广度和股汇背景。图中灰格仍保留探索读数,不能据其颜色宣称单格显著。', '',
|
||||
'## 截至数据末日的区间判断', '',
|
||||
'以下是2026年10月23日收盘快照,不代表10月29日实时判断。概率来自事先固定的完整模型,并与当前同步基线并列;未根据本轮结果更换当前模型。价格百分位是相对均价的ATR偏离位置在过去一年的分位,不是指数绝对点位的历史分位。', '',
|
||||
'完整模型的概率校准表现不佳,下列数字只作为原始模型分数,不能直接解释成可信胜率。三指数平均价格相关性为0.925,较此前非重叠60日上升0.022,尚未达到训练期强同步形成门槛0.075。三个风格的成交活跃度分别为常态参照的82.4%、79.7%、86.4%;从价格位置、量能和模型分歧判断,目前更像共同下跌后的分化修复,未形成全市场一致的可确认方向。', '',
|
||||
'| 对象 | 收盘点位 | 均价偏离百分位 | 成交百分位 | 5日推进效率 | 原始模型下跌/震荡/上涨 | 模型倾向 |',
|
||||
'| --- | ---: | ---: | ---: | ---: | --- | --- |']
|
||||
for s in PRIMARY:
|
||||
c=results[s]['current'];m=c['models']['full']
|
||||
lines.append(f"| {NAMES[s]} | {n(c.get('close'))} | {p(c['price_rank'],1)} | {p(c['volume_rank'],1)} | {p(c['efficiency'],1)} | "+'/'.join(p(v,1) for v in m['probabilities'])+f" | {m['direction']} |")
|
||||
lines += ['', '', '',
|
||||
'| 风格 | 当前所在价格层 | 层下沿相距现价 | 上方同步区域或参考 |',
|
||||
'| --- | --- | ---: | --- |']
|
||||
for s in STYLES:
|
||||
info=diag['styles'][s];z=results[s]['current']['regions']['upper']
|
||||
upper='无' if z is None else f"{z['low']:.0f}至{z['high']:.0f}({'同步增强候选' if z['enhanced'] else '几何参考'})"
|
||||
lines.append(f"| {NAMES[s]} | {info['current_band'][0]:.0f}至{info['current_band'][1]:.0f} | {info['lower_edge_distance_pct']:+.2f}% | {upper} |")
|
||||
lines += ['', '当前层下沿4016、2553、2521点分别是三个风格的历史分位投影边界,可用于观察是否继续跌出已有状态范围;本轮没有证明它们会形成支撑。由于三个风格都在最下层,其下方没有完整相邻格,双侧首次到达统计的有效分母为零。三个风格同格类比原有74、84、52个日期,均因缺少完整下方区域未进入双侧比较。', '',
|
||||
'区间由训练期同成交层的同步残差、样本覆盖筛选,再按快照日均价和ATR投影。没有合格同步增强区域时,只显示相邻几何参考;超出历史有限边界时不外推一个目标。首次到达与触区响应的逐项状态保留在完整结果中。', '',
|
||||
'| 风格 | 当前同步基线下跌/震荡/上涨 |',
|
||||
'| --- | --- |']
|
||||
for s in STYLES:
|
||||
c=results[s]['current']
|
||||
lines.append(f"| {NAMES[s]} | "+'/'.join(p(v,1) for v in c['models']['static']['probabilities'])+' |')
|
||||
lines += ['', '响应定义为触区后五个完整收盘内,在先穿透外侧半个ATR之前反向移动一个ATR;ATR与区间在信号日冻结。开盘越界、触区即穿透和观察不完整分别记账。', '',
|
||||
'## 概率校准', '', '', '',
|
||||
'| 风格 | 当前上涨原始分数 | 所在概率组历史日数 | 该组平均预测 | 实际上涨达标比例 |',
|
||||
'| --- | ---: | ---: | ---: | ---: |']
|
||||
for s in STYLES:
|
||||
info=diag['styles'][s];g=info['calibration_group']
|
||||
lines.append(f"| {NAMES[s]} | {p(info['current_up_score'],1)} | {g['n']} | {p(g['predicted'],1)} | {p(g['observed'],1)} |")
|
||||
lines += ['', '此表是历史外推概率组的诊断,不是对当前样本另做概率重标定。它解释为什么当前中证2000的50.7%原始上涨分数只能描述模型倾向。', '',
|
||||
'## 方向事件路径与费用压力', '',
|
||||
'', '',
|
||||
'| 对象 | 事件数 | 10基点方向幅度均值/中位数 | 30基点 | 60基点 |',
|
||||
'| --- | ---: | --- | --- | --- |']
|
||||
for s in PRIMARY:
|
||||
z=results[s]['supplementary'];parts=[]
|
||||
for bp in [10,30,60]:
|
||||
v=z['summary'][f'{bp}bps'];parts.append(p(v['mean'])+'/'+p(v['median']))
|
||||
lines.append(f"| {NAMES[s]} | {z['event_count']} | "+' | '.join(parts)+' |')
|
||||
lines += ['', '事件按固定完整模型概率至少50%、比反方向高15个百分点触发,并间隔至少20交易日。偏空只检验价格下跌方向的幅度,不假定股票可卖空。指数开盘标签与统一摩擦不构成账户净收益,也不代表PMS现有09:35调度可按开盘成交。', '',
|
||||
'## 下一轮值得优先检验的改动', '',
|
||||
'第一,先去掉逐格交互,分别比较单维网格、二维平滑网格和全局三因素。当前完整模型有456列输入,每个训练窗约1190个重叠日标签;网格扩展已经降低外推表现。模型复杂度是否掩盖了原始因素的价值,需要另立对照验证。', '',
|
||||
'第二,保留同步形成与高位停滞的区别,但按行情段评估。高同步且不再增强只有21个日期,未来收益窗口相连后合为'+str(len(diag['styles']['000300.SH']['overlapping_label_clusters']))+'段;不能用21个日期当21次独立发现。其明显较差的路径更适合进一步检验为趋势衰减条件。', '',
|
||||
'第三,在最低和最高价格层内增加事先固定的ATR细格。当前三个风格均处最下层,分位网格将不同深度的下跌压在同一格,无法形成完整的下方候选区域。补足尾部结构后,再检验支撑反应与反转确认。', '',
|
||||
'以上是结果后的研究建议,本轮没有重跑新参数或覆盖原结果。', '',
|
||||
'## 可复核资料', '',
|
||||
'- [固定协议](protocol.json)与[摘要](protocol.sha256)。',
|
||||
'- [完整统计](results/results.json)、[逐项比较](results/comparisons.json)、[数据覆盖](results/data_coverage.json)。',
|
||||
'- [独立复核](results/audit.json)与[运行摘要](results/manifest.json)。',
|
||||
'- 逐日特征和预测保存在results目录的压缩JSON中;源行情沿用既有本地缓存,协议记录其摘要。',
|
||||
'- 研究代码为engine.py、regions.py、run.py、render.py、audit.py及test_engine.py。', '',
|
||||
'所有结果在开发机计算。线上应用、参数、持仓、数据库和容器均未改动;本研究代码不进入业务镜像,无受影响业务容器。', '']
|
||||
(HERE/'宏观网格首轮研究报告.md').write_text('\n'.join(lines))
|
||||
|
||||
|
||||
def main():
|
||||
sys.addaudithook(no_network)
|
||||
pdata=protocol();results=json.loads((OUT/'results.json').read_text());comps=json.loads((OUT/'comparisons.json').read_text())
|
||||
data,_,_=load_data(pdata)
|
||||
FIG.mkdir(exist_ok=True);style()
|
||||
region_heatmaps(results);support_heatmaps(results);incremental(comps);one_dimension(results)
|
||||
current_chart(results,data);paths_chart(results,data);calibration_chart(results)
|
||||
diag=diagnostics(results,data);write_report(results,comps,pdata,diag)
|
||||
dump(OUT/'render_manifest.json',{'figures':{x.name:sha(x) for x in FIG.glob('*.png')},'render_code':sha(HERE/'render.py')})
|
||||
print('已生成8幅研究图与报告。')
|
||||
|
||||
|
||||
if __name__=='__main__':main()
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
numpy==2.3.5
|
||||
pandas==2.2.3
|
||||
scipy==1.16.3
|
||||
scikit-learn==1.7.2
|
||||
matplotlib==3.10.7
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
"status": "passed",
|
||||
"verified_at_beijing": "2026-10-29T11:34:52+08:00",
|
||||
"counts": {
|
||||
"direct_feature_rows": 600,
|
||||
"qrs_reference_rows": 18,
|
||||
"forward_returns": 3942,
|
||||
"class_labels": 3942,
|
||||
"prediction_probability_rows": 39420,
|
||||
"fold_boundaries": 18,
|
||||
"grid_partitions": 6,
|
||||
"prefix_features": 14,
|
||||
"prediction_replays": 36,
|
||||
"current_region_boundaries": 5,
|
||||
"metrics": 60,
|
||||
"path_risk_rows": 3285,
|
||||
"historical_first_passage_rows": 393,
|
||||
"current_probability_replays": 12
|
||||
},
|
||||
"protocol_sha256": "4bf71734005efa223946b796484b4914296ab363a29a4bad302cb102923bef1f",
|
||||
"raw_digests": "全部匹配",
|
||||
"network": "经审计钩子禁止联网;只读本地缓存",
|
||||
"primary_comparison_count": 28,
|
||||
"secondary_comparison_count": 21,
|
||||
"limitations": "复核研究计算,不替代原始供应商行情逐日历史版本或真实成交验收。"
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
"status": "passed",
|
||||
"verified_at_beijing": "2026-10-29T11:34:52+08:00",
|
||||
"counts": {
|
||||
"direct_feature_rows": 600,
|
||||
"qrs_reference_rows": 18,
|
||||
"forward_returns": 3942,
|
||||
"class_labels": 3942,
|
||||
"prediction_probability_rows": 39420,
|
||||
"fold_boundaries": 18,
|
||||
"grid_partitions": 6,
|
||||
"prefix_features": 14,
|
||||
"prediction_replays": 36,
|
||||
"current_region_boundaries": 5,
|
||||
"metrics": 60,
|
||||
"path_risk_rows": 3285,
|
||||
"historical_first_passage_rows": 393,
|
||||
"current_probability_replays": 12
|
||||
},
|
||||
"protocol_sha256": "4bf71734005efa223946b796484b4914296ab363a29a4bad302cb102923bef1f",
|
||||
"raw_digests": "全部匹配",
|
||||
"network": "经审计钩子禁止联网;只读本地缓存",
|
||||
"primary_comparison_count": 28,
|
||||
"secondary_comparison_count": 21,
|
||||
"limitations": "复核研究计算,不替代原始供应商行情逐日历史版本或真实成交验收。"
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
{
|
||||
"000300.SH": {
|
||||
"rows": 5294,
|
||||
"first": "2005-01-04",
|
||||
"last": "2026-10-23",
|
||||
"invalid_ohlc": 0,
|
||||
"invalid_amount": 0
|
||||
},
|
||||
"000905.SH": {
|
||||
"rows": 5294,
|
||||
"first": "2005-01-04",
|
||||
"last": "2026-10-23",
|
||||
"invalid_ohlc": 0,
|
||||
"invalid_amount": 0
|
||||
},
|
||||
"000852.SH": {
|
||||
"rows": 5294,
|
||||
"first": "2005-01-04",
|
||||
"last": "2026-10-23",
|
||||
"invalid_ohlc": 0,
|
||||
"invalid_amount": 0
|
||||
},
|
||||
"399006.SZ": {
|
||||
"rows": 3981,
|
||||
"first": "2010-05-31",
|
||||
"last": "2026-10-23",
|
||||
"invalid_ohlc": 1,
|
||||
"invalid_amount": 1
|
||||
},
|
||||
"000688.SH": {
|
||||
"rows": 1649,
|
||||
"first": "2019-12-31",
|
||||
"last": "2026-10-23",
|
||||
"invalid_ohlc": 1,
|
||||
"invalid_amount": 0
|
||||
},
|
||||
"000001.SH": {
|
||||
"rows": 5294,
|
||||
"first": "2005-01-04",
|
||||
"last": "2026-10-23",
|
||||
"invalid_ohlc": 0,
|
||||
"invalid_amount": 0
|
||||
},
|
||||
"932000.CSI": {
|
||||
"rows": 3113,
|
||||
"first": "2013-12-31",
|
||||
"last": "2026-10-23",
|
||||
"invalid_ohlc": 1,
|
||||
"invalid_amount": 0
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
{
|
||||
"verified_at_beijing": "2026-10-29T11:50:49+08:00",
|
||||
"protocol_sha256": "4bf71734005efa223946b796484b4914296ab363a29a4bad302cb102923bef1f",
|
||||
"protocol_commit": "2877c59",
|
||||
"local_links_checked": 20,
|
||||
"figures_visually_checked": 8,
|
||||
"tests_passed": 12,
|
||||
"audit": {
|
||||
"direct_feature_rows": 600,
|
||||
"qrs_reference_rows": 18,
|
||||
"forward_returns": 3942,
|
||||
"class_labels": 3942,
|
||||
"prediction_probability_rows": 39420,
|
||||
"fold_boundaries": 18,
|
||||
"grid_partitions": 6,
|
||||
"prefix_features": 14,
|
||||
"prediction_replays": 36,
|
||||
"current_region_boundaries": 5,
|
||||
"metrics": 60,
|
||||
"path_risk_rows": 3285,
|
||||
"historical_first_passage_rows": 393,
|
||||
"current_probability_replays": 12
|
||||
},
|
||||
"runtime": {
|
||||
"python": "3.12.14",
|
||||
"numpy": "2.3.5",
|
||||
"pandas": "2.2.3",
|
||||
"scipy": "1.16.3",
|
||||
"sklearn": "1.7.2",
|
||||
"matplotlib": "3.10.7"
|
||||
},
|
||||
"files_sha256": {
|
||||
".gitignore": "e99f6ee4eb88882ea6589ecb717e10a41401763241b246b6821bc091cfe2227e",
|
||||
"README.md": "03daa2a7578093ccb1cb59a56b486c92f55b8a20100e181f3c260694ec531c09",
|
||||
"audit.py": "50f303e912f6519158497eadebb64240bfd93a77ab139b3c8ae8ca9eedc4953a",
|
||||
"engine.py": "d448f018fbf6286859161a0068f3c03d0416b53f840fee3d8478aaa6298053d6",
|
||||
"figures/三因素增量比较.png": "a7e03fa9dda75fac682c0492d07ef96e5342d4a112e14c6d093f597ac5cf8909",
|
||||
"figures/三因素逐年表现.png": "b36ef3fc1163b54dc927a22fbb18c74eddb1d20366342b9657f384222b67c6e5",
|
||||
"figures/上涨概率校准.png": "0a5f4c661cc853c1ddc603c34270813d4a264aae42c13af2b044268342e1cd2c",
|
||||
"figures/区域同步与收益热力图.png": "e685b158a513e46a6eca949ecc98522a7d4518894ab3a2cd1b0a937dc289bc93",
|
||||
"figures/单维价格与成交分层.png": "2b31dc4fc39a26dccae4ff73ae517bc112860610fb7d56703ed1e30b1cf266d3",
|
||||
"figures/各风格事件路径.png": "cd48e8baac4a3665a59e631bad524e109575e58946157cbd663bcbe7ef1df36b",
|
||||
"figures/当前区间快照.png": "f45ca3cfd00d7f9ddb88355f63974b111b19036f1d12f199969105a0907ccaea",
|
||||
"figures/网格样本覆盖.png": "000b543fe43ac2a5135c0cfe3bfb3e9ee6442c202c917aae967dd4c9a79a3178",
|
||||
"finalize.py": "ab738095daddaf73b9dfb30cea502a70cbeb9a379c475be997c9e5e7fff772e7",
|
||||
"protocol.json": "4bf71734005efa223946b796484b4914296ab363a29a4bad302cb102923bef1f",
|
||||
"protocol.sha256": "9bfd3b7a21e2b49230b291b8a3408b1cac221b57d9d8e2f345e43b54f46991de",
|
||||
"regions.py": "f074add1dda60c4b0d1d184b3094759c3a02abbc7e423a4f58383a1e06f54deb",
|
||||
"render.py": "0a76fcbd4ce1cdfde1f7d9cbc45861233d3ab13e861e46d30753759a779ca8e8",
|
||||
"requirements.txt": "34363f1189b1cf8def8ac011877a23fd3a0028a8a2e0f9ae0196f9589c6e99b7",
|
||||
"results/audit.json": "2f08ef2233894da26e3f9f9e4bf0b32ade123c438683030ec40416beec1fc92a",
|
||||
"results/audit.log": "2f08ef2233894da26e3f9f9e4bf0b32ade123c438683030ec40416beec1fc92a",
|
||||
"results/comparisons.json": "ef872d4c536c84221120d78285ea3af77db97b922c5bd844c8af42784b29beff",
|
||||
"results/data_coverage.json": "e13ae618908f0a0284bf179b4ca752251c5d773935a13931a89addf7803efd71",
|
||||
"results/diagnostics.json": "6701c2d07be586547232f3f680391ae40081e14919581d92db02c2e5c551a9bd",
|
||||
"results/event_paths.json": "49f0d4cb176a4cae1d5351b15fda420ef305baca3cebdca49605e0ca348e57bd",
|
||||
"results/features_000300.SH.json.gz": "350053d8db599cdaa2b688577a4673940cb368af11aa4448cc59eed894db4271",
|
||||
"results/features_000688.SH.json.gz": "64aedac55ecdbf9f1cec7f82096021984afb24b983cb5fe5e9a26ce317c7ddd9",
|
||||
"results/features_000852.SH.json.gz": "c2285752c1ab4a9760cc16ea01ca040023d68d65cbbd35becd1db073d4b8707e",
|
||||
"results/features_000905.SH.json.gz": "9f58af8717b21da22087285993dec108d757ac6242a683f32cc0ed5f0e54f999",
|
||||
"results/features_399006.SZ.json.gz": "c5562ff33889389b81e9120ed1c2a40285fcf8ce893b249ce6d790b34a17ee44",
|
||||
"results/features_932000.CSI.json.gz": "ab8c5340bab1e7ec1065485a78e957fae6689e9a06a556010f6f4eb2de6a09df",
|
||||
"results/features_market.json.gz": "f63f0e2b994e5f32dfbb42c66d7b92e62c6fb1c24b9150985f9afd79f8097013",
|
||||
"results/manifest.json": "2fecb9958521c0c5916edb9a7fe55f08f730e15b282ff86f65d7b5ee6d4307d6",
|
||||
"results/predictions_000300.SH.json.gz": "b216dd599a4ea276c7246d541ade27f188b2f4e94292c2b0789826f395d2f804",
|
||||
"results/predictions_000852.SH.json.gz": "640d55fd75bed5f0c8fa983f28cb1550d6ae8b46a0c3acba39b41fd00c6b7373",
|
||||
"results/predictions_000905.SH.json.gz": "bb58eab31ddda157b9090535c4b4f46e0a4f77e711fdb7bf72fdfc915876a4f2",
|
||||
"results/predictions_399006.SZ.json.gz": "1deefbea27456b0deeff4ead32727c4279818ef2de2273a47b577d429c0317c9",
|
||||
"results/predictions_932000.CSI.json.gz": "1a55bcf098b1a34b1a93208f76046744df2e64a535672037abea7edd05da5869",
|
||||
"results/predictions_market.json.gz": "2ea1d3580a08b51c666cf020f6c1527efff9dc4a3cce666be9c0077b7813c96d",
|
||||
"results/render.log": "f8c3894d89cce3a0e6938c3ebaabe42dfd0263676818b1011e809a3613392475",
|
||||
"results/render_manifest.json": "6e00af890ed031d5b0fa7d01254f57a2edabbcab1fd9c30a0b7f9850608146ba",
|
||||
"results/results.json": "3362c17a4644cec3713217ce3149b0fa948327e8b03995f8c415a8ad9119afc4",
|
||||
"results/run.log": "29786f733e226a8bc8ea12fb03d70e443899ca46f2376e156f07da22521be1b1",
|
||||
"results/unit_tests.txt": "dac29fa0fc8bec3a87edfb2f95deed47799b0e141929afb3bcc63e96cdeb73ec",
|
||||
"run.py": "d00db62465972abac17625ccc687e578f26ba1737aae789de276bf772c47332c",
|
||||
"test_engine.py": "308577f36b9bff63881d18c3ecd5cc069bd62a46ace3696f46fb1c6f0da3c858",
|
||||
"宏观网格首轮研究报告.md": "3b78401702fa2a42275c3749a585c555662588a56f1af23148289dade2dc1092"
|
||||
},
|
||||
"online_changes": false,
|
||||
"source_data_cutoff": "2026-10-23"
|
||||
}
|
||||
|
|
@ -0,0 +1,138 @@
|
|||
{
|
||||
"scope": "结果后的集中度和展示诊断;未重选参数,未变更主比较、协议或统计家族。",
|
||||
"clustering": "按20日标签实际退出位置连通合并;相连窗口计同一段,不声称这些段相互独立。",
|
||||
"styles": {
|
||||
"000300.SH": {
|
||||
"static_high_days": 21,
|
||||
"overlapping_label_clusters": [
|
||||
{
|
||||
"first": "2024-11-05",
|
||||
"last": "2024-12-11",
|
||||
"dates": 7
|
||||
},
|
||||
{
|
||||
"first": "2025-01-21",
|
||||
"last": "2025-01-22",
|
||||
"dates": 2
|
||||
},
|
||||
{
|
||||
"first": "2025-06-24",
|
||||
"last": "2025-06-26",
|
||||
"dates": 3
|
||||
},
|
||||
{
|
||||
"first": "2025-12-10",
|
||||
"last": "2025-12-10",
|
||||
"dates": 1
|
||||
},
|
||||
{
|
||||
"first": "2026-06-15",
|
||||
"last": "2026-07-17",
|
||||
"dates": 8
|
||||
}
|
||||
],
|
||||
"current_band": [
|
||||
4016.2526309647255,
|
||||
4369.79705617408
|
||||
],
|
||||
"lower_observation_edge": 4016.2526309647255,
|
||||
"lower_edge_distance_pct": -2.169764657456996,
|
||||
"current_up_score": 0.26369420815636674,
|
||||
"calibration_group": {
|
||||
"class": 2,
|
||||
"bin": 1,
|
||||
"n": 232,
|
||||
"predicted": 0.2890509209683672,
|
||||
"observed": 0.4224137931034483
|
||||
}
|
||||
},
|
||||
"399006.SZ": {
|
||||
"static_high_days": 21,
|
||||
"overlapping_label_clusters": [
|
||||
{
|
||||
"first": "2024-11-05",
|
||||
"last": "2024-12-11",
|
||||
"dates": 7
|
||||
},
|
||||
{
|
||||
"first": "2025-01-21",
|
||||
"last": "2025-01-22",
|
||||
"dates": 2
|
||||
},
|
||||
{
|
||||
"first": "2025-06-24",
|
||||
"last": "2025-06-26",
|
||||
"dates": 3
|
||||
},
|
||||
{
|
||||
"first": "2025-12-10",
|
||||
"last": "2025-12-10",
|
||||
"dates": 1
|
||||
},
|
||||
{
|
||||
"first": "2026-06-15",
|
||||
"last": "2026-07-17",
|
||||
"dates": 8
|
||||
}
|
||||
],
|
||||
"current_band": [
|
||||
2553.1998854671347,
|
||||
3127.743621035444
|
||||
],
|
||||
"lower_observation_edge": 2553.1998854671347,
|
||||
"lower_edge_distance_pct": -4.876768971318713,
|
||||
"current_up_score": 0.07650387594295283,
|
||||
"calibration_group": {
|
||||
"class": 2,
|
||||
"bin": 0,
|
||||
"n": 254,
|
||||
"predicted": 0.08973370979213298,
|
||||
"observed": 0.3346456692913386
|
||||
}
|
||||
},
|
||||
"932000.CSI": {
|
||||
"static_high_days": 21,
|
||||
"overlapping_label_clusters": [
|
||||
{
|
||||
"first": "2024-11-05",
|
||||
"last": "2024-12-11",
|
||||
"dates": 7
|
||||
},
|
||||
{
|
||||
"first": "2025-01-21",
|
||||
"last": "2025-01-22",
|
||||
"dates": 2
|
||||
},
|
||||
{
|
||||
"first": "2025-06-24",
|
||||
"last": "2025-06-26",
|
||||
"dates": 3
|
||||
},
|
||||
{
|
||||
"first": "2025-12-10",
|
||||
"last": "2025-12-10",
|
||||
"dates": 1
|
||||
},
|
||||
{
|
||||
"first": "2026-06-15",
|
||||
"last": "2026-07-17",
|
||||
"dates": 8
|
||||
}
|
||||
],
|
||||
"current_band": [
|
||||
2520.872565545826,
|
||||
2943.557380562127
|
||||
],
|
||||
"lower_observation_edge": 2520.872565545826,
|
||||
"lower_edge_distance_pct": -11.742491358980656,
|
||||
"current_up_score": 0.5069465543558562,
|
||||
"calibration_group": {
|
||||
"class": 2,
|
||||
"bin": 2,
|
||||
"n": 153,
|
||||
"predicted": 0.48206095782871244,
|
||||
"observed": 0.30718954248366015
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,314 @@
|
|||
{
|
||||
"000300.SH": {
|
||||
"1": {
|
||||
"n": 11,
|
||||
"mean_path": [
|
||||
0.0,
|
||||
0.006570640838816785,
|
||||
0.005499871942907403,
|
||||
0.004987104553688196,
|
||||
0.008199709776004099,
|
||||
0.010969844503162034,
|
||||
0.006272277520588828,
|
||||
-0.0030806031987567835,
|
||||
-0.0022135764984020975,
|
||||
0.00018151167339644755,
|
||||
0.0029863152185506795,
|
||||
0.008236788378884953,
|
||||
0.006115807062520845,
|
||||
0.0013070049144290733,
|
||||
0.002171783581381996,
|
||||
0.003999868779768081,
|
||||
0.0017714660491681335,
|
||||
0.003269690363675112,
|
||||
0.003951614528760533,
|
||||
0.002421425204398778,
|
||||
0.0036090873456986372,
|
||||
0.0020222163081141764
|
||||
],
|
||||
"median_path": [
|
||||
0.0,
|
||||
0.0043662091427658645,
|
||||
0.005321364575022747,
|
||||
0.003205784877707174,
|
||||
0.007518405629493241,
|
||||
0.00943328696756951,
|
||||
0.010152571044576897,
|
||||
-0.0011107493362669807,
|
||||
-0.0019917213641608145,
|
||||
-0.0008075856268581205,
|
||||
-0.0003746426279759296,
|
||||
0.0005094718399183407,
|
||||
-0.005209016254262622,
|
||||
-0.0026281609443274956,
|
||||
-0.01461980850232203,
|
||||
-0.0057437160318477565,
|
||||
-0.006562112310238399,
|
||||
-0.006897405782681276,
|
||||
-0.009940268602562696,
|
||||
-0.0024767085423940127,
|
||||
0.001855770468057738,
|
||||
0.0028642704416566023
|
||||
]
|
||||
},
|
||||
"-1": {
|
||||
"n": 16,
|
||||
"mean_path": [
|
||||
0.0,
|
||||
0.0017155757019183052,
|
||||
-0.00198860550099373,
|
||||
-0.003225138930491943,
|
||||
-0.003353368277224239,
|
||||
0.0017582150778387567,
|
||||
0.0004617089265468166,
|
||||
-0.001430140365670421,
|
||||
-0.00038824359191843433,
|
||||
-0.001168641571723153,
|
||||
0.0020228821721833407,
|
||||
0.0003290162393931456,
|
||||
0.003196644317157875,
|
||||
0.007738745440861235,
|
||||
0.011247533678700239,
|
||||
0.014404659179101602,
|
||||
0.02486270372727844,
|
||||
0.028651343807676442,
|
||||
0.032872752747964334,
|
||||
0.025197643540077586,
|
||||
0.022964190942168436,
|
||||
0.02080992982227594
|
||||
],
|
||||
"median_path": [
|
||||
0.0,
|
||||
-0.000230485654999113,
|
||||
-0.0033905468266189343,
|
||||
-0.007555147077344282,
|
||||
-0.003133119746633939,
|
||||
0.0041407870294815075,
|
||||
0.0022648675601451007,
|
||||
0.00034396021787513886,
|
||||
0.0029262009629338337,
|
||||
0.0019063383433660097,
|
||||
0.0039657594729219925,
|
||||
-0.00546371173049881,
|
||||
-0.003253307856481369,
|
||||
0.016403308746715806,
|
||||
0.016125307418420443,
|
||||
0.016208842391408074,
|
||||
0.01724090252700139,
|
||||
0.01506437842896835,
|
||||
0.011145047628305815,
|
||||
0.00838488372792201,
|
||||
0.009835181967619233,
|
||||
0.010373699498943512
|
||||
]
|
||||
}
|
||||
},
|
||||
"399006.SZ": {
|
||||
"1": {
|
||||
"n": 17,
|
||||
"mean_path": [
|
||||
0.0,
|
||||
0.0017475340130127656,
|
||||
-0.0032888203713133226,
|
||||
-0.005251450481605469,
|
||||
-7.252422356732669e-05,
|
||||
-0.0039119098202735235,
|
||||
-0.009856960836738038,
|
||||
-0.012720273409437632,
|
||||
-0.008824097325051758,
|
||||
-0.012505252153489313,
|
||||
-0.020837546933386752,
|
||||
-0.020904406644915847,
|
||||
-0.022400711693341378,
|
||||
-0.021838696149184746,
|
||||
-0.021646198866086527,
|
||||
-0.013864273104082003,
|
||||
-0.013522691124358973,
|
||||
-0.018189679156226,
|
||||
-0.012125998852570445,
|
||||
-0.0012818741024102794,
|
||||
0.008838431003603087,
|
||||
0.023020165090002456
|
||||
],
|
||||
"median_path": [
|
||||
0.0,
|
||||
0.0041786706755211345,
|
||||
-0.004497460478977255,
|
||||
-0.004988316558131922,
|
||||
0.006586213294172083,
|
||||
0.0038223536827466376,
|
||||
-0.0037706616421404604,
|
||||
-0.0031449060443611287,
|
||||
-0.013637967677807783,
|
||||
-0.010955586292708541,
|
||||
-0.017852431684471037,
|
||||
-0.014070132280793346,
|
||||
-0.012206806188073571,
|
||||
-0.033839041973844464,
|
||||
-0.014858169850237357,
|
||||
-0.03260925906839496,
|
||||
-0.040172844082062786,
|
||||
-0.031780907061525254,
|
||||
-0.028339070871618466,
|
||||
-0.016163144797198936,
|
||||
-0.01170803757862282,
|
||||
-0.009535726771904951
|
||||
]
|
||||
},
|
||||
"-1": {
|
||||
"n": 10,
|
||||
"mean_path": [
|
||||
0.0,
|
||||
0.004056303001543837,
|
||||
-0.007340020369873412,
|
||||
0.0004287623934398188,
|
||||
-0.00466766101274364,
|
||||
0.003397407451186896,
|
||||
0.00488996440535393,
|
||||
0.013488079131279718,
|
||||
0.019319660316223907,
|
||||
0.018224816229959494,
|
||||
0.024670397814035416,
|
||||
0.02894616689471511,
|
||||
0.02942120315130765,
|
||||
0.029207201527238325,
|
||||
0.021789282350598748,
|
||||
0.024348358422447824,
|
||||
0.030017760554270478,
|
||||
0.02788315484834928,
|
||||
0.026736080550371688,
|
||||
0.03618725940145488,
|
||||
0.031222562965888424,
|
||||
0.029247165109277683
|
||||
],
|
||||
"median_path": [
|
||||
0.0,
|
||||
0.002451658112198274,
|
||||
-0.007009369584621505,
|
||||
0.00628881452517982,
|
||||
0.008518619673313355,
|
||||
0.0068310106091452605,
|
||||
0.008488329747544765,
|
||||
0.0059818576514185096,
|
||||
0.007682093956626002,
|
||||
0.009815493452015156,
|
||||
0.02172195177750924,
|
||||
0.013770012312893543,
|
||||
0.018064758008629722,
|
||||
0.012031243789256352,
|
||||
0.01044940930704652,
|
||||
0.0038621705468688816,
|
||||
0.007762057774402253,
|
||||
0.0014679485406692838,
|
||||
-0.0024084360722859133,
|
||||
0.013395062806290214,
|
||||
0.006491135658612257,
|
||||
-0.0018220050499206386
|
||||
]
|
||||
}
|
||||
},
|
||||
"932000.CSI": {
|
||||
"1": {
|
||||
"n": 17,
|
||||
"mean_path": [
|
||||
0.0,
|
||||
0.006151907253094903,
|
||||
0.010670434521707853,
|
||||
0.014492854914044662,
|
||||
0.019864937286353863,
|
||||
0.013249666916294523,
|
||||
0.015748838439154898,
|
||||
0.017814993389524768,
|
||||
0.007441579148114954,
|
||||
0.003385573564183113,
|
||||
0.01034876814376415,
|
||||
0.007619216373177764,
|
||||
0.009441238893569893,
|
||||
0.002568099748092036,
|
||||
-0.0016437736954200536,
|
||||
0.0014556539846399296,
|
||||
0.0013417723281636883,
|
||||
0.002979117271802014,
|
||||
0.011460237233028196,
|
||||
0.009521837453306925,
|
||||
0.010755172182752367,
|
||||
0.00980431068230021
|
||||
],
|
||||
"median_path": [
|
||||
0.0,
|
||||
0.0074836199458110375,
|
||||
0.014469526445338055,
|
||||
0.02004466208363631,
|
||||
0.025204158268612886,
|
||||
0.02454174629711381,
|
||||
0.011986090554124917,
|
||||
0.01604787693245724,
|
||||
0.005236440683663357,
|
||||
-0.00023639847997558583,
|
||||
0.0053307813604062115,
|
||||
0.015273648171072685,
|
||||
0.007651218345288147,
|
||||
0.007049451211357027,
|
||||
0.0012980471683023964,
|
||||
0.006989113921299417,
|
||||
0.007008355031342894,
|
||||
0.008904082649575651,
|
||||
0.02079928988191937,
|
||||
0.023106041964465307,
|
||||
0.03119643184693044,
|
||||
0.024914155681522665
|
||||
]
|
||||
},
|
||||
"-1": {
|
||||
"n": 7,
|
||||
"mean_path": [
|
||||
0.0,
|
||||
-0.003397993415062544,
|
||||
0.0036673096645584474,
|
||||
0.010316131663968038,
|
||||
0.01696367757785752,
|
||||
-0.0018382968924728032,
|
||||
0.00828106848211853,
|
||||
0.016115601729485212,
|
||||
0.008479191488379199,
|
||||
0.017756236744682487,
|
||||
0.02313624963959569,
|
||||
0.021790108890670568,
|
||||
0.018083371553326875,
|
||||
0.012741419235333895,
|
||||
0.005602306714250692,
|
||||
-0.0020253245104017953,
|
||||
0.004386545014846762,
|
||||
0.003618598157228457,
|
||||
0.00228961368496085,
|
||||
0.00786472659491493,
|
||||
-0.0012563249231803012,
|
||||
-0.004214129458256975
|
||||
],
|
||||
"median_path": [
|
||||
0.0,
|
||||
-0.00320784825428555,
|
||||
-0.0013144449573886385,
|
||||
0.008097845528645342,
|
||||
-0.0022354177071969294,
|
||||
0.004777020911769236,
|
||||
-0.00041628424604922554,
|
||||
0.0038620429000082446,
|
||||
3.070070986921358e-05,
|
||||
0.017004791264934926,
|
||||
0.010046505609958789,
|
||||
0.021202662089879976,
|
||||
0.0054897881607161025,
|
||||
-0.006866153698911526,
|
||||
-0.0005434875610921264,
|
||||
-0.007454332581189882,
|
||||
-0.013922744791735608,
|
||||
-0.032715024846297514,
|
||||
-0.03563057164769323,
|
||||
-0.0206371165210939,
|
||||
-0.048597926984077056,
|
||||
-0.037920907084848965
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"completed_at_beijing": "2026-10-29T11:33:50+08:00",
|
||||
"protocol_sha256": "4bf71734005efa223946b796484b4914296ab363a29a4bad302cb102923bef1f",
|
||||
"raw_files": {
|
||||
"research/macro_timing_20261026/raw/export.json": "a1b6678eb5aa0578b0a5f679304bfb65ef4a965a1260c6bed8d45bb672c0d67c",
|
||||
"research/macro_timing_20261026/raw/csi2000.json": "45e62d8a5ba450a701c29774356dc47004ac07cf70275ebf601f0602d5a52122"
|
||||
},
|
||||
"code": {
|
||||
"engine.py": "d448f018fbf6286859161a0068f3c03d0416b53f840fee3d8478aaa6298053d6",
|
||||
"regions.py": "f074add1dda60c4b0d1d184b3094759c3a02abbc7e423a4f58383a1e06f54deb",
|
||||
"run.py": "d00db62465972abac17625ccc687e578f26ba1737aae789de276bf772c47332c"
|
||||
},
|
||||
"runtime": {
|
||||
"python": "3.12.14 (main, Aug 25 2026, 13:50:22) [Clang 22.1.3 ]",
|
||||
"numpy": "2.3.5",
|
||||
"pandas": "2.2.3"
|
||||
},
|
||||
"data_cutoff": "2026-10-23",
|
||||
"network": "socket audit disabled network",
|
||||
"online_changes": false
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
已生成8幅研究图与报告。
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"figures": {
|
||||
"三因素增量比较.png": "a7e03fa9dda75fac682c0492d07ef96e5342d4a112e14c6d093f597ac5cf8909",
|
||||
"上涨概率校准.png": "0a5f4c661cc853c1ddc603c34270813d4a264aae42c13af2b044268342e1cd2c",
|
||||
"单维价格与成交分层.png": "2b31dc4fc39a26dccae4ff73ae517bc112860610fb7d56703ed1e30b1cf266d3",
|
||||
"各风格事件路径.png": "cd48e8baac4a3665a59e631bad524e109575e58946157cbd663bcbe7ef1df36b",
|
||||
"三因素逐年表现.png": "b36ef3fc1163b54dc927a22fbb18c74eddb1d20366342b9657f384222b67c6e5",
|
||||
"当前区间快照.png": "f45ca3cfd00d7f9ddb88355f63974b111b19036f1d12f199969105a0907ccaea",
|
||||
"网格样本覆盖.png": "000b543fe43ac2a5135c0cfe3bfb3e9ee6442c202c917aae967dd4c9a79a3178",
|
||||
"区域同步与收益热力图.png": "e685b158a513e46a6eca949ecc98522a7d4518894ab3a2cd1b0a937dc289bc93"
|
||||
},
|
||||
"render_code": "0a76fcbd4ce1cdfde1f7d9cbc45861233d3ab13e861e46d30753759a779ca8e8"
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
全市场共识 2024 训练1193日 检验242日
|
||||
全市场共识 2025 训练1191日 检验243日
|
||||
全市场共识 2026 训练1191日 检验172日
|
||||
沪深300 2024 训练1193日 检验242日
|
||||
沪深300 2025 训练1191日 检验243日
|
||||
沪深300 2026 训练1191日 检验172日
|
||||
创业板 2024 训练1193日 检验242日
|
||||
创业板 2025 训练1191日 检验243日
|
||||
创业板 2026 训练1191日 检验172日
|
||||
中证2000 2024 训练1193日 检验242日
|
||||
中证2000 2025 训练1191日 检验243日
|
||||
中证2000 2026 训练1191日 检验172日
|
||||
中证500 2024 训练1193日 检验242日
|
||||
中证500 2025 训练1191日 检验243日
|
||||
中证500 2026 训练1191日 检验172日
|
||||
中证1000 2024 训练1193日 检验242日
|
||||
中证1000 2025 训练1191日 检验243日
|
||||
中证1000 2026 训练1191日 检验172日
|
||||
完成。结果与固定协议摘要已保存。
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
............
|
||||
----------------------------------------------------------------------
|
||||
Ran 12 tests in 0.570s
|
||||
|
||||
OK
|
||||
|
|
@ -0,0 +1,300 @@
|
|||
#!/usr/bin/env python3
|
||||
"""固定协议的离线主实验,结果写入本研究目录。"""
|
||||
from __future__ import annotations
|
||||
import gzip
|
||||
import json
|
||||
import sys
|
||||
from datetime import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from threadpoolctl import threadpool_limits
|
||||
from sklearn.linear_model import Ridge
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from engine import *
|
||||
from regions import region_table, select_regions, historical_region_stats, visits
|
||||
|
||||
OUT = HERE / 'results'
|
||||
|
||||
|
||||
def paths(d, dates, horizon=20):
|
||||
records = []
|
||||
for date in dates:
|
||||
t = d.index.get_loc(date)
|
||||
entry = d.open.iloc[t+1]
|
||||
points = np.r_[entry, d.close.iloc[t+1:t+horizon+1].to_numpy(), d.open.iloc[t+horizon+1]]
|
||||
peak = np.maximum.accumulate(points)
|
||||
records.append({'date': date, 'mdd': np.min(points/peak-1),
|
||||
'mae': min(d.low.iloc[t+1:t+horizon+1].min(), d.open.iloc[t+horizon+1])/entry-1,
|
||||
'mfe': max(d.high.iloc[t+1:t+horizon+1].max(), d.open.iloc[t+horizon+1])/entry-1})
|
||||
return pd.DataFrame(records).set_index('date') if records else pd.DataFrame()
|
||||
|
||||
|
||||
def run_target(s, f, lab, p, data):
|
||||
x = {name: design(f, name) for name in VARIANTS}
|
||||
records, folds = [], []
|
||||
for year in sorted(f.index.year.unique()):
|
||||
split = split_fold(f, lab, year, x['full'])
|
||||
if split is None:
|
||||
continue
|
||||
train, test = split
|
||||
if not len(test):
|
||||
continue
|
||||
print(f'{NAMES[s]} {year} 训练{len(train)}日 检验{len(test)}日', flush=True)
|
||||
fold = {'year': int(year), 'train_n': len(train), 'test_n': len(test),
|
||||
'train_first': train[0], 'train_last': train[-1], 'train_exit_max': lab.loc[train, 'exit_date'].max(),
|
||||
'test_first': test[0], 'test_last': test[-1], 'models': {}}
|
||||
thresholds = {'form_high': f.loc[train, 'price_form'].quantile(.8),
|
||||
'form_low': f.loc[train, 'price_form'].quantile(.2),
|
||||
'sync_high': f.loc[train, 'price_sync'].quantile(.8)}
|
||||
block = f.loc[test, ['cell', 'price_bin', 'volume_bin', 'price_rank', 'volume_rank',
|
||||
'price_sync', 'volume_sync', 'qrs_sync', 'price_form',
|
||||
'entry_direction', 'efficiency']].copy()
|
||||
block['year'] = year
|
||||
block['visit'] = visits(f.cell).loc[test]
|
||||
for c in ['return', 'scale', 'z', 'class', 'entry_date', 'exit_date']:
|
||||
block[c] = lab.loc[test, c]
|
||||
block['forming'] = (f.loc[test, 'price_form'] >= thresholds['form_high']) & (f.loc[test, 'price_form'] > 0)
|
||||
block['weak_forming'] = f.loc[test, 'price_form'] <= thresholds['form_low']
|
||||
block['static_high'] = (f.loc[test, 'price_sync'] >= thresholds['sync_high']) & (f.loc[test, 'price_form'] <= 0)
|
||||
sync_scale = StandardScaler().fit(f.loc[train, BASE])
|
||||
sync_model = Ridge(alpha=100).fit(sync_scale.transform(f.loc[train, BASE]), f.loc[train, 'price_sync'])
|
||||
block['residual_sync'] = block.price_sync - sync_model.predict(sync_scale.transform(f.loc[test, BASE]))
|
||||
fold['thresholds'] = thresholds
|
||||
for name in VARIANTS:
|
||||
proba, zhat, metadata = fit_models(x[name].loc[train], lab.loc[train, 'class'], lab.loc[train, 'z'], x[name].loc[test])
|
||||
for j in range(3):
|
||||
block[f'{name}_p{j}'] = proba[:, j]
|
||||
block[f'{name}_zhat'] = zhat
|
||||
block[f'{name}_brier'] = brier(proba, block['class'])
|
||||
block[f'{name}_mse'] = (zhat - block.z)**2
|
||||
fold['models'][name] = metadata
|
||||
folds.append(fold)
|
||||
records.append(block)
|
||||
if not records:
|
||||
return pd.DataFrame(), folds
|
||||
result = pd.concat(records).sort_index()
|
||||
result['residual_return'] = result['return'] - result.static_zhat*result.scale
|
||||
if s != 'market':
|
||||
result = result.join(paths(data[s], result.index))
|
||||
return result, folds
|
||||
|
||||
|
||||
def predictions_summary(d):
|
||||
out = {}
|
||||
for name in VARIANTS:
|
||||
prob = d[[f'{name}_p{i}' for i in range(3)]].to_numpy()
|
||||
cls = d['class'].to_numpy(int)
|
||||
calibration = []
|
||||
for label in [0, 1, 2]:
|
||||
for k in range(5):
|
||||
ix = (prob[:, label] >= k/5) & (prob[:, label] < (k+1)/5 if k < 4 else prob[:, label] <= 1)
|
||||
calibration.append({'class': label, 'bin': k, 'n': int(ix.sum()),
|
||||
'predicted': prob[ix, label].mean() if ix.any() else None,
|
||||
'observed': (cls[ix] == label).mean() if ix.any() else None})
|
||||
sig = np.where((prob[:, 2] >= .5) & (prob[:, 2]-prob[:, 0] >= .15), 1,
|
||||
np.where((prob[:, 0] >= .5) & (prob[:, 0]-prob[:, 2] >= .15), -1, 0))
|
||||
directional = []
|
||||
for direction in [-1, 1]:
|
||||
ix = sig == direction
|
||||
directional.append({'direction': direction, 'n': int(ix.sum()),
|
||||
'mean_return': d['return'].to_numpy()[ix].mean() if ix.any() else None,
|
||||
'median_return': np.median(d['return'].to_numpy()[ix]) if ix.any() else None,
|
||||
'class_accuracy': (cls[ix] == (2 if direction == 1 else 0)).mean() if ix.any() else None})
|
||||
out[name] = {'n': len(d), 'brier': d[f'{name}_brier'].mean(), 'mse': d[f'{name}_mse'].mean(),
|
||||
'accuracy': (prob.argmax(axis=1) == cls).mean(), 'coverage': (sig != 0).mean(),
|
||||
'calibration': calibration, 'directional': directional}
|
||||
return out
|
||||
|
||||
|
||||
def comparisons(d, s, p, calendar):
|
||||
results = []
|
||||
pairs = p['validation']['primary_comparisons']
|
||||
if d.empty:
|
||||
return [{'target': s, 'candidate': a, 'reference': b, 'n': 0, 'p': 1., 'status': '不足五完整训练年'} for a,b in pairs]
|
||||
span = calendar[(calendar >= d.index.min()) & (calendar <= d.index.max())]
|
||||
largest = d['return'].abs().idxmax()
|
||||
loc = calendar.get_loc(largest)
|
||||
keep = ~d.index.isin(calendar[max(0, loc-20):loc+21])
|
||||
for a,b in pairs:
|
||||
diff = d[f'{b}_brier'] - d[f'{a}_brier']
|
||||
iv = block_interval(diff, span, **{'repetitions': 2000, 'block': 60, 'seed': 20261029})
|
||||
by_year = {str(y): (g[f'{b}_brier'].mean()-g[f'{a}_brier'].mean())/g[f'{b}_brier'].mean()*100 for y,g in d.groupby('year')}
|
||||
results.append({'target': s, 'candidate': a, 'reference': b, 'n': len(d),
|
||||
'first': d.index[0], 'last': d.index[-1], **iv,
|
||||
'relative_improvement_pct': diff.mean()/d[f'{b}_brier'].mean()*100,
|
||||
'yearly_improvement_pct': by_year,
|
||||
'mse_improvement_pct': (1-d[f'{a}_mse'].mean()/d[f'{b}_mse'].mean())*100,
|
||||
'largest_event_date': largest, 'without_largest_41day_mean_difference': diff.loc[keep].mean(),
|
||||
'status': 'ok'})
|
||||
return results
|
||||
|
||||
|
||||
def grid_statistics(d, f, lab):
|
||||
records = []
|
||||
# 展示优先用真正的历史外推日期;无外推者显示可兑现描述。
|
||||
if d.empty:
|
||||
ix = f.index[f.cell.notna() & lab['return'].notna() & f.price_sync.notna()]
|
||||
show = f.loc[ix].copy().join(lab[['return', 'z', 'class']])
|
||||
show['visit'] = visits(f.cell).loc[ix]
|
||||
kind = '历史描述,无合格外推'
|
||||
else:
|
||||
show, kind = d, '历史滚动外推'
|
||||
for cell in range(25):
|
||||
g = show[show.cell == cell]
|
||||
row = {'cell': cell, 'price_bin': cell//5, 'volume_bin': cell%5, 'n': len(g),
|
||||
'visits': int(g.visit.sum()), 'scope': kind,
|
||||
'price_sync': g.price_sync.mean(), 'median_return': g['return'].median(),
|
||||
'mean_return': g['return'].mean(), 'up_rate': (g['class'] == 2).mean() if len(g) else None,
|
||||
'down_rate': (g['class'] == 0).mean() if len(g) else None,
|
||||
'display_supported': bool(len(g) >= 30 and g.visit.sum() >= 5)}
|
||||
if not d.empty:
|
||||
row['residual_sync'] = g.residual_sync.mean()
|
||||
if 'mdd' in g:
|
||||
row['median_mdd'] = g.mdd.median()
|
||||
row['median_mae'] = g.mae.median()
|
||||
for name in ['formation', 'direction', 'efficiency', 'full']:
|
||||
row[f'{name}_improvement'] = (g.static_brier-g[f'{name}_brier']).mean()
|
||||
for col in ['forming', 'weak_forming', 'static_high']:
|
||||
subset = g[g[col]]
|
||||
row[col] = {'n': len(subset), 'visits': int(subset.visit.sum()),
|
||||
'mean_return': subset['return'].mean(), 'median_return': subset['return'].median(),
|
||||
'mean_residual_return': subset.residual_return.mean()}
|
||||
for direction in [-1, 1]:
|
||||
subset = g[g.entry_direction == direction]
|
||||
row[f'entry_{direction}'] = {'n': len(subset), 'median_return': subset['return'].median()}
|
||||
records.append(row)
|
||||
univariate = {}
|
||||
for dim in ['price_rank', 'volume_rank']:
|
||||
decile = bins(show[dim], 10)
|
||||
univariate[dim] = [{'bin': k, 'n': int((decile == k).sum()),
|
||||
'sync': show.loc[decile == k, 'price_sync'].mean(),
|
||||
'mean_return': show.loc[decile == k, 'return'].mean(),
|
||||
'median_return': show.loc[decile == k, 'return'].median()} for k in range(10)]
|
||||
return records, univariate
|
||||
|
||||
|
||||
def current_prediction(s, f, lab, p, data):
|
||||
date = f.index[-1]
|
||||
split = split_fold(f, lab, date.year)
|
||||
if split is None or design(f, 'full').loc[date].isna().any():
|
||||
return {'date': date, 'status': '不足五完整训练年或当前特征缺失', 'name': NAMES[s]}
|
||||
train, _ = split
|
||||
row = f.loc[date]
|
||||
current = {'date': date, 'name': NAMES[s], 'status': 'ok', 'price_rank': row.price_rank,
|
||||
'volume_rank': row.volume_rank, 'price_bin': row.price_bin, 'volume_bin': row.volume_bin,
|
||||
'cell': row.cell, 'price_z': row.price_z, 'activity': row.activity,
|
||||
'sync': row.price_sync, 'formation': row.price_form, 'efficiency': row.efficiency,
|
||||
'entry_direction': row.entry_direction, 'dwell_days': row.dwell*20,
|
||||
'train_first': train[0], 'train_last': train[-1], 'train_exit_max': lab.loc[train,'exit_date'].max(),
|
||||
'models': {}}
|
||||
for name in ['static', 'full']:
|
||||
x = design(f, name)
|
||||
probabilities, r, _ = fit_models(x.loc[train], lab.loc[train, 'class'], lab.loc[train,'z'], x.loc[[date]])
|
||||
probs = probabilities[0]
|
||||
label = '偏多' if probs[2] >= .5 and probs[2]-probs[0] >= .15 else ('偏空' if probs[0] >= .5 and probs[0]-probs[2] >= .15 else '方向未集中')
|
||||
current['models'][name] = {'probabilities': probs, 'direction': label,
|
||||
'predicted_return': r[0]*row.sigma*np.sqrt(20), 'predicted_z': r[0]}
|
||||
if s != 'market':
|
||||
table = region_table(f, train)
|
||||
current['close'] = data[s].close.iloc[-1]
|
||||
current['anchor'], current['atr'] = row.anchor, row.atr
|
||||
current['regions'] = select_regions(row, current['close'], table)
|
||||
current['region_training'] = table
|
||||
current['historical_first_passage'] = historical_region_stats(f, data[s], train, table, row.cell, pd.Timestamp(f'{date.year}-01-01'))
|
||||
return current
|
||||
|
||||
|
||||
def supplementary(d, f, lab5, lab10, calendar):
|
||||
if d.empty:
|
||||
return {}
|
||||
prob = d[[f'full_p{i}' for i in range(3)]].to_numpy()
|
||||
sig = np.where((prob[:, 2] >= .5) & (prob[:, 2]-prob[:, 0] >= .15), 1,
|
||||
np.where((prob[:, 0] >= .5) & (prob[:, 0]-prob[:, 2] >= .15), -1, 0))
|
||||
chosen, last = [], -100
|
||||
for date, direction in zip(d.index, sig):
|
||||
t = calendar.get_loc(date)
|
||||
if direction and t-last >= 20:
|
||||
chosen.append((date, direction))
|
||||
last = t
|
||||
events = []
|
||||
for date, direction in chosen:
|
||||
item = {'date': date, 'direction': direction, 'return20': d.loc[date, 'return'],
|
||||
'return5': lab5.loc[date,'return'], 'return10': lab10.loc[date,'return']}
|
||||
for bp in [10, 30, 60]:
|
||||
item[f'directional_minus_{bp}bps'] = direction*item['return20'] - bp/10000
|
||||
events.append(item)
|
||||
return {'events': events, 'event_count': len(events),
|
||||
'note': '固定full概率门槛;间隔20交易日;指数方向幅度压力,不是账户交易收益。',
|
||||
'summary': {f'{bp}bps': {'mean': np.mean([e[f'directional_minus_{bp}bps'] for e in events]) if events else None,
|
||||
'median': np.median([e[f'directional_minus_{bp}bps'] for e in events]) if events else None} for bp in [10,30,60]}}
|
||||
|
||||
|
||||
def main():
|
||||
sys.addaudithook(no_network)
|
||||
p = protocol()
|
||||
OUT.mkdir(exist_ok=True)
|
||||
data, macro, quality = load_data(p)
|
||||
features = make_features(data, macro)
|
||||
lab = labels(data, features, 20)
|
||||
lab5, lab10 = labels(data, features, 5), labels(data, features, 10)
|
||||
calendar = data['000001.SH'].index
|
||||
results, comparisons_all = {}, []
|
||||
for s in p['primary_targets'] + p['secondary_targets']:
|
||||
f = features[s]
|
||||
pred, folds = run_target(s, f, lab[s], p, data)
|
||||
comps = comparisons(pred, s, p, calendar)
|
||||
comparisons_all += comps
|
||||
grid, univariate = grid_statistics(pred, f, lab[s])
|
||||
current = current_prediction(s, f, lab[s], p, data)
|
||||
results[s] = {'name': NAMES[s], 'n': len(pred), 'folds': folds,
|
||||
'metrics': predictions_summary(pred) if len(pred) else {},
|
||||
'grid': grid, 'univariate': univariate, 'current': current,
|
||||
'supplementary': supplementary(pred, f, lab5[s], lab10[s], calendar)}
|
||||
if len(pred):
|
||||
states = {}
|
||||
for state in ['forming', 'weak_forming', 'static_high']:
|
||||
g = pred[pred[state]]
|
||||
states[state] = {'n': len(g), 'visits': int(g.visit.sum()),
|
||||
'mean_return': g['return'].mean(), 'median_return': g['return'].median(),
|
||||
'mean_residual_return': g.residual_return.mean(),
|
||||
'up_rate': (g['class'] == 2).mean(), 'down_rate': (g['class'] == 0).mean()}
|
||||
results[s]['synchrony_states'] = states
|
||||
if len(pred):
|
||||
pred.to_json(OUT/f'predictions_{s}.json.gz', orient='table', date_format='iso', compression='gzip', double_precision=15)
|
||||
f.to_json(OUT/f'features_{s}.json.gz', orient='table', date_format='iso', compression='gzip', double_precision=15)
|
||||
if s == '932000.CSI':
|
||||
desc = []
|
||||
for period, mask in [('发布前回溯', f.index < pd.Timestamp(p['micro_publication'])), ('发布后', f.index >= pd.Timestamp(p['micro_publication']))]:
|
||||
valid = mask & f.cell.notna() & lab[s]['return'].notna()
|
||||
desc.append({'period': period, 'n': int(valid.sum()), 'first': f.index[valid][0], 'last': f.index[valid][-1],
|
||||
'mean_return': lab[s].loc[valid,'return'].mean(), 'median_return': lab[s].loc[valid,'return'].median()})
|
||||
results[s]['publication_slices'] = desc
|
||||
results[s]['training_contains_backcast'] = True
|
||||
dump(OUT/'partial_results.json', results)
|
||||
for targets, family in [(p['primary_targets'], '28项主检验'), (p['secondary_targets'], '21项扩展')]:
|
||||
subset = [r for r in comparisons_all if r['target'] in targets]
|
||||
corrected = holm([r['p'] for r in subset])
|
||||
for r, adj in zip(subset, corrected):
|
||||
r['p_holm'] = adj
|
||||
r['family'] = family
|
||||
r['accepted'] = bool(r.get('mean', -1) > 0 and r.get('ci95', [0])[0] is not None and r.get('ci95', [0])[0] > 0
|
||||
and adj < .05 and sum(v > 0 for v in r.get('yearly_improvement_pct', {}).values()) >= 2
|
||||
and len(r.get('yearly_improvement_pct', {})) >= 3)
|
||||
dump(OUT/'results.json', results)
|
||||
dump(OUT/'comparisons.json', comparisons_all)
|
||||
dump(OUT/'data_coverage.json', quality)
|
||||
manifest = {'completed_at_beijing': datetime.now(ZoneInfo('Asia/Shanghai')).isoformat(timespec='seconds'),
|
||||
'protocol_sha256': sha(HERE/'protocol.json'), 'raw_files': p['raw_files'],
|
||||
'code': {name: sha(HERE/name) for name in ['engine.py','regions.py','run.py']},
|
||||
'runtime': {'python': sys.version, 'numpy': np.__version__, 'pandas': pd.__version__},
|
||||
'data_cutoff': p['data_cutoff'], 'network': 'socket audit disabled network',
|
||||
'online_changes': False}
|
||||
dump(OUT/'manifest.json', manifest)
|
||||
(OUT/'partial_results.json').unlink(missing_ok=True)
|
||||
print('完成。结果与固定协议摘要已保存。', flush=True)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
with threadpool_limits(limits=1):
|
||||
main()
|
||||
|
|
@ -0,0 +1,99 @@
|
|||
"""经仓库单测护栏运行的合成数据测试。"""
|
||||
import unittest
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from engine import past_rank, bins, entry_path, own_features, labels, qrs, split_fold, holm, brier
|
||||
from regions import first_passage, response_path
|
||||
|
||||
|
||||
def synthetic(n=1000):
|
||||
rng = np.random.default_rng(19)
|
||||
close = 100*np.exp(np.cumsum(rng.normal(.0002, .01, n)))
|
||||
return pd.DataFrame({'open': close*.999, 'high': close*1.02, 'low': close*.98,
|
||||
'close': close, 'amount': rng.uniform(500, 1500, n), 'volume': 100},
|
||||
index=pd.bdate_range('2010-01-01', periods=n))
|
||||
|
||||
|
||||
class CalculationTests(unittest.TestCase):
|
||||
def test_past_rank_excludes_today_and_ties(self):
|
||||
s = pd.Series([1., 2., 3., 4., 4., 4.])
|
||||
self.assertEqual(past_rank(s, 3).iloc[3], 1)
|
||||
self.assertEqual(past_rank(s, 3).iloc[5], 2/3)
|
||||
|
||||
def test_grid_boundary(self):
|
||||
self.assertEqual(bins(pd.Series([0, .2, .8, 1.])).tolist(), [0, 1, 4, 4])
|
||||
|
||||
def test_future_edits_do_not_change_features(self):
|
||||
d = synthetic()
|
||||
original = own_features(d)
|
||||
modified = d.copy()
|
||||
modified.iloc[850:] *= 5
|
||||
altered = own_features(modified)
|
||||
pd.testing.assert_frame_equal(original.iloc[:850], altered.iloc[:850])
|
||||
|
||||
def test_truncation_is_identical(self):
|
||||
d = synthetic()
|
||||
pd.testing.assert_frame_equal(own_features(d).iloc[:850], own_features(d.iloc[:850]))
|
||||
|
||||
def test_missing_history_resets(self):
|
||||
d = synthetic()
|
||||
d.loc[d.index[700], ['open', 'high', 'low', 'close']] = np.nan
|
||||
self.assertTrue(qrs(d).iloc[700:].isna().all())
|
||||
self.assertTrue(own_features(d).price_rank.iloc[700:750].isna().all())
|
||||
|
||||
def test_efficiency_bounds_and_path(self):
|
||||
f = own_features(synthetic())
|
||||
self.assertTrue(f.efficiency.dropna().between(0, 1+1e-12).all())
|
||||
b = pd.Series([np.nan, 1, 1, 2, 2, 0, np.nan, 3, 3, 2])
|
||||
e = entry_path(b, pd.Series([1.]*10))
|
||||
self.assertEqual(e.entry_direction.iloc[3], 1)
|
||||
self.assertEqual(e.entry_bin_direction.iloc[5], -1)
|
||||
self.assertTrue(e.iloc[7:9].isna().all().all())
|
||||
|
||||
def test_forward_open_alignment(self):
|
||||
d = synthetic(100)
|
||||
data = {s: d for s in ['000300.SH', '000905.SH', '000852.SH']}
|
||||
f = {s: own_features(d) for s in data}
|
||||
f['market'] = f['000300.SH']
|
||||
l = labels(data, f, 20)
|
||||
self.assertAlmostEqual(l['000300.SH']['return'].iloc[40], d.open.iloc[61]/d.open.iloc[41]-1)
|
||||
self.assertEqual(l['000300.SH'].exit_date.iloc[40], d.index[61])
|
||||
self.assertTrue(l['000300.SH']['return'].iloc[-21:].isna().all())
|
||||
|
||||
def test_market_majority_not_mean_return(self):
|
||||
d = synthetic(100)
|
||||
data = {s: d.copy() for s in ['000300.SH', '000905.SH', '000852.SH']}
|
||||
for s in data:
|
||||
data[s].loc[d.index[61], 'open'] = data[s].open.iloc[41] * (1.1 if s != '000852.SH' else .5)
|
||||
data[s].loc[d.index[61], 'high'] = max(data[s].high.iloc[61], data[s].open.iloc[61])
|
||||
data[s].loc[d.index[61], 'low'] = min(data[s].low.iloc[61], data[s].open.iloc[61])
|
||||
f = {s: own_features(x) for s, x in data.items()}
|
||||
f['market'] = f['000300.SH']
|
||||
self.assertEqual(labels(data, f)['market']['class'].iloc[40], 2)
|
||||
|
||||
def test_split_removes_label_overlap(self):
|
||||
idx = pd.bdate_range('2018-01-01', '2024-12-31')
|
||||
f = pd.DataFrame({'x': 1.}, index=idx)
|
||||
l = pd.DataFrame({'class': 1., 'exit_date': pd.Series(idx, index=idx).shift(-21)}, index=idx)
|
||||
train, test = split_fold(f, l, 2024, all_features=f)
|
||||
self.assertTrue((l.loc[train, 'exit_date'] < idx[idx.year == 2024][0]).all())
|
||||
self.assertTrue((train.year >= 2019).all())
|
||||
|
||||
def test_first_passage_ignores_intraday_and_gaps(self):
|
||||
self.assertEqual(first_passage(100, np.array([101, 104, 99]), 96, 103), ('upper', 2))
|
||||
self.assertEqual(first_passage(104, np.array([100]), 96, 103), ('gap_upper', 0))
|
||||
self.assertEqual(first_passage(100, np.array([99, 101]), 96, 103), ('timeout', None))
|
||||
|
||||
def test_response_obeys_failure_order(self):
|
||||
z = {'low': 95, 'high': 97}
|
||||
self.assertEqual(response_path(np.array([96., 94., 98., 99., 99., 99.]), 2, z, 'lower'), 'failed')
|
||||
self.assertEqual(response_path(np.array([96., 97., 98., 97., 97., 97.]), 2, z, 'lower'), 'success')
|
||||
self.assertEqual(response_path(np.array([96., 98.]), 2, z, 'lower'), 'incomplete')
|
||||
|
||||
def test_brier_and_multiplicity(self):
|
||||
np.testing.assert_allclose(brier(np.eye(3), [0, 1, 2]), 0)
|
||||
np.testing.assert_allclose(holm([.01, .02, .5]), [.03, .04, .5])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
@ -0,0 +1,185 @@
|
|||
# 宏观价格成交网格与趋势增量实验
|
||||
|
||||
本轮以2026年10月23日为行情末日,检验同步形成、进入方向和价格推进效率能否改善20日趋势判断。协议于2026年10月29日北京时间11时10分固定并提交,全部计算使用开发机既有缓存。
|
||||
|
||||
主结果来自2024、2025、2026三个年度的历史滚动检验;每次只用此前五个完整年训练,并删除退出日期跨入检验年的标签。已有历史参与过假设形成,因此这属于历史外推研究。
|
||||
|
||||
**本轮结论:三项因素在当前网格交互模型中均未取得总体正增量,完整模型误差增加8.48%至13.05%。** 原有信息基线的误差也低于网格模型,因此下一版应优先检验更简约的网格表示,而不是继续增加指标。
|
||||
|
||||
值得保留的研究线索是同步形成与高同步停滞的状态差异,以及中证2000相对较强的低位修复倾向;两者目前都不能转成确定趋势信号。当前三个风格落在最低价格层,分位网格在尾部过粗,导致没有完整下方相邻格。这是区间设计需要改进的具体位置。
|
||||
|
||||
## 三项因素的增量价值
|
||||
|
||||
Brier分数是上涨、震荡、下跌三种概率的平方误差之和,越小越好。下表正值表示相较网格与当前同步模型的误差下降。四个目标各七项比较统一进行28项Holm校正。
|
||||
|
||||
| 判断对象 | 检验日数 | 同步形成 | 进入方向 | 推进效率 | 三项合用 |
|
||||
| --- | ---: | ---: | ---: | ---: | ---: |
|
||||
| 全市场共识 | 657 | -4.17% | -2.60% | -3.19% | -8.48% |
|
||||
| 沪深300 | 657 | -3.17% | -6.01% | -1.88% | -9.77% |
|
||||
| 创业板 | 657 | -9.77% | -2.47% | -2.70% | -13.05% |
|
||||
| 中证2000 | 657 | -5.31% | -4.55% | -3.62% | -9.60% |
|
||||
|
||||
达到预设综合标准的主比较有0项。综合标准要求改善为正、区块区间不跨零、校正后p小于0.05,并在至少三个年度中的两个年度改善。
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
| 对象 | 对比 | Brier绝对改善 | 95%区块区间 | 校正p | 收益预测MSE改善 |
|
||||
| --- | --- | ---: | --- | ---: | ---: |
|
||||
| 全市场共识 | 同步形成 相对 网格与当前同步 | -0.0304 | -0.0768 至 0.0091 | 1.000 | -7.48% |
|
||||
| 全市场共识 | 进入方向 相对 网格与当前同步 | -0.0190 | -0.0521 至 0.0200 | 1.000 | -4.21% |
|
||||
| 全市场共识 | 推进效率 相对 网格与当前同步 | -0.0233 | -0.0454 至 0.0007 | 0.784 | -5.22% |
|
||||
| 全市场共识 | 三项合用 相对 网格与当前同步 | -0.0619 | -0.1053 至 -0.0200 | 0.092 | -10.37% |
|
||||
| 全市场共识 | 三项合用 相对 移除同步形成 | -0.0217 | -0.0613 至 0.0108 | 1.000 | -1.93% |
|
||||
| 全市场共识 | 三项合用 相对 移除进入方向 | -0.0183 | -0.0495 至 0.0184 | 1.000 | +0.75% |
|
||||
| 全市场共识 | 三项合用 相对 移除推进效率 | -0.0166 | -0.0363 至 0.0036 | 1.000 | -1.75% |
|
||||
| 沪深300 | 同步形成 相对 网格与当前同步 | -0.0232 | -0.0674 至 0.0226 | 1.000 | -3.85% |
|
||||
| 沪深300 | 进入方向 相对 网格与当前同步 | -0.0440 | -0.0712 至 -0.0172 | 0.084 | -6.52% |
|
||||
| 沪深300 | 推进效率 相对 网格与当前同步 | -0.0137 | -0.0359 至 0.0082 | 1.000 | -2.86% |
|
||||
| 沪深300 | 三项合用 相对 网格与当前同步 | -0.0715 | -0.1298 至 -0.0184 | 0.231 | -8.53% |
|
||||
| 沪深300 | 三项合用 相对 移除同步形成 | -0.0180 | -0.0540 至 0.0188 | 1.000 | -0.59% |
|
||||
| 沪深300 | 三项合用 相对 移除进入方向 | -0.0337 | -0.0535 至 -0.0138 | 0.014 | -5.30% |
|
||||
| 沪深300 | 三项合用 相对 移除推进效率 | -0.0101 | -0.0252 至 0.0032 | 1.000 | +0.79% |
|
||||
| 创业板 | 同步形成 相对 网格与当前同步 | -0.0718 | -0.0970 至 -0.0447 | 0.014 | -23.70% |
|
||||
| 创业板 | 进入方向 相对 网格与当前同步 | -0.0182 | -0.0499 至 0.0136 | 1.000 | -9.81% |
|
||||
| 创业板 | 推进效率 相对 网格与当前同步 | -0.0199 | -0.0394 至 -0.0004 | 0.782 | -199.33% |
|
||||
| 创业板 | 三项合用 相对 网格与当前同步 | -0.0959 | -0.1316 至 -0.0620 | 0.014 | -100.70% |
|
||||
| 创业板 | 三项合用 相对 移除同步形成 | -0.0579 | -0.0832 至 -0.0303 | 0.014 | +7.97% |
|
||||
| 创业板 | 三项合用 相对 移除进入方向 | -0.0081 | -0.0332 至 0.0191 | 1.000 | +9.25% |
|
||||
| 创业板 | 三项合用 相对 移除推进效率 | -0.0194 | -0.0409 至 0.0004 | 0.997 | -51.51% |
|
||||
| 中证2000 | 同步形成 相对 网格与当前同步 | -0.0391 | -0.0804 至 0.0027 | 0.997 | -12.20% |
|
||||
| 中证2000 | 进入方向 相对 网格与当前同步 | -0.0335 | -0.0643 至 -0.0057 | 0.522 | -7.01% |
|
||||
| 中证2000 | 推进效率 相对 网格与当前同步 | -0.0267 | -0.0519 至 -0.0029 | 0.585 | -0.58% |
|
||||
| 中证2000 | 三项合用 相对 网格与当前同步 | -0.0708 | -0.1170 至 -0.0201 | 0.143 | -14.00% |
|
||||
| 中证2000 | 三项合用 相对 移除同步形成 | -0.0128 | -0.0519 至 0.0235 | 1.000 | -8.20% |
|
||||
| 中证2000 | 三项合用 相对 移除进入方向 | -0.0229 | -0.0421 至 -0.0041 | 0.360 | -3.43% |
|
||||
| 中证2000 | 三项合用 相对 移除推进效率 | -0.0093 | -0.0263 至 0.0073 | 1.000 | +0.57% |
|
||||
|
||||
区块区间对已固定的外推预测误差按60交易日抽样2000次,保留时间相关性;不把每一天或每一网格当成独立试验,也不涵盖全部训练重估不确定性。
|
||||
|
||||
## 各风格概率表现与覆盖
|
||||
|
||||
| 对象 | 当前同步模型Brier | 完整模型Brier | 完整模型准确率 | 方向判断覆盖率 |
|
||||
| --- | ---: | ---: | ---: | ---: |
|
||||
| 全市场共识 | 0.7295 | 0.7914 | 36.2% | 21.3% |
|
||||
| 沪深300 | 0.7311 | 0.8026 | 35.2% | 41.7% |
|
||||
| 创业板 | 0.7352 | 0.8311 | 32.7% | 39.4% |
|
||||
| 中证2000 | 0.7374 | 0.8082 | 30.6% | 26.5% |
|
||||
|
||||
| 对象 | 原有信息基线 | 加网格 | 加当前同步 | 再加三项因素 |
|
||||
| --- | ---: | ---: | ---: | ---: |
|
||||
| 全市场共识 | 0.6939 | 0.7195 | 0.7295 | 0.7914 |
|
||||
| 沪深300 | 0.6548 | 0.6994 | 0.7311 | 0.8026 |
|
||||
| 创业板 | 0.6502 | 0.6891 | 0.7352 | 0.8311 |
|
||||
| 中证2000 | 0.6511 | 0.6854 | 0.7374 | 0.8082 |
|
||||
|
||||
上表各列为同一日期集合的Brier,逐步扩展使用信息。它说明整套网格相较原有信息的变化;主检验的三因素增量仍以当前同步模型为对照。
|
||||
|
||||
全市场标签要求沪深300、中证500、中证1000中至少两个出现同方向、超过各自半个预测期波动尺度的变化;它与三指数平均收益的正负并不等同。风格标签分别计算,创业板与宽基的重叠不增加全市场票数。
|
||||
|
||||
中证2000的历史外推日期均在发布后,五年训练包含发布前回溯数据;它也不等同PMS微盘股票篮子。科创50因自身QRS预热后不足五个完整训练年,不生成同等级外推预测,描述结果保留在机器结果中。
|
||||
|
||||
## 同步形成与同步高位的状态差异
|
||||
|
||||
| 风格 | 状态 | 日期数 | 20日均值/中位数 | 相较静态模型预测的平均收益差 |
|
||||
| --- | --- | ---: | --- | ---: |
|
||||
| 沪深300 | 同步形成较强 | 134 | 1.37%/1.80% | 1.41% |
|
||||
| 沪深300 | 同步较高且不再增强 | 21 | -1.80%/-3.77% | -1.51% |
|
||||
| 沪深300 | 同步形成较弱 | 129 | 0.83%/0.47% | 0.22% |
|
||||
| 创业板 | 同步形成较强 | 134 | 3.40%/2.51% | 6.12% |
|
||||
| 创业板 | 同步较高且不再增强 | 21 | -5.84%/-9.08% | -3.02% |
|
||||
| 创业板 | 同步形成较弱 | 129 | 2.56%/0.57% | 1.04% |
|
||||
| 中证2000 | 同步形成较强 | 134 | 2.95%/1.70% | 4.69% |
|
||||
| 中证2000 | 同步较高且不再增强 | 21 | -5.96%/-12.87% | -8.45% |
|
||||
| 中证2000 | 同步形成较弱 | 129 | 3.13%/3.46% | 0.84% |
|
||||
|
||||
强弱分位阈值只在对应训练期估计。这张表展示状态差异,不控制三组网格构成完全相同;三因素的正式增量结论以同日期模型比较为准。
|
||||
|
||||
## 价格与成交分层
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
二维网格按各自此前252日价格位置与成交活跃度分成五层。价格位置以此前60日均价和20日ATR归一;活跃度使用含当日5日平均成交额除以此前60日成交额中位数。同步残差由每个训练期的基线特征岭回归去除动量、波动、QRS、普通涨跌广度和股汇背景。图中灰格仍保留探索读数,不能据其颜色宣称单格显著。
|
||||
|
||||
## 截至数据末日的区间判断
|
||||
|
||||
以下是2026年10月23日收盘快照,不代表10月29日实时判断。概率来自事先固定的完整模型,并与当前同步基线并列;未根据本轮结果更换当前模型。价格百分位是相对均价的ATR偏离位置在过去一年的分位,不是指数绝对点位的历史分位。
|
||||
|
||||
完整模型的概率校准表现不佳,下列数字只作为原始模型分数,不能直接解释成可信胜率。三指数平均价格相关性为0.925,较此前非重叠60日上升0.022,尚未达到训练期强同步形成门槛0.075。三个风格的成交活跃度分别为常态参照的82.4%、79.7%、86.4%;从价格位置、量能和模型分歧判断,目前更像共同下跌后的分化修复,未形成全市场一致的可确认方向。
|
||||
|
||||
| 对象 | 收盘点位 | 均价偏离百分位 | 成交百分位 | 5日推进效率 | 原始模型下跌/震荡/上涨 | 模型倾向 |
|
||||
| --- | ---: | ---: | ---: | ---: | --- | --- |
|
||||
| 全市场共识 | — | 2.8% | 24.2% | 17.5% | 26.7%/34.7%/38.6% | 方向未集中 |
|
||||
| 沪深300 | 4105.33 | 1.6% | 31.7% | 30.9% | 48.3%/25.3%/26.4% | 方向未集中 |
|
||||
| 创业板 | 2684.10 | 2.4% | 29.4% | 29.2% | 43.3%/49.1%/7.7% | 方向未集中 |
|
||||
| 中证2000 | 2856.27 | 10.7% | 20.6% | 6.0% | 32.7%/16.6%/50.7% | 偏多 |
|
||||
|
||||

|
||||
|
||||
| 风格 | 当前所在价格层 | 层下沿相距现价 | 上方同步区域或参考 |
|
||||
| --- | --- | ---: | --- |
|
||||
| 沪深300 | 4016至4370 | -2.17% | 4576至4661(同步增强候选) |
|
||||
| 创业板 | 2553至3128 | -4.88% | 3128至3324(几何参考) |
|
||||
| 中证2000 | 2521至2944 | -11.74% | 2944至3042(同步增强候选) |
|
||||
|
||||
当前层下沿4016、2553、2521点分别是三个风格的历史分位投影边界,可用于观察是否继续跌出已有状态范围;本轮没有证明它们会形成支撑。由于三个风格都在最下层,其下方没有完整相邻格,双侧首次到达统计的有效分母为零。三个风格同格类比原有74、84、52个日期,均因缺少完整下方区域未进入双侧比较。
|
||||
|
||||
区间由训练期同成交层的同步残差、样本覆盖筛选,再按快照日均价和ATR投影。没有合格同步增强区域时,只显示相邻几何参考;超出历史有限边界时不外推一个目标。首次到达与触区响应的逐项状态保留在完整结果中。
|
||||
|
||||
| 风格 | 当前同步基线下跌/震荡/上涨 |
|
||||
| --- | --- |
|
||||
| 沪深300 | 57.5%/19.2%/23.3% |
|
||||
| 创业板 | 45.4%/45.0%/9.6% |
|
||||
| 中证2000 | 27.4%/20.8%/51.8% |
|
||||
|
||||
响应定义为触区后五个完整收盘内,在先穿透外侧半个ATR之前反向移动一个ATR;ATR与区间在信号日冻结。开盘越界、触区即穿透和观察不完整分别记账。
|
||||
|
||||
## 概率校准
|
||||
|
||||

|
||||
|
||||
| 风格 | 当前上涨原始分数 | 所在概率组历史日数 | 该组平均预测 | 实际上涨达标比例 |
|
||||
| --- | ---: | ---: | ---: | ---: |
|
||||
| 沪深300 | 26.4% | 232 | 28.9% | 42.2% |
|
||||
| 创业板 | 7.7% | 254 | 9.0% | 33.5% |
|
||||
| 中证2000 | 50.7% | 153 | 48.2% | 30.7% |
|
||||
|
||||
此表是历史外推概率组的诊断,不是对当前样本另做概率重标定。它解释为什么当前中证2000的50.7%原始上涨分数只能描述模型倾向。
|
||||
|
||||
## 方向事件路径与费用压力
|
||||
|
||||

|
||||
|
||||
| 对象 | 事件数 | 10基点方向幅度均值/中位数 | 30基点 | 60基点 |
|
||||
| --- | ---: | --- | --- | --- |
|
||||
| 全市场共识 | 22 | -1.36%/-0.67% | -1.56%/-0.87% | -1.86%/-1.17% |
|
||||
| 沪深300 | 27 | -1.25%/-0.06% | -1.45%/-0.26% | -1.75%/-0.56% |
|
||||
| 创业板 | 27 | 0.27%/-0.67% | 0.07%/-0.87% | -0.23%/-1.17% |
|
||||
| 中证2000 | 24 | 0.72%/2.48% | 0.52%/2.28% | 0.22%/1.98% |
|
||||
|
||||
事件按固定完整模型概率至少50%、比反方向高15个百分点触发,并间隔至少20交易日。偏空只检验价格下跌方向的幅度,不假定股票可卖空。指数开盘标签与统一摩擦不构成账户净收益,也不代表PMS现有09:35调度可按开盘成交。
|
||||
|
||||
## 下一轮值得优先检验的改动
|
||||
|
||||
第一,先去掉逐格交互,分别比较单维网格、二维平滑网格和全局三因素。当前完整模型有456列输入,每个训练窗约1190个重叠日标签;网格扩展已经降低外推表现。模型复杂度是否掩盖了原始因素的价值,需要另立对照验证。
|
||||
|
||||
第二,保留同步形成与高位停滞的区别,但按行情段评估。高同步且不再增强只有21个日期,未来收益窗口相连后合为5段;不能用21个日期当21次独立发现。其明显较差的路径更适合进一步检验为趋势衰减条件。
|
||||
|
||||
第三,在最低和最高价格层内增加事先固定的ATR细格。当前三个风格均处最下层,分位网格将不同深度的下跌压在同一格,无法形成完整的下方候选区域。补足尾部结构后,再检验支撑反应与反转确认。
|
||||
|
||||
以上是结果后的研究建议,本轮没有重跑新参数或覆盖原结果。
|
||||
|
||||
## 可复核资料
|
||||
|
||||
- [固定协议](protocol.json)与[摘要](protocol.sha256)。
|
||||
- [完整统计](results/results.json)、[逐项比较](results/comparisons.json)、[数据覆盖](results/data_coverage.json)。
|
||||
- [独立复核](results/audit.json)与[运行摘要](results/manifest.json)。
|
||||
- 逐日特征和预测保存在results目录的压缩JSON中;源行情沿用既有本地缓存,协议记录其摘要。
|
||||
- 研究代码为engine.py、regions.py、run.py、render.py、audit.py及test_engine.py。
|
||||
|
||||
所有结果在开发机计算。线上应用、参数、持仓、数据库和容器均未改动;本研究代码不进入业务镜像,无受影响业务容器。
|
||||