美文网首页
【Bayes推理】狼来了故事 Python实现

【Bayes推理】狼来了故事 Python实现

作者: 唯师默蓝 | 来源:发表于2019-04-12 10:20 被阅读0次
# D表示小孩说谎事件
# H表示小孩可信事件
import matplotlib.pyplot as plt

def Tell_Truth():

    # 村名对小孩的可信程度:P(H)
    # P_H = 0.8
    # 村名对小孩的不可信程度:P(-H)
    P_H = float(input("请输入村名对小孩的可信程度P(H):"))
    # P_mH = 0.2
    P_mH = float(input("请输入村名对小孩的不可信程度P(-H):"))
    # 可信的孩子说谎的可能性为:P(D/H) = 0.1
    # P_D_H = 0.1
    P_D_H = float(input("可信的孩子说谎的可能性为P(D/H):"))
    # 不可信的孩子说谎的可能性:p(D/-H) = 0.5
    # P_D_mH = 0.5
    P_D_mH = float(input("不可信的孩子说谎的可能性p(D/-H):"))
    P=[]
    times=[]
    for i in range(1,11):
        # 计算后验概率,P_H_D:击中率 -> P(H/D) ,P_D_mH:误报率 -> P(D/-H)
        # P_H_D = P_H * P_D_H / (P_H * P_D_H + P_mH * P_D_mH)
        P_H_D = P_H * (1 - P_D_H) / (P_H * (1 - P_D_H) + P_mH * (1 - P_D_mH))
        # print("小孩第", i ,"次说谎,村民对其的可信度为:",P_H_D)
        print("小孩第", i ,"次说实话,村民对其的可信度为:",P_H_D)
        P.append(P_H_D)
        times.append(i)
        P_H = P_H_D
        P_mH = 1 - P_H_D
    plt.plot(times , P, color='darkblue', label='PN distance')
    plt.xlabel(u"小孩说实话次数",fontproperties = "SimHei")
    plt.ylabel(u"小孩的可信度",fontproperties = "SimHei")
    plt.show()

def Lie():
    # 村名对小孩的可信程度:P(H)
    # P_H = 0.8
    # 村名对小孩的不可信程度:P(-H)
    P_H = float(input("请输入村名对小孩的可信程度P(H):"))
    # P_mH = 0.2
    P_mH = float(input("请输入村名对小孩的不可信程度P(-H):"))
    # 可信的孩子说谎的可能性为:P(D/H) = 0.1
    # P_D_H = 0.1
    P_D_H = float(input("可信的孩子说谎的可能性为P(D/H):"))
    # 不可信的孩子说谎的可能性:p(D/-H) = 0.5
    # P_D_mH = 0.5
    P_D_mH = float(input("不可信的孩子说谎的可能性p(D/-H):"))
    P = []
    times = []
    for i in range(1, 11):
        # 计算后验概率,P_H_D:击中率 -> P(H/D) ,P_D_mH:误报率 -> P(D/-H)
        P_H_D = (P_H * P_D_H) / (P_H *  P_D_H + P_mH * P_D_mH)
        print("小孩第", i ,"次说谎,村民对其的可信度为:",P_H_D)
        P.append(P_H_D)
        times.append(i)
        P_H = P_H_D
        P_mH = 1 - P_H_D
    plt.plot(times, P, color='darkblue', label='PN distance')
    plt.xlabel(u"小孩说实话次数", fontproperties="SimHei")
    plt.ylabel(u"小孩的可信度", fontproperties="SimHei")
    plt.show()
if __name__ == '__main__':
    Lie()
    # Tell_Truth()

相关文章

网友评论

      本文标题:【Bayes推理】狼来了故事 Python实现

      本文链接:https://www.haomeiwen.com/subject/oepqwqtx.html