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 (p) 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.
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.
Theory
The action of tossing a coin has two possible outcomes: Head or Tail. You don’t know which outcome you will obtain on a particular toss, but you do know that it will be either Head or Tail (we rule out the possibility of the coin landing on its edge!). Contrast this with a science experiment. For example, if your experiment is to drop an object, you know the outcome for sure: the object will fall towards the ground. However, tossing a coin is a random experiment, as you do know the set of outcomes, but you do not know the exact outcome for a particular execution of the random experiment.
When we flip a coin there is always a probability to get a head or a tail is 50 percent.
Suppose a coin tossed then we get two possible outcomes either a ‘head’ (H) or a ‘tail’ (T), and it is impossible to predict whether the result of a toss will be a ‘head’ or ‘tail’.
The probability for equally likely outcomes in an event is:
Number of favourable outcomes ÷ Total number of possible outcomes
Total number of possible outcomes = 2
(i) If the favourable outcome is head (H).
Number of favourable outcomes = 1.
Therefore, P(getting a head)
P(H) =Number of favorable outcomestotal number of possible outcomes
= 1/2.
(ii) If the favourable outcome is tail (T).
Number of favourable outcomes = 1.
Therefore, P(getting a tail)
P(H) =Number of favorable outcomestotal number of possible outcomes
= 1/2.
Program for coin toss
import random
def coinToss(number):
recordList= [] # multiple assignment
for i in range(number): # do this 'number' amount of times
flip = random.randint(0, 1)
if (flip == 0):
# print("Heads")
recordList.append(1)
else:
# print("Tails")
recordList.append(0)
print("Coin Toss result is :",str(recordList))
print("No of Heads :",recordList.count(1), "No of Tails :",recordList.count(0))
coinToss(10)
Output
Coin Toss result is : [1, 0, 0, 0, 0, 1, 1, 0, 0, 1]
No of Heads : 4 No of Tails : 6
Program for N vs absolute error
import random
import matplotlib.pyplot as plt
N = [10, 100, 500,1000,5000,10000,50000]
ab_errList = []
for j in N:
recordList= []
for i in range(j): # do this 'number' amount of times
flip = random.randint(0, 1)
if (flip == 0):
# print("Heads")
recordList.append(1)
else:
# print("Tails")
recordList.append(0)
# print("Coin Toss result is :",str(recordList))
print("Value of N is :",j)
print("No of Heads :",recordList.count(1), "No of Tails :",recordList.count(0))
Prob_H = (recordList.count(1))/j
Prob_T = (recordList.count(0))/j
print("Probability of Head is :",Prob_H)
print("Probability of Tail is :",Prob_T)
ab_err = 0.5 - Prob_H
print("Absolute error :",round(ab_err,3))
print("")
ab_errList.append(round(ab_err,3))
print(ab_errList)
plt.plot(N,ab_errList)
plt.title("N vs Absolute error")
plt.xlabel("N")
plt.ylabel("Absolute error")
plt.show()
Output
Value of N is : 100
No of Heads : 56 No of Tails : 44
Probability of Head is : 0.56
Probability of Tail is : 0.44
Absolute error : -0.06
Value of N is : 500
No of Heads : 250 No of Tails : 250
Probability of Head is : 0.5
Probability of Tail is : 0.5
Absolute error : 0.0
Value of N is : 1000
No of Heads : 497 No of Tails : 503
Probability of Head is : 0.497
Probability of Tail is : 0.503
Absolute error : 0.003
Value of N is : 5000
No of Heads : 2529 No of Tails : 2471
Probability of Head is : 0.5058
Probability of Tail is : 0.4942
Absolute error : -0.006
Value of N is : 50000
No of Heads : 25041 No of Tails : 24959
Probability of Head is : 0.50082
Probability of Tail is : 0.49918
Absolute error : -0.001
[-0.06, 0.0, 0.003, -0.006, -0.001]

Program for threshold value
# importing the library files
import numpy as np
import matplotlib.pyplot as plt
#creating 50 uniform random vector with maximum value 10
s = np.random.uniform(0,10,50)
print(s) # printing the generated random values
plt.plot(s) # plotting the values
vt = 2 # setting threshold values
vt_p=np.full(s.shape,vt) # creating a threshold value vector
plt.plot(vt_p) # plotting the threshold value
# counting the values greater than threshold value
g_th = np.count_nonzero(s >= vt)
print(" Number of values crossed threshold values : ",g_th) # printing the count
# counting the values less than threshold value
l_th = np.count_nonzero(s < vt)
print(" Number of values under threshold values : ",l_th)
plt.show() # showing the plot
Output
[8.59601571 2.1675848 1.31812203 6.70697231 7.19163019 9.52270116
6.71189414 9.27098889 8.32502068 3.73868206 0.89465273 0.21778838
4.08227646 4.68405434 7.55652276 5.01307735 3.58359511 4.07557471
8.20357085 1.00133828 3.24105448 6.97113761 1.53276455 0.43369831
9.45985372 7.63847741 7.79364829 7.92720212 8.29402836 9.85319232
2.19810726 3.9236389 2.75348292 6.6756277 6.46252142 1.00647122
4.40360799 3.30109673 6.29580294 0.15285471 2.31633687 1.37331892
4.64744879 4.3281915 8.02196388 3.28039791 2.55710401 0.83424787
4.09506397 1.13120688]
Number of values crossed threshold values : 39
Number of values under threshold values : 11

Inference
Simulated coin toss that maps a head as 1 and tail as 0.
Tossed the coin N = 100, 500, 1000, 5000 and 500000 times and computed the probability (p) of head in each case.
Computed the absolute error | 0:5 - p | in each case and plot against N and understood the law of large numbers.
Created a uniform random vector with maximum magnitude 10, plotted and observed.
Threshold set as (VT = 2) and counted how many times the random function has crossed VT .
Counted how many times the function has gone above and below the threshold.