Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. # Problem:
  2. # I toss a coin 10 times. Determine the probability at least two `heads` appear if at least one `heads` appears.
  3.  
  4. # Answer:
  5.  
  6. # Let's define:
  7. # P(A) := the probability that at least two `heads` appear
  8. # P(B) := the probability that at least one `heads` appears
  9. # And we want to find P(A|B)
  10.  
  11. # We can think of P(A) in terms of the complement:
  12. # P(~A) := the probability that **at most** one `heads` appears
  13. # where
  14. # P(A) = 1 - P(~A)
  15.  
  16. # Similarly for P(B):
  17. # P(~B) := the probability that **no** `heads` appear
  18. # where
  19. # P(B) = 1 - P(~B)
  20.  
  21. # P(~A) and P(~B) are found using the Binomial distribution for 1 and 0 successes respectively.
  22.  
  23. N <- 10 # Number of coin tosses
  24. p <- 0.5 # P(Heads) == P(Tails)
  25.  
  26. prob_not_A <- pbinom(1, size = N, prob = p)
  27. prob_A <- 1 - prob_not_A
  28.  
  29. prob_not_B <- pbinom(0, size = N, prob = p)
  30. prob_B <- 1 - prob_not_B
  31.  
  32. # Using Bayes' theorem we have that:
  33. # P(A|B) * P(B) = P(B|A) * P(A)
  34. # But P(B|A) = 1 (can you see why?)
  35. # So our answer is as follows:
  36. # P(A|B) = P(A) / P(B)
  37.  
  38. answer <- prob_A / prob_B
  39.  
  40. print(answer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement