Discussion 5
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?
A. See below. Col 1 approaches 0 with some variation. Not so good for column 2.
coinToss <- function(n, bin=100) {
# put n flips in a vector and sum
flips <- cumsum(sample(c(0,1), n, replace=TRUE))
# set the number of 100-count bins
bins <- seq(bin, n, by=bin)
# do the math
results <- as.data.frame(cbind(abs(flips[bins] / bins - .5),
abs(flips[bins] - bins/2)))
colnames(results) <- c('Heads-1/2', 'Heads-1/2*Flips')
return(results)
}
set.seed(1234)
coinToss(10000)## Heads-1/2 Heads-1/2*Flips
## 1 0.0500000000 5
## 2 0.0150000000 3
## 3 0.0033333333 1
## 4 0.0075000000 3
## 5 0.0140000000 7
## 6 0.0300000000 18
## 7 0.0242857143 17
## 8 0.0237500000 19
## 9 0.0211111111 19
## 10 0.0180000000 18
## 11 0.0118181818 13
## 12 0.0025000000 3
## 13 0.0069230769 9
## 14 0.0050000000 7
## 15 0.0060000000 9
## 16 0.0006250000 1
## 17 0.0047058824 8
## 18 0.0033333333 6
## 19 0.0036842105 7
## 20 0.0015000000 3
## 21 0.0023809524 5
## 22 0.0009090909 2
## 23 0.0017391304 4
## 24 0.0012500000 3
## 25 0.0036000000 9
## 26 0.0026923077 7
## 27 0.0033333333 9
## 28 0.0042857143 12
## 29 0.0065517241 19
## 30 0.0060000000 18
## 31 0.0041935484 13
## 32 0.0028125000 9
## 33 0.0024242424 8
## 34 0.0005882353 2
## 35 0.0014285714 5
## 36 0.0002777778 1
## 37 0.0013513514 5
## 38 0.0013157895 5
## 39 0.0005128205 2
## 40 0.0002500000 1
## 41 0.0007317073 3
## 42 0.0007142857 3
## 43 0.0002325581 1
## 44 0.0002272727 1
## 45 0.0011111111 5
## 46 0.0010869565 5
## 47 0.0012765957 6
## 48 0.0012500000 6
## 49 0.0002040816 1
## 50 0.0014000000 7
## 51 0.0005882353 3
## 52 0.0003846154 2
## 53 0.0003773585 2
## 54 0.0014814815 8
## 55 0.0010909091 6
## 56 0.0007142857 4
## 57 0.0003508772 2
## 58 0.0003448276 2
## 59 0.0006779661 4
## 60 0.0010000000 6
## 61 0.0019672131 12
## 62 0.0025806452 16
## 63 0.0031746032 20
## 64 0.0029687500 19
## 65 0.0026153846 17
## 66 0.0040909091 27
## 67 0.0037313433 25
## 68 0.0035294118 24
## 69 0.0033333333 23
## 70 0.0031428571 22
## 71 0.0028169014 20
## 72 0.0018055556 13
## 73 0.0009589041 7
## 74 0.0020270270 15
## 75 0.0016000000 12
## 76 0.0015789474 12
## 77 0.0016883117 13
## 78 0.0019230769 15
## 79 0.0015189873 12
## 80 0.0023750000 19
## 81 0.0013580247 11
## 82 0.0014634146 12
## 83 0.0018072289 15
## 84 0.0020238095 17
## 85 0.0012941176 11
## 86 0.0012790698 11
## 87 0.0008045977 7
## 88 0.0009090909 8
## 89 0.0015730337 14
## 90 0.0017777778 16
## 91 0.0015384615 14
## 92 0.0017391304 16
## 93 0.0015053763 14
## 94 0.0006382979 6
## 95 0.0000000000 0
## 96 0.0012500000 12
## 97 0.0014432990 14
## 98 0.0015306122 15
## 99 0.0013131313 13
## 100 0.0010000000 10