Chapter 1.1 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?

set.seed(25)
CoinToss <- function(n){
  s <- sample(0:1,n,rep=T)
  prop_h <- c()
  num_h <- c()
  
  for(i in seq(0,n,100)){
      #Tails
      Num_Tails <- sum(s[c(0:i)]==0) 
      Percent_Tails <- sum(s[c(0:i)]==0)/i
      
      #Heads
      Num_Heads <- sum(s[c(0:i)]==1) 
      Percent_Heads <- sum(s[c(0:i)]==1)/i
      
      #PROPORTION OF HEADS Minus 1/2
      prop_h_minus_half <- Percent_Heads - 0.5
      num_h_minus_half_tosses <- Num_Heads - .5 * i
      index <- i/100
      prop_h[index] <- prop_h_minus_half
      num_h[index] <- num_h_minus_half_tosses
      
      #(prop_h)
      #print(num_h)
  }
  return(cbind(prop_h, num_h))
}

Results of running the CoinToss program for n = 10,000.

CT <- CoinToss(10000)
CT
##               prop_h num_h
##   [1,] -0.0600000000    -6
##   [2,] -0.0350000000    -7
##   [3,] -0.0233333333    -7
##   [4,] -0.0075000000    -3
##   [5,]  0.0160000000     8
##   [6,]  0.0150000000     9
##   [7,]  0.0171428571    12
##   [8,]  0.0112500000     9
##   [9,]  0.0111111111    10
##  [10,]  0.0150000000    15
##  [11,]  0.0163636364    18
##  [12,]  0.0175000000    21
##  [13,]  0.0200000000    26
##  [14,]  0.0200000000    28
##  [15,]  0.0173333333    26
##  [16,]  0.0131250000    21
##  [17,]  0.0176470588    30
##  [18,]  0.0211111111    38
##  [19,]  0.0184210526    35
##  [20,]  0.0155000000    31
##  [21,]  0.0166666667    35
##  [22,]  0.0136363636    30
##  [23,]  0.0117391304    27
##  [24,]  0.0104166667    25
##  [25,]  0.0108000000    27
##  [26,]  0.0088461538    23
##  [27,]  0.0103703704    28
##  [28,]  0.0114285714    32
##  [29,]  0.0134482759    39
##  [30,]  0.0113333333    34
##  [31,]  0.0116129032    36
##  [32,]  0.0081250000    26
##  [33,]  0.0072727273    24
##  [34,]  0.0102941176    35
##  [35,]  0.0088571429    31
##  [36,]  0.0080555556    29
##  [37,]  0.0070270270    26
##  [38,]  0.0039473684    15
##  [39,]  0.0007692308     3
##  [40,]  0.0000000000     0
##  [41,] -0.0007317073    -3
##  [42,] -0.0002380952    -1
##  [43,] -0.0011627907    -5
##  [44,] -0.0002272727    -1
##  [45,] -0.0008888889    -4
##  [46,] -0.0019565217    -9
##  [47,] -0.0006382979    -3
##  [48,] -0.0014583333    -7
##  [49,] -0.0022448980   -11
##  [50,] -0.0014000000    -7
##  [51,] -0.0005882353    -3
##  [52,] -0.0011538462    -6
##  [53,] -0.0016981132    -9
##  [54,] -0.0033333333   -18
##  [55,] -0.0023636364   -13
##  [56,] -0.0026785714   -15
##  [57,] -0.0024561404   -14
##  [58,] -0.0020689655   -12
##  [59,] -0.0013559322    -8
##  [60,] -0.0011666667    -7
##  [61,] -0.0001639344    -1
##  [62,]  0.0006451613     4
##  [63,]  0.0015873016    10
##  [64,]  0.0028125000    18
##  [65,]  0.0018461538    12
##  [66,]  0.0025757576    17
##  [67,]  0.0023880597    16
##  [68,]  0.0025000000    17
##  [69,]  0.0023188406    16
##  [70,]  0.0011428571     8
##  [71,]  0.0004225352     3
##  [72,]  0.0011111111     8
##  [73,]  0.0010958904     8
##  [74,]  0.0001351351     1
##  [75,] -0.0005333333    -4
##  [76,]  0.0000000000     0
##  [77,]  0.0006493506     5
##  [78,] -0.0001282051    -1
##  [79,]  0.0003797468     3
##  [80,] -0.0003750000    -3
##  [81,] -0.0008641975    -7
##  [82,] -0.0001219512    -1
##  [83,] -0.0008433735    -7
##  [84,] -0.0010714286    -9
##  [85,] -0.0007058824    -6
##  [86,] -0.0006976744    -6
##  [87,] -0.0010344828    -9
##  [88,] -0.0012500000   -11
##  [89,] -0.0006741573    -6
##  [90,] -0.0002222222    -2
##  [91,] -0.0007692308    -7
##  [92,] -0.0004347826    -4
##  [93,] -0.0005376344    -5
##  [94,] -0.0007446809    -7
##  [95,] -0.0007368421    -7
##  [96,] -0.0004166667    -4
##  [97,] -0.0003092784    -3
##  [98,] -0.0004081633    -4
##  [99,]  0.0001010101     1
## [100,] -0.0007000000    -7

The plots below show that the numbers do approach zero as the number of tosses increases.

plot(CT[,1], type = "l", xlab= "Number of tosses in hundreds", ylab = "Proportion of Heads - 1/2", main = "PROPORTION of Heads Minus 0.5")

plot(CT[,2], type = "l", xlab= "Number of tosses in hundreds", ylab = "No of Heads - 1/2 No of Tosses", main = "NUMBER of Heads Minus Half the Number of Tosses")