Foundations of Probability in Python
Alexander A. Ramírez M.
CEO @ Synergy Vision
...with coin flips
Heads or Tails
Bernoulli random experiment
from scipy.stats import bernoulli
bernoulli.rvs(p=0.5, size=1)
array([0])
Another draw
bernoulli.rvs(p=0.5, size=1)
array([1])
Change size
parameter to flip more...
bernoulli.rvs(p=0.5, size=10)
array([0, 0, 0, 0, 0, 0, 1, 1, 0, 0])
How many heads?
sum(bernoulli.rvs(p=0.5, size=10))
5
Another draw...
sum(bernoulli.rvs(p=0.5, size=10))
2
Binomial random variable
from scipy.stats import binom
binom.rvs(n=10, p=0.5, size=1)
array([7])
Many draws
binom.rvs(n=10, p=0.5, size=10)
array([6, 2, 3, 5, 5, 5, 5, 4, 6, 6])
Biased coin draws
binom.rvs(n=10, p=0.3, size=10)
array([3, 4, 3, 3, 2, 2, 2, 2, 3, 6])
random_state
parameter of the rvs()
functionfrom scipy.stats import binom
binom.rvs(n=10, p=0.5, size=1, random_state=42)
numpy.random.seed()
import numpy as np
np.random.seed(42)
Flipping 10 fair coins with a random seed
from scipy.stats import binom
import numpy as np
np.random.seed(42)
binom.rvs(n=10, p=0.5, size=1)
array([4])
Foundations of Probability in Python