Bernoulli Variable
In this lesson, we will learn about finding the probability of a Bernoulli variable.
We'll cover the following...
In the example below, we will flip a coin five times in a row and record how many times we obtain tails (varying from 0-5). We will be performing the experiment 1000 times.
We will then compute the cumulative probability, print the values to the screen and make a plot of the cumulative probability function using a bar graph.
Press + to interact
import numpy as npimport matplotlib.pyplot as pltimport numpy.random as rndN = 1000tails = np.sum(rnd.randint(0, 1+1, (5, 1000)), axis=0)counttails = np.zeros(6, dtype='int')for i in range(6):counttails[i] = np.count_nonzero(tails == i)prob = counttails / Ncum_prob = np.cumsum(prob)print('probababilties:', prob)print('cumulative probabilities:', cum_prob)plt.bar(range(0, 6), cum_prob)plt.xticks(range(0, 6))plt.xlabel('number of tails in two flips')plt.ylabel('cumulative probability');
Execute the code several times and see that the graph changes a bit every time.