Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Given a coin probability p, what is the expected hitting time of getting k trials in a row?
- p = 0.505
- k = 14
- # Analytical solution
- failure_p = sum(p ** i * (1 - p) for i in range(k))
- cycle_time = sum(p ** i * (1 - p) * (i + 1) for i in range(k))
- expected_cycles = 1 / (1 - failure_p)
- print(cycle_time * expected_cycles + k)
- # Stochastic verification
- import random
- trials = 100
- average_n = 0
- for i in range(trials):
- in_a_row = 0
- n = 0
- while in_a_row != k:
- n += 1
- if random.random() < p:
- in_a_row += 1
- else:
- in_a_row = 0
- average_n += n
- print(average_n / trials)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement