Add results of exercise 21
This commit is contained in:
parent
bbeaf169fe
commit
2a01de329a
3 changed files with 34 additions and 0 deletions
BIN
chapter-1/ex-21-Figure_1.png
Normal file
BIN
chapter-1/ex-21-Figure_1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 24 KiB |
BIN
chapter-1/ex-21-Figure_2.png
Normal file
BIN
chapter-1/ex-21-Figure_2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 25 KiB |
34
chapter-1/ex-21.py
Executable file
34
chapter-1/ex-21.py
Executable file
|
@ -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()
|
Loading…
Add table
Reference in a new issue