- HubPages
*»* - Technology
*»* - Computers & Software
*»* - Computer Science & Programming
*»* - Programming Languages

# Binomial Probability Using Python

## Questions Answered

We discuss in brief what is a *binomial event*, how *binomial probability* works and then go on to *program binomial probability* in Python. We will also solve the following interesting binomial problem.

*Problem: The ratio of boys to girls for babies born in a country is 1.09:1. What proportion of its families with exactly 6 children will have at least 3 boys?*

Assumption: There are only 2 possible genders.

## Binomial Event (Bernoulli Trial)

A binomial event is a statistical experiment that has the following properties:

- The experiment consists of n repeated trials. e.g. 10 throws of a coin.
- The trials are independent of each other. e.g. Current outcome doesn't affect the next
- The outcome of each trial is either success () or failure (). o.r. Head or Tail.
- Probability of success of 1 trial is p and that of failure is 1-p. i.e. total probability is 1.

## Ingredients for Coding in Python

We need:

*from statistics Import math*to call builtin*factorial*function.- Write a function for calculating the binomial distribution
- That's it.

## Python Code

# Import stat module for factorial from statistics import math # Define function to get binomial distribution. # Here x in the number of successful outcomes # n is number of trials # p is probability of success. # example:binom_dist(2, 5, 1.09) gives probability of 2 boys # in a family of 5 in a country where

# male to female ratio at birth is 1.09:1

def binom_dist(x, n, p): success_prob = 0 success_prob

=(math.factorial(n)/(math.factorial(x)*math.factorial(n-x)))*(p**x)*((1-p)**(n-x)) return boys

## Using this code to solve problems

Building on the child birth example. Let's assume that male:female ratio is 1.1.

*Problem1: What is the probability that a family of 2 has exactly 1 boy and 1 girl.*

print(binom_dist(1,2,1.1)) output: 0.773

*Problem2: What is the probability that a family of 6 has at least 3 girls.*

This is a little tricky one. At least 3 girls means cases where girls' number is 3,4,5 &6. Now, we need to calculate cumulative probability.

# Since we need prob at multiple points we have to loop it in # We declare a cum_prob variable # We use 1/1.1 as prob of success as we are using it for girls #

n=6 p=1/1.1

cum_prob=0

for i in range(3,n+1,1):

cum_prob+=run_binom(i, n, p) print(cum_prob)

#output: 0.611

## Bare code

from statistics import math def binom_dist(x, n, p): boys = 0 boys =(math.factorial(n)/(math.factorial(x)*math.factorial(n-x)))*(p**x)*((1-p)**(n-x)) return boys #Problem1: What is the probability that a family of 2 has exactly 1 boy and 1 girl. print(binom_dist(1,2,1.1)) #output: 0.773 #Problem2: What is the probability that a family of 6 has at least 3 girls. # Since we need prob at multiple points we have to loop it in # We declare a cum_prob variable # We use 1/1.1 as prob of success as we are using it for girls # n=6 p=1/1.1 cum_prob=0 for i in range(3,n+1,1): cum_prob+=run_binom(i, n, p) print(cum_prob) #output: 0.611