2

I am trying to calculate the probability of an outcome with a hierarchical tree structure enter image description here

The top is computer Computer A, the next 2 are Computer B & C, and the last 4 are Computer BD, BE, and CD, CE. I am trying to find the probability that if computer A gets infected with a virus what is the probability that B or C gets infected with a virus. And if B or C gets infected what is the probability that BD, BE, CD, CE gets infected with a virus

I want to run 100 trials to find the answer. I am new to doing probability on python. However here is the code I have so far:

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

Is there a better way and simpler way for me to get the results? random.uniform???

Joshua Grigonis
  • 748
  • 5
  • 18
royalblue
  • 439
  • 2
  • 8
  • 18
  • 2
    I think you do not have to perform a simulation. Can't you derive this using the *Markov blanket*? This is after all a nice *probability graph*. – Willem Van Onsem Jan 07 '16 at 22:08
  • hmm never heard of it.. will this feature present an easier way to calculate my desired outcome? – royalblue Jan 07 '16 at 22:15
  • 1
    "If A gets infected, what's the probability that B gets infected" -- this is actually plain simple [conditional probability](https://en.wikipedia.org/wiki/Conditional_probability), no need for more advanced tools – Andrea Corbellini Jan 07 '16 at 23:08

2 Answers2

1

As far as I understood this is what you are trying to achieve:

#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)

When I run the file from the console, I get:

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
OfirD
  • 9,442
  • 5
  • 47
  • 90
mabe02
  • 2,676
  • 2
  • 20
  • 35
1

Brilliant code from mabe02, perhaps worth adding a very minor improvement to the core function avoid confusion/future bugs:

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))

Doing so would actually bring probabilities closer to the starting one which is expected as n_repetitions gets bigger.

Although for more specifics on conditional probability you should definitely have a look at this post: A simple explanation of Naive Bayes Classification

Olivier
  • 23
  • 6