Skip to content

Instantly share code, notes, and snippets.

@tkphd
Last active April 1, 2022 18:19
Show Gist options
  • Save tkphd/f96963caf479a9c90d3b766c1b291b64 to your computer and use it in GitHub Desktop.
Save tkphd/f96963caf479a9c90d3b766c1b291b64 to your computer and use it in GitHub Desktop.
How often would 983 births produce 453 girls? With a male ratio of 51.47%, I see it in 0.80% of 100,000 samples. Low, but not extraordinary.
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import default_rng
rng = default_rng()
n = int(100_000)
N = int(1_000_000_000)
pop = int(983) # children born in Kiev
act = int(453) # girls born in Kiev
ratio = 0.5147 # ref: <https://pubmed.ncbi.nlm.nih.gov/31492547/>
vals = rng.uniform(low=0.0, high=1.0, size=N)
kids = np.where(vals < ratio, 0, 1) # zero if male, 1 if female
fem = np.zeros(n)
for i in range(n):
idx = rng.integers(0, N, size=pop)
fem[i] = int(np.sum(kids[idx]))
real = np.sum(np.where(fem == act, 1, 0)) # 1 if it matches
print("{} female births were observed in {} of {} samples ({:.2f}%)".format(act, real, n, float(100.0 * real) / n))
plt.figure(figsize=(4,4))
plt.title("Bootstrap of Gender Ratio per {} Births".format(pop))
plt.xlabel("number of female children")
plt.ylabel("count for {} samples".format(n))
plt.hist(fem, bins=51)
plt.savefig("fem.png", dpi=400, bbox_inches="tight")
@tkphd
Copy link
Author

tkphd commented Apr 1, 2022

Example of output:

453 female births were observed in 769 of 100000 samples (0.77%)

fem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment