tradingSystem/research/signal_independence_20261111/diagnostics.py

41 lines
2.5 KiB
Python
Raw Normal View History

2026-11-11 15:10:55 +08:00
"""记录原值复现误差、QRS净化后的支持量及同窗事件的有效样本数。"""
import gzip,json
import numpy as np
import pandas as pd
import analyze as a
import weights as w
f=pd.read_csv(a.DATA/'normalized.csv.gz');f.t=pd.to_datetime(f.t)
bars=pd.DataFrame([json.loads(x) for x in gzip.open(a.DATA/'daily.jsonl.gz','rt')]);bars['trade_date']=pd.to_datetime(bars.timestamp).dt.date
bars['code']=bars.symbol.map(a.code)
for col in ['high','low','close']:bars[col]=pd.to_numeric(bars[col])
bycode={c:g for c,g in bars.groupby('code')};calc=w.DailyQRSComputer(None,n_regression=18,m_lookback=60,threshold=.7)
cache={};errs=[]
for r in f[f.source=='qrs'].itertuples():
key=(r.code,r.evidence_date)
if key not in cache:cache[key]=calc._compute_from_dataframe(r.code,bycode[r.code],pd.Timestamp(r.evidence_date).date()).qrs_value
errs.append(abs(cache[key]-r.value))
result={'historical_qrs_reproduction':{'rows':len(errs),'within_1e_6':int(sum(x<1e-6 for x in errs)),
'error_quantiles':np.quantile(errs,[0,.5,.95,1]).tolist(),
'note':'原始历史日线可能有修订、请求暖机范围差异;未将重算伪称当时实发。'},'event_pairs':[]}
for aa,bb in [('intensity','distribution'),('mtf','bionic'),('distribution','bionic')]:
for window in [15,30,60]:
matched=[];seen=set();by_b={k:g for k,g in f[f.source==bb].groupby(['day','code'])}
for key,g in f[f.source==aa].groupby(['day','code']):
q=by_b.get(key)
if q is None:continue
qt=[(a.trade_minute(r.t),r) for r in q.itertuples()];qt=[x for x in qt if x[0] is not None]
if not qt:continue
for row in g.itertuples():
now=a.trade_minute(row.t)
if now is None:continue
lag,other=min(((z-now,r) for z,r in qt),key=lambda x:abs(x[0]))
if abs(lag)<=window:
matched.append((key[0],key[1],row.direction,other.direction,other.id));seen.add(other.id)
p=pd.DataFrame(matched,columns=['day','code','x','y','b_id'])
result['event_pairs'].append({'a':aa,'b':bb,'minutes':window,'matched_a_events':len(p),'unique_b_events':len(seen),
'stock_days':len(p[['day','code']].drop_duplicates()),'dates':p.day.nunique(),
'agree':float((p.x==p.y).mean()),'note':'最近匹配可能多对一,事件数不作独立样本数'})
a.OUT.joinpath('diagnostics.json').write_text(json.dumps(result,ensure_ascii=False,indent=2))
print(json.dumps(result,ensure_ascii=False,indent=2))