Add results of exercise 21

This commit is contained in:
Petar Kapriš 2025-02-25 22:36:23 +01:00
parent bbeaf169fe
commit 2a01de329a
3 changed files with 34 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

34
chapter-1/ex-21.py Executable file
View 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()