diff --git a/chapter-1/ex-21-Figure_1.png b/chapter-1/ex-21-Figure_1.png new file mode 100644 index 0000000..bb40521 Binary files /dev/null and b/chapter-1/ex-21-Figure_1.png differ diff --git a/chapter-1/ex-21-Figure_2.png b/chapter-1/ex-21-Figure_2.png new file mode 100644 index 0000000..77a8313 Binary files /dev/null and b/chapter-1/ex-21-Figure_2.png differ diff --git a/chapter-1/ex-21.py b/chapter-1/ex-21.py new file mode 100755 index 0000000..7f2d0de --- /dev/null +++ b/chapter-1/ex-21.py @@ -0,0 +1,34 @@ +#!/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()