跳轉到

finlab.tools

finlab.tools.event_study

create_factor_data

create_factor_data(factor, adj_close, days=[5, 10, 20, 60], event=None)

create factor data, which contains future return

PARAMETER DESCRIPTION
factor

factor data where index is datetime and columns is asset id

TYPE: DataFrame

adj_close

adj close where index is datetime and columns is asset id

TYPE: DataFrame

days

future return considered

TYPE: List[int] DEFAULT: [5, 10, 20, 60]

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: DataFrame

benchmark_adj_close

benchmark for CAPM

TYPE: DataFrame

stock_adj_close

stock price for CAPM

TYPE: DataFrame

sample_period

period for fitting CAPM

TYPE: (int, int) DEFAULT: (-45, -20)

estimation_period

period for calculating alpha (abnormal return)

TYPE: (int, int) DEFAULT: (-5, 20)

plot

plot the result

TYPE: bool DEFAULT: True

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)

plot_event_study

plot_event_study(returns)

Plot the event study for the given returns.

PARAMETER DESCRIPTION
returns

A DataFrame containing the returns data.

TYPE: DataFrame

Return

ax (matplotlib.axes.Axes): The axes object containing the plot.

finlab.tools.factor_analysis

calc_metric

calc_metric(factor, adj_close, days=[10, 20, 60, 120], func=corr)

計算因子

PARAMETER DESCRIPTION
factor

因子

TYPE: DataFrame

adj_close

股價

TYPE: DataFrame

days

預測天數. Defaults to [10, 20, 60, 120].

TYPE: list DEFAULT: [10, 20, 60, 120]

func

計算函數. Defaults to corr.

TYPE: function DEFAULT: corr

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(factor, adj_close, days=[10, 20, 60, 120])

計算因子的IC

PARAMETER DESCRIPTION
factor

因子

TYPE: DataFrame

adj_close

股價

TYPE: DataFrame

days

預測天數. Defaults to [10, 20, 60, 120].

TYPE: list DEFAULT: [10, 20, 60, 120]

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