事件研究法python代码
生活随笔
收集整理的這篇文章主要介紹了
事件研究法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 npplt.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) / sigmat=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')運行結果如下:
總結
以上是生活随笔為你收集整理的事件研究法python代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我关注的一些技术微信公众号
- 下一篇: Docker容器与本地文件相互拷贝