【发布时间】: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