title: “Assignment1” author: “Sabuj Ganguly” date: “2025-09-05” output: html_document
##Coin Flip Simulation Analysis:
rm(list=ls())
n=5
p=0.5 #assuming the coin is unbiased
i=0:n
theo_prob=dbinom(i,n,p) #the given problem is the binomial distribution of size 5 and probability 0.5
theo_prob
## [1] 0.03125 0.15625 0.31250 0.31250 0.15625 0.03125
set.seed(123)
sim_no=1000 #no. of repetition of the coin flipping experiment is 1000
single_experiment=sample(c(0,1),n,replace=TRUE) #this simulates one experiment of flipping a coin n=5 times
single_experiment
## [1] 0 0 0 1 0
sum(single_experiment) #this sum provides us the no. of heads we are getting in one particular experiment
## [1] 1
flips=replicate(sim_no,sum(sample(c(0,1),n,replace=TRUE))) #replicating it 1000 times will provide us the no. of heads occurred in 1000 repetition
table(flips) #this table provides how many times each possible no. of heads (0,1,2,3,4,5)occurred in the 1000 experiments
## flips
## 0 1 2 3 4 5
## 32 148 328 300 160 32
simulated=table(flips)/sim_no
simulated
## flips
## 0 1 2 3 4 5
## 0.032 0.148 0.328 0.300 0.160 0.032
results=data.frame(Heads=0:5,Theoretical=round(theo_prob,3),Empirical=round(as.numeric(simulated[as.character(0:5)]),3)) #comparison of theoretical and empirical result
print(results)
## Heads Theoretical Empirical
## 1 0 0.031 0.032
## 2 1 0.156 0.148
## 3 2 0.312 0.328
## 4 3 0.312 0.300
## 5 4 0.156 0.160
## 6 5 0.031 0.032