Financial Analysis [CHI]
在上述指数中选出沪深300,上证国债, 人民币实际指数,Wind商品指数作为可选资产,利用月收益率进行投资组合分析,在允许卖空和禁止卖空的条件下计算有效前沿和最小方差组合。
후선300, 상하이 국채, 위안화 실질지수, Wind(중국 주식 프로그램) 상품지수를 기준으로 월별 수익률을 활용한 포트폴리오를 분석하고 공매도 허용과 공매도 금지 조건 하에 분산을 최소화 하는 조합을 찾아보기
데이터는 指数行情序列daily.xlsx 를 사용
In [35]:
from pandas import Series, DataFrame
import pandas as pd
from numpy.random import randn
import numpy as np
import os
import matplotlib as mpl
import matplotlib.pyplot as plt
from datetime import datetime
%matplotlib inline
import scipy as sp
import scipy.optimize as scopt
import scipy.stats as spstats
import matplotlib.mlab as mlab
from math import log
In [36]:
# 读入"指数行情序列daily.xlsx"
stockinfo=pd.read_excel("指数行情序列daily.xlsx",index_col = 0, skiprows = 1,na_values=['0.0000'])
stockinfo.head()
Out[36]:
沪深300 | 上证综指 | 深证综指 | 上证国债 | 人民币实际指数 | Wind商品指数 | |
---|---|---|---|---|---|---|
时间 | ||||||
1990-12-19 00:00:00 | NaN | 99.98 | NaN | NaN | NaN | NaN |
1990-12-20 00:00:00 | NaN | 104.39 | NaN | NaN | NaN | NaN |
1990-12-21 00:00:00 | NaN | 109.13 | NaN | NaN | NaN | NaN |
1990-12-24 00:00:00 | NaN | 114.55 | NaN | NaN | NaN | NaN |
1990-12-25 00:00:00 | NaN | 120.25 | NaN | NaN | NaN | NaN |
In [37]:
# '沪深300','上证国债','人民币实际指数','Wind商品指数' 的月对数收益率
range_month = pd.date_range('1990-12-19', '2017-10-18', freq='BM')
stockinfo_month = DataFrame(stockinfo,index = range_month, columns=['沪深300','上证国债','人民币实际指数','Wind商品指数'])
stockinfo_month = stockinfo_month.fillna(method='ffill')
logp_month=stockinfo_month.applymap(log)
LogReturnMonth=logp_month-logp_month.shift(1)
LogReturnMonth.head()
Out[37]:
沪深300 | 上证国债 | 人民币实际指数 | Wind商品指数 | |
---|---|---|---|---|
1990-12-31 | NaN | NaN | NaN | NaN |
1991-01-31 | NaN | NaN | NaN | NaN |
1991-02-28 | NaN | NaN | NaN | NaN |
1991-03-29 | NaN | NaN | NaN | NaN |
1991-04-30 | NaN | NaN | NaN | NaN |
In [38]:
#投资组合分析
LogReturnMonth.describe()
Out[38]:
沪深300 | 上证国债 | 人民币实际指数 | Wind商品指数 | |
---|---|---|---|---|
count | 188.000000 | 175.000000 | 87.000000 | 269.000000 |
mean | 0.006087 | 0.002668 | 0.002250 | 0.000436 |
std | 0.087956 | 0.007821 | 0.012314 | 0.063290 |
min | -0.362959 | -0.058051 | -0.027694 | -0.515980 |
25% | -0.043460 | 0.000061 | -0.004775 | -0.032273 |
50% | 0.005262 | 0.002626 | 0.001148 | 0.000000 |
75% | 0.048115 | 0.004689 | 0.009261 | 0.035283 |
max | 0.246305 | 0.042749 | 0.030545 | 0.215217 |
In [39]:
Mu=LogReturnMonth.mean()
Sigma=LogReturnMonth.cov()
Sigma
Out[39]:
沪深300 | 上证国债 | 人民币实际指数 | Wind商品指数 | |
---|---|---|---|---|
沪深300 | 0.007736 | -0.000076 | -0.000136 | 0.002631 |
上证国债 | -0.000076 | 0.000061 | 0.000002 | -0.000069 |
人民币实际指数 | -0.000136 | 0.000002 | 0.000152 | -0.000246 |
Wind商品指数 | 0.002631 | -0.000069 | -0.000246 | 0.004006 |
In [40]:
Sigma=np.matrix(Sigma)
N=max(Mu.shape)
Mu=np.matrix(Mu).T
onesv=np.matrix(np.ones((N,1)))
SI=Sigma.I
Mu.T*SI*onesv
A=Mu.T*SI*onesv; A=A[0,0]
B=Mu.T*SI*Mu; B=B[0,0]
C=onesv.T*SI*onesv; C=C[0,0]
D=B*C-A**2
g=SI*onesv*B/D-SI*Mu*A/D
h=SI*Mu*C/D-SI*onesv*A/D
mmin=np.min(Mu)
mmax=np.max(Mu)
meanv=np.linspace(mmin,mmax,100)
wv=list(map(lambda x: g+x*h,meanv))
sigmav=list(map(lambda x: np.sqrt(x.T*Sigma*x)[0,0],wv))
mumin=-g.T*Sigma*h/(h.T*Sigma*h)
sigmamin=np.sqrt(g.T*Sigma*g-(g.T*Sigma*h)**2/(h.T*Sigma*h))
muf=0.001 #设无风险收益率为 0.001
wt=SI*(Mu-muf)
wt=wt/(onesv.T*wt)
mut=wt.T*Mu
sigmat=np.sqrt(wt.T*Sigma*wt)
%matplotlib inline
Sig=LogReturnMonth.std()
plt.plot(sigmav,meanv)
plt.plot(Sig,Mu,"bo")
plt.plot(sigmamin,mumin,"ro")
plt.plot([0,sigmat.item(0)],[muf,mut.item(0)],"yo--")
plt.xlim([-0.005,0.09])
plt.ylim([-0.001,0.013])
Out[40]:
(-0.001, 0.013)
- 以“上证国债”平均收益率作为无风险收益,计算沪深300,Wind商品指数,人民币实际指数的切线组合。
In [7]:
LogReturnMonth.describe()
Out[7]:
沪深300 | 上证国债 | 人民币实际指数 | Wind商品指数 | |
---|---|---|---|---|
count | 188.000000 | 175.000000 | 87.000000 | 269.000000 |
mean | 0.006087 | 0.002668 | 0.002250 | 0.000436 |
std | 0.087956 | 0.007821 | 0.012314 | 0.063290 |
min | -0.362959 | -0.058051 | -0.027694 | -0.515980 |
25% | -0.043460 | 0.000061 | -0.004775 | -0.032273 |
50% | 0.005262 | 0.002626 | 0.001148 | 0.000000 |
75% | 0.048115 | 0.004689 | 0.009261 | 0.035283 |
max | 0.246305 | 0.042749 | 0.030545 | 0.215217 |
In [8]:
# 可以知道‘上证国债’的平均收益率为 0.002668
# 即无风险收益率为 0.002668
# 下面考虑 ‘沪深300’,‘人民币实际指数’,‘Wind商品指数’的组合
In [9]:
LogReturnMonth1 = DataFrame(LogReturnMonth,columns = ['沪深300','人民币实际指数','Wind商品指数'])
Mu=LogReturnMonth1.mean()
Sigma=LogReturnMonth1.cov()
Sigma
Out[9]:
沪深300 | 人民币实际指数 | Wind商品指数 | |
---|---|---|---|
沪深300 | 0.007736 | -0.000136 | 0.002631 |
人民币实际指数 | -0.000136 | 0.000152 | -0.000246 |
Wind商品指数 | 0.002631 | -0.000246 | 0.004006 |
In [10]:
Sigma=np.matrix(Sigma)
N=max(Mu.shape)
Mu=np.matrix(Mu).T
onesv=np.matrix(np.ones((N,1)))
SI=Sigma.I
Mu.T*SI*onesv
A=Mu.T*SI*onesv; A=A[0,0]
B=Mu.T*SI*Mu; B=B[0,0]
C=onesv.T*SI*onesv; C=C[0,0]
D=B*C-A**2
g=SI*onesv*B/D-SI*Mu*A/D
h=SI*Mu*C/D-SI*onesv*A/D
mmin=np.min(Mu)
mmax=np.max(Mu)
meanv=np.linspace(mmin,mmax,100)
wv=list(map(lambda x: g+x*h,meanv))
sigmav=list(map(lambda x: np.sqrt(x.T*Sigma*x)[0,0],wv))
mumin=-g.T*Sigma*h/(h.T*Sigma*h)
sigmamin=np.sqrt(g.T*Sigma*g-(g.T*Sigma*h)**2/(h.T*Sigma*h))
muf=0.002668 #无风险收益率
wt=SI*(Mu-muf)
wt=wt/(onesv.T*wt)
mut=wt.T*Mu
sigmat=np.sqrt(wt.T*Sigma*wt)
Sig=LogReturnMonth1.std()
plt.plot(sigmav,meanv)
plt.plot(Sig,Mu,"bo")
plt.plot(sigmamin,mumin,"ro")
plt.plot([0,sigmat.item(0)],[muf,mut.item(0)],"yo--")
plt.xlim([-0.005,0.10])
plt.ylim([0,0.013])
Out[10]:
(0, 0.013)
In [11]:
weightm=np.array([[x.item(0),x.item(1),x.item(2)] for x in wv])
for i in range(3):
plt.plot(meanv,weightm[:,i],label=i+1)
plt.legend(loc="best")
plt.title("weighs for 3 assets as function of $\mu_p$")
Out[11]:
Text(0.5, 1.0, 'weighs for 3 assets as function of $\\mu_p$')
- 重新考虑(2)中的投资组合问题,将数据分为2组(按时间先后等分),利用每组数据进行投资组合分析并进行比较(包括比较有效前沿,权重向量等)
In [12]:
# LogReturnMonth 一共有322行。把它分成均等分成两组。
LogReturnMonth_1 = LogReturnMonth.iloc[:161]
LogReturnMonth_2 = LogReturnMonth.iloc[161:]
In [13]:
#先考虑第一组的数据
LogReturnMonth_1.describe()
Out[13]:
沪深300 | 上证国债 | 人民币实际指数 | Wind商品指数 | |
---|---|---|---|---|
count | 27.000000 | 14.000000 | 0.0 | 108.000000 |
mean | 0.000540 | -0.006349 | NaN | -0.006313 |
std | 0.060646 | 0.017543 | NaN | 0.049921 |
min | -0.110781 | -0.058051 | NaN | -0.147341 |
25% | -0.047721 | -0.006868 | NaN | -0.029708 |
50% | 0.002308 | -0.002356 | NaN | -0.003461 |
75% | 0.037200 | 0.002762 | NaN | 0.013250 |
max | 0.144533 | 0.015584 | NaN | 0.136337 |
In [14]:
########## 第一部分没有 ‘人民币实际指数 ’的数据。所以只考虑 '沪深300'和'Wind商品指数'的组合
LogReturnMonth_1 = DataFrame(LogReturnMonth_1,columns = ['沪深300','Wind商品指数'])
Sigma1=LogReturnMonth_1.cov()
Sigma1
Out[14]:
沪深300 | Wind商品指数 | |
---|---|---|
沪深300 | 0.003678 | 0.001293 |
Wind商品指数 | 0.001293 | 0.002492 |
In [15]:
#可以得到数据
muf=-0.006349 ;mu1=0.000540;mu2=-0.006313 ;sig1=0.060646;sig2=0.049921;rho12=0.001293
In [16]:
import sympy as sy
w=sy.symbols('w',positive = True, real = True)
ExpRet=mu2+(mu1-mu2)*w
Varf=sy.simplify(sig1**2*w**2+sig2**2*(1-w)**2+2*w*(1-w)*rho12*sig1*sig2)
StdRet=sy.sqrt(Varf)
#init_printing()
ExpRet,sy.simplify(StdRet)
meanf= sy.lambdify(w, ExpRet, "numpy")
varf=sy.lambdify(w,Varf,"numpy")
#%matplotlib qt
%matplotlib inline
fig=plt.figure(figsize=(8,6))
plt.text(sig1+0.002,mu1-0.001,"$R_1$")
plt.text(sig2+0.002,mu2-0.001,"$R_2$")
wmin=float(-Varf.coeff(w,1)/(2*Varf.coeff(w,2)))
mx=np.sqrt(varf(wmin));my=meanf(wmin)
plt.plot([mx,0],[my,muf],"bo")
plt.text(mx+0.005,my,"M (minimum variance portfolio)",fontsize=15)
plt.text(0,muf+0.005,"F",fontsize=15)
wv=np.linspace(0,wmin,100)
stdv=np.sqrt(varf(wv))
meanv=meanf(wv)
plt.plot(stdv,meanv,"r-")
plt.annotate("$w\in$"+str([0,wmin]), size=15,xy=(stdv[50]+0.002,meanv[50]),
xytext=(stdv[50]+0.05,meanv[50]),
arrowprops=dict(facecolor="red"))
wv=np.linspace(wmin,1,100)
stdv=np.sqrt(varf(wv))
meanv=meanf(wv)
plt.plot(stdv,meanv,"b-")
plt.annotate("efficient frontier\n"+"$w\in$"+str([wmin,1]), size=15,xy=(stdv[80]+0.002,meanv[80]),
xytext=(stdv[80]+0.05,meanv[80]),
arrowprops=dict(facecolor="blue"))
plt.xlim([-0.02,max(sig1,sig2)*1.5])
plt.ylim([-0.008,0.002])
plt.plot([0,mx],[muf,my],"b--")
plt.plot([0,sig2],[muf,mu2],"r-.")
# tangency portfolio
v1=mu1-muf;v2=mu2-muf;
Wt=(v1*sig2**2-v2*rho12*sig1*sig2)/(v1*sig2**2+v2*sig1**2-(v1+v2)*rho12*sig1*sig2)
Tx=np.sqrt(varf(Wt));Ty=meanf(Wt)
plt.plot([Tx],[Ty],"bo")
plt.plot([Tx,0],[Ty,muf],"b--")
plt.text(Tx+0.005,Ty,"T (tangency portfolio)",fontsize=15)
Out[16]:
Text(0.06527697636542062, 0.0004981687571580596, 'T (tangency portfolio)')
In [17]:
#然后虑第二组的数据
LogReturnMonth_2.describe()
Out[17]:
沪深300 | 上证国债 | 人民币实际指数 | Wind商品指数 | |
---|---|---|---|---|
count | 161.000000 | 161.000000 | 87.000000 | 161.000000 |
mean | 0.007017 | 0.003452 | 0.002250 | 0.004964 |
std | 0.091859 | 0.005812 | 0.012314 | 0.070650 |
min | -0.362959 | -0.009635 | -0.027694 | -0.515980 |
25% | -0.042615 | 0.000434 | -0.004775 | -0.033657 |
50% | 0.005739 | 0.002756 | 0.001148 | 0.001970 |
75% | 0.049290 | 0.004707 | 0.009261 | 0.048019 |
max | 0.246305 | 0.042749 | 0.030545 | 0.215217 |
In [18]:
#无风险资产收益率为 0.003452
LogReturnMonth_2 = DataFrame(LogReturnMonth_2,columns = ['沪深300','人民币实际指数','Wind商品指数'])
Mu=LogReturnMonth_2.mean()
Sigma=LogReturnMonth_2.cov()
Sigma
Out[18]:
沪深300 | 人民币实际指数 | Wind商品指数 | |
---|---|---|---|
沪深300 | 0.008438 | -0.000136 | 0.002870 |
人民币实际指数 | -0.000136 | 0.000152 | -0.000246 |
Wind商品指数 | 0.002870 | -0.000246 | 0.004991 |
In [19]:
Sigma=np.matrix(Sigma)
N=max(Mu.shape)
Mu=np.matrix(Mu).T
onesv=np.matrix(np.ones((N,1)))
SI=Sigma.I
Mu.T*SI*onesv
A=Mu.T*SI*onesv; A=A[0,0]
B=Mu.T*SI*Mu; B=B[0,0]
C=onesv.T*SI*onesv; C=C[0,0]
D=B*C-A**2
g=SI*onesv*B/D-SI*Mu*A/D
h=SI*Mu*C/D-SI*onesv*A/D
mmin=np.min(Mu)
mmax=np.max(Mu)
meanv=np.linspace(mmin,mmax,100)
wv=list(map(lambda x: g+x*h,meanv))
sigmav=list(map(lambda x: np.sqrt(x.T*Sigma*x)[0,0],wv))
mumin=-g.T*Sigma*h/(h.T*Sigma*h)
sigmamin=np.sqrt(g.T*Sigma*g-(g.T*Sigma*h)**2/(h.T*Sigma*h))
muf=0.003452 #无风险收益率
wt=SI*(Mu-muf)
wt=wt/(onesv.T*wt)
mut=wt.T*Mu
sigmat=np.sqrt(wt.T*Sigma*wt)
Sig=LogReturnMonth_2.std()
plt.plot(sigmav,meanv)
plt.plot(Sig,Mu,"bo")
plt.plot(sigmamin,mumin,"ro")
plt.plot([0,sigmat.item(0)],[muf,mut.item(0)],"yo--")
plt.xlim([-0.005,0.10])
plt.ylim([0,0.013])
Out[19]:
(0, 0.013)
In [20]:
weightm=np.array([[x.item(0),x.item(1),x.item(2)] for x in wv])
for i in range(3):
plt.plot(meanv,weightm[:,i],label=i+1)
plt.legend(loc="best")
plt.title("weighs for 3 assets as function of $\mu_p$")
Out[20]:
Text(0.5, 1.0, 'weighs for 3 assets as function of $\\mu_p$')
二。在所有股票(教学网共享的stockclose.zip)中随机选择10只,选择5年周收益率(根据情况选择起始时间),设无风险受益为1%。
In [21]:
## 选择000001.SZ ~ 000011.SZ 十只股票。考虑 2014~2018 的周收益率。
- 其中随机选择两只股票,计算其股票有效前沿,最小方差组合,以及切线组合的sharp比
In [22]:
stocks=pd.read_csv("stockclose.csv",index_col = 0,parse_dates = True)
stocks
Out[22]:
000001.SZ | 000002.SZ | 000004.SZ | 000005.SZ | 000006.SZ | 000007.SZ | 000008.SZ | 000009.SZ | 000010.SZ | 000011.SZ | ... | 688020.SH | 688022.SH | 688028.SH | 688029.SH | 688033.SH | 688066.SH | 688088.SH | 688122.SH | 688333.SH | 688388.SH | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1990-12-19 | NaN | NaN | NaN | 3.83 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1990-12-20 | NaN | NaN | NaN | 3.83 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1990-12-21 | NaN | NaN | NaN | 3.83 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1990-12-24 | NaN | NaN | NaN | 3.83 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1990-12-25 | NaN | NaN | NaN | 3.83 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1990-12-26 | NaN | NaN | NaN | 3.83 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1990-12-27 | NaN | NaN | NaN | 3.83 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1990-12-28 | NaN | NaN | NaN | 3.83 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1990-12-31 | NaN | NaN | NaN | 3.83 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-02 | NaN | NaN | NaN | 3.83 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-03 | NaN | NaN | NaN | 3.83 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-04 | NaN | NaN | NaN | 18.03 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-07 | NaN | NaN | NaN | 17.85 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-08 | NaN | NaN | NaN | 17.85 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-09 | NaN | NaN | NaN | 17.67 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-10 | NaN | NaN | NaN | 17.58 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-11 | NaN | NaN | NaN | 17.45 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-14 | NaN | NaN | 15.98 | 17.31 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-15 | NaN | NaN | 15.90 | 17.22 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-16 | NaN | NaN | 15.90 | 17.13 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-17 | NaN | NaN | 15.74 | 17.04 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-18 | NaN | NaN | 15.74 | 17.04 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-21 | NaN | NaN | 15.38 | 16.79 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-22 | NaN | NaN | 15.42 | 16.71 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-23 | NaN | NaN | 15.34 | 16.63 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-24 | NaN | NaN | 15.26 | 16.55 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-25 | NaN | NaN | 15.26 | 16.55 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-28 | NaN | NaN | 15.10 | 16.39 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-29 | NaN | 14.58 | 14.94 | 16.23 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1991-01-30 | NaN | 14.51 | 14.87 | 16.15 | NaN | NaN | NaN | NaN | NaN | NaN | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
2019-09-12 | 14.68 | 27.78 | 20.01 | 3.49 | 5.54 | 8.02 | 3.69 | 4.92 | 3.42 | 10.54 | ... | 95.20 | 60.22 | 109.00 | 138.24 | 40.04 | 55.27 | 63.32 | 46.35 | 68.27 | 57.04 |
2019-09-16 | 14.45 | 27.21 | 20.14 | 3.42 | 5.49 | 8.07 | 3.68 | 4.86 | 3.37 | 10.36 | ... | 95.66 | 60.38 | 112.48 | 144.20 | 40.26 | 56.35 | 63.58 | 46.66 | 68.65 | 57.63 |
2019-09-17 | 14.24 | 26.82 | 20.50 | 3.34 | 5.36 | 8.00 | 3.57 | 4.71 | 3.35 | 10.17 | ... | 94.09 | 59.06 | 112.50 | 142.07 | 39.42 | 57.92 | 62.65 | 47.07 | 67.53 | 56.69 |
2019-09-18 | 14.41 | 26.94 | 20.00 | 3.32 | 5.36 | 7.98 | 3.59 | 4.66 | 3.32 | 10.18 | ... | 91.98 | 60.30 | 113.01 | 133.90 | 38.35 | 56.15 | 62.19 | 45.80 | 71.05 | 55.95 |
2019-09-19 | 14.84 | 26.73 | 22.00 | 3.33 | 5.34 | 8.10 | 3.60 | 4.70 | 3.32 | 10.25 | ... | 93.77 | 62.14 | 116.22 | 135.06 | 39.25 | 56.80 | 64.02 | 46.86 | 77.00 | 56.91 |
2019-09-20 | 15.34 | 26.80 | 21.03 | 3.37 | 5.32 | 8.07 | 3.96 | 4.72 | 3.29 | 10.28 | ... | 93.21 | 61.48 | 116.50 | 134.88 | 39.96 | 56.80 | 63.70 | 46.51 | 77.30 | 56.69 |
2019-09-23 | 15.38 | 26.15 | 20.04 | 3.30 | 5.38 | 8.09 | 4.03 | 4.60 | 3.45 | 10.61 | ... | 93.96 | 60.80 | 117.69 | 134.10 | 38.95 | 56.90 | 62.85 | 46.22 | 75.20 | 56.62 |
2019-09-24 | 15.18 | 26.13 | 19.89 | 3.29 | 5.28 | 8.09 | 3.88 | 4.56 | 3.45 | 10.31 | ... | 96.99 | 62.50 | 121.11 | 141.00 | 39.75 | 58.25 | 65.86 | 47.30 | 76.18 | 58.25 |
2019-09-25 | 15.75 | 26.02 | 19.06 | 3.19 | 5.26 | 8.59 | 3.75 | 4.53 | 3.38 | 10.16 | ... | 93.59 | 63.26 | 111.98 | 136.66 | 38.77 | 54.80 | 63.11 | 45.58 | 77.35 | 56.17 |
2019-09-26 | 15.71 | 26.22 | 18.60 | 3.10 | 5.10 | 8.63 | 3.47 | 4.44 | 3.39 | 9.73 | ... | 86.68 | 56.21 | 101.52 | 129.30 | 35.35 | 48.60 | 55.16 | 41.08 | 69.89 | 52.10 |
2019-09-27 | 15.90 | 26.00 | 18.88 | 3.16 | 5.10 | 8.46 | 3.48 | 4.50 | 3.37 | 9.75 | ... | 93.98 | 59.57 | 103.95 | 130.85 | 35.83 | 49.70 | 55.40 | 41.40 | 81.80 | 52.92 |
2019-09-30 | 15.59 | 25.90 | 18.80 | 3.18 | 5.03 | 8.38 | 3.43 | 4.41 | 3.36 | 9.59 | ... | 87.80 | 55.90 | 93.41 | 133.16 | 34.08 | 45.92 | 50.72 | 38.76 | 78.80 | 49.65 |
2019-10-08 | 16.20 | 26.36 | 18.83 | 3.16 | 5.06 | 8.46 | 3.43 | 4.59 | 3.40 | 9.69 | ... | 85.40 | 52.38 | 89.80 | 133.96 | 32.98 | 44.29 | 49.78 | 36.95 | 70.01 | 48.23 |
2019-10-09 | 16.25 | 26.80 | 18.73 | 3.20 | 5.13 | 8.48 | 3.43 | 4.62 | 3.40 | 9.85 | ... | 86.37 | 52.67 | 91.30 | 140.60 | 33.10 | 44.65 | 53.51 | 37.13 | 69.41 | 48.58 |
2019-10-10 | 16.24 | 26.62 | 18.94 | 3.23 | 5.12 | 8.91 | 3.48 | 4.64 | 3.40 | 9.84 | ... | 86.90 | 53.10 | 91.60 | 148.70 | 33.42 | 45.16 | 53.55 | 37.39 | 72.07 | 49.13 |
2019-10-11 | 16.81 | 26.81 | 18.87 | 3.24 | 5.15 | 9.60 | 3.51 | 4.65 | 3.39 | 9.82 | ... | 85.30 | 51.74 | 88.61 | 148.50 | 33.15 | 44.20 | 52.66 | 37.38 | 71.90 | 48.72 |
2019-10-14 | 17.22 | 27.12 | 19.05 | 3.28 | 5.22 | 9.50 | 3.58 | 4.75 | 3.41 | 9.94 | ... | 89.60 | 52.48 | 89.51 | 152.29 | 33.80 | 44.69 | 54.00 | 37.65 | 72.99 | 49.45 |
2019-10-15 | 17.18 | 26.77 | 18.84 | 3.35 | 5.22 | 9.64 | 3.59 | 4.76 | 3.44 | 10.17 | ... | 87.68 | 52.38 | 89.66 | 154.69 | 33.25 | 43.11 | 53.12 | 36.29 | 76.10 | 48.52 |
2019-10-16 | 16.79 | 27.07 | 19.44 | 3.40 | 5.19 | 9.25 | 3.56 | 4.77 | 3.39 | 9.92 | ... | 85.70 | 50.76 | 86.43 | 150.01 | 31.95 | 41.08 | 51.29 | 34.46 | 77.92 | 46.40 |
2019-10-17 | 16.70 | 27.35 | 19.00 | 3.30 | 5.16 | 9.37 | 3.59 | 4.72 | 3.37 | 9.79 | ... | 83.84 | 49.86 | 82.90 | 160.13 | 32.26 | 39.67 | 50.29 | 33.46 | 76.75 | 45.20 |
2019-10-18 | 16.51 | 26.96 | 18.79 | 3.19 | 5.06 | 9.29 | 3.54 | 4.65 | 3.44 | 9.67 | ... | 90.99 | 50.28 | 82.80 | 162.00 | 32.69 | 40.08 | 51.19 | 33.83 | 79.38 | 45.93 |
2019-10-21 | 16.89 | 27.64 | 18.85 | 3.23 | 5.07 | 9.36 | 3.55 | 4.60 | 3.50 | 9.53 | ... | 88.49 | 47.51 | 78.00 | 165.03 | 31.09 | 38.22 | 50.00 | 32.56 | 77.93 | 44.04 |
2019-10-22 | 16.42 | 27.36 | 18.85 | 3.20 | 5.11 | 9.23 | 3.59 | 4.64 | 3.52 | 9.70 | ... | 90.05 | 47.37 | 78.18 | 164.50 | 31.32 | 38.55 | 52.29 | 33.05 | 73.58 | 44.67 |
2019-10-23 | 16.45 | 26.78 | 18.87 | 3.17 | 5.09 | 9.18 | 3.52 | 4.66 | 3.66 | 9.50 | ... | 93.49 | 50.27 | 82.68 | 160.00 | 33.50 | 41.68 | 54.78 | 36.00 | 81.00 | 47.18 |
2019-10-24 | 16.87 | 26.66 | 18.60 | 3.16 | 5.09 | 8.93 | 3.53 | 4.74 | 3.59 | 9.46 | ... | 91.64 | 51.31 | 79.05 | 155.70 | 33.70 | 41.45 | 52.88 | 35.53 | 76.52 | 46.25 |
2019-10-25 | 16.88 | 26.97 | 18.44 | 3.19 | 5.12 | 9.05 | 3.56 | 4.80 | 3.55 | 9.57 | ... | 87.70 | 48.40 | 76.10 | 154.10 | 32.58 | 39.90 | 51.60 | 34.68 | 71.48 | 47.69 |
2019-10-28 | 16.66 | 26.88 | 18.66 | 3.27 | 5.19 | 9.08 | 3.58 | 4.79 | 3.56 | 9.64 | ... | 88.45 | 49.21 | 76.88 | 163.11 | 33.08 | 41.07 | 53.06 | 35.09 | 71.50 | 49.29 |
2019-10-29 | 16.91 | 26.67 | 18.30 | 3.21 | 5.14 | 9.05 | 3.52 | 4.66 | 3.55 | 9.46 | ... | 85.27 | 47.68 | 71.60 | 167.38 | 31.94 | 39.40 | 50.31 | 33.82 | 68.40 | 48.02 |
2019-10-30 | 16.43 | 26.65 | 17.57 | 3.09 | 5.22 | 8.99 | 3.45 | 4.61 | 3.55 | 9.42 | ... | 83.80 | 46.80 | 68.55 | 168.88 | 30.80 | 39.29 | 47.00 | 32.68 | 64.31 | 47.06 |
2019-10-31 | 16.26 | 26.53 | 18.19 | 3.02 | 5.23 | 8.95 | 3.43 | 4.56 | 3.54 | 9.06 | ... | 79.02 | 43.71 | 63.56 | 167.59 | 29.08 | 37.99 | 45.20 | 30.95 | 59.20 | 44.22 |
7057 rows × 3660 columns
In [23]:
range_week = pd.date_range('2014-01-01', '2018-12-31', freq='W-FRI')
stocks = DataFrame(stocks, index = range_week, columns = ['000001.SZ','000002.SZ','000004.SZ','000005.SZ','000006.SZ',
'000007.SZ','000008.SZ','000009.SZ','000010.SZ','000011.SZ'])
logp_week=stocks.applymap(log)
LogReturnWeek=logp_week-logp_week.shift(1)
LogReturnWeek = LogReturnWeek.fillna(method = 'ffill')
LogReturnWeek
Out[23]:
000001.SZ | 000002.SZ | 000004.SZ | 000005.SZ | 000006.SZ | 000007.SZ | 000008.SZ | 000009.SZ | 000010.SZ | 000011.SZ | |
---|---|---|---|---|---|---|---|---|---|---|
2014-01-03 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
2014-01-10 | -0.009263 | -0.060465 | -0.041528 | -0.062914 | -0.135648 | 0.010657 | -0.096093 | -0.049282 | -0.108803 | -0.106233 |
2014-01-17 | -0.029187 | -0.041500 | 0.020980 | 0.000000 | 0.028642 | -0.031589 | 0.017074 | -0.004603 | -0.024729 | 0.002907 |
2014-01-24 | 0.015558 | 0.095567 | 0.052244 | 0.034046 | 0.068208 | -0.003654 | 0.047234 | 0.064755 | 0.026164 | 0.056432 |
2014-01-31 | 0.015558 | 0.095567 | 0.052244 | 0.034046 | 0.068208 | -0.003654 | 0.047234 | 0.064755 | 0.026164 | 0.056432 |
2014-02-07 | 0.015558 | 0.095567 | 0.052244 | 0.034046 | 0.068208 | -0.003654 | 0.047234 | 0.064755 | 0.026164 | 0.056432 |
2014-02-14 | 0.059694 | -0.005413 | 0.065751 | 0.045557 | 0.035559 | -0.020218 | 0.033902 | 0.136531 | 0.125163 | 0.027361 |
2014-02-21 | -0.040546 | -0.028908 | -0.009897 | -0.016327 | 0.002181 | 0.049438 | 0.092275 | 0.000000 | -0.024067 | -0.039923 |
2014-02-28 | -0.041361 | -0.063422 | 0.075855 | -0.033476 | -0.065265 | -0.072393 | -0.026696 | 0.018962 | -0.067620 | -0.040120 |
2014-03-07 | -0.030098 | 0.078668 | -0.006403 | 0.004246 | 0.025260 | -0.033048 | 0.016512 | 0.004462 | -0.005502 | 0.013072 |
2014-03-14 | -0.045462 | 0.035139 | -0.101324 | -0.004246 | -0.011403 | 0.007174 | -0.006160 | -0.023424 | -0.013889 | -0.002890 |
2014-03-21 | 0.044536 | 0.061796 | 0.014896 | 0.029352 | 0.044851 | -0.025746 | 0.054121 | -0.000912 | 0.005579 | 0.057641 |
2014-03-28 | -0.000927 | 0.013639 | 0.012374 | 0.090792 | 0.178293 | -0.011476 | -0.063433 | -0.036232 | -0.135321 | 0.117176 |
2014-04-04 | 0.000927 | -0.001232 | -0.012374 | -0.062277 | -0.046957 | 0.016354 | 0.011370 | 0.013158 | 0.020489 | -0.062677 |
2014-04-11 | 0.054994 | -0.019926 | 0.047863 | -0.004024 | -0.013553 | 0.028779 | 0.023363 | 0.044736 | -0.007831 | -0.023561 |
2014-04-18 | -0.054067 | -0.021616 | 0.006654 | -0.016261 | 0.000000 | 0.008631 | -0.029549 | -0.033594 | -0.040102 | -0.022774 |
2014-04-25 | 0.040822 | 0.020357 | -0.013353 | -0.067823 | -0.039767 | -0.043103 | 0.009264 | -0.047268 | -0.146001 | -0.057158 |
2014-05-02 | 0.040822 | 0.020357 | -0.013353 | -0.067823 | -0.039767 | -0.043103 | 0.009264 | -0.047268 | -0.146001 | -0.057158 |
2014-05-09 | 0.040822 | 0.020357 | -0.013353 | -0.067823 | -0.039767 | -0.043103 | 0.009264 | -0.047268 | -0.146001 | -0.057158 |
2014-05-16 | 0.019714 | 0.058841 | 0.010330 | 0.004435 | 0.010764 | -0.018788 | -0.110348 | -0.001004 | -0.001867 | 0.006015 |
2014-05-23 | 0.027135 | 0.056798 | 0.026523 | 0.004415 | 0.050115 | -0.005186 | -0.032753 | 0.008000 | 0.025832 | 0.026629 |
2014-05-30 | -0.006932 | 0.047913 | 0.012242 | -0.008850 | -0.041586 | -0.002603 | 0.004582 | 0.016791 | -0.046607 | 0.005822 |
2014-06-06 | -0.010490 | -0.035718 | 0.026268 | -0.013423 | 0.010560 | 0.000000 | -0.013809 | 0.000000 | 0.022642 | -0.017570 |
2014-06-13 | -0.117344 | 0.021584 | -0.009676 | 0.013423 | -0.034191 | 0.000000 | 0.003470 | 0.000000 | 0.016652 | 0.020469 |
2014-06-20 | -0.027042 | -0.036236 | -0.013554 | -0.022473 | -0.039917 | 0.000000 | -0.049714 | 0.000000 | 0.055322 | -0.062707 |
2014-06-27 | -0.003050 | 0.023101 | 0.050990 | 0.026907 | 0.042088 | 0.000000 | 0.019231 | 0.000000 | 0.084839 | 0.031845 |
2014-07-04 | 0.009123 | 0.017868 | 0.022089 | 0.055929 | 0.023581 | 0.000000 | 0.000000 | 0.000000 | 0.078201 | 0.013343 |
2014-07-11 | -0.024516 | 0.039357 | 0.001408 | -0.034046 | -0.008511 | 0.000000 | 0.000000 | -0.185657 | 0.002946 | -0.013343 |
2014-07-18 | -0.007265 | 0.009040 | -0.009901 | 0.025642 | 0.041847 | 0.000000 | 0.000000 | 0.000000 | 0.000000 | 0.027964 |
2014-07-25 | 0.062583 | 0.098475 | 0.005670 | 0.016737 | 0.014242 | 0.000000 | 0.000000 | 0.000000 | -0.093988 | 0.007231 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
2018-06-08 | -0.006893 | 0.026941 | -0.040537 | -0.032499 | -0.010197 | 0.036905 | -0.123145 | -0.008857 | 0.087011 | -0.028703 |
2018-06-15 | 0.004929 | 0.046809 | -0.057746 | -0.068350 | 0.008746 | -0.010197 | 0.000000 | -0.039933 | 0.059992 | 0.029447 |
2018-06-22 | -0.031971 | 0.003922 | -0.042187 | -0.059620 | -0.139980 | 0.000000 | 0.000000 | -0.109484 | 0.015076 | -0.163745 |
2018-06-29 | -0.080297 | -0.133023 | 0.029513 | 0.010187 | -0.021942 | -0.019964 | 0.000000 | 0.012320 | -0.025254 | -0.030251 |
2018-07-06 | -0.048460 | -0.058163 | -0.028988 | -0.030877 | -0.096676 | 0.001493 | 0.000000 | -0.037426 | 0.012707 | -0.084797 |
2018-07-13 | 0.025087 | 0.021735 | -0.015348 | 0.024098 | 0.036905 | 0.064259 | 0.000000 | 0.043530 | 0.160623 | 0.010758 |
2018-07-20 | 0.025571 | -0.024756 | -0.045835 | 0.033448 | -0.001813 | -0.062024 | 0.000000 | 0.022067 | -0.043963 | -0.019647 |
2018-07-27 | 0.015251 | 0.007749 | 0.030247 | 0.051293 | 0.091839 | 0.034373 | 0.000000 | 0.003960 | 0.017818 | 0.141314 |
2018-08-03 | -0.037449 | -0.100987 | 0.037219 | -0.074589 | -0.095476 | -0.071512 | 0.000000 | -0.075934 | -0.024582 | -0.058527 |
2018-08-10 | 0.035285 | 0.094965 | -0.047564 | 0.036368 | 0.053204 | 0.028921 | -0.227690 | 0.029414 | 0.026787 | 0.037639 |
2018-08-17 | -0.046572 | -0.021366 | 0.012510 | -0.049927 | -0.073444 | -0.031241 | -0.084483 | -0.055333 | -0.024527 | -0.047735 |
2018-08-24 | 0.129693 | 0.003959 | -0.021859 | 0.013560 | 0.023876 | -0.010113 | 0.061435 | 0.010882 | 0.026728 | 0.093295 |
2018-08-31 | 0.009921 | 0.054252 | -0.040592 | -0.003373 | -0.018316 | -0.044772 | 0.010309 | 0.000000 | -0.008830 | -0.028988 |
2018-09-07 | -0.011917 | -0.035552 | -0.027415 | 0.003373 | -0.035752 | 0.008954 | 0.192949 | -0.008696 | 0.095109 | -0.021864 |
2018-09-14 | -0.017129 | 0.004728 | -0.065367 | -0.003373 | -0.019343 | -0.001622 | -0.036603 | 0.004357 | 0.004024 | 0.015790 |
2018-09-21 | 0.080980 | 0.101063 | 0.016902 | 0.036488 | 0.155266 | -0.032168 | 0.002191 | 0.023632 | -0.012121 | 0.058318 |
2018-09-28 | 0.034994 | -0.059898 | -0.013121 | -0.043268 | -0.047954 | -0.421094 | -0.037909 | -0.014974 | -0.020535 | -0.047068 |
2018-10-05 | 0.034994 | -0.059898 | -0.013121 | -0.043268 | -0.047954 | -0.421094 | -0.037909 | -0.014974 | -0.020535 | -0.047068 |
2018-10-12 | 0.034994 | -0.059898 | -0.013121 | -0.043268 | -0.047954 | -0.421094 | -0.037909 | -0.014974 | -0.020535 | -0.047068 |
2018-10-19 | 0.043692 | -0.011070 | 0.000000 | -0.003839 | 0.010299 | 0.191506 | -0.010152 | -0.046884 | -0.060484 | -0.075676 |
2018-10-26 | 0.038291 | 0.099266 | 0.000000 | 0.048790 | 0.044095 | 0.095310 | 0.080827 | 0.074489 | 0.055959 | 0.031857 |
2018-11-02 | -0.008083 | 0.033453 | -0.008844 | 0.010929 | 0.007813 | -0.071315 | 0.027844 | 0.024451 | -0.006826 | 0.014453 |
2018-11-09 | -0.049918 | -0.044433 | 0.026301 | 0.039081 | -0.021633 | 0.016598 | 0.013637 | 0.035591 | -0.027780 | -0.012216 |
2018-11-16 | 0.001894 | 0.050911 | 0.069817 | 0.093090 | 0.092958 | 0.113965 | -0.050937 | 0.252091 | 0.063658 | 0.113927 |
2018-11-23 | -0.023936 | 0.004831 | -0.050838 | -0.082692 | -0.046347 | 0.161294 | -0.038746 | -0.141847 | -0.031322 | -0.025241 |
2018-11-30 | 0.003868 | 0.021455 | -0.016509 | 0.033902 | 0.022515 | -0.294651 | -0.074298 | -0.004184 | -0.053689 | -0.002047 |
2018-12-07 | -0.007752 | -0.003939 | 0.006759 | 0.006645 | 0.057674 | 0.112329 | 0.041673 | 0.002094 | -0.069507 | 0.080673 |
2018-12-14 | -0.010758 | 0.043250 | 0.111709 | -0.026847 | 0.001750 | 0.012423 | 0.012674 | -0.038384 | -0.094282 | 0.009407 |
2018-12-21 | -0.073427 | -0.087218 | -0.115390 | -0.038133 | -0.059423 | 0.032790 | -0.030693 | -0.019759 | -0.019972 | -0.085990 |
2018-12-28 | -0.007435 | -0.017891 | -0.014861 | -0.054460 | -0.039740 | -0.037740 | 0.010336 | -0.045359 | -0.062427 | -0.065355 |
261 rows × 10 columns
In [24]:
# 随机取两支股票
from random import randint
columns = ['000001.SZ','000002.SZ','000004.SZ','000005.SZ','000006.SZ','000007.SZ','000008.SZ','000009.SZ','000010.SZ','000011.SZ']
x = randint(0,len(columns)-1)
random_stock1 = columns[x]
columns.pop(x)
y = randint(0,len(columns)-1)
random_stock2 = columns[y]
columns.pop(y)
Out[24]:
'000009.SZ'
In [25]:
New_Logreturn = DataFrame(LogReturnWeek,columns = [random_stock1,random_stock2])
New_Logreturn.head()
Out[25]:
000004.SZ | 000009.SZ | |
---|---|---|
2014-01-03 | NaN | NaN |
2014-01-10 | -0.041528 | -0.049282 |
2014-01-17 | 0.020980 | -0.004603 |
2014-01-24 | 0.052244 | 0.064755 |
2014-01-31 | 0.052244 | 0.064755 |
In [26]:
New_Logreturn_describe = New_Logreturn.describe()
New_Logreturn_describe
Out[26]:
000004.SZ | 000009.SZ | |
---|---|---|
count | 260.000000 | 260.000000 |
mean | 0.001880 | -0.002791 |
std | 0.066844 | 0.069152 |
min | -0.210721 | -0.347465 |
25% | -0.026044 | -0.035235 |
50% | 0.000000 | 0.000000 |
75% | 0.031177 | 0.027181 |
max | 0.234152 | 0.309941 |
In [27]:
New_Logreturn_cov = New_Logreturn.cov()
New_Logreturn_cov
Out[27]:
000004.SZ | 000009.SZ | |
---|---|---|
000004.SZ | 0.004468 | 0.001366 |
000009.SZ | 0.001366 | 0.004782 |
In [28]:
mu1=New_Logreturn_describe.iloc[1][0]
mu2=New_Logreturn_describe.iloc[1][1]
sig1=New_Logreturn_describe.iloc[2][0]
sig2=New_Logreturn_describe.iloc[2][1]
rho12=New_Logreturn_cov.iloc[0][1]
ExpRet=mu2+(mu1-mu2)*w
Varf=sy.simplify(sig1**2*w**2+sig2**2*(1-w)**2+2*w*(1-w)*rho12*sig1*sig2)
StdRet=sy.sqrt(Varf)
ExpRet,sy.simplify(StdRet)
Out[28]:
(0.00467094443288054*w - 0.00279094790037867, sqrt(0.00923745156044764*w**2 - 0.00955123437010399*w + 0.00478193065259381))
In [29]:
meanf= sy.lambdify(w, ExpRet, "numpy")
varf=sy.lambdify(w,Varf,"numpy")
%matplotlib inline
fig=plt.figure(figsize=(8,6))
plt.text(sig1+0.002,mu1-0.001,"$R_1$")
plt.text(sig2+0.002,mu2-0.001,"$R_2$")
wmin=float(-Varf.coeff(w,1)/(2*Varf.coeff(w,2)))
mx=sy.sqrt(varf(wmin));my=meanf(wmin)
plt.plot(mx,my,"bo")
plt.text(mx+0.005,my,"minimum variance portfolio")
wv=np.linspace(0,wmin,100)
stdv=np.sqrt(varf(wv))
meanv=meanf(wv)
plt.plot(stdv,meanv,"r-")
plt.annotate("$w\in$"+str([0,wmin]), size=20,xy=(stdv[50]+0.002,meanv[50]),
xytext=(stdv[50]+0.05,meanv[50]),
arrowprops=dict(facecolor="red"))
plt.xlim([0,0.09])
plt.ylim([-0.006,0.01])
plt.xlabel(r"$\sigma_r$")
plt.ylabel(r"$E(R)$")
wv=np.linspace(wmin,1,100)
stdv=np.sqrt(varf(wv))
meanv=meanf(wv)
plt.plot(stdv,meanv,"b-")
plt.annotate("efficient frontier\n"+"$w\in$"+str([wmin,1]), size=20,xy=(stdv[50]+0.002,meanv[50]),
xytext=(stdv[50]+0.05,meanv[50]),
arrowprops=dict(facecolor="blue"))
print("由于随机取2只股票,不知道图出现在哪儿,所以把坐标系设大一点")
由于随机取2只股票,不知道图出现在哪儿,所以把坐标系设大一点
- 逐步随机添加一只股票,每添加一只股票后重新计算股票有效前沿,最小方差组合,以及切线组合的sharp比。利用上述结果分析股票数量增加对投资组合的影响。
In [30]:
# 为了方便按columns里的股票循序依次添加
columns
Out[30]:
['000001.SZ', '000002.SZ', '000005.SZ', '000006.SZ', '000007.SZ', '000008.SZ', '000010.SZ', '000011.SZ']
Categories :
Practice