如何解决Python3如何在项目组合管理中合并Pandas Dataframe
我想创建一个将资产A和资产B的权重的数据框架(concat ??)组合在一起的数据框架,并计算投资组合的收益和方差到与“权重数据框架”合并的新数据框架中。 >
到目前为止,我已经完成了以下部分:
输入
meanA = 0.013
meanB = 0.021
stdA = 0.11
stdB = 0.091
covar = 0.007
A_weights = -0.01
A_n = 1
B_weights = 1.01
B_n = 0
counter = 0.01
weighted_A = []
weighted_B = []
portfolio_returns = []
portfolio_variance = []
while A_weights < A_n:
GS_weights += counter
weighted_A.append(A_weights)
print('\n')
while B_weights > B_n:
B_weights -= counter
weighted_B.append(B_weights)
weighted_df = pd.DataFrame({'Weights_A':weighted_A,'Weights_B':weighted_B})
print (weighted_df.head())
print('\n')
for numbers in weighted_df:
portfolio_returns.append(weighted_A[0] * meanA + weighted_B[0] * meanB)
print('the portfolio returns are:')
print(portfolio_returns)
for numbers2 in weighted_df:
portfolio_variance.append(weighted_A[0]**2 * stdA**2 + weighted_B[0]**2 * stdB**2 +(2*weighted_A[0]*weighted_B[0]*stdA*stdB*covar))
print('the portfolio variance are:')
print(portfolio_variance)
输出
Weights_A Weights_B
0 0.00 1.00
1 0.01 0.99
2 0.02 0.98
3 0.03 0.97
4 0.04 0.96
the portfolio returns are:
[0.001933126999985676,0.001933126999985676]
the portfolio variance are:
[0.0003083897052577906,0.0003083897052577906]
我该如何编码以得到如下结果:
Weights_A Weights_B Port. Return Port. Variance
0 0.00 1.00 0.00193 0.000308
1 0.01 0.99 ... ...
2 0.02 0.98 ... ...
3 0.03 0.97 ... ...
4 0.04 0.96
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。