# 资产瞎配模型（二）：对瞎配（一）中净值计算错误的纠正

00

01

02

03

def EqualWeight(datas,period):
ret = datas.pct_change(1).fillna(0)
data_norm = datas/datas.iloc[0,]*1000
result = data_norm.copy()
result['m'] = result.index
result['m'] = result.m.apply(lambda x:x.month)
weights = pd.DataFrame(columns = datas.columns,index = datas.index).fillna(0)
N = pd.DataFrame(columns = datas.columns,index = datas.index).fillna(0)
if period == 'month':
for i in range(result.shape[0]):
if i == 0:
weights.iloc[i,:] = 1/datas.shape[1]
price = datas.loc[datas.index[i],:]
n = weights.iloc[i,:].values/price.values
N.loc[result.index[i],:] = n
elif result.m[i] != result.m[i - 1]:
weights.iloc[i,:] = 1/datas.shape[1]
price = datas.loc[datas.index[i],:]
n = weights.iloc[i,:].values/price.values
N.loc[result.index[i],:] = n
else:
N.iloc[i,:] = N.iloc[i-1,:]
weights.iloc[i,:] = N.iloc[i,:]*datas.loc[datas.index[i],:]

elif period == '6month':
for i in range(result.shape[0]):
if i == 0:
weights.iloc[i,:] = 1/datas.shape[1]
price = datas.loc[datas.index[i],:]
n = weights.iloc[i,:].values/price.values
N.loc[result.index[i],:] = n

elif (result.m[i] != result.m[i - 1] and  result.m[i]%6==0) :
weights.iloc[i,:] = 1/datas.shape[1]
price = datas.loc[datas.index[i],:]
n = weights.iloc[i,:].values/price.values
N.loc[result.index[i],:] = n
else:
N.iloc[i,:] = N.iloc[i-1,:]
weights.iloc[i,:] = N.iloc[i,:]*datas.loc[datas.index[i],:]

elif period == 'year':
for i in range(result.shape[0]):
if i == 0 :
weights.iloc[i,:] = 1/datas.shape[1]
price = datas.loc[datas.index[i],:]
n = weights.iloc[i,:].values/price.values
N.loc[result.index[i],:] = n
elif (result.m[i] != result.m[i - 1] and  result.m[i]%12==0) :
weights.iloc[i,:] = 1/datas.shape[1]
price = datas.loc[datas.index[i],:]
n = weights.iloc[i,:].values/price.values
N.loc[result.index[i],:] = n
else:
N.iloc[i,:] = N.iloc[i-1,:]
weights.iloc[i,:] = N.iloc[i,:]*datas.loc[datas.index[i],:]

else:
return '请输入调仓周期'

result['mv'] = 0
result['nav'] = 1
for i in range(result.shape[0]):
result.loc[result.index[i],'mv'] = (datas.iloc[i,:]*N.iloc[i,:]).sum()
if i == 0:
pass
elif all(weights.iloc[i,:] == weights.iloc[i-1,:]):

else:

result['nav'] = result.nav/result.nav[0]*1000

return weights,result

04

05

def EqualVolWeight(datas,period ='month'):
ret = datas.pct_change(1).fillna(0)
data_norm = datas/datas.iloc[0,]*1000
result = data_norm.copy()
result['m'] = result.index
result['m'] = result.m.apply(lambda x:x.month)
weights = pd.DataFrame(columns = datas.columns,index = datas.index).fillna(0)
N = pd.DataFrame(columns = datas.columns,index = datas.index).fillna(0)

if period == 'month':
for i in range(result.shape[0]):
if i == 0:
pass
elif result.m[i] != result.m[i - 1]:
vol = ret.iloc[:i].std()
weights.iloc[i,:] = (1/vol)/((1/vol).sum())

price = datas.loc[datas.index[i],:]
V = (weights.iloc[i,:]*price).sum()
n = V*weights.iloc[i,:].values/price.values
N.loc[result.index[i],:] = n

else:
N.iloc[i,:] = N.iloc[i-1,:]
w = N.iloc[i,:]*datas.loc[datas.index[i],:]

weights.iloc[i,:] = w/w.sum()

elif period == '6month':
for i in range(result.shape[0]):
if i == 0:
pass
elif (result.m[i] != result.m[i - 1] and  result.m[i]%6==0) :
vol = ret.iloc[:i].std()
weights.iloc[i,:] = (1/vol)/((1/vol).sum())
price = datas.loc[datas.index[i],:]
V = (weights.iloc[i,:]*price).sum()
n = V*weights.iloc[i,:].values/price.values
N.loc[result.index[i],:] = n/n.sum()
else:
N.iloc[i,:] = N.iloc[i-1,:]
w = N.iloc[i,:]*datas.loc[datas.index[i],:]

weights.iloc[i,:] = w/w.sum()

elif period == 'year':
for i in range(result.shape[0]):
if i == 0:
pass
elif (result.m[i] != result.m[i - 1] and  result.m[i]%12==0) :
vol = ret.iloc[:i].std()
weights.iloc[i,:] = (1/vol)/((1/vol).sum())
price = datas.loc[datas.index[i],:]
V = (weights.iloc[i,:]*price).sum()
n = V*weights.iloc[i,:].values/price.values
N.loc[result.index[i],:] = n
else:
N.iloc[i,:] = N.iloc[i-1,:]
w = N.iloc[i,:]*datas.loc[datas.index[i],:]

weights.iloc[i,:] = w/w.sum()

else:
return '请输入调仓周期'

result['mv'] = 0
result['nav'] = 1
for i in range(result.shape[0]):
result.loc[result.index[i],'mv'] = (datas.iloc[i,:]*N.iloc[i,:]).sum()
if all(N.iloc[i,:]==0):
pass
elif all(N.iloc[i,:] == N.iloc[i-1,:] ):

else:

result['nav'] = result.nav/result.nav[0]*1000

return weights,result

06

GMV

sigma = ret.iloc[:i].cov()
weight = np.dot(np.mat(sigma).I,np.ones([sigma.shape[1],1]))
weights.iloc[i,:] =  np.array(weight/(weight.sum())).T[0]

07

GMO+卖空限制

def funs(weight,sigma):
weight = np.array([weight]).T
result = np.dot(np.dot(weight.T,np.mat(sigma)),weight)[0,0]
return(result)

res =  minimize(funs,weight, method='SLSQP',args = (sigma,),
bounds=bnds,constraints=cons,tol=1e-8)
weights.iloc[i,:] =  res.x

08

Risk Parity

def funsRP(weight,sigma):
weight = np.array([weight]).T
X = np.multiply(weight,np.dot(sigma.values,weight))
result = np.square(np.dot(X,np.ones([1,X.shape[0]])) - X.T).sum()
return(result)

A股大概是怎么都不愿意配一点了，为了避免单个资产权重过高或者过低的问题，对资产权重加以限制

09

Risk Parity + w<=40%

10

Risk Parity + w>=10%

11

12

def performance(datas):
nav = (datas.nav[datas.shape[0]-1]/1000)**(1/12) - 1
vol = (datas.nav.pct_change(1)).std()*np.sqrt(250)
Sharp = nav/vol
return nav,vol,Sharp

• 等波动率优于RP优于GMO优于等权重。如果看收益和波动率，等波动率下的年化收益是所有方法里最低的，但波动率也是最小的，小一个数量级。因为资产中有货币这一基本没有波动的资产，导致等波动率情况下货币占了80%以上的比例。除过等波动率的情况看，风险平价要更优一些。
• 对于半年度和年度再平衡的策略，刚开始不满六个月/十二个月的时候，我所有的权重都设置的0，导致这段时间这些策略收益一直是0，其实至少可以全配货币或者按无风险利率累积。

13

1. Bodnar T, Schmid W. A test for the weights of the global minimum variance portfolio in an elliptical model[J]. Metrika, 2008, 67(2):127.
2. 20160725-华泰证券-风险平价模型实证研究：风险平价模型在大类资产配置及行业配置中的应用
3. 20180309-华宝证券-华宝证券金融工程专题报告：资产配置的流程、框架与运用
4. 20180928-东北证券-东北证券大类资产配臵“全解析”专题研究之一：：风险平价性质探究
5. 20181114-爱建证券-爱建证券量化资产配置系列报告：从不同维度和角度探索风险平价资产配置方法的稳定性

0 条评论

• ### 资产瞎配模型（一）

大类资产配置是量化中一个重要的领域，本文尝试实现若干资产配置模型。全文纯属瞎配，欢迎指正！

• ### 研报复制（四）：基于Logistic回归的大小盘轮动

一种是从技术面出发，基于量价指标建立模型，典型的比如上篇复制的基于相对强弱指标的大小盘轮动策略。

• ### 【PAT乙级】旧键盘打字

版权声明：本文为博主原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接和本声明。 ...

• ### python列表与元组的用法

7.列表生成式   #[i*i for i in range(10)]       [i*i for i in range(10) if i>5]

• ### <进击的虫师>如何让程序"懂很多"?

? 最近在做一个有意思的小项目, 在一个聊天对话中, 你向电脑提出问题, 他会自动分词,然后根据关键字, 自动答复你 对所有的关键字做出解释, 工作量实在...

• ### Python版组合数计算方法优化思路和源码

总体说明：本文的优化思路并不局限于Python，但C、C++、C#、Java等语言无法使用内置类型直接表示大整数，需要通过数组等特定形式并自己实现大整数乘除法才...

• ### Python:os.path.join()产生的斜杠在Windows和Linux下的不同表现和解决方法

在Linux/macOS下会显示 a/b/c 而在Windows下会显示 a\b\c