Search code examples
pythonprobability

Calculate Conditional Probability Python


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


Solution

  • 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