A **binomial test** compares a sample proportion to a hypothesized proportion.

For example, suppose we have a 6-sided die. If we roll it 12 times, we would expect the number “3” to show up 1/6 of the time, which would be 12 * (1/6) = 2 times.

If the number “3” actually shows up 4 times, is that evidence that the die is biased towards the number “3”? We could perform a binomial test to answer that question.

In Python, we can perform a binomial test using the binom_test() function from the scipy.stats library, which uses the following syntax:

**binom_test(x, n=None, p=0.5, alternative=’two-sided’)**

where:

**x:**number of “successes”**n:**total number of trials**p:**the probability of success on each trial**alternative:**the alternative hypothesis. Default is ‘two-sided’ but you can also specify ‘greater’ or ‘less.’

This function returns the p-value of the test. We can load this function by using the following sytax:

from scipy.stats import binom_test

The following examples illustrate how to perform binomial tests in Python.

**Example 1: We roll a 6-sided die 24 times and it lands on the number “3” exactly 6 times. Perform a binomial test to determine if the die is biased towards the number “3.”**

The null and alternative hypotheses for our test are as follows:

**H _{0}:** π ≤ 1/6 (the die is not biased towards the number “3”)

**H _{A}:** π > 1/6

**π is the symbol for population proportion.*

We will enter the following formula into Python:

binom_test(x=6, n=24, p=1/6, alternative='greater') 0.1995295129479586

Because this p-value (0.1995) is not less than 0.05, we fail to reject the null hypothesis. We do not have sufficient evidence to say the die is biased towards the number “3.”

**Example 2: We flip a coin 30 times and it lands on heads exactly 19 times. Perform a binomial test to determine if the coin is biased towards heads.**

The null and alternative hypotheses for our test are as follows:

**H _{0}:** π ≤ 1/2 (the coin is not biased towards heads)

**H _{A}:** π > 1/2

We will enter the following formula into Python:

binom_test(x=19, n=30, p=1/2, alternative='greater') 0.10024421103298661

Because this p-value (0.10024) is not less than 0.05, we fail to reject the null hypothesis. We do not have sufficient evidence to say the coin is biased towards heads.

**Example 3: A shop makes widgets with 80% effectiveness. They implement a new system that they hope will improve the rate of effectiveness. They randomly select 50 widgets from a recent production run and find that 47 of them are effective. Perform a binomial test to determine if the new system leads to higher effectiveness.**

The null and alternative hypotheses for our test are as follows:

**H _{0}:** π ≤ 0.80 (the new system does not lead to an increase in effectiveness)

**H _{A}:** π > 0.80

We will enter the following formula into Python:

binom_test(x=47, n=50, p=0.8, alternative='greater') 0.005656361012155314

Because this p-value (0.00565) is less than 0.05, we reject the null hypothesis. We have sufficient evidence to say the new system leads to an increase in effectiveness.