我对python 2.7很新,我在计算证券组合的方差和标准差方面遇到了一些麻烦。这是我到目前为止所做的:
然后我遇到了投资组合差异计算的错误。这是我使用的脚本:
# Portfolio variance calc
pfolio_var = np.dot(weightsarray.T, np.dot(sec_returns.cov() * 250, weightsarray))
pfolio_var
# Portfolio volatility
pfolio_vol = (np.dot(weightsarray.T, np.dot(sec_returns.cov() * 250, weightsarray))) ** 0.5
pfolio_vol
and here is the error I receive:
ValueError Traceback (most recent call last)
<ipython-input-30-6b33caaac89a> in <module>()
1 # Portfolio variance calc
----> 2 pfolio_var = np.dot(weightsarray.T, np.dot(sec_returns.cov() * 250, weightsarray))
3 pfolio_var
ValueError: shapes (9,9) and (1,9) not aligned: 9 (dim 1) != 1 (dim 0)
提前感谢您的帮助!
答案 0 :(得分:0)
尝试以下方法:
投资组合差异
portfolio_variance=np.dot(weights.T,np.dot(log_returns.cov()*252,weights))
投资组合波动
portfolio_vol = np.sqrt(np.dot(weights.T,np.dot(log_returns.cov()*252,weights)))