均值,方差: 概率质量函数PMF

__author__ = 'dell'



import Pmf

import matplotlib.pyplot as pyplot





pmf = Pmf.MakePmfFromList([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 res





def PmfVar(pmf):

    mu = PmfMean(pmf)

    t = [p * ((v - mu) ** 2) for v, p in pmf.Items()]

    res = sum(t)

    return res



print 'Mean by local ', PmfMean(pmf)

print 'Var by local ', PmfVar(pmf)

运行结果:
D:\Python27\python.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

 

你可能感兴趣的:(函数)