Python中N个资产组合的投资组合差异(Portfolio Variance of a Portfolio of N Assets in Python)

编程入门 行业动态 更新时间:2024-10-27 22:33:57
Python中N个资产组合的投资组合差异(Portfolio Variance of a Portfolio of N Assets in Python)

投资组合差异计算如下:

port_var = W'_p * S * W_p

对于N组合的投资组合在哪里

W'_p = transpose of vector of weights of stocks in portfolios S = sample covariance matrix W_p = vector of weights of stocks in portfolios

我有以下numpy矩阵。

投资组合中股票权重的数组(向量)(有10只股票):

weights = np.array( [[ 0.09], [ 0.05], [ 0.15], [ 0.10], [ 0.15], [ 0.15], [ 0.08], [ 0.08], [ 0.1 ], [ 0.05]])

股票收益的协方差矩阵:

covar = np.array([[ 0.00154474 0.00079555 0.00099691 0.00052596 0.0005363 0.00062005 0.00064031 0.00037494 0.00018826 0.00132809], [ 0.00079555 0.00287429 0.00058536 0.00091774 0.00046885 0.00110434 0.00137141 0.00046724 0.00030414 0.0016615 ], [ 0.00099691 0.00058536 0.00155757 0.00056336 0.00052395 0.00060104 0.00057223 0.00021365 0.00017057 0.00130247], [ 0.00052596 0.00091774 0.00056336 0.00126312 0.00031941 0.00088137 0.00024493 0.00025136 0.00011519 0.00135475], [ 0.0005363 0.00046885 0.00052395 0.00031941 0.00054093 0.00045649 0.00042927 0.00021928 0.00016835 0.00093471], [ 0.00062005 0.00110434 0.00060104 0.00088137 0.00045649 0.00133081 0.00060353 0.0003967 0.00024983 0.00168281], [ 0.00064031 0.00137141 0.00057223 0.00024493 0.00042927 0.00060353 0.00468731 0.00059557 0.00020384 0.00078669], [ 0.00037494 0.00046724 0.00021365 0.00025136 0.00021928 0.0003967 0.00059557 0.00082333 0.00017191 0.00066816], [ 0.00018826 0.00030414 0.00017057 0.00011519 0.00016835 0.00024983 0.00020384 0.00017191 0.00036348 0.0004505 ], [ 0.00132809 0.0016615 0.00130247 0.00135475 0.00093471 0.00168281 0.00078669 0.00066816 0.0004505 0.00530036]])

当我计算

weights.T * covar * weights

结果是一个与covar大小相同的数组。 我是投资组合理论的新手,但我认为投资组合的方差应该是标量(单一值)。

有没有人有这方面的经验可能有帮助?

Portfolio variance is calculated as:

port_var = W'_p * S * W_p

for a portfolio with N assest where

W'_p = transpose of vector of weights of stocks in portfolios S = sample covariance matrix W_p = vector of weights of stocks in portfolios

I have the following numpy matrixes.

Array (vector) of weights of stocks in the portfolio (there are 10 stocks):

weights = np.array( [[ 0.09], [ 0.05], [ 0.15], [ 0.10], [ 0.15], [ 0.15], [ 0.08], [ 0.08], [ 0.1 ], [ 0.05]])

Covariance matrix of stock returns:

covar = np.array([[ 0.00154474 0.00079555 0.00099691 0.00052596 0.0005363 0.00062005 0.00064031 0.00037494 0.00018826 0.00132809], [ 0.00079555 0.00287429 0.00058536 0.00091774 0.00046885 0.00110434 0.00137141 0.00046724 0.00030414 0.0016615 ], [ 0.00099691 0.00058536 0.00155757 0.00056336 0.00052395 0.00060104 0.00057223 0.00021365 0.00017057 0.00130247], [ 0.00052596 0.00091774 0.00056336 0.00126312 0.00031941 0.00088137 0.00024493 0.00025136 0.00011519 0.00135475], [ 0.0005363 0.00046885 0.00052395 0.00031941 0.00054093 0.00045649 0.00042927 0.00021928 0.00016835 0.00093471], [ 0.00062005 0.00110434 0.00060104 0.00088137 0.00045649 0.00133081 0.00060353 0.0003967 0.00024983 0.00168281], [ 0.00064031 0.00137141 0.00057223 0.00024493 0.00042927 0.00060353 0.00468731 0.00059557 0.00020384 0.00078669], [ 0.00037494 0.00046724 0.00021365 0.00025136 0.00021928 0.0003967 0.00059557 0.00082333 0.00017191 0.00066816], [ 0.00018826 0.00030414 0.00017057 0.00011519 0.00016835 0.00024983 0.00020384 0.00017191 0.00036348 0.0004505 ], [ 0.00132809 0.0016615 0.00130247 0.00135475 0.00093471 0.00168281 0.00078669 0.00066816 0.0004505 0.00530036]])

When I compute

weights.T * covar * weights

The result is an array the same size as covar. I am new to portfolio theory but I would imagine that the variance of the portfolio should be a scalar (single value).

Does anyone have experience with this that might help?

最满意答案

np.dot(weights.T,np.dot(covar,weights)) # array([[ 0.00064654]])

对于2D numpy数组, np.dot等效于矩阵乘法。

对于使用1D数组编写的2D数组, np.dot等效于矩阵向量乘法。

对于1D阵列, np.dot相当于内部产品。

对于numpy数组, *执行逐元素乘法(如果需要,可以进行广播 )。


weights.T*np.matrix(covar)*weights #matrix([[ 0.00064654]])

或者,如果将np.matrix转换为np.matrix ,则*等效于矩阵乘法。

np.dot(weights.T,np.dot(covar,weights)) # array([[ 0.00064654]])

For 2D numpy arrays, np.dot is equivalent to matrix multiplication.

For a 2D array np.dotted with a 1D array, np.dot is equivalent to matrix-vector multiplication.

For 1D arrays, np.dot is equivalent to the inner product.

For numpy arrays, the * performs element-wise multiplication (with broadcasting if necessary).


weights.T*np.matrix(covar)*weights #matrix([[ 0.00064654]])

Alternatively, if you convert covar to a np.matrix, then * is equivalent to matrix multiplication.

更多推荐

本文发布于:2023-07-27 15:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292448.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   投资组合   差异   资产   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!