贝叶斯统计

时间:2014-08-20 16:16:18

标签: python bayesian

我需要知道如何找到两个离散分布的贝叶斯概率。例如,分布如下:

hypo_A=[ 0.1,0.4,0.5,0.0,0.0,0.0]
hypo_B=[ 0.1,0.1,0.1,0.3,0.3,0.1]

先前两者同样可能

贝叶斯公式为p(x/H) = (p(H/x)*p(x))/(summation(p(H/x`)*p(x`)))

基本上我需要知道如何在python中增加这些不等分布。

1 个答案:

答案 0 :(得分:1)

我强烈建议您阅读Think Bayes本书。

这是我用python写的贝叶斯统计的简单植入:

from collections import namedtuple
hypothesis=namedtuple('hypothesis',['likelihood','belief'])
class DiscreteBayes:
    def __init__(self):
        """initiates the hypothesis list"""
        self.hypo=dict()
    def normalize(self):
        """normalizes the sum of all beliefs to 1"""
        s=sum([float(h.belief) for h in self.hypo.values()])
        self.hypo=dict([(k,hypothesis(likelihood=h.likelihood,belief=h.belief/s)) for k,h in self.hypo.items()])
    def update(self,data):
        """updates beliefs based on new data"""
        if type(data)!=list:
            data=[data]
        for datum in data:
            self.hypo=dict([(k,hypothesis(likelihood=h.likelihood,belief=h.belief*h.likelihood(datum))) for k,h in self.hypo.items()])
        self.normalize()
    def predict(self,x):
        """predict new data based on previously seen"""
        return sum([float(h.belief)*float(h.likelihood(x)) for h in self.hypo.values()])

在你的情况下:

hypo_A = [ 0.1,0.4,0.5,0.0,0.0,0.0]
hypo_B = [ 0.1,0.1,0.1,0.3,0.3,0.1]
d = DiscreteBayes()
d.hypo['hypo_A'] = hypothesis(likelihood=hypo_A.get ,belief=1)
d.hypo['hypo_B'] = hypothesis(likelihood=hypo_B.get ,belief=1)
d.normalize()
x = 1
d.update(x) #updating beliefs after seeing x
d.predict(x) #the probability of seeing x in the future
print (d.hypo)