# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# %matplotlib inline
# %config InlineBackend.figure_format = 'retina'

import numpy as np
from matplotlib import pyplot as plt

# +
q = 0.5

coin_flips = np.random.rand(100000, 50) < q
print(coin_flips.shape)
# -

num_heads = coin_flips.sum(axis=1)
print(num_heads.shape)

plt.hist(num_heads, np.arange(51));

import scipy.misc

ks = np.arange(51)

c = scipy.misc.comb(50, ks)

plt.plot(c)

p1 = q ** ks
print(p1)

p2 = (1 - q) ** (50 - ks)
print(p2)

binom = c * p1 * p2 # Pr(k heads in n trials)

plt.hist(num_heads, np.arange(51) - 0.5, density=True);
plt.plot(ks, binom, 'k-')



StarcoderdataJupyterScriptsDedupFiltered