##### Lesson 29: Large number games in R #####
# Setting the working directory
setwd(“provide the path to your folder“)
list.files()
###############################################################################
# Coin Toss Experiment #
# Generate 1 uniform random number
r = runif(1)
# Generate 10 uniform random number
r = runif(10)
# Generate 100 uniform random number
r = runif(100)
# coin toss experiment
N = 100
r = runif(N)
x = matrix(NA,nrow = N,ncol = 1)
for (i in 1:N)
{
if(r[i] < 0.5 )
{ x[i,1] = "H" } else
{ x[i,1] = "T" }
}
# Probability of Head
P_H = length(which(x=="H"))/N
print(P_H)
# Probability of Tail
P_T = length(which(x=="T"))/N
print(P_T)
###############################################################################
# Shortcut Methods in R using sample command #
# Coin Toss
# create a vector with H and T named toss to select from
toss = c("H","T")
# use the command sample to sample 1 value from toss with replacement
sample(toss,1,replace=T) # toss a coin 1 time
# sample 10 times will toss a coin 10 times. use replace = T
sample(toss,10,replace=T) # toss a coin 10 time
# sample 100 times will toss a coin 100 times. use replace = T
sample(toss,100,replace=T) # toss a coin 100 time
# to summarize the values use the table command.
x = sample(toss,100,replace=T) # toss a coin 100 times
table(x) # summarize the values in x
# Large Numbers Experiment
# Tossing Coin
toss = c("H","T")
number_toss = seq(from = 5, to = 20000,by=10) # increasing number of tosses
P_H = matrix(NA,nrow=length(number_toss),ncol=1) # create an empty matrix to fill probability each time
P_T = matrix(NA,nrow=length(number_toss),ncol=1) # create an empty matrix to fill probability each time
for (i in 1:length(number_toss))
{
x = sample(toss,number_toss[i],replace=T)
P_H[i,1]=length(which(x=="H"))/number_toss[i]
P_T[i,1]=length(which(x=="T"))/number_toss[i]
}
plot(number_toss,P_H,xlab="Number of Tosses",ylab="Probability of Head",type="l",font=2,font.lab=2)
abline(h=0.5,col="red",lwd=1)
# Rolling Dice
# create a vector with numbers 1 to 6 to replicate a dice
dice = c(1,2,3,4,5,6)
# roll a dice 1 time; i.e. sample 1 number at random from numbers 1 to 6
sample(dice,1,replace=T) # roll a dice 1 time
# roll a dice 10 times; i.e. sample 10 numbers with replacement from numbers 1 to 6
sample(dice,10,replace=T) # roll a dice 10 time
# roll a dice 100 times ; you should approximately get 16 1s, 16 2s etc..
x = sample(dice,100,replace=T) # roll a dice 100 times
table(x)
# Show that the probability of getting a number 1 in a dice is 1/6
# Rolling Dice
dice = c(1,2,3,4,5,6)
number_dice = seq(from = 5, to = 20000,by=10) # increasing number of rolls
P_1 = matrix(NA,nrow=length(number_dice),ncol=1) # create an empty matrix to fill probability each time
for (i in 1:length(number_dice))
{
x = sample(dice,number_dice[i],replace=T)
P_1[i,1]=length(which(x==1))/number_dice[i]
}
plot(number_dice,P_1,xlab="Number of Rolls",ylab="P(1)",font=2,type="l",font.lab=2)
abline(h=(1/6))
# Pebbles in an Urn #
# pick 1 stone from a basket of 10 stones 6 of them are White, 4 of them are Black.
stone = c("W","W","W","W","W","W","B","B","B","B")
sample(stone,1,replace=T) # pick 1 stone from a basket of 10 stones
sample(stone,10,replace=T) # pick 10 stone from a basket of 10 stones
x = sample(stone,10000,replace=T) # pick 10000 stone from a basket of 10 stones
table(x) # summarize
# Show that the probability of getting the color stones approaches the true probability
# Pebbles
stone = c("W","W","W","W","W","W","B","B","B","B")
number = seq(from = 5, to = 20000,by=10) # increasing number of picks
P_W = matrix(NA,nrow=length(number),ncol=1) # create an empty matrix to fill probability each time
P_B = matrix(NA,nrow=length(number),ncol=1) # create an empty matrix to fill probability each time
for (i in 1:length(number))
{
x = sample(stone,number[i],replace=T)
P_W[i,1]=length(which(x=="W"))/number[i]
P_B[i,1]=length(which(x=="B"))/number[i]
}
plot(number,P_W,type="l",font=2,font.lab=2,xlab="Number of Draws with Replacement",ylim=c(0,1),ylab="Probability of a White Pebble")
abline(h=(0.6),col="red")
###############################################################################