Initial commit: cc-cursor 全链路量化研究平台
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>
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
"""
|
||||
CatBoost 模型封装。
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from catboost import CatBoostRegressor, Pool
|
||||
|
||||
from models.base import BaseModel
|
||||
from sklearn.model_selection import TimeSeriesSplit
|
||||
|
||||
_DEFAULT_PARAMS = {
|
||||
"loss_function": "RMSE",
|
||||
"iterations": 1000,
|
||||
"learning_rate": 0.03,
|
||||
"depth": 5,
|
||||
"random_seed": 42,
|
||||
"verbose": False,
|
||||
"allow_writing_files": False,
|
||||
"min_data_in_leaf": 20,
|
||||
}
|
||||
|
||||
|
||||
class CatBoostModel(BaseModel):
|
||||
"""CatBoost 回归模型。"""
|
||||
|
||||
name = "catboost"
|
||||
|
||||
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_seed"] = random_seed
|
||||
self.early_stopping = early_stopping
|
||||
self.eval_ratio = eval_ratio
|
||||
self._model: CatBoostRegressor | None = None
|
||||
self._feature_names: list[str] = []
|
||||
|
||||
def fit(self, X: pd.DataFrame, y: pd.Series) -> "CatBoostModel":
|
||||
self._feature_names = list(X.columns)
|
||||
|
||||
n = len(X)
|
||||
val_size = int(n * self.eval_ratio)
|
||||
|
||||
X_train, y_train = X, y
|
||||
eval_set = None
|
||||
|
||||
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 = Pool(X_val, y_val)
|
||||
|
||||
self._model = CatBoostRegressor(**self.params)
|
||||
self._model.fit(
|
||||
X_train, y_train,
|
||||
eval_set=eval_set,
|
||||
early_stopping_rounds=self.early_stopping if eval_set else None,
|
||||
verbose=False,
|
||||
)
|
||||
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) -> pd.DataFrame:
|
||||
if self._model is None:
|
||||
return pd.DataFrame()
|
||||
imp = self._model.get_feature_importance()
|
||||
names = self._feature_names
|
||||
df = pd.DataFrame({"feature": names, "importance": imp})
|
||||
total = df["importance"].sum()
|
||||
df["importance_pct"] = df["importance"] / total * 100 if total > 0 else 0
|
||||
return df.sort_values("importance", ascending=False)
|
||||
|
||||
def cv_evaluate(
|
||||
self, X: pd.DataFrame, y: pd.Series, n_folds: int = 5
|
||||
) -> pd.DataFrame:
|
||||
"""时间序列交叉验证评估。"""
|
||||
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 = CatBoostModel(
|
||||
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:
|
||||
"""实际使用的树数量。"""
|
||||
if self._model is None:
|
||||
return None
|
||||
return self._model.tree_count_
|
||||
Reference in New Issue
Block a user