Exercise 1

Modify the program CoinTosses to toss a coin n times and print out after every 100 tosses the proportion of heads minus 1/2. Do these numbers appear to approach 0 as n increases? Modify the program again to print out, every 100 times, both of the following quantities: the proportion of heads minus 1/2, and the number of heads minus half the number of tosses. Do these numbers appear to approach 0 as n increases?

We are going to flip the coin at 100, 200, 400, 1000 times.

n=10
Flip1Coin = function(n) sample(c("Heads", "Tails"), n, rep = T)
Flip1Coin(n)
##  [1] "Heads" "Heads" "Tails" "Heads" "Tails" "Tails" "Heads" "Heads" "Heads"
## [10] "Heads"
#Flips the coin 100 times
A=Flip1Coin(100)

#tallies the number of heads
sum_heads = sum(A == "Heads")

# Form a table of sum of both heads and tails
table(A)
## A
## Heads Tails 
##    50    50
#find the proportion of heads and tails
prop_table = prop.table(table(A))

#Proportion of heads - 1/2
heads_prop_100 = prop_table["Heads"]-0.5
heads_prop_100
## Heads 
##     0
#the number of heads minus half the number of tosses
sum_100 = sum_heads-n/2
sum_100
## [1] 45
#Flips the coin 200 times
A=Flip1Coin(200)

#tallies the number of heads
sum_heads = sum(A == "Heads")

# Form a table of sum of both heads and tails
table(A)
## A
## Heads Tails 
##   109    91
#find the proportion of heads and tails
prop_table = prop.table(table(A))

#Proportion of heads - 1/2
heads_prop_200 = prop_table["Heads"]-0.5
heads_prop_200
## Heads 
## 0.045
#the number of heads minus half the number of tosses
sum_200 = sum_heads-n/2
sum_200
## [1] 104
#Flips the coin 400 times
A=Flip1Coin(400)

#tallies the number of heads
sum_heads = sum(A == "Heads")

# Form a table of sum of both heads and tails
table(A)
## A
## Heads Tails 
##   203   197
#find the proportion of heads and tails
prop_table = prop.table(table(A))

#Proportion of heads - 1/2
heads_prop_400 = prop_table["Heads"]-0.5
heads_prop_400
##  Heads 
## 0.0075
#the number of heads minus half the number of tosses
sum_400 = sum_heads-n/2
sum_400
## [1] 198
#Flips the coin 1000 times
A=Flip1Coin(1000)

#tallies the number of heads
sum_heads = sum(A == "Heads")

# Form a table of sum of both heads and tails
table(A)
## A
## Heads Tails 
##   495   505
#find the proportion of heads and tails
prop_table = prop.table(table(A))

#Proportion of heads - 1/2
heads_prop_1000 = prop_table["Heads"]-0.5
heads_prop_1000
##  Heads 
## -0.005
#the number of heads minus half the number of tosses
sum_1000 = sum_heads-n/2
sum_1000
## [1] 490

Data Analysis

# Plot the number of toss vs the prop of heads
prop_heads=c(heads_prop_100, heads_prop_200, heads_prop_400, heads_prop_1000)

sums_heads=c(sum_100, sum_200, sum_400, sum_1000)

toss= c(100,200,400,1000)

plot(toss, prop_heads)

plot(toss, sums_heads)

Conclusion

We can tell that as n increases the prop of heads reaches 0 which means the more coin tosses we perform, proportion of heads approaches 50%.