python coding language 1. Binomial distribution (20 + 10 points) Write a program tosimulate an experiment of
Question:
python coding language
1. Binomial distribution (20 + 10 points) Write a program tosimulate an experiment of tossing a fair coin 16 times and countingthe number of heads. Repeat this experiment 10**5 times to obtainthe number of heads for every 16 tosses; save the number of headsin a vector of size 10**5 (call it headCounts). You should be ableto do this in 1-3 lines of numpy code. (Use np.random.uniform1 togenerate a 2d array of 10**5 x 16 random numbers between 0 and 1,then convert values less than 0.5 to True - or “head”, and countthe number of heads for each simulated coin.) Use this 10**5 vectorto complete the following questions.
a. Plot the histogram of headCounts using plt.hist, withparameter bins = range(18) 2 . Label your plots clearly.
b. Plot the PMF using plt.hist with parameter bins = range(18)and normed=True, or the PMF code in lectures. Label your plotsclearly.
c. Calculate the probability of having NO MORE THAN k heads outof 16 tosses, where k = 0, 1, 2, … 15, 16. Plot this as anempirical CDF (i.e. the probability is calculated from yourexperimental results). You can calculate the probabilities usingvalues returned from 2a/2b by plt.hist. Label your plotsclearly.
d. Use the binomial distribution CDF (use scipy.stats.binom.cdf)to compute the theoretical probability (i.e., from a formulawithout tossing thousands of coins – simulation or real) of havingNO MORE THAN k heads out of 16 tosses, where k = 0, 1, 2, …, 15,16, and compare these probabilities with the probabilities youobtained in 2c. (Plot the probabilities you obtained from thesimulation results in 2c against the probabilities from yourtheoretical calculation here, as either a scatter plot or a linegraph. Plot in log scale is recommended to visualize smallprobabilities.)
code skeleton:
# -*- coding: utf-8 -*-
"""
"""
#%% import necessary modules
import numpy as np
from matplotlib.pyplot import *
#%% Q1 setup
import scipy.stats as stats
import numpy.random as rn
rn.seed(0)
#%% Q1a
m = 10**5; # number of coins
N = 16; # number of tosses each coin
# generate headCounts and plot histogram
plt.figure()
plt.title('Q1a: ')
plt.show()
#%% Q1b
plt.figure()
plt.title('Q1b: ')
plt.show()
#%% Q1c
plt.figure()
plt.title('Q1c: ')
plt.show()
#%% Q1d
plt.figure()
plt.title('Q1d: ')
plt.show()