March 14, 2017 Pre-Class


Project Goals:

With this project we will simulate a famoues probability problem. This will not require knowledge of probability or statistics but only the logic to follow the steps in order to simulate this problem. This is one way to solve problems by using the computer.

1

Gambler’s Ruin: Suppose you have a bankroll of $1000 and make bets of $100 on a fair game. By simulating the outcome directly for at most 5000 iterations of the game (or hands), estimate: a. the probability that you have “busted” (lost all your money) by the time you have placed your one hundredth bet. b. the probability that you have busted by the time you have placed your five hundredth bet by simulating the outcome directly. c. the mean time you go bust, given that you go bust within the first 5000 hands. d. the mean and variance of your bankroll after 100 hands (including busts). e. the mean and variance of your bankroll after 500 hands (including busts).

Note: you must stop playing if your player has gone bust. How will you handle this in the for loop?

For this project, I use the original meaning of gambler’s ruin: a persistent gambler who raises his bet to a fixed fraction of bankroll when he wins, but does not reduce it when he loses, will eventually and inevitably go broke, even if he has a positive expected value on each bet.

(a)

library(epiDisplay)
## Loading required package: foreign
## Warning: package 'foreign' was built under R version 3.2.5
## Loading required package: survival
## Loading required package: MASS
## Loading required package: nnet
GambRuinFunc=function(A,B,p)
{
  Rec=c(A)
  while(A>0) {
    ProbVal=runif(1)
    if(ProbVal <= p)
    {
      B = A+B
#A persistent gambler who raises his bet to a fixed fraction of bankroll when he wins
      A = (B+A)*0.1
    }else{
      B = B-A
#But does not reduce it when he loses
      A = A
      }
    Rec = c(Rec,A)
    if(B<A){break}
  }
  Duration = length(Rec)
  return(Duration)
}
set.seed(1)
Durations = numeric()
M = 5000
for(i in 1:M)
{
  NextDuration = GambRuinFunc(A=100,B=900,p=0.5)
  Durations = c(Durations,NextDuration)
}
prob100<-length(which(Durations<=100))/5000
prob100
## [1] 0.119

The probability that you have “busted” (lost all your money) by the time you have placed your one hundredth bet is 0.1192.

(b)

prob500<-length(which(Durations<=500))/5000
prob500
## [1] 0.477

The probability that you have busted by the time you have placed your five hundredth bet by simulating the outcome directly is 0.4768.

(c)

d5000<-Durations[which(Durations<=5000)]
mean(d5000)
## [1] 759

the mean time you go bust, given that you go bust within the first 5000 hand is 758.8801.

(d)

library(epiDisplay)
GambRuinFunc1=function(A,B,p)
{
  Rec=c(A)
  while(A>0) {
    ProbVal=runif(1)
    if(ProbVal <= p)
    {
      B = A+B
      A = (B+A)*0.1
    }else{
      B = B-A
      A = A
      }
    Rec = c(Rec,A)
    if(length(Rec)==100){break}
    if(B<A){break}
  }
  Duration = length(Rec)
  return(B)
}
set.seed(1)
m100 = numeric()
M = 5000
for(i in 1:M)
{
  nextm = GambRuinFunc1(A=100,B=900,p=0.5)
  m100 = c(m100,nextm)
}
mean(m100)
## [1] 896
var(m100)
## [1] 2841197

The mean of your bankroll after 100 hands (including busts) is 895.8597; The variance of your bankroll after 100 hands (including busts) is 2841197.

(e)

library(epiDisplay)
GambRuinFunc2=function(A,B,p)
{
  Rec=c(A)
  while(A>0) {
    ProbVal=runif(1)
    if(ProbVal <= p)
    {
      B = A+B
      A = (B+A)*0.1
    }else{
      B = B-A
      A = A
      }
    Rec = c(Rec,A)
    if(length(Rec)==500){break}
    if(B<A){break}
  }
  Duration = length(Rec)
  return(B)
}
set.seed(1)
m500 = numeric()
M = 5000
for(i in 1:M)
{
  nextm = GambRuinFunc2(A=100,B=900,p=0.5)
  m500 = c(m500,nextm)
}
mean(m500)
## [1] 574
var(m500)
## [1] 30074552

The mean of your bankroll after 500 hands (including busts) is 573.5246; The variance of your bankroll after 500 hands (including busts) is 30074552.

2

Repeat the previous problem with betting on black in American roulette, where the probability of winning on any spin is 18/38 for an even payout.

(a)

library(epiDisplay)
GambRuinFunc=function(A,B,p)
{
  Rec=c(A)
  while(A>0) {
    ProbVal=runif(1)
    if(ProbVal <= p)
    {
      B = A+B
      A = (B+A)*0.1
    }else{
      B = B-A
      A = A
      }
    Rec = c(Rec,A)
    if(B<A){break}
  }
  Duration = length(Rec)
  return(Duration)
}
set.seed(1)
Durations = numeric()
M = 5000
for(i in 1:M)
{
  NextDuration = GambRuinFunc(A=100,B=900,p=18/38)
  Durations = c(Durations,NextDuration)
}
prob100<-length(which(Durations<=100))/5000
prob100
## [1] 0.173

The probability that you have “busted” (lost all your money) by the time you have placed your one hundredth bet is 0.173.

(b)

prob500<-length(which(Durations<=500))/5000
prob500
## [1] 0.63

The probability that you have busted by the time you have placed your five hundredth bet by simulating the outcome directly is 0.6296.

(c)

d5000<-Durations[which(Durations<=5000)]
mean(d5000)
## [1] 505

the mean time you go bust, given that you go bust within the first 5000 hand is 505.1114.

(d)

library(epiDisplay)
GambRuinFunc1=function(A,B,p)
{
  Rec=c(A)
  while(A>0) {
    ProbVal=runif(1)
    if(ProbVal <= p)
    {
      B = A+B
      A = (B+A)*0.1
    }else{
      B = B-A
      A = A
      }
    Rec = c(Rec,A)
    if(length(Rec)==100){break}
    if(B<A){break}
  }
  Duration = length(Rec)
  return(B)
}
set.seed(1)
m100 = numeric()
M = 5000
for(i in 1:M)
{
  nextm = GambRuinFunc1(A=100,B=900,p=18/38)
  m100 = c(m100,nextm)
}
mean(m100)
## [1] 469
var(m100)
## [1] 849960

The mean of your bankroll after 100 hands (including busts) is 469.1183; The variance of your bankroll after 100 hands (including busts) is 849960.

(e)

library(epiDisplay)
GambRuinFunc2=function(A,B,p)
{
  Rec=c(A)
  while(A>0) {
    ProbVal=runif(1)
    if(ProbVal <= p)
    {
      B = A+B
      A = (B+A)*0.1
    }else{
      B = B-A
      A = A
      }
    Rec = c(Rec,A)
    if(length(Rec)==500){break}
    if(B<A){break}
  }
  Duration = length(Rec)
  return(B)
}
set.seed(1)
m500 = numeric()
M = 5000
for(i in 1:M)
{
  nextm = GambRuinFunc2(A=100,B=900,p=18/38)
  m500 = c(m500,nextm)
}
mean(m500)
## [1] 32.9
var(m500)
## [1] 95787

The mean of your bankroll after 500 hands (including busts) is 32.88478; The variance of your bankroll after 500 hands (including busts) is 95786.5.

3

Markov Chains. Suppose you have a game where the probability of winning on your first hand is 48%; each time you win, that probability goes up by one percentage point for the next game (to a maximum of 100%, where it must stay), and each time you lose, it goes back down to 48%. Assume you cannot go bust and that the size of your wager is a constant $100.

(a)

Is this a fair game? Simulate one hundred thousand sequential hands to determine the size of your return. Then repeat this simulation 99 more times to get a range of values to calculate the expectation.

MCFunc=function(A,B,p)
{
  k=0
  prob<-p
  while(A>0) {
    ProbVal=runif(1)
    if(ProbVal <= p)
    {
      B = B+A
      p = p+0.01
    }else{
      B = B-A
      p = prob
    }
    k=k+1
    if(k==100000){break}
    if(B<A){break}
  }
  return(B)
}

size = numeric()
M = 100
for(i in 1:M)
{
  nextb = MCFunc(A=100,B=900,p=0.48)
  size = c(size,nextb)
}
mean(size)
## [1] 0

The expectation of the size of return is 0, which means one will go busted after one hundred thousand sequential hands, so the game is not fair.

(b)

Repeat this process but change the starting probability to a new value within 2% either way. Get the expected return after 100 repetitions. Keep exploring until you have a return value that is as fair as you can make it. Can you do this automatically?

set.seed(1)
prop = numeric()
for (j in 1:20) {
size = numeric()
M = 100
for(i in 1:M)
{
  nextb = MCFunc(A=100,B=900,p=0.46+0.002*j)
  size = c(size,nextb)
}
nextprop = length(which(size==0))/100
prop<-c(prop,nextprop)
}
prop
##  [1] 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00
## [15] 0.96 0.94 0.88 0.80 0.71 0.67

We found that if we choose the starting probability as 0.5, the proportion of size of return equals to 0 is closest to 0.5, which means the game is fairest under the starting probability of 0.5.

(c)

Repeat again, keeping the initial probability at 48%, but this time change the probability increment to a value different from 1%. Get the expected return after 100 repetitions. Keep changing this value until you have a return value that is as fair as you can make it.

MCFunc1=function(A,B,p,step)
{
  k=0
  prob<-p
  while(A>0) {
    ProbVal=runif(1)
    if(ProbVal <= p)
    {
      B = B+A
      p = p+step
    }else{
      B = B-A
      p = prob
    }
    k=k+1
    if(k==100000){break}
    if(B<A){break}
  }
  return(B)
}

set.seed(1)
prop = numeric()
for (j in 1:20) {
size = numeric()
M = 100
for(i in 1:M)
{
  nextb = MCFunc1(A=100,B=900,p=0.48,step=0.002*j)
  size = c(size,nextb)
}
nextprop = length(which(size==0))/100
prop<-c(prop,nextprop)
}
prop
##  [1] 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 0.94 0.92 0.74 0.70 0.67 0.60
## [15] 0.57 0.57 0.50 0.39 0.38 0.34
0.002*17
## [1] 0.034

We found that if we choose the probability increment to 0.034, the proportion of size of return equals to 0 is 0.5, which means the game is fair under the probability increment of 0.034.

4

For the last two examples in the previous question, you calculated a mean value. Because you saved these final results in a vector, use the bootstrap to estimate the variance of the return in each case for your final answer. Once you have these results, which game has the smaller variance in returns?

For Game 1 (Change the starting probability)

bestp1<-0.5
size1 = numeric()
M = 100
for(i in 1:M)
{
  nextb = MCFunc(A=100,B=900,p=bestp1)
  size1 = c(size1,nextb)
}
###Bootstrap
set.seed(1)
bs1<-sample(size1,10000,replace = TRUE)
mean(bs1)
## [1] 56836
var(bs1)
## [1] 10785467594

For Game 2 (Change the probability increment)

bestp2<-0.034
size2 = numeric()
M = 100
for(i in 1:M)
{
  nextb = MCFunc1(A=100,B=900,p=0.48,step=bestp2)
  size2 = c(size2,nextb)
}
set.seed(1)
bs2<-sample(size2,10000,replace = TRUE)
mean(bs2)
## [1] 5449959
var(bs2)
## [1] 2.47e+13
var(bs1)/var(bs2)
## [1] 0.000437

The variance for game1 is 10785467594, the variance for game1 is 2.469009e+13. The proportion between the variance of two games’ sizes of return (0.0004368338) shows that the first game (Change the starting probability) has the smaller variance in returns.

comments powered by Disqus