计算条件概率Python

时间:2016-01-07 22:05:34

标签: python probability

我正在尝试使用分层树结构enter image description here

来计算结果的概率
  

顶部是计算机计算机A,接下来的两个是计算机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 ???

2 个答案:

答案 0 :(得分:0)

据我所知,这是你想要实现的目标:

import imp
A = imp.load_source('A', 'path')
C = A.B

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

#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} % changes of getting virus on {1}".format(round(prob_comp/100.0, 2), computer_name))

for key in virus_probabilities:
     check_probability(key, 1000)

答案 1 :(得分:0)

来自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