Congratulations — if you’ve made it this far, you’ve now written your own function to simulate a binomial test!
More formally, the binomial distribution describes the number of expected “successes” in an experiment with some number of “trials”. In the example you just worked through, the experiment consisted of 500 people visiting Live-it-LIVE.com. For each of those trials (visitors), we expected a 10% chance of a purchase (success), but observed only 41 successes (less than 10%).
SciPy has a function called
binom_test(), which performs a binomial test for you. The default alternative hypothesis for the
binom_test() function is two-sided, but this can be changed using the
alternative parameter (eg.,
alternative = 'less' will run a one-sided lower tail test).
binom_test() requires three inputs, the number of observed successes, the number of total trials, and an expected probability of success. For example, with 10 flips of a fair coin (trials), the expected probability of heads is 0.5. Let’s imagine we get 2 heads (observed successes) in 10 flips. Is the coin weighted? The function call for this binomial test would look like:
from scipy import binom_test p_value = binom_test(2, n=10, p=0.5) print(p_value) #output: 0.109
This tells us that IF the true probability of heads is 0.5, the probability of observing 2 or fewer heads OR 8 or more heads is 0.109 (10.9%).
binom_test function to run the same binomial test that you just simulated: A two-sided test for whether the observed 41 purchases among 500 visitors to Live-it-LIVE.com is far enough from the expected 10% purchase rate to convince you that the purchase rate was different from expectation this week.
Save the p-value as
p_value_2sided and print it out. Is this p-value similar to what you calculated via simulation (approximately 0.2)?
Run the same hypothesis test as in step 1, but now as a one-sided test where the alternative hypothesis is that the probability of a visitor making a purchase was less than 10% (0.1).
Save the p-value as
p_value_1sided and print it out. Is this p-value similar to what you calculated via simulation (approximately 0.1)?