7 Sprints 全部完成: Sprint 0: 基础设施 (DataManager + MariaDB) Sprint 1: 因子引擎 (34因子/12分类) Sprint 2: VectorBT 回测 (5策略+截面) Sprint 3: Optuna 优化 (+Walk-Forward) Sprint 4: ML 模型 (LightGBM+CatBoost) Sprint 5: Qwen 情绪因子 (三源新闻+日期对齐) Sprint 6: Agent 系统 (4Agent+日报.md/.html) 生产加固 (15项): Tushare双源fallback, SSH自动恢复, pool_pre_ping, save_daily先删后插, load_dotenv绝对路径, 日报5d/20d修复, RiskAgent改上证指数, 昨日对比+数据截止, mac_report utf8mb4, CLAUDE-*.md 9条已知Bug, demo全参数化, djapi数据源归一化, indexDatas API修正 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
127 lines
4.1 KiB
Python
127 lines
4.1 KiB
Python
"""
|
|
LightGBM 模型封装。
|
|
"""
|
|
|
|
import numpy as np
|
|
import pandas as pd
|
|
import lightgbm as lgb
|
|
|
|
from models.base import BaseModel
|
|
from sklearn.model_selection import TimeSeriesSplit
|
|
|
|
_DEFAULT_PARAMS = {
|
|
"objective": "regression",
|
|
"metric": "rmse",
|
|
"boosting_type": "gbdt",
|
|
"num_leaves": 15,
|
|
"learning_rate": 0.03,
|
|
"feature_fraction": 0.7,
|
|
"bagging_fraction": 0.7,
|
|
"bagging_freq": 5,
|
|
"verbose": -1,
|
|
"n_estimators": 1000,
|
|
"random_state": 42,
|
|
"min_data_in_leaf": 20,
|
|
}
|
|
|
|
|
|
class LightGBMModel(BaseModel):
|
|
"""LightGBM 回归模型。"""
|
|
|
|
name = "lightgbm"
|
|
|
|
def __init__(
|
|
self,
|
|
params: dict | None = None,
|
|
early_stopping: int = 50,
|
|
eval_ratio: float = 0.2,
|
|
random_seed: int = 42,
|
|
):
|
|
self.params = params or _DEFAULT_PARAMS.copy()
|
|
self.params["random_state"] = random_seed
|
|
self.early_stopping = early_stopping
|
|
self.eval_ratio = eval_ratio
|
|
self._model: lgb.Booster | None = None
|
|
self._feature_names: list[str] = []
|
|
|
|
def fit(self, X: pd.DataFrame, y: pd.Series) -> "LightGBMModel":
|
|
self._feature_names = list(X.columns)
|
|
|
|
n = len(X)
|
|
val_size = int(n * self.eval_ratio)
|
|
|
|
callbacks = []
|
|
eval_set = None
|
|
X_train, y_train = X, y
|
|
|
|
# 验证集足够大时才启用早停(至少 50 条)
|
|
if val_size >= 50:
|
|
split_idx = n - val_size
|
|
X_train, X_val = X.iloc[:split_idx], X.iloc[split_idx:]
|
|
y_train, y_val = y.iloc[:split_idx], y.iloc[split_idx:]
|
|
eval_set = [(X_val, y_val)]
|
|
callbacks = [
|
|
lgb.early_stopping(stopping_rounds=self.early_stopping, verbose=False),
|
|
lgb.log_evaluation(0),
|
|
]
|
|
|
|
self._model = lgb.LGBMRegressor(**self.params)
|
|
self._model.fit(
|
|
X_train, y_train,
|
|
eval_set=eval_set,
|
|
callbacks=callbacks if callbacks else None,
|
|
)
|
|
return self
|
|
|
|
def predict(self, X: pd.DataFrame) -> pd.Series:
|
|
if self._model is None:
|
|
raise RuntimeError("模型尚未训练")
|
|
preds = self._model.predict(X[self._feature_names])
|
|
return pd.Series(preds, index=X.index, name="pred")
|
|
|
|
def get_feature_importance(self, importance_type: str = "gain") -> pd.DataFrame:
|
|
"""特征重要性。importance_type: 'gain' | 'split'"""
|
|
if self._model is None:
|
|
return pd.DataFrame()
|
|
imp = self._model.booster_.feature_importance(importance_type=importance_type)
|
|
names = self._model.booster_.feature_name()
|
|
df = pd.DataFrame({"feature": names, "importance": imp})
|
|
df["importance_pct"] = df["importance"] / df["importance"].sum() * 100
|
|
return df.sort_values("importance", ascending=False)
|
|
|
|
def cv_evaluate(
|
|
self, X: pd.DataFrame, y: pd.Series, n_folds: int = 5
|
|
) -> pd.DataFrame:
|
|
"""
|
|
时间序列交叉验证评估(不 shuffle)。
|
|
|
|
返回每折的 IC (相关系数) 和 MSE。
|
|
"""
|
|
tscv = TimeSeriesSplit(n_splits=n_folds)
|
|
results = []
|
|
for fold, (train_idx, test_idx) in enumerate(tscv.split(X)):
|
|
X_train, X_test = X.iloc[train_idx], X.iloc[test_idx]
|
|
y_train, y_test = y.iloc[train_idx], y.iloc[test_idx]
|
|
|
|
model = LightGBMModel(
|
|
params=self.params,
|
|
early_stopping=self.early_stopping,
|
|
eval_ratio=0.0, # 不使用内部验证,直接全量训练
|
|
)
|
|
model.fit(X_train, y_train)
|
|
preds = model.predict(X_test)
|
|
ic = preds.corr(y_test)
|
|
mse = ((preds - y_test) ** 2).mean()
|
|
results.append({"fold": fold, "ic": round(ic, 4), "mse": round(mse, 4)})
|
|
|
|
df = pd.DataFrame(results)
|
|
df.loc["mean"] = df.mean()
|
|
return df
|
|
|
|
@property
|
|
def n_estimators_used(self) -> int | None:
|
|
"""实际使用的树数量(早停后可能 < n_estimators)。"""
|
|
if self._model is None:
|
|
return None
|
|
return self._model.booster_.current_iteration()
|