Predict Results from a Fair Coin
During your interview loop, you may receive coding questions related to statistics, data manipulation, machine learning, or software engineering.
In this lesson, we'll walk through a statistics-based coding question that you could receive in a technical screen. This question comprises of several parts, each intended to assess your understanding of statistical concepts and your ability to apply these concepts using programming skills.
Practice this interview question in your preferred .ipynb environment.
Part 1
Consider you have a coin. You flip it 30 times and receive 20 heads and 10 tails. Calculate the probability of obtaining this exact result with a fair coin.
A fair coin has a 50% chance of landing on heads and a 50% chance of landing on tails.
Part 2
If you were to evaluate the likelihood of obtaining at least 20 heads in 30 flips of a fair coin, akin to how p-values are calculated, how would you approach this?
Part 3
Determine the probability of getting this result (20 heads in 30 flips) using a simulation approach.
Part 4
Determine the likelihood of obtaining at least 20 heads in 30 flips of a fair coin using simulation.
Part 1
To start, we want to calculate the probability of getting exactly 20 heads out of 30 coin flips if the coin is fair. A fair coin means there's a 50% chance of flipping heads and a 50% chance of flipping tails on any given toss.
Python# P(X=20) X~Binomial(0.5, 30) import scipy.stats as sc sc.binom.pmf(20, 30, 0.5) #2.8%
Part 2
P-values help us understand if the observed results (like getting 20 heads) are extreme enough to doubt a null hypothesis (in this case, that the coin is fair).
Python#H0: the coin is fair #H1: the coin is not fair # p-value is the probability of obtaining 20 heads in 30 flips or something more extreme (farther from a fair coin) # if the null is true # probability of 20, 21, 22, ..., 30 heads out of 30 flips 1-sc.binom.cdf(19, 30, 0.5) #4.9%
The result suggests that getting 20 or more heads is quite unlikely (less than 5% chance) if the coin is fair. It's a hint that the coin might not be fair, although there's still a small chance (4.9%) these results could occur with a fair coin.
Hypothesis testing evaluates whether observed data significantly deviate from what's expected under a null hypothesis, using significance values to determine the threshold for this deviation. For our coin flip example, a significance value (p-value) less than 5% suggests it's quite unlikely to get 20 or more heads by chance if the coin were fair, indicating a statistically significant deviation from what we'd expect with a fair coin.
Part 3
Now, let's simulate flipping a fair coin 30 times, repeating this experiment 100,000 times to see how often we get 20 or more heads. This simulation approach gives us another way to understand the likelihood of our observed outcome.
Pythonimport numpy as np np.random.binomial(1, 0.5) # random flip of the coin np.random.binomial(30, 0.5) # 30 random flips of the coin results = [np.random.binomial(30, 0.5) for _ in range(100000)] sum([1 if val == 20 else 0 for val in results])/100000
Part 4
And if we're interested in finding out how often we get 20 or more heads, not just exactly 20 (similar to how we calculate p-values):
Pythonimport scipy.stats as ss ss.binom.pmf(20, 30, 0.5) # pvalue is the probability of this results (20 heads) or one more extreme (farther from a fair coin) if # the coin is truly fair # probability of 20, 21, 22, ... 30 sum([1 if val >= 20 else 0 for val in results])/100000 1-ss.binom.cdf(19, 30, 0.5)
This simulation method confirms our earlier calculations, showing again that getting 20 or more heads is unusual for a fair coin, supporting the idea that our coin might not be fair. However, it also illustrates that there's a slight chance (similar to the 4.9% we calculated before) of these results happening with a fair coin.
By comparing these approaches, we reinforce our understanding of statistical analysis and hypothesis testing, providing a comprehensive view of how to interpret the results of our coin flips.