tradingSystem/research/macro_grid_20261029/audit.py

183 lines
10 KiB
Python

#!/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()