Search⌘ K
AI Features

Bernoulli Variable

Explore how to simulate Bernoulli variables by flipping a coin multiple times and recording successes. Understand the binomial distribution formula, compute theoretical probabilities, and compare these to experimental results. Learn to visualize cumulative probability functions and deepen your grasp of random variables in scientific computing.

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 ...

Python 3.5
import numpy as np
import matplotlib.pyplot as plt
import numpy.random as rnd
N = 1000
tails = 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 / N
cum_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.

Probability of a

...
Ask