这可能导致scipy / numpy,但是现在我对任何功能感到满意,因为我在这些包中找不到任何东西。我有一个矩阵,其中包含多变量分布的数据(比方说,2,为了它的乐趣)。是否有任何计算(更高)时刻的函数?我能找到的只是numpy.mean()和numpy.cov():o
谢谢:)
/编辑:
更详细一些:我有多变量数据,即行显示变量和列观察的矩阵。现在我想用一种简单的方法计算数据的关节时刻,如http://en.wikipedia.org/wiki/Central_moment#Multivariate_moments中所定义。 我对python / scipy很新,所以我不确定我是最好的编码这个的人,特别是对于n变量的情况(注意维基百科定义是n = 2),我我预计会有一些开箱即用的东西,因为我认为这将是一个标准问题。
/ EDIT2:
仅仅为了将来,如果有人想要做类似的事情,下面的代码(仍在审查中)应该给出样本等效的原始时刻E(X ^ 2),E(Y ^ 2),它现在只适用于两个变量,但如果有需要,它应该是可扩展的。如果你看到一些错误或不洁/不熟悉的代码,请随时发表评论。
from numpy import *
# this function should return something as
# moments[0] = 1
# moments[1] = mean(X), mean(Y)
# moments[2] = 1/n*X'X, 1/n*X'Y, 1/n*Y'Y
# moments[3] = mean(X'X'X), mean(X'X'Y), mean(X'Y'Y),
# mean(Y'Y'Y)
# etc
def getRawMoments(data, moment, axis=0):
a = moment
if (axis==0):
n = float(data.shape[1])
X = matrix(data[0,:]).reshape((n,1))
Y = matrix(data[1,:]).reshape((n,1))
else:
n = float(data.shape[0])
X = matrix(data[:,0]).reshape((n,1))
Y = matrix(data[:,1]).reshape((n,11))
result = 1
Z = hstack((X,Y))
iota = ones((1,n))
moments = {}
moments[0] = 1
#first, generate huge-ass matrix containing all x-y combinations
# for every power-combination k,l such that k+l = i
# for all 0 <= i <= a
for i in arange(1,a):
if i==2:
moments[i] = moments[i-1]*Z
# if even, postmultiply with X.
elif i%2 == 1:
moments[i] = kron(moments[i-1], Z.T)
# Else, postmultiply with X.T
elif i%2==0:
temp = moments[i-1]
temp2 = temp[:,0:n]*Z
temp3 = temp[:,n:2*n]*Z
moments[i] = hstack((temp2, temp3))
# since now we have many multiple moments
# such as x**2*y and x*y*x, filter non-distinct elements
momentsDistinct = {}
momentsDistinct[0] = 1
for i in arange(1,a):
if i%2 == 0:
data = 1/n*moments[i]
elif i == 1:
temp = moments[i]
temp2 = temp[:,0:n]*iota.T
data = 1/n*hstack((temp2))
else:
temp = moments[i]
temp2 = temp[:,0:n]*iota.T
temp3 = temp[:,n:2*n]*iota.T
data = 1/n*hstack((temp2, temp3))
momentsDistinct[i] = unique(data.flat)
return momentsDistinct(result, axis=1)