Experiment 6
Experiment 6
● Toss the coin N = 100, 500, 1000, 5000 and 500000 times and compute the probability of head in each case.
● Compute the absolute error | 0.5 - p | in each case and plot against N and understand the law of large numbers.
import random
import numpy as np
import matplotlib.pyplot as plt
toss = random.randint(0,1)
toss
0
def simulate_coin_tosses(n):
head_count = 0
for i in range(n):
toss = random.randint(0,1)
head_count = head_count + toss
prob_head = head_count / n
return prob_head
# Number of tosses
tosses = [100, 500, 1000, 5000, 500000, 10000000]
# Compute probabilities
probabilities = [simulate_coin_tosses(N) for N in tosses]
# Compute absolute error
abs_error = np.abs([0.5 - prob for prob in probabilities])
plt.plot(tosses, abs_error)
[<matplotlib.lines.Line2D at 0x7937146578b0>]
● Create a uniform random vector with maximum magnitude 10, plot and observe.
● Set a threshold (VT = 2) and count how many times the random function has crossed VT.
● Count how many times the function has gone above and below the threshold
V = np.random.uniform(-10,10,50)
V
array([-2.5019043 , 9.92800898, 9.18860564, 4.03814547, -8.32602085,
-4.62902519, 4.90598813, -2.78021766, 4.40395613, -1.69548297,
-8.02997691, -9.71811404, 3.23565624, -2.84906365, 2.40482712,
-2.58466674, 2.01146746, -9.33397569, 9.66224759, 7.70335279,
-7.76537481, 8.94489937, -9.58613204, -4.28871075, 7.76294716,
-5.27018699, -6.82051899, 5.00622901, -9.9293128 , 9.99295474,
9.37501322, -5.23907958, 4.2588923 , -8.56645244, 6.71349577,
-4.26030998, 9.18278934, -1.6194921 , 5.2129525 , -5.57167894,
6.48638577, 4.75692264, -9.5393969 , -0.23456792, 1.35172229,
-7.2574579 , 4.17570958, -0.05796969, -5.27291005, 4.1732533 ])