You are on page 1of 1

Probability Theory Assignment

Ishan Kakkar (PHD2105)


8th of October, 2021
In the source (book) the author of the book Digital Dice provides a closed form solution for a fair coin as:

4lmn
μ =  
3(l + m − n − 2)

For the given assumption of l ≤ m ≤ n for any $p the expected tosses for loser ejection or ruin becomes:

1
μ =  
3p(1 − p)

Incorporating both these conditions the Ruin Part of the Monte Carlo Function in R is as follows:

loser_ejection <- function(l, m, n , p){

counter <- c(l, m, n)

toss_count <- 0

while(!any(counter==0)){

trial <- rbinom(3,1,p)

toss_count <- toss_count + 1

if( sum(trial) == 2 ) {

head_1 <- which(trial==1)

tail_0 <- which(trial==0)

counter[head_1] <- counter[head_1] - 1

counter[tail_0] <- counter[tail_0] + 2

} else if (sum(trial)==1){

head_1 <- which(trial==1)

tail_0 <- which(trial==0)

counter[head_1] <- counter[head_1] + 2

counter[tail_0] <- counter[tail_0] - 1

toss_count

Iteration 1 Simulations:
User Defined Value of l :

knitr::opts_chunk$set(eval = FALSE)

l = as.numeric(readline(prompt = "Enter Value of l Coins: "))

## Enter Value of l Coins:

print(paste("The entered value of l is", l))

User Defined Value of m:

knitr::opts_chunk$set(eval = FALSE)

m = as.numeric(readline(prompt = "Enter Value of m Coins: "))

print(paste("The entered value of m is", m))

User Defined Value of n:

knitr::opts_chunk$set(eval = FALSE)

n = as.numeric(readline(prompt = "Enter Value of n Coins: "))

knitr::opts_chunk$set(eval = FALSE)

print(paste("The entered value of n is", n))

User Defined Value of p probability:

knitr::opts_chunk$set(eval = FALSE)

p = as.numeric(readline(prompt = "Enter Value of p probability: "))

print(paste("The entered value of p is", p))

The Simulation Part of the Monte Carlo Function in R for a sequence of 100000 tosses is as follows:

set.seed(1)

knitr::opts_chunk$set(eval = FALSE)

results <- data.frame( l_coins = rep( l, times=1),

m_coins = rep( m, times=1),

n_coins = rep( n, times=1),

p_prob = p)

results$average <- apply(results, 1, function(z) {

mean(replicate(100000,loser_ejection(z[1], z[2], z[3], z[4])))

})

Initializing table for Comparison of different values of l , m, n, p

results_main = results

Iteration 2 Simulations for different values of l, m, n but same


value of p:
User Defined Value of l :

knitr::opts_chunk$set(eval = FALSE)

l = as.numeric(readline(prompt = "Enter Value of l Coins: "))

print(paste("The entered value of l is", l))

User Defined Value of m:

knitr::opts_chunk$set(eval = FALSE)

m = as.numeric(readline(prompt = "Enter Value of m Coins: "))

print(paste("The entered value of m is", m))

User Defined Value of n:

knitr::opts_chunk$set(eval = FALSE)

n = as.numeric(readline(prompt = "Enter Value of n Coins: "))

knitr::opts_chunk$set(eval = FALSE)

print(paste("The entered value of n is", n))

User Defined Value of p probability:

knitr::opts_chunk$set(eval = FALSE)

p = as.numeric(readline(prompt = "Enter Value of p probability: "))

print(paste("The entered value of p is", p))

The Simulation Part of the Monte Carlo Function in R for a sequence of 100000 tosses is as follows:

set.seed(1)

knitr::opts_chunk$set(eval = FALSE)

results <- data.frame( l_coins = rep( l, times=1),

m_coins = rep( m, times=1),

n_coins = rep( n, times=1),

p_prob = p)

results$average <- apply(results, 1, function(z) {

mean(replicate(100000,loser_ejection(z[1], z[2], z[3], z[4])))

})

Appending the results of l , m, n, p in the second iteration to the table initialized previously

results_main = rbind(results_main, results)

Iteration 3 Simulations for different values of l, m, n but same


value of p:
User Defined Value of l :

knitr::opts_chunk$set(eval = FALSE)

l = as.numeric(readline(prompt = "Enter Value of l Coins: "))

print(paste("The entered value of l is", l))

User Defined Value of m:

knitr::opts_chunk$set(eval = FALSE)

m = as.numeric(readline(prompt = "Enter Value of m Coins: "))

print(paste("The entered value of m is", m))

User Defined Value of n:

knitr::opts_chunk$set(eval = FALSE)

n = as.numeric(readline(prompt = "Enter Value of n Coins: "))

knitr::opts_chunk$set(eval = FALSE)

print(paste("The entered value of n is", n))

User Defined Value of p probability:

knitr::opts_chunk$set(eval = FALSE)

p = as.numeric(readline(prompt = "Enter Value of p probability: "))

print(paste("The entered value of p is", p))

The Simulation Part of the Monte Carlo Function in R for a sequence of 100000 tosses is as follows:

set.seed(1)

knitr::opts_chunk$set(eval = FALSE)

results <- data.frame( l_coins = rep( l, times=1),

m_coins = rep( m, times=1),

n_coins = rep( n, times=1),

p_prob = p)

results$average <- apply(results, 1, function(z) {

mean(replicate(100000,loser_ejection(z[1], z[2], z[3], z[4])))

})

Appending the results of l , m, n, p in the second iteration to the table initialized previously

results_main = rbind(results_main, results)

Iteration 4 Simulations for Same values of l, m, n as in Iteration


1 but different value of p:
User Defined Value of l :

knitr::opts_chunk$set(eval = FALSE)

l = as.numeric(readline(prompt = "Enter Value of l Coins: "))

print(paste("The entered value of l is", l))

User Defined Value of m:

knitr::opts_chunk$set(eval = FALSE)

m = as.numeric(readline(prompt = "Enter Value of m Coins: "))

print(paste("The entered value of m is", m))

User Defined Value of n:

knitr::opts_chunk$set(eval = FALSE)

n = as.numeric(readline(prompt = "Enter Value of n Coins: "))

knitr::opts_chunk$set(eval = FALSE)

print(paste("The entered value of n is", n))

User Defined Value of p probability:

knitr::opts_chunk$set(eval = FALSE)

p = as.numeric(readline(prompt = "Enter Value of p probability: "))

print(paste("The entered value of p is", p))

The Simulation Part of the Monte Carlo Function in R for a sequence of 100000 tosses is as follows:

set.seed(1)

knitr::opts_chunk$set(eval = FALSE)

results <- data.frame( l_coins = rep( l, times=1),

m_coins = rep( m, times=1),

n_coins = rep( n, times=1),

p_prob = p)

results$average <- apply(results, 1, function(z) {

mean(replicate(100000,loser_ejection(z[1], z[2], z[3], z[4])))

})

Appending the results of l , m, n, p in the second iteration to the table initialized previously

results_main = rbind(results_main, results)

Iteration 5 Simulations for Same values of l, m, n as in Iteration


2 but different value of p:
User Defined Value of l :

knitr::opts_chunk$set(eval = FALSE)

l = as.numeric(readline(prompt = "Enter Value of l Coins: "))

print(paste("The entered value of l is", l))

User Defined Value of m:

knitr::opts_chunk$set(eval = FALSE)

m = as.numeric(readline(prompt = "Enter Value of m Coins: "))

print(paste("The entered value of m is", m))

User Defined Value of n:

knitr::opts_chunk$set(eval = FALSE)

n = as.numeric(readline(prompt = "Enter Value of n Coins: "))

knitr::opts_chunk$set(eval = FALSE)

print(paste("The entered value of n is", n))

User Defined Value of p probability:

knitr::opts_chunk$set(eval = FALSE)

p = as.numeric(readline(prompt = "Enter Value of p probability: "))

print(paste("The entered value of p is", p))

The Simulation Part of the Monte Carlo Function in R for a sequence of 100000 tosses is as follows:

set.seed(1)

knitr::opts_chunk$set(eval = FALSE)

results <- data.frame( l_coins = rep( l, times=1),

m_coins = rep( m, times=1),

n_coins = rep( n, times=1),

p_prob = p)

results$average <- apply(results, 1, function(z) {

mean(replicate(100000,loser_ejection(z[1], z[2], z[3], z[4])))

})

Appending the results of l , m, n, p in the second iteration to the table initialized previously

results_main = rbind(results_main, results)

Iteration 6 Simulations for Same values of l, m, n as in Iteration


3 but different value of p:
User Defined Value of l :

knitr::opts_chunk$set(eval = FALSE)

l = as.numeric(readline(prompt = "Enter Value of l Coins: "))

print(paste("The entered value of l is", l))

User Defined Value of m:

knitr::opts_chunk$set(eval = FALSE)

m = as.numeric(readline(prompt = "Enter Value of m Coins: "))

print(paste("The entered value of m is", m))

User Defined Value of n:

knitr::opts_chunk$set(eval = FALSE)

n = as.numeric(readline(prompt = "Enter Value of n Coins: "))

knitr::opts_chunk$set(eval = FALSE)

print(paste("The entered value of n is", n))

User Defined Value of p probability:

knitr::opts_chunk$set(eval = FALSE)

p = as.numeric(readline(prompt = "Enter Value of p probability: "))

print(paste("The entered value of p is", p))

The Simulation Part of the Monte Carlo Function in R for a sequence of 100000 tosses is as follows:

set.seed(1)

knitr::opts_chunk$set(eval = FALSE)

results <- data.frame( l_coins = rep( l, times=1),

m_coins = rep( m, times=1),

n_coins = rep( n, times=1),

p_prob = p)

results$average <- apply(results, 1, function(z) {

mean(replicate(100000,loser_ejection(z[1], z[2], z[3], z[4])))

})

Appending the results of l , m, n, p in the second iteration to the table initialized previously

results_main = rbind(results_main, results)

Iteration 7 Simulations for Same values of l, m, n as in Iteration


1 but different value of p:
User Defined Value of l :

knitr::opts_chunk$set(eval = FALSE)

l = as.numeric(readline(prompt = "Enter Value of l Coins: "))

print(paste("The entered value of l is", l))

User Defined Value of m:

knitr::opts_chunk$set(eval = FALSE)

m = as.numeric(readline(prompt = "Enter Value of m Coins: "))

print(paste("The entered value of m is", m))

User Defined Value of n:

knitr::opts_chunk$set(eval = FALSE)

n = as.numeric(readline(prompt = "Enter Value of n Coins: "))

knitr::opts_chunk$set(eval = FALSE)

print(paste("The entered value of n is", n))

User Defined Value of p probability:

knitr::opts_chunk$set(eval = FALSE)

p = as.numeric(readline(prompt = "Enter Value of p probability: "))

print(paste("The entered value of p is", p))

The Simulation Part of the Monte Carlo Function in R for a sequence of 100000 tosses is as follows:

set.seed(1)

knitr::opts_chunk$set(eval = FALSE)

results <- data.frame( l_coins = rep( l, times=1),

m_coins = rep( m, times=1),

n_coins = rep( n, times=1),

p_prob = p)

results$average <- apply(results, 1, function(z) {

mean(replicate(100000,loser_ejection(z[1], z[2], z[3], z[4])))

})

Appending the results of l , m, n, p in the second iteration to the table initialized previously

results_main = rbind(results_main, results)

Iteration 8 Simulations for Same values of l, m, n as in Iteration


2 but different value of p:
User Defined Value of l :

knitr::opts_chunk$set(eval = FALSE)

l = as.numeric(readline(prompt = "Enter Value of l Coins: "))

print(paste("The entered value of l is", l))

User Defined Value of m:

knitr::opts_chunk$set(eval = FALSE)

m = as.numeric(readline(prompt = "Enter Value of m Coins: "))

print(paste("The entered value of m is", m))

User Defined Value of n:

knitr::opts_chunk$set(eval = FALSE)

n = as.numeric(readline(prompt = "Enter Value of n Coins: "))

knitr::opts_chunk$set(eval = FALSE)

print(paste("The entered value of n is", n))

User Defined Value of p probability:

knitr::opts_chunk$set(eval = FALSE)

p = as.numeric(readline(prompt = "Enter Value of p probability: "))

print(paste("The entered value of p is", p))

The Simulation Part of the Monte Carlo Function in R for a sequence of 100000 tosses is as follows:

set.seed(1)

knitr::opts_chunk$set(eval = FALSE)

results <- data.frame( l_coins = rep( l, times=1),

m_coins = rep( m, times=1),

n_coins = rep( n, times=1),

p_prob = p)

results$average <- apply(results, 1, function(z) {

mean(replicate(100000,loser_ejection(z[1], z[2], z[3], z[4])))

})

Appending the results of l , m, n, p in the second iteration to the table initialized previously

results_main = rbind(results_main, results)

Iteration 9 Simulations for Same values of l, m, n as in Iteration


3 but different value of p:
User Defined Value of l :

knitr::opts_chunk$set(eval = FALSE)

l = as.numeric(readline(prompt = "Enter Value of l Coins: "))

print(paste("The entered value of l is", l))

User Defined Value of m:

knitr::opts_chunk$set(eval = FALSE)

m = as.numeric(readline(prompt = "Enter Value of m Coins: "))

print(paste("The entered value of m is", m))

User Defined Value of n:

knitr::opts_chunk$set(eval = FALSE)

n = as.numeric(readline(prompt = "Enter Value of n Coins: "))

knitr::opts_chunk$set(eval = FALSE)

print(paste("The entered value of n is", n))

User Defined Value of p probability:

knitr::opts_chunk$set(eval = FALSE)

p = as.numeric(readline(prompt = "Enter Value of p probability: "))

print(paste("The entered value of p is", p))

The Simulation Part of the Monte Carlo Function in R for a sequence of 100000 tosses is as follows:

set.seed(1)

knitr::opts_chunk$set(eval = FALSE)

results <- data.frame( l_coins = rep( l, times=1),

m_coins = rep( m, times=1),

n_coins = rep( n, times=1),

p_prob = p)

results$average <- apply(results, 1, function(z) {

mean(replicate(100000,loser_ejection(z[1], z[2], z[3], z[4])))

})

Appending the results of l , m, n, p in the second iteration to the table initialized previously

results_main = rbind(results_main, results)

knitr::opts_chunk$set(eval = FALSE)

View(results_main)

1
Hence out of all possible values for p, for p = , the game comes to end quicky. For p → 0 or p → ∞ , it
2
becomes more and more likely that all three coins will show the same face on a toss. Since such an outcome
results in no coins changing hands, this raises the number of tosses we expect to see before one of the men is
ruined.

You might also like