贝叶斯统计
我想知道如何找到两个离散分布的贝叶斯概率。比如,分布是这样给出的:
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 个回答
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)