Simulación Gambler´s Ruin

En este documento se va a realizar una simulación del juego Gambler´s ruin, con probabilidad de ganar 1 euro igual a 0.4 y de perder 1 euro 0.6. El número de euros que se juegan inicialmente ira desde 1 hasta 4 euros y haremos 10000 simulaciones del experimento para cada caso. Nuestro objetivo es realizar un estudio de la probabilidad de obtener una ganancia de 5 unidades frente a la situación de perder todo el capital invertido. Los resultados vienen recogidos al final, donde el valor 0 significa estar en bancarrota y el valor 5 haber alcanzado dicha ganancia.

# EXAMPLES
 
# Simulation by means of replication of the random experimen

# Example: Gambler's ruin

#Consider a gambling game in which on any turn you win €1 with probability p or you lose €1 with probability 1-p. Suppose that you adopt the rule that you quit playing if your fortune reaches €N. If your fortune reaches €0, you are forced to stop gambling.
# Let X_n be the amount of money that you have after n plays.
aux= c(0,0)
for(j in 1:4){

niter=10000
estado=numeric(niter)   

p=0.4 # probability of winning
N=5   # maximum fortune
initial.state=j

for(i in 1:niter){
  

X=rep(NA,1) 
X[1]=initial.state
n=1
while ( (X[n] != 0) && (X[n] != N) ){
  n=n+1
    bet=sample(c(1,-1),1,prob=c(p,1-p))  # simulation of the bet
    X.next=X[n-1]+bet
    X=c(X,X.next)   # the new term is added to the vector
    }
estado[i]=X.next
}

#X
aux= rbind(aux,t(as.matrix(table(estado)/niter)))

}
aux= aux[-1,]
rownames(aux)=1:4
aux
##        0      5
## 1 0.9217 0.0783
## 2 0.8096 0.1904
## 3 0.6384 0.3616
## 4 0.3900 0.6100

Cada fila de la matriz anterior representa el capital inicial jugado.

Tabla repair chair

##      123   12   13   23    1    2    3
## 123 0.93 0.04 0.02 0.01 0.00 0.00 0.00
## 12  0.00 0.97 0.00 0.00 0.02 0.01 0.00
## 13  0.00 0.00 0.95 0.00 0.04 0.00 0.01
## 23  0.00 0.00 0.00 0.94 0.00 0.04 0.02
## 1   1.00 0.00 0.00 0.00 0.00 0.00 0.00
## 2   1.00 0.00 0.00 0.00 0.00 0.00 0.00
## 3   1.00 0.00 0.00 0.00 0.00 0.00 0.00