所以计算两个序列的协方差,np.cov()得到的是一个2x2的矩阵。而第一行第二列的那个数值才是我们要求的值。
np.cov(x, y)
array([[11.71 , -4.286 ],
[-4.286 , 2.144133]])
import pandas as pd
import numpy as np
import QUANTAXIS as QA
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
stock = QA.QA_fetch_stock_day_adv('002352','2018-12-28','2019-12-31')
index = QA.QA_fetch_index_day_adv('000300','2018-12-28','2019-12-31')
returns_stock = stock.data.close.pct_change()
returns_index = index.data.close.pct_change()
#计算两组数据的协方差
covr = np.cov(returns_stock[1:],returns_index[1:])[0][1]
#计算沪深300指数数据的方差
var = np.var(returns_index)
#计算贝塔值
beta = covr/var
print("Covariance:{},Variance:{},Beta:{}".format(covr,var,beta))
利用tushare上的日频数据进行计算
import tushare as ts
from pandas import DataFrame
import time
ts.set_token('你的token')
pro = ts.pro_api()
stock_info = pro.stock_basic()
startdate: str = input('请输入开始日期\n')
enddate: str = input('请输入结束日期\n')
SH_code = '000001.SH'
SZ_code = '399300.SZ'
SH = pro.index_daily(ts_code=SH_code,start_date=startdate,end_date=enddate)
#print(SH)
SH_std = SH.std()
SH_var = SH.var()
#print(SH_std.pct_chg)
SZ = pro.index_daily(ts_code=SH_code,start_date=startdate,end_date=enddate)
SZ_std = SZ.std()
SZ_var = SZ.var()
beta_value = {}
rank=[]
rank_code=[]
rank_beta=[]
rank_name=[]
for i in range(0,len(stock_info)):
if i > 0 and i % 120 == 0:
time.sleep(60)
code = stock_info.at[i,'ts_code']
change = pro.daily(ts_code=code,start_date=startdate,end_date=enddate)
if change.empty == False:
std = change.std()
change_std = std.pct_chg
if code[7:] == 'SH':
cov = SH_std.pct_chg * change_std
beta = cov / SH_var.pct_chg
elif code[7:] == 'SZ':
cov = SZ_std.pct_chg * change_std
beta = cov / SZ_var.pct_chg
beta_value[stock_info.at[i,'ts_code']] = (beta,stock_info.at[i,'name'])
else:
continue
rank = sorted(beta_value.items(),key=lambda x:x[1],reverse=True)
print(rank)
for i in range(0,len(rank)):
rank_code.append(rank[i][0])
rank_name.append(rank[i][1][1])
rank_beta.append(rank[i][1][0])
data={
'股票代码':rank_code,
'股票名称':rank_name,
'beta':rank_beta,
}
df = DataFrame(data)
df.to_excel('贝塔系数.xlsx')