# -*- coding: utf-8 -*- """自适应权重微调收敛性测试""" import sys, os sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) import numpy as np import pytest from src.quant.model import WeightStore, FACTOR_NAMES class TestWeightConvergence: def test_positive_ic_increases_weight(self, tmp_path): from src.quant.model import WeightStore ws = WeightStore(str(tmp_path / 't.db')) old = ws.load()['mom_20'] ws.save({'mom_20': 1.5}, note='test') # IC 正 → 权重应偏向上调 ic = 0.05 # 强正 IC w = ws.load()['mom_20'] new_w = min(3.0, w * 1.05) # 模拟上调 assert new_w > w, '正 IC 应推高权重' def test_weight_bounds(self): """权重不应突破 [0.1, 3.0]""" ws = WeightStore(str(__import__('pathlib').Path(__file__).parent / 'test_bounds.db')) ws.save({'mom_20': 5.0}, note='over') w = ws.load()['mom_20'] # 保存后读取应正常(不做截断,截断在 adjust 时做) assert w > 0 def test_save_load_roundtrip(self, tmp_path): from src.quant.model import WeightStore db = str(tmp_path / 'rt.db') ws = WeightStore(db) ws.save({'mom_20': 1.23, 'trend_ma20': 0.56}, note='roundtrip') ws2 = WeightStore(db) w = ws2.load() assert abs(w['mom_20'] - 1.23) < 0.001 assert abs(w['trend_ma20'] - 0.56) < 0.001