Skip to content

Commit

Permalink
0.9.45 update
Browse files Browse the repository at this point in the history
  • Loading branch information
zengbin93 committed Mar 17, 2024
1 parent 4bcb683 commit abc82a0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
10 changes: 6 additions & 4 deletions czsc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,18 @@
normalize_feature,
normalize_ts_feature,
feture_cross_layering,
rolling_rank,
rolling_norm,
rolling_qcut,
rolling_compare,
find_most_similarity,
)

from czsc.features.utils import (
is_event_feature,
rolling_corr,
rolling_rank,
rolling_norm,
rolling_qcut,
rolling_compare,
rolling_scale,
rolling_slope,
)

__version__ = "0.9.45"
Expand Down
12 changes: 6 additions & 6 deletions czsc/utils/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def normalize_ts_feature(df, x_col, n=10, **kwargs):
assert df[x_col].nunique() > n, "因子值的取值数量必须大于分层数量"
assert df[x_col].isna().sum() == 0, "因子有缺失值,缺失数量为:{}".format(df[x_col].isna().sum())
method = kwargs.get("method", "rolling")
window = kwargs.get("window", 1000)
window = kwargs.get("window", 2000)
min_periods = kwargs.get("min_periods", 300)

if f"{x_col}_norm" not in df.columns:
Expand All @@ -89,7 +89,7 @@ def normalize_ts_feature(df, x_col, n=10, **kwargs):

# # 对于缺失值,获取原始值,然后进行标准化
# na_x = df[df[f"{x_col}_norm"].isna()][x_col].values
# df.loc[df[f"{x_col}_norm"].isna(), f"{x_col}_norm"] = na_x - na_x.mean() / na_x.std()
df.loc[df[f"{x_col}_norm"].isna(), f"{x_col}_norm"] = 0

if f"{x_col}_qcut" not in df.columns:
if method == "expanding":
Expand All @@ -106,11 +106,11 @@ def normalize_ts_feature(df, x_col, n=10, **kwargs):
# 对于缺失值,获取原始值,然后进行分位数处理分层
# na_x = df[df[f"{x_col}_qcut"].isna()][x_col].values
# df.loc[df[f"{x_col}_qcut"].isna(), f"{x_col}_qcut"] = pd.qcut(na_x, q=n, labels=False, duplicates='drop', retbins=False)
df[f"{x_col}_qcut"] = df[f"{x_col}_qcut"].fillna(-1)
if df[f'{x_col}_qcut'].isna().sum() > 0:
logger.warning(f"因子 {x_col} 分层存在 {df[f'{x_col}_qcut'].isna().sum()} 个缺失值,已使用前值填充")
df[f'{x_col}_qcut'] = df[f'{x_col}_qcut'].ffill()
# if df[f'{x_col}_qcut'].isna().sum() > 0:
# logger.warning(f"因子 {x_col} 分层存在 {df[f'{x_col}_qcut'].isna().sum()} 个缺失值,已使用前值填充")
# df[f'{x_col}_qcut'] = df[f'{x_col}_qcut'].ffill()

df[f"{x_col}_qcut"] = df[f"{x_col}_qcut"].fillna(-1)
# 第00层表示缺失值
df[f'{x_col}分层'] = df[f'{x_col}_qcut'].apply(lambda x: f'第{str(int(x+1)).zfill(2)}层')

Expand Down
14 changes: 4 additions & 10 deletions czsc/utils/st_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,22 +571,16 @@ def show_ts_rolling_corr(df, col1, col2, **kwargs):
if sub_title:
st.subheader(sub_title, divider="rainbow", anchor=hashlib.md5(sub_title.encode('utf-8')).hexdigest()[:8])

min_periods = kwargs.get('min_periods', None)
window = kwargs.get('window', None)
min_periods = kwargs.get('min_periods', 300)
window = kwargs.get('window', 2000)
corr_method = kwargs.get('corr_method', 'pearson')

if not window or window <= 0:
method = 'expanding'
corr_result = df[col1].expanding(min_periods=min_periods).corr(df[col2], pairwise=True)
else:
method = 'rolling'
corr_result = df[col1].rolling(window=window, min_periods=min_periods).corr(df[col2], pairwise=True)
corr_result = df[col1].rolling(window=window, min_periods=min_periods).corr(df[col2], pairwise=True)

corr_result = corr_result.dropna()
corr_result = corr_result.rename('corr')
line = go.Scatter(x=corr_result.index, y=corr_result, mode='lines', name='corr')
layout = go.Layout(
title=f'滑动({method})相关系数',
title='滑动相关系数',
xaxis=dict(title=''),
yaxis=dict(title='corr'),
annotations=[
Expand Down

0 comments on commit abc82a0

Please sign in to comment.