Skip to content

Instantly share code, notes, and snippets.

@hichamjanati
Created April 18, 2024 09:17
Show Gist options
  • Save hichamjanati/67d5c6bf49865a3221c92a7c734afc62 to your computer and use it in GitHub Desktop.
Save hichamjanati/67d5c6bf49865a3221c92a7c734afc62 to your computer and use it in GitHub Desktop.
The famous Bayes table game. Computes the probability that Bob wins knowing the score is 5-3 for Alice.
import numpy as np
import time
def estimate_prob_bob_win(n_situations, seed=None, timeout=10):
rng = np.random.default_rng(seed)
bob_wins = 0
n_games = 0
t_start = time.time()
while(True):
p = rng.random()
score_alice = rng.binomial(8, p)
if score_alice == 5:
n_games += 1
score_alice_restant = rng.binomial(3, p)
if score_alice_restant == 0:
bob_wins += 1
if n_games == n_situations:
break
if time.time() - t_start > timeout:
print(f"Warning ! Timeout after {n_games} games")
break
return bob_wins / n_games
if __name__ == "__main__":
probas = [estimate_prob_bob_win(1000) for _ in range(100)]
probas = np.array(probas)
print(f"P(Bob gagne | A = 5, B = 3) = {probas.mean()}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment