Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Problem:
- # I toss a coin 10 times. Determine the probability at least two `heads` appear if at least one `heads` appears.
- # Answer:
- # Let's define:
- # P(A) := the probability that at least two `heads` appear
- # P(B) := the probability that at least one `heads` appears
- # And we want to find P(A|B)
- # We can think of P(A) in terms of the complement:
- # P(~A) := the probability that **at most** one `heads` appears
- # where
- # P(A) = 1 - P(~A)
- # Similarly for P(B):
- # P(~B) := the probability that **no** `heads` appear
- # where
- # P(B) = 1 - P(~B)
- # P(~A) and P(~B) are found using the Binomial distribution for 1 and 0 successes respectively.
- N <- 10 # Number of coin tosses
- p <- 0.5 # P(Heads) == P(Tails)
- prob_not_A <- pbinom(1, size = N, prob = p)
- prob_A <- 1 - prob_not_A
- prob_not_B <- pbinom(0, size = N, prob = p)
- prob_B <- 1 - prob_not_B
- # Using Bayes' theorem we have that:
- # P(A|B) * P(B) = P(B|A) * P(A)
- # But P(B|A) = 1 (can you see why?)
- # So our answer is as follows:
- # P(A|B) = P(A) / P(B)
- answer <- prob_A / prob_B
- print(answer)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement