Uploaded by noah20272

Example of PMF,PDF,CDF

advertisement
Example_of_PMF,PDF,CDF
September 16, 2022
[ ]: %matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
##Experiement is toss the fair coin 3 times. X is number of heads. Draw its PMF.
[ ]: def hist(x,no_bins):
#write code here
return bins,probability
[ ]: print('\nLet genrate random numbers for 10000 sample for 3 tosses. ')
random = np.random.rand(10000,3)
print(random[:5])
print('\nConsider >0.5 as head (1) elese tails (0).')
samples=#write code here
print(samples[:5])
print('\nX : Number of heads in each sample ')
X = samples.sum(axis=1)
print(X[:5])
print('\n PMF of X. Number of bins= number of possible random variables')
no_toss = 3
bins,probability = hist(X,no_toss+1)
plt.figure()
plt.bar(bins,probability)
Let genrate random numbers for 10000 sample for 3 tosses.
[[0.63585359 0.72278869 0.33297534]
[0.45968539 0.86801524 0.39887931]
[0.07912994 0.45521963 0.24896988]
[0.58041962 0.49377273 0.22111671]
[0.87400636 0.86318499 0.2733343 ]]
Consider >0.5 as head (1) elese tails (0).
1
[[1
[0
[0
[1
[1
1
1
0
0
1
0]
0]
0]
0]
0]]
X : Number of heads in each sample
[2 1 0 1 2]
PMF of X. Number of bins= number of possible random variables
[ ]: <BarContainer object of 4 artists>
[ ]: print('\nFunction to Generate $n$ tosses of coin')
def toss(n,s):
#write code here
return samples,X
no_toss = 10
no_samples = 10000
samples,X = toss(no_toss,no_samples)
print(samples.shape)
print('\n Histogram of X')
bins,probability = hist(X,no_toss+1)
plt.figure()
plt.bar(bins,probability)
2
Function to Generate $n$ tosses of coin
(10000, 10)
Histogram of X
[ ]: <BarContainer object of 11 artists>
[ ]: print('Function to generate CDF')
def CDF(bins,probability):
#write code here
return bins,cdf
bins,cdf = CDF(bins,probability)
plt.figure()
plt.plot(bins,cdf)
plt.show()
Function to generate CDF
3
4
Download