Multivariable Cumulants and Moments in python -
in mathematica can convert multivariable moments in cumulants , using momentconvert:
momentconvert[cumulant[{2, 2,1}], "moment"] // traditionalform
as 1 can try in wolframcloud.
i same in python. there library in python capable of this?
at least 1 direction programmed myself:
# http://code.activestate.com/recipes/577211-generate-the-partitions-of-a-set-by-index/ collections import defaultdict class partition: def __init__(self, s): self.data = list(s) self.m = len(s) self.table = self.rgf_table() def __getitem__(self, i): #generates set partitions index if > len(self) - 1: raise indexerror l = self.unrank_rgf(i) result = self.as_set_partition(l) return result def __len__(self): return self.table[self.m,0] def as_set_partition(self, l): # transform restricted growth function partition n = max(l[1:]+[1]) m = self.m data = self.data p = [[] _ in range(n)] in range(m): p[l[i+1]-1].append(data[i]) return p def rgf_table(self): # compute table values m = self.m d = defaultdict(lambda:1) in range(1,m+1): j in range(0,m-i+1): d[i,j] = j * d[i-1,j] + d[i-1,j+1] return d def unrank_rgf(self, r): # unrank restricted growth function m = self.m l = [1 _ in range(m+1)] j = 1 d = self.table in range(2,m+1): v = d[m-i,j] cr = j*v if cr <= r: l[i] = j + 1 r -= cr j += 1 else: l[i] = r // v + 1 r %= v return l # s = set(range(4)) # p = partition(s) # x in p: # print (x) # using https://en.wikipedia.org/wiki/cumulant#joint_cumulants import math def cum2mom(arr, state): def e(op): return qu.expect(op, state) def arr2str(arr,sep): r = '' i,x in enumerate(arr): r += str(x) if i<len(arr)-1: r += sep return r if isinstance( arr[0],str): myprod = lambda x: arr2str(x,'*') mysum = lambda x: arr2str(x,'+') e=lambda x: 'e('+str(x)+')' myfloat = str else: myfloat = lambda x: x myprod = np.prod mysum = sum s = set(range(len(arr))) p = partition(s) return mysum([ myprod([myfloat(math.factorial(len(pi)-1) * (-1)**(len(pi)-1)) ,myprod([ e(myprod([ arr[i] in b ])) b in pi])]) pi in p]) print(cum2mom(['a','b','c','d'],1) ) import qutip qu print(cum2mom([qu.qeye(3) in range(3)],qu.qeye(3)) )
it's designed work qutip opjects , works strings verify correct separation , prefactors.
exponents of variables can represented repeating variable.
Comments
Post a Comment