4Geeks logo
4Geeks logo

Bootcamps

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Academy

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Full-Stack Software Developer - 16w

Data Science and Machine Learning - 16 wks

Search from all Lessons


LoginGet Started
← Back to Lessons

Weekly Coding Challenge

Every week, we pick a real-life project to build your portfolio and get ready for a job. All projects are built with ChatGPT as co-pilot!

Start the Challenge

Podcast: Code Sets You Free

A tech-culture podcast where you learn to fight the enemies that blocks your way to become a successful professional in tech.

Listen the podcast
Edit on Github
Open in Colab

Binomial Probability with Python

Origin in Games

Many historians of probability note the emergence of a theory of probability as linked to humans attempts to understand games of chance. We begin with an early example called Senet that is at least as old as the year 3000 BC. Here, players moves are decided by dropping 4 paddles with two colored faces. The players are allowed to move based on the number of colored paddles that are facing up. Our goal is to explore the likely outcomes of the drops to begin to understand probability.

PROBLEM

The table below begins to explore these possible combinations. We will look at further examples with more possible combinations but this will get us started.

sticks# ways to get 0 white1 white2 white3 white4 white5 white
111nananana
2121nanana
31331nana
414641na
5??????
6??????

Binomial Distribution

The example above is one example of a more general kind of distribution of events. We have 4 two-sided sticks, and we will treat these as 4 Bernoulli Trials. To determine the probability of an outcome, we must:

  • Count how many ways there are for this specific outcome to occur
  • Mutiply this by the probability of successes and failures

As a function, we would have inputs that determine the outcome (success or failure, red or white, heads or tails, etc.), number of successes and failures, and the probability of a success. Below we have the functional form:

f(k,n,p)=Pr(k;n,p)=Pr(X=k)=(nk)pk(1p)nkf(k, n, p) = \Pr(k;n,p)=\Pr(X=k)={\binom {n}{k}}p^{k}(1-p)^{n-k}

We unpack this as follows:

  • (nk)\binom {n}{k} counts the number of ways an outcome can occur. For example, (42)\binom{4}{2} could be interpreted as how many ways are there to get two white sticks from four total.
  • pkp^k is the probability of a success to the power of the number of successes. For example 122\frac{1}{2}^2 would be interpreted as the probability of getting a white stick twice.
  • (1p)k(1 - p)^k is the probability of a failure nkn-k times. For example (112)2(1 - \frac{1}{2})^2 can be interpreted as the probability of a black stick two out of four attempts.

Using the computer

In [ ]:
from scipy.special import comb
In [ ]:
#ways to pick 2 things from 4
comb(4, 2)
Out[ ]:
6.0
In [ ]:
#prob of success to the number of successes
(1/2)**2
Out[ ]:
0.25
In [ ]:
#prob of failures to number of failures
(1/2)**2
Out[ ]:
0.25
In [ ]:
#put it all together
n_choose_k = comb(4, 2)
p_to_the_k = (1/2)**2
one_minus_p_to_the_n_minus_k = (1/2)**2
#probability of getting 2 white sticks from four
n_choose_k*p_to_the_k*one_minus_p_to_the_n_minus_k
Out[ ]:
0.375

Problems

Suppose we instead play the game of Sennet using 5 sticks.

  1. How many ways are there to get three white sticks?
  2. What is the probability that we will get three white sticks?
  3. Suppose we toss 10 coins. How many different outcomes contain exactly three heads?
  4. Suppose we toss 20 coins. How many different outcomes contain exactly three heads?
  5. Complete the next two rows of the table for sennet. Do you recognize a pattern?