In [1]:

```
from scipy.stats import binom
import matplotlib.pyplot as plt
# experiment: 10 balls, 7 are white and 3 black
# we pick a ball 10 times with replacement
# What is the probability that there are:
# - 0 white balls
# - 1 white ball
# - 2 white balls
# - 3 white balls
# ..
# - 10 white balls
# So the number of white balls is what we are interested in.
# This number we call x, so since we are asking for the probability
# for various numbers of white balls (here 0 to 10) we create
# a vector X=[0,1,..10] for convenience
n = 10 #number of trials
p = 7/10 #probability for the occurrence of p
q = 1-p #probability for the occurrence of q
X = list(range(n+1))
#What we can do is, we can make different plots for
#different ratios of black and white balls, thus
#changing the probability for the occurrence of the balls
#we can then see how the distribtion changes
dist = [binom.pmf(x,n,p) for x in X]
```

In [2]:

```
P=[0.2, 0.5, 0.8]
fig,ax1 = plt.subplots(figsize=(6,6))
ax1.set_ylabel("probability", color="blue")
ax1.set_xlabel("number of occurrences of our favored event, i.e. draw of a white ball")
ax1.bar(X, dist,color='blue')
ax1.set_title('p='+str(p))
plt.interactive(True)
plt.show()
```

How to print several distributions in a loop

In [3]:

```
#array with the probabilites for the occurrence of the event of interest
#(in our example this would be the ratio of white balls to the total number of balls)
P=[0.2, 0.5, 0.8]
#make a figure with one row and 3 ( len(P) ) columns
fig, axes =plt.subplots(1,len(P),figsize=(18,6))
colors=['blue', 'red','green']
for i in list(range(len(P))):
ax1=axes[i]
ax1.set_ylabel("probability", color=colors[i])
ax1.set_xlabel("number of occurrences of our favored event, i.e. draw of a white ball")
#compute the distribution for the p value in P[i]
dist=[binom.pmf(x,n,P[i]) for x in X]
ax1.bar(X, dist,color=colors[i])
ax1.set_title('p='+str(P[i]))
plt.tight_layout()
plt.interactive(True)
plt.show()
```

Was this helpful?

0 / 0