301 lines
17 KiB
Python
301 lines
17 KiB
Python
#!/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()
|