事件研究法python代码

事件研究法python代码,第1张

事件研究法python代码

事件研究法(event study)由Ball & Brown(1968)以及Famaetal(1969)开创,其原理是基于有效市场假说,通过研究某一未预期到的事件发生前后样本股票收益率的异常波动,揭示股票收益率对市场披露信息的反应程度,以及对股东财富价值的正向或负向影响。

本文给出的python代码以研究“811汇改”对中国银行股票收益率带来的影响为例。

注意:本文使用的是tushare的pro接口,利用该接口获取数据需要一定积分,积分可以在注册后通过完成任务获取。附官网注册链接:Tushare大数据社区

高校学生或老师也可以联系社区管理员进行认证,认证完成即可获得一定积分,然后就可以使用一些(我觉得还挺多的)里面的数据。认证只需不到24h,很方便!

我的tushareID:480696

#事件研究法

import pandas as pd
import tushare as ts
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression #线性回归
import csv
import numpy as np

plt.rcParams['font.sans-serif']=['SimHei'] #中文
plt.rcParams['axes.unicode_minus'] = False
pro = ts.pro_api('这里用你的token替换')
plt.style.use('fivethirtyeight')


#市场收益率Rm
df = pro.index_daily(ts_code='000001.SH', start_date='20140101', end_date='20191231')
df_index = df[['trade_date', 'pct_chg']].copy()
df_index['trade_date'] = pd.to_datetime(df_index['trade_date'])
df_index = df_index.sort_values("trade_date")
df_index = df_index.reset_index().drop("index", axis=1)
df_index['pct_chg'] = df_index['pct_chg'] / 100


#企业收益率Ri
ts_code='601988.SH'
d2= pro.daily(ts_code=ts_code, start_date='20140101', end_date='20191231')
d0 = d2[['trade_date', 'pct_chg']].copy()
d0.columns = ['trade_date', 'return']
d0['trade_date'] = pd.to_datetime(d0['trade_date'])
d0 = d0.sort_values("trade_date")
d0 = d0.reset_index().drop("index", axis=1)
d0['return'] = d0['return'] / 100


#合并Ri和Rm
df_final = d0.merge(df_index, on='trade_date', how='left')
df_final.to_excel("整理数据.xlsx")


#计算预期收益率
def get_OLS(X, y, pre_X):
    linear_m = LinearRegression().fit(X, y)
    r_2 = linear_m.score(X, y) #值越接近1拟合优度越好
    pre_y=linear_m.predict(X)
    Residual = sum((y - pre_y)**2)
    L_xx = len(X) * np.var(X)
    sigma = np.sqrt(Residual / (len(X)-2))
    t = linear_m.coef_ * np.sqrt(L_xx) / sigma
    t=round(float(t),4)

    print(f"构建模型,拟合优度为{round(r_2*100, 2)}%")
    print(f"Ri = {round(linear_m.intercept_,3)} + {round(linear_m.coef_[0],3)}Rm + e")

    if 1.65<=abs(t)<1.96:
        print(f'回归模型的t值为{t},回归系数在10%的置信水平下显著')
    elif 1.96<=abs(t)<2.58:
        print(f'回归模型的t值为{t},回归系数在5%的置信水平下显著')
    elif abs(t)>=2.58:
        print(f'回归模型的t值为{t},回归系数在1%的置信水平下显著')
    else:
        print(f'回归模型的t值为{t},回归系数不显著')

    return linear_m.predict(pre_X)


#计算AR,CAR
def get_data(event):
    print("事件日为: ", event)
    q,h = df_final[df_final['trade_date'] == event].index[0]-15, df_final[df_final['trade_date'] == event].index[0]+15 #事件窗口[-15,15]
    target = df_final.loc[q:h].copy()
    estimate = df_final.loc[q-195:q-6].copy() #估计窗口[-210,-21]
    X = estimate[['pct_chg']] #估计期市场回报率
    y = estimate['return'] #估计期企业回报率
    predict_X = target[['pct_chg']] #窗口期市场回报率
    target['E(Rt)'] = get_OLS(X, y, predict_X) #企业预期收益率
    target['ARt'] = target['return']-target['E(Rt)'] #企业异常收益率
    target['CARt'] =  target['ARt'].cumsum() #累计异常收益率 = 异常收益率在窗口期的求和
    return target


#绘制图像
def main(e):
    a = get_data(e)
    print(a)
    a.set_index('trade_date')[['ARt', 'CARt']].plot()

#结果
events = ['2015-08-11']
for e in events:
    main(e)

plt.title('中国银行')
plt.savefig('.中国银行.jpg',bbox_inches='tight')

运行结果如下:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5720602.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存