0% found this document useful (0 votes)
11 views

Experiment 6

Uploaded by

nivedhv05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Experiment 6

Uploaded by

nivedhv05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

keyboard_arrow_down Experiment 6 : Coin Toss and the Level Crossing Problem

● Simulate a coin toss that maps a head as 1 and tail as 0.

● 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 ])

# Create a uniform random vector with maximum magnitude 10


random_vector = np.random.uniform(-10, 10, 1000)

# Set the threshold


VT = 2

# Plot the random vector


plt.plot(random_vector, marker='o', linestyle='None')
plt.axhline(VT, color='r', linestyle='--',
label='Threshold (|VT| = 2)')
plt.axhline(-VT, color='r', linestyle='--')
plt.xlabel('Index')
plt.ylabel('Value')
plt.title('Uniform Random Vector with Threshold')
plt.legend()
plt.grid(True)
plt.show()
# Count how many times the random function has
#crossed the threshold
c ossed t e t es o d
crossings_above = np.sum(random_vector > VT)
crossings_below = np.sum(random_vector < -VT)
print(f"Number of times the function crossed above the
threshold: {crossings_above}")
print(f"Number of times the function crossed below the
threshold: {crossings_below}")

You might also like