bqrziiy4 作业提交
由bqrziiy4创建,最终由small_q 被浏览 84 用户
import pandas as pd
import numpy as np
import dai
sql = """
SELECT date, instrument,
m_avg(turn, 20) as avg_turn_20,
m_lead(close, 5) / close -1 AS future_return_5,
FROM cn_stock_prefactors
WHERE date >='2024-01-01' AND date <='2024-07-08'
ORDER BY date, instrument
"""
df_raw = dai.query(sql).df()
df_copy = df_raw.copy()
df_copy.replace([np.inf, -np.inf], np.nan)
df_copy = df_copy.dropna()
def cal_ic(df):
return df['future_return_5'].corr(df['avg_turn_20'], method='spearman')
df = df_copy.groupby('date').apply(cal_ic).reset_index().rename(columns={0:'IC'})
ic_mean = np.nanmean(df['IC'])
ir = ic_mean / np.nanstd(df['IC'])
print("IC均值为", round(ic_mean, 3), "IR为", round(ir,3))
\