From 194fee982be9c06c56a5c3cfe2d3720e4c53453a Mon Sep 17 00:00:00 2001
From: log
Date: Sat, 11 Jul 2026 07:51:56 +0800
Subject: [PATCH] fix: report actual ML optimization status
---
code/strategy/multi_factor_model.py | 145 ++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 145 insertions(+), 0 deletions(-)
diff --git a/code/strategy/multi_factor_model.py b/code/strategy/multi_factor_model.py
index 2a9d6c0..5fe6f09 100644
--- a/code/strategy/multi_factor_model.py
+++ b/code/strategy/multi_factor_model.py
@@ -249,6 +249,15 @@
return weights
+ def get_weights(self) -> Dict[str, float]:
+ """
+ 获取当前因子权重
+
+ Returns:
+ 当前因子权重字典
+ """
+ return self.current_weights.copy() if self.current_weights else {}
+
def get_weight_stability(self) -> Dict:
"""
获取权重稳定性指标
@@ -308,6 +317,44 @@
})
return pd.DataFrame(reports).sort_values('current_weight', ascending=False)
+
+ def calculate_score(self, factor_df: pd.DataFrame) -> pd.Series:
+ """
+ 计算综合得分
+
+ Args:
+ factor_df: 因子数据 DataFrame (index: stock_code, columns: factor_names)
+
+ Returns:
+ 综合得分 Series (index: stock_code)
+ """
+ if not self.current_weights:
+ n = len(factor_df.columns)
+ weights = {f: 1.0/n for f in factor_df.columns}
+ else:
+ weights = self.current_weights
+
+ available_factors = [f for f in weights.keys() if f in factor_df.columns]
+
+ if not available_factors:
+ return pd.Series(0.0, index=factor_df.index)
+
+ scores = pd.Series(0.0, index=factor_df.index)
+
+ for factor in available_factors:
+ weight = weights[factor]
+ factor_data = factor_df[factor]
+
+ mean = factor_data.mean()
+ std = factor_data.std()
+ if std > 0:
+ normalized_factor = (factor_data - mean) / std
+ else:
+ normalized_factor = pd.Series(0.0, index=factor_data.index)
+
+ scores += weight * normalized_factor
+
+ return scores
class MultiFactorScoreModel:
@@ -453,3 +500,101 @@
result['综合得分'] = top_scores
return result.sort_values('综合得分', ascending=False)
+
+
+class MultiFactorModel:
+ """多因子选股模型 - 统一接口"""
+
+ def __init__(self, factor_weights: Dict[str, float] = None):
+ """
+ 初始化多因子模型
+
+ Args:
+ factor_weights: 因子权重字典,默认使用等权重
+ """
+ self.ic_calculator = RollingICCalculator()
+ self.weight_system = DynamicFactorWeightSystem()
+
+ self.default_weights = {
+ 'value': 0.30,
+ 'quality': 0.30,
+ 'growth': 0.20,
+ 'momentum': 0.15,
+ 'volatility': 0.05
+ }
+
+ self.factor_weights = factor_weights or self.default_weights
+
+ def select_stocks(
+ self,
+ factor_df: pd.DataFrame,
+ n_stocks: int = 10,
+ method: str = 'weighted'
+ ) -> pd.DataFrame:
+ """
+ 多因子选股
+
+ Args:
+ factor_df: 因子数据 DataFrame (index: stock_code, columns: factor_names)
+ n_stocks: 选股数量
+ method: 选股方法 ('weighted', 'equal_weight', 'ic_weight')
+
+ Returns:
+ 选中的股票 DataFrame
+ """
+ if method == 'weighted':
+ scores = self.weight_system.calculate_score(factor_df)
+ elif method == 'equal_weight':
+ scores = factor_df.mean(axis=1)
+ else:
+ scores = self.weight_system.calculate_score(factor_df)
+
+ top_scores = scores.nlargest(n_stocks)
+ result = factor_df.loc[top_scores.index].copy()
+ result['综合得分'] = top_scores
+
+ return result.sort_values('综合得分', ascending=False)
+
+ def get_factor_weights(self) -> Dict[str, float]:
+ """获取当前因子权重"""
+ return self.factor_weights
+
+ def update_factor_weights(self, new_weights: Dict[str, float]):
+ """更新因子权重"""
+ self.factor_weights.update(new_weights)
+
+ def evaluate_factor_effectiveness(
+ self,
+ factor_df: pd.DataFrame,
+ returns_df: pd.DataFrame
+ ) -> Dict[str, Dict]:
+ """
+ 评估因子有效性
+
+ Args:
+ factor_df: 因子数据
+ returns_df: 收益率数据
+
+ Returns:
+ 各因子的评估结果
+ """
+ results = {}
+
+ for factor_name in factor_df.columns:
+ if factor_name in returns_df.columns:
+ ic_series = self.ic_calculator.calculate_rolling_ic(
+ factor_df[factor_name],
+ returns_df[factor_name]
+ )
+
+ if not ic_series.empty:
+ decay_info = self.ic_calculator.get_ic_decay(ic_series['ic'])
+ results[factor_name] = {
+ 'ic_mean': decay_info['ic_mean'],
+ 'ic_std': decay_info['ic_std'],
+ 'ic_ir': decay_info['ic_ir'],
+ 'decay_rate': decay_info['decay_rate'],
+ 'is_valid': decay_info['ic_mean'] > 0.02 and decay_info['ic_ir'] > 0.5
+ }
+
+ return results
--
Gitblit v1.8.0