# 导入函数库
import statsmodels.api as sm
from statsmodels import regression
import numpy as np
import pandas as pd
import jqdata
import matplotlib.pyplot as plt
#得到标的与时间数据
security = get_industry_stocks('801192','2016-12-31')
#创建一个pandas数据结构,用于储存各家银行的数据信息
DF = pd.DataFrame(np.zeros((len(security),6)),index=security,columns=['EVA','RORAC','PE_TTM','PE_LYR','PB','MC'])
#对于任何一家银行,获取税前利润和财务指标
for k in range(len(security)):
df = get_fundamentals(query(
income.total_profit,
valuation.pe_ratio,
valuation.pe_ratio_lyr,
valuation.pb_ratio,
valuation.circulating_market_cap
).filter(
valuation.code == security[k]
), statDate = '2016')
ebt = df['total_profit']
PE_TTM = df['pe_ratio']
PE_LYR = df['pe_ratio_lyr']
PB = df['pb_ratio']
MC = df['circulating_market_cap']
#获取RWA数据
bank_ind = get_fundamentals(query(
bank_indicator.weighted_risky_asset,
).filter(
bank_indicator.code == security[k]
), statDate = '2016')
rwa = bank_ind['weighted_risky_asset']
#利用最近5年的数据回归计算β,即银行业股票和大盘之间的相关性
market_portofolio = get_price('000300.XSHG', start_date='2012-01-01', end_date='2016-12-30', frequency='daily', fields=['close'])['close']
bank_portofolio = get_price('399387.XSHE', start_date='2012-01-01', end_date='2016-12-30', frequency='daily', fields=['close'])['close']
market_return =[[0]for i in range(len(market_portofolio)-1)]
bank_return =[[0] for i in range(len(market_portofolio)-1)]
for i in range(len(market_portofolio)-1):
market_return[i] =[(market_portofolio[i+1]-market_portofolio[i])/market_portofolio[i]]
bank_return[i] = [(bank_portofolio[i+1]-bank_portofolio[i])/bank_portofolio[i]]
Y = market_return
X = bank_return
results = regression.linear_model.OLS(Y, X).fit()
beta = results.params
#计算几何平均市场收益率,有两种方法,第一种方法是几年增长率的几何平均
market_re_exp = (market_portofolio.iloc[-1]/market_portofolio.iloc[1])**(0.2)-1
#第二种方法是每一年的增长率的算数平均
market_re_one = market_portofolio.iloc[365]/market_portofolio.iloc[1]-1
market_re_two = market_portofolio.iloc[730]/market_portofolio.iloc[366]-1
market_re_three = market_portofolio.iloc[1095]/market_portofolio.iloc[731]-1
market_re_four = market_portofolio.iloc[-1]/market_portofolio.iloc[1096]-1
#这里我们选用第二种方法,以算数平均作为市场收益率的取值
market_re = np.mean([market_re_one,market_re_two,market_re_three,market_re_four])
#取2016年长期国债利率作为无风险利率,为3.01%
risk_free_return = 0.0301
#利用CAPM模型,计算COE
COE = risk_free_return +beta[0]*(market_re-risk_free_return)
#根据2017银行业监管规定,商业银行资本充足率为10.5%
CAR = 0.105
#计算经济利润eva
eva = ebt - rwa * CAR *COE
RORAC = ebt/(rwa*CAR)
#以亿元人民币为单位,放入以pandas为数据结构的DF中
DF.ix[k,0]=eva[0]/10e7
DF.ix[k,1]=RORAC[0]
DF.ix[k,2]=PE_TTM[0]
DF.ix[k,3]=PE_LYR[0]
DF.ix[k,4]=PB[0]
DF.ix[k,5]=MC[0]
#下面开始回归!比如想找经济利润与pe的关系,为了方便使用线性函数拟合,这里先取log
x = log(DF.ix[:,0].values)
X = sm.add_constant(x)
y = log(DF.ix[:,3].values)
model = sm.OLS(y,X)
results = model.fit()
y_fitted = results.fittedvalues
#最后一步!反映在图上
fig, ax = plt.subplots(figsize=(8,6))
ax.plot(x, y, 'o', label='data')
ax.plot(x, y_fitted, 'r--',label='OLS')
ax.legend(loc='best')