有没有人知道计算经验/样本协方差图的好方法,如果可能的话在Python中?
这是一本书的屏幕截图,其中包含对covariagram的良好定义:

如果我理解正确,对于给定的滞后/宽度h,我应该得到所有由h(或小于h)分隔的点,将其值相乘,并为每个点计算其均值在这种情况下,定义为m(x_i).但是,根据m(x_ {i})的定义,如果我想计算m(x1),我需要获得距离x1的距离h内的值的平均值.这看起来像是一个非常密集的计算.
首先,我是否正确理解了这一点?如果是这样,假设二维空间,计算这个的好方法是什么?我试图用Python编写这个代码(使用numpy和pandas),但它需要几秒钟,我甚至不确定它是否正确,这就是为什么我不会在这里发布代码.这是一个非常天真的实现的另一种尝试:
from scipy.spatial.distance import pdist, squareform
distances = squareform(pdist(np.array(coordinates))) # coordinates is a nx2 array
z = np.array(z) # z are the values
cutoff = np.max(distances)/3.0 # somewhat arbitrary cutoff
width = cutoff/15.0
widths = np.arange(0, cutoff + width, width)
Z = []
Cov = []
for w in np.arange(len(widths)-1): # for each width
# for each pairwise distance
for i in np.arange(distances.shape[0]):
for j in np.arange(distances.shape[1]):
if distances[i, j] <= widths[w+1] and distances[i, …Run Code Online (Sandbox Code Playgroud)