统计学之-贝叶斯法则编程

今天学习统计学的贝叶斯编程。

抛两次硬币,第1个硬币的概率p1=0.5,另一个硬币的概率 p2=0.8,求抛两次出现一次正面的概率

#Return the probability of flipping one head each from two coins
#One coin has a probability of heads of p1 and the other of p2

def f(p1,p2):
    return p1*(1-p2) + (1-p1)*p2
#Insert your code here

print f(0.5,0.8)

抛硬币

有两个硬币,出现头的概率为p1,p2,选择第一个硬币的概率为p0,抛一次,求抛一次,出现正面的概率。

#Two coins have probabilities of heads of p1 andd p2
#The probability of selecting the first coin is p0
#Return the probability of a flip landing on heads

def f(p0,p1,p2):
#Insert your code here
    return p0*p1 + (1-p0)*p2

癌症案例

获得癌症的概率为P(C) = 0.1, 癌症中良性概率为 0.9,非癌症中恶性概率为 0.8,求良性的概率。

file

可以使用画图来计算:
file

代码实现:

#Calculate the probability of a positive result given that
#p0=P(C)
#p1=P(Positive|C)
#p2=P(Negative|Not C)

def f(p0,p1,p2):
#Insert your code here
    return p0*p1 + (1-p0)*(1-p2)

print f(0.01, 0.9, 0.8)

为者常成,行者常至