下面的函数计算多变量时间序列的时间间隔(t0,t1)
的比例矩阵(协方差矩阵)。我想重写这个函数,以便它不需要列表或for循环。有没有办法只使用numpy数组操作执行以下操作?似乎我需要一个numpy.outer
版本接受2d数组作为输入,然后沿指定轴获取外部产品。但我无法在numpy中找到这样的功能。
import numpy as np
def scale_matrix(multivariate_time_series, t0=0, t1=0):
# multivariate_time_series is a 2d array.
if t1==0:
t1 = len(multivariate_time_series)
a = np.mean([np.outer(multivariate_time_series[t,:],multivariate_time_series[t,:])
for t in range(t0,t1)], axis=0)
return a
答案 0 :(得分:1)
您可以使用矩阵乘法或einsum
:
>>> data = np.random.random((20, 5))
>>> t0 = t1 = 0
>>> data_r = data[t0:t1 or len(data)]
>>>
>>> data_r.T@data_r/data_r.shape[0]
array([[0.31445868, 0.15057765, 0.25087819, 0.26003647, 0.24403643],
[0.15057765, 0.32387482, 0.25741824, 0.27916451, 0.26457779],
[0.25087819, 0.25741824, 0.38244811, 0.31093482, 0.30124948],
[0.26003647, 0.27916451, 0.31093482, 0.39589237, 0.30220028],
[0.24403643, 0.26457779, 0.30124948, 0.30220028, 0.3548833 ]])
>>>
>>> np.einsum('ij,ik->jk', data_r, data_r)/data_r.shape[0]
array([[0.31445868, 0.15057765, 0.25087819, 0.26003647, 0.24403643],
[0.15057765, 0.32387482, 0.25741824, 0.27916451, 0.26457779],
[0.25087819, 0.25741824, 0.38244811, 0.31093482, 0.30124948],
[0.26003647, 0.27916451, 0.31093482, 0.39589237, 0.30220028],
[0.24403643, 0.26457779, 0.30124948, 0.30220028, 0.3548833 ]])
>>>
>>> scale_matrix(data, t0, t1)
array([[0.31445868, 0.15057765, 0.25087819, 0.26003647, 0.24403643],
[0.15057765, 0.32387482, 0.25741824, 0.27916451, 0.26457779],
[0.25087819, 0.25741824, 0.38244811, 0.31093482, 0.30124948],
[0.26003647, 0.27916451, 0.31093482, 0.39589237, 0.30220028],
[0.24403643, 0.26457779, 0.30124948, 0.30220028, 0.3548833 ]])