【问题标题】:Calculate Conditional Probability Python计算条件概率 Python
【发布时间】:2016-04-12 11:39:46
【问题描述】:

我正在尝试使用分层树结构 计算结果的概率

顶部是计算机 A,接下来的 2 是计算机 B 和 C,然后 最后 4 个是计算机 BD、BE 和 CD、CE。我试图找到 如果计算机 A 感染病毒的概率是什么 B 或 C 感染病毒的概率。如果 B 或 C 得到 被感染 BD、BE、CD、CE 被感染的概率是多少 有病毒

我想进行 100 次试验以找到答案。我是在 python 上做概率的新手。然而,这是我到目前为止的代码:

import random, time

#prob that computers will get virus
CompA = 0.50
CompB = .25 
CompC = .25
CompBD = .125
CompBE= .125
CompCD= .125
CompCE= .125



def generate():
    x = random.random()
    if x =< CompA: #Computer A has virus
       prob_compa= sum(generate() for i in range(100)) #prob that Comp A has virus  in a 100 rounds
       print (prob_compa/100 + 'percent chance of getting virus')

        try:
            if CompB<.125:
                 prob_compa sum(generate() for i in range(100)) #prob that Comp B has virus  in a 100 rounds
                print (prob_compa/100 + 'percent chance of getting virus')
                 elif CompB<.125:
                 prob_compa= sum(generate() for i in range(100)) #prob that Comp C is sick  in a 100 rounds
       print (prob_compa/100 + 'percent chance of getting virus')

      #I continue this method for the rest of the tree

有没有更好更简单的方法让我得到结果? random.uniform???

【问题讨论】:

  • 我认为您不必执行模拟。你不能用马尔科夫毯推导出这个吗?这毕竟是一个很好的概率图
  • hmm 从没听说过.. 这个功能会提供一种更简单的方法来计算我想要的结果吗?
  • “如果A被感染,B被感染的概率是多少”——其实很简单conditional probability,不需要更高级的工具

标签: python probability


【解决方案1】:

据我了解,这是您想要实现的目标:

#python_test2.py
import random, time

virus_probabilities= { "CompA" : 0.50, "CompB" : .25, "CompC" : .25, "CompBD" : .125,
                   "CompBE" : .125, "CompCD" : .125, "CompCE" : .125}

def check_probability(computer_name, n_repetitions = 100):
    prob_comp, repetitions = 0, 0
    p_computer = virus_probabilities[computer_name]
    while repetitions < n_repetitions:
     x = random.random()
     if x <= p_computer:
          prob_comp += 1
     repetitions += 1
    print ("{0} % chance of getting virus on {1}".format(round(prob_comp/100.0, 2), computer_name))

for key in virus_probabilities:
     check_probability(key, 1000)

当我从控制台运行文件时,我得到:

mabe@ubuntu:~/Desktop $ python test_2.py
2.49 % chance of getting virus on CompB
2.6 % chance of getting virus on CompC
5.07 % chance of getting virus on CompA
1.38 % chance of getting virus on CompBE
1.16 % chance of getting virus on CompBD
1.18 % chance of getting virus on CompCD
1.36 % chance of getting virus on CompCE

【讨论】:

    【解决方案2】:

    来自 mabe02 的精彩代码,或许值得对核心功能添加一个非常小的改进,以避免混淆/未来的错误:

    def check_probability(computer_name, n_repetitions):
        prob_comp, repetitions = 0, 0
        p_computer = virus_probabilities[computer_name]
        while repetitions < n_repetitions:
         x = random.random()
         if x <= p_computer:
              prob_comp += 1
         repetitions += 1
        print ("{0} % changes of getting virus on {1}".format(round(prob_comp/n_repetitions, 2), computer_name))
    

    这样做实际上会使概率更接近起始概率,这是随着 n_repetitions 变大而预期的。

    尽管有关条件概率的更多细节,您绝对应该看看这篇文章:A simple explanation of Naive Bayes Classification

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      相关资源
      最近更新 更多