finlab.tools
finlab.tools.event_study
create_factor_data
create factor data, which contains future return
PARAMETER | DESCRIPTION |
---|---|
factor |
factor data where index is datetime and columns is asset id
TYPE:
|
adj_close |
adj close where index is datetime and columns is asset id
TYPE:
|
days |
future return considered
TYPE:
|
Return
Analytic plots and tables
Warning
This function is not identical to finlab.ml.alphalens.create_factor_data
Examples:
from finlab.tools.event_study import create_factor_data
from finlab.tools.event_study import event_study
factor = data.get('price_earning_ratio:股價淨值比')
adj_close = data.get('etl:adj_close')
benchmark = data.get('benchmark_return:發行量加權股價報酬指數')
# create event dataframe
dividend_info = data.get('dividend_announcement')
v = dividend_info[['stock_id', '除權交易日']].set_index(['stock_id', '除權交易日'])
v['value'] = 1
event = v[~v.index.duplicated()].reset_index().drop_duplicates(
subset=['stock_id', '除權交易日']
).pivot(index='除權交易日', columns='stock_id', values='value').notna()
# calculate factor_data
factor_data = create_factor_data({'pb':factor}, adj_close, event=event)
r = event_study(factor_data, benchmark, adj_close)
plt.bar(r.columns, r.mean().values)
plt.plot(r.columns, r.mean().cumsum().values)
event_study
event_study(factor_data, benchmark_adj_close, stock_adj_close, sample_period=(-45, -20), estimation_period=(-5, 20), plot=True)
Run event study and returns the abnormal returns of each stock on each day.
PARAMETER | DESCRIPTION |
---|---|
factor_data |
factor data where index is datetime and columns is asset id
TYPE:
|
benchmark_adj_close |
benchmark for CAPM
TYPE:
|
stock_adj_close |
stock price for CAPM
TYPE:
|
sample_period |
period for fitting CAPM
TYPE:
|
estimation_period |
period for calculating alpha (abnormal return)
TYPE:
|
plot |
plot the result
TYPE:
|
Return
Abnormal returns of each stock on each day.
Examples:
from finlab.tools.event_study import create_factor_data
from finlab.tools.event_study import event_study
factor = data.get('price_earning_ratio:股價淨值比')
adj_close = data.get('etl:adj_close')
benchmark = data.get('benchmark_return:發行量加權股價報酬指數')
# create event dataframe
dividend_info = data.get('dividend_announcement')
v = dividend_info[['stock_id', '除權交易日']].set_index(['stock_id', '除權交易日'])
v['value'] = 1
event = v[~v.index.duplicated()].reset_index().drop_duplicates(
subset=['stock_id', '除權交易日']
).pivot(index='除權交易日', columns='stock_id', values='value').notna()
# calculate factor_data
factor_data = create_factor_data({'pb':factor}, adj_close, event=event)
r = event_study(factor_data, benchmark, adj_close)
plt.bar(r.columns, r.mean().values)
plt.plot(r.columns, r.mean().cumsum().values)
finlab.tools.factor_analysis
calc_metric
計算因子
PARAMETER | DESCRIPTION |
---|---|
factor |
因子
TYPE:
|
adj_close |
股價
TYPE:
|
days |
預測天數. Defaults to [10, 20, 60, 120].
TYPE:
|
func |
計算函數. Defaults to corr.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
pd.DataFrame: 因子計算結果 |
Example
factor = data.indicator('RSI') adj_close = data.get('etl:adj_close') calc_metric(factor, adj_close)
date | factor_10 | factor_20 | factor_60 | factor_120 |
---|---|---|---|---|
2010-01-01 | 0.1 | 0.2 | 0.3 | 0.4 |
2010-01-02 | 0.1 | 0.2 | 0.3 | 0.4 |
2010-01-03 | 0.1 | 0.2 | 0.3 | 0.4 |
2010-01-04 | 0.1 | 0.2 | 0.3 | 0.4 |
2010-01-05 | 0.1 | 0.2 | 0.3 | 0.4 |
ic
計算因子的IC
PARAMETER | DESCRIPTION |
---|---|
factor |
因子
TYPE:
|
adj_close |
股價
TYPE:
|
days |
預測天數. Defaults to [10, 20, 60, 120].
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
pd.DataFrame: 因子計算結果 |
Example
factor = data.indicator('RSI') adj_close = data.get('etl:adj_close') calc_metric(factor, adj_close)
date | factor_10 | factor_20 | factor_60 | factor_120 |
---|---|---|---|---|
2010-01-01 | 0.1 | 0.2 | 0.3 | 0.4 |
2010-01-02 | 0.1 | 0.2 | 0.3 | 0.4 |
2010-01-03 | 0.1 | 0.2 | 0.3 | 0.4 |
2010-01-04 | 0.1 | 0.2 | 0.3 | 0.4 |
2010-01-05 | 0.1 | 0.2 | 0.3 | 0.4 |