Guest User

Untitled

a guest
Mar 24th, 2019
50
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #rejection sampling
  2. #P(j|-a)
  3. import random
  4. samples = 0
  5. PJA = 0
  6. PnotB = 0
  7. iterations = 2000000
  8. for i in range(iterations): #not number of samples, number of samples will be sampled
  9. rEQ = random.random()
  10. rB = random.random()
  11. rA = random.random()
  12.  
  13. Pb = rB <= 0.001
  14. Peq = rEQ <= 0.002
  15.  
  16. if Pb and Peq:
  17. Pa = rA <= 0.95
  18. elif Pb and not Peq:
  19. Pa = rA <= 0.94
  20. elif not Pb and Peq:
  21. Pa = rA <= 0.29
  22. elif not Pb and not Peq:
  23. Pa = rA <= 0.001
  24.  
  25. if not Pb:
  26. PnotB += 1
  27.  
  28. if Pa:
  29. samples += 1
  30. else:
  31. continue #rejection
  32.  
  33. rJ = random.random()
  34. PJ = rJ <= 0.9
  35.  
  36. if PJ:
  37. PJA += 1
  38.  
  39. PnotB = PnotB/iterations
  40. PJA = 0 if samples == 0 else PJA / samples
  41. print("P(-b)", PnotB)
  42. print("P(j|a) =", PJA)
  43. print("P(j|a, -b) =", PnotB*PJA)
RAW Paste Data