all-of-stats-hijinks/chapter-1/ex-21.py

34 lines
588 B
Python
Executable file

#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
import random
p = 0.3
n = 1000
def experiment(p, n):
proportion = np.empty(1000)
heads = 0
random.seed()
for i in range(n):
if random.random() < p:
heads += 1
proportion[i] = heads/(i+1)
f1 = plt.figure(1)
return proportion
xaxis = np.arange(1,1001)
tosses = np.empty(1000)
proportion1 = experiment(0.3, 1000)
proportion2 = experiment(0.03, 1000)
f1 = plt.figure(1)
plt.plot(xaxis, proportion1)
f2 = plt.figure(2)
plt.plot(xaxis, proportion2)
plt.show()