__author__ = 'dell'import Pmfimport matplotlib.pyplot as pyplotpmf = Pmf.fzdds([1, 2, 2, 3, 5])print 'Mean by Pmf ', pmf.Mean()print 'Var by Pmf ', pmf.Var()def PmfMean(pmf): t = [x * v for x, v in pmf.Items()] res = sum(t) return resdef PmfVar(pmf): mu = PmfMean(pmf) t = [p * ((v - mu) ** 2) for v, p in pmf.Items()] res = sum(t) return resprint 'Mean by local ', PmfMean(pmf)print 'Var by local ', PmfVar(pmf)
运行结果:
D:Python27python.exe F:/sync_code/python/survivalanalysis.py
Mean by Pmf 2.6
Var by Pmf 1.84
Mean by local 2.6
Var by local 1.84