我想计算投资组合的差异:
假设我有2个具有权重的资产:
weights = np.array([.3,.7]).reshape(1,2)
以下2个相关矩阵:
correl = np.array([[[1,.4],[.4,1]],[[1,.6],[.6,1]]])
我想做重量x correl [0]&权重x correl [1]一步
有没有办法选择(2,2,2)张量的轴到多个子矩阵?
答案 0 :(得分:0)
矩阵乘法运算符@
将多于二维的数组视为矩阵堆栈,因此您需要做的只是
>>> weights @ correl
array([[[0.58, 0.82]],
[[0.72, 0.88]]])
>>>
# one-matrix-at-a-time for reference
>>> weights @ correl[0]
array([[0.58, 0.82]])
>>> weights @ correl[1]
array([[0.72, 0.88]])