-
Notifications
You must be signed in to change notification settings - Fork 842
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # .gitignore
- Loading branch information
Showing
21 changed files
with
2,494 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .plotting import ( | ||
model_performance_graph, | ||
plot_factor_autocorr, | ||
plot_group_score_return, | ||
plot_group_turnover, | ||
plot_score_ic, | ||
) |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
''' | ||
Author: hugo2046 [email protected] | ||
Date: 2023-04-11 15:34:21 | ||
LastEditors: hugo2046 [email protected] | ||
LastEditTime: 2023-04-11 15:41:59 | ||
Description: 计算组件 | ||
''' | ||
|
||
import pandas as pd | ||
import numpy as np | ||
|
||
|
||
def calc_sigma(df: pd.DataFrame, bench: pd.Series = None) -> pd.DataFrame: | ||
"""计算sigma | ||
Args: | ||
df (pd.DataFrame): 当日截面pct_chg | ||
bench (pd.Series, optional): 指数收益序列 index-datetime values-pct_chg. Defaults to None. | ||
当为None时,使用截面上的所有股票的平均收益率作为benchmark | ||
Returns: | ||
pd.Series: index-datetime columns-code values-sigma | ||
""" | ||
|
||
if bench is None: | ||
bench: pd.DataFrame = df.mean(axis=1) | ||
|
||
a: pd.DataFrame = df.sub(bench, axis=0).abs() | ||
b: pd.DataFrame = df.abs().add(bench.abs(), axis=0) + 0.1 | ||
|
||
return a.div(b) | ||
|
||
|
||
def calc_weight(sigma: pd.DataFrame, delta: float = 0.7) -> pd.DataFrame: | ||
"""计算权重 | ||
Args: | ||
sigma (pd.DataFrame): index-datetime columns-code values-sigma | ||
Returns: | ||
pd.DataFrame: index-datetime columns-code values-weight | ||
""" | ||
|
||
rank: pd.DataFrame = sigma.rank(axis=1,ascending=False) | ||
|
||
a: pd.DataFrame = rank.apply(lambda x: np.power(delta, x), axis=1) | ||
# b: pd.DataFrame = a.apply(lambda x: np.multiply(x, 1 / len(x)), axis=1).sum(axis=1) | ||
b: pd.DataFrame = a.mean(axis=1) | ||
return a.div(b, axis=0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
''' | ||
Author: hugo2046 [email protected] | ||
Date: 2023-04-04 10:49:17 | ||
LastEditors: hugo2046 [email protected] | ||
LastEditTime: 2023-04-06 20:18:28 | ||
Description: | ||
''' | ||
|
||
from alphalens.utils import quantize_factor | ||
import pandas as pd | ||
from typing import Dict,List | ||
|
||
|
||
|
||
def clean_factor_data(factor_data: pd.DataFrame) -> pd.DataFrame: | ||
"""预处理因子数据 | ||
Args: | ||
factor_data (pd.DataFrame): MultiIndex level0:datetime level1:instrument MultiColumns level0:feature level1:label | ||
Returns: | ||
pd.DataFrame: MultiIndex level0:date level1:assert columns->factor next_ret | ||
""" | ||
clean_factor: pd.DataFrame = factor_data.copy() | ||
if isinstance(clean_factor.columns,pd.MultiIndex): | ||
clean_factor.columns = clean_factor.columns.droplevel(0) | ||
|
||
clean_factor.index.names = ["date", "assert"] | ||
|
||
return clean_factor | ||
|
||
|
||
def get_factor_group_returns( | ||
clean_factor: pd.DataFrame, quantile: int, no_raise: bool = False | ||
) -> pd.DataFrame: | ||
"""获取单因子分组收益 | ||
Args: | ||
clean_factor (pd.DataFrame): MultiIndex level0:date level1:assert columns->factor next_ret | ||
quantile (int): 分组 | ||
no_raise (bool, optional):Defaults to False. | ||
Returns: | ||
pd.DataFrame: _description_ | ||
""" | ||
sel_cols: List = [col for col in clean_factor.columns.tolist() if col != "next_ret"] | ||
|
||
returns_dict: Dict = {} | ||
for col in sel_cols: | ||
clean_factor[f"{col}_group"] = quantize_factor( | ||
clean_factor.rename(columns={col: "factor"})[["factor"]], | ||
quantiles=quantile, | ||
no_raise=no_raise, | ||
) | ||
returns_dict[col] = pd.pivot_table( | ||
clean_factor.reset_index(), | ||
index="date", | ||
columns=f"{col}_group", | ||
values="next_ret", | ||
) | ||
|
||
df: pd.DataFrame = pd.concat(returns_dict, axis=1) | ||
df.index.names = ["date"] | ||
df.columns.names = ["factor_name", "group"] | ||
return df | ||
|
||
|
||
|
Oops, something went wrong.