Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gambler's Ruin: Shiny app at http://www.statistics.calpoly.edu/shiny
Gambler's Ruin Shiny App
Base R code created by Peter Chi
Shiny app files created by Peter Chi
Cal Poly Statistics Dept Shiny Series
http://statistics.calpoly.edu/shiny
Title: Gambler's Ruin
Author: Peter Chi
AuthorUrl: http://statweb.calpoly.edu/~pchi
License: MIT
DisplayMode: Normal
Tags: Gambler's Ruin, Markov Chain
Type: Shiny
The MIT License (MIT)
Copyright (c) 2015 Peter Chi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
##################################################
# R code written by: #
# #
# Peter B. Chi (pchi@calpoly.edu) #
# Department of Statistics #
# Cal Poly State Univ, San Luis Obispo #
# Web: www.calpoly.edu/pchi #
# #
# ............................................ #
# #
# Shiny app site: #
# calpolystat.shinyapps.io/GamblersRuin #
# ............................................ #
# #
# Code updated on: 26SEP2014 #
##################################################
run.ruin<-function(fortuneA,fortuneB,probA){
path<-fortuneA # track with respect to player A
while(path[length(path)]!=0 & path[length(path)]!=(fortuneA+fortuneB)){
path<-c(path,ifelse(rbinom(1,1,probA),path[length(path)]+1,path[length(path)]-1))
}
return(path)
}
last.summ<-function(path){
winner<-ifelse(path[length(path)]==0,"B","A")
turns<-length(path)-1
return(list(winner=winner,turns=turns))
}
run.lots<-function(fortuneA,fortuneB,probA,num){
wins<-rep(NA,num)
game.length<-rep(NA,num)
for(i in 1:num){
one.run<-run.ruin(fortuneA,fortuneB,probA)
result<-last.summ(one.run)
wins[i]<-result$winner
game.length[i]<-result$turns
}
wins.A<-sum(wins=="A")
avg.length<-mean(game.length)
return(list(last.run=one.run,playerA.wins=wins.A,average=avg.length))
}
plot.ruin<-function(path,fortuneA,fortuneB){
summary<-last.summ(path)
par(mar=c(5.1,5,4.1,2.1))
plot(path~seq(0,(length(path)-1)),type='l',main="Graphical Representation of last game",
xlab="Turn Number",ylab="",yaxt="n",ylim=c(0,fortuneA+fortuneB),col="skyblue3",lwd=2.5)
axis(2,at=c(0,fortuneA+fortuneB),labels=c("Player B","Player A"),las=2)
mtext(paste("The winner is Player",summary$winner,"after",summary$turns,"turns"),
3,line=0.25,cex=1.2)
mtext("Fortune",2,line=1)
}
# --------------------------
# App Title: Gambler's Ruin
# Author: Peter Chi
# --------------------------
library(shiny)
source("ruin.R")
shinyServer(function(input, output, session) {
all.out<-reactive({
input$go
withProgress(session, {
setProgress(message = "Calculating, please wait.",
detail = " ", value=1)
isolate(run.lots(input$fortuneA,input$fortuneB,input$probA,input$num))
})
})
output$ruinPlot <- renderPlot({
input$go
isolate(plot.ruin(all.out()$last.run,input$fortuneA,input$fortuneB))
})
output$summary <- renderText({
input$go
isolate(paste("Number of games run: ",input$num,sep="", "\n",
"Number of games won by Player A: ",all.out()$playerA.wins," (percentage=",
round(all.out()$playerA.wins/input$num*100,digits=2),"%)", "\n",
"Average game length: ",round(all.out()$average,digits=2), " turns"))
})
})
# --------------------------
# App Title: Gambler's Ruin
# Author: Peter Chi
# --------------------------
library(shiny)
library(shinyIncubator)
shinyUI(fluidPage(
tags$head(tags$link(rel = "icon", type = "image/x-icon", href =
"https://webresource.its.calpoly.edu/cpwebtemplate/5.0.1/common/images_html/favicon.ico")),
progressInit(),
titlePanel("Gambler's Ruin"),
sidebarLayout(
sidebarPanel(
p("On each 'turn,' Player A wins $1 from Player B, with the probability specified
below. Otherwise, Player B wins $1 from Player A."),
p("A 'game' consists of a series of turns, which continues until one player has all of the money."),
p(),br(),br(),
sliderInput("num",
label="Number of games to run",
min=1,max=100,value=1,step=1),
sliderInput("fortuneA",
label="Initial fortune of Player A",
min=1,max=100,value=10,step=1),
sliderInput("fortuneB",
label="Initial fortune of Player B",
min=1,max=100,value=10,step=1),
p(),br(),
sliderInput("probA",
label="Probability that Player A wins on any given turn:",
min=0,max=1,value=0.5,step=0.005),
br(),
div(actionButton("go",label="Run"),align="right"),
p(),br(),br(),
div("Shiny app by", a(href= "http://statweb.calpoly.edu/pchi/", target="_blank", "Peter Chi"),align="right", style = "font-size: 8pt"),
div("Base R code by", a(href= "http://statweb.calpoly.edu/pchi/", target="_blank", "Peter Chi"),align="right", style = "font-size: 8pt"),
div("Shiny source files:", a(href= "https://gist.github.com/calpolystat/96f9adc5c37f4414fbd1", target="_blank","GitHub Gist"),align="right", style = "font-size: 8pt"),
div(a(href = "http://www.statistics.calpoly.edu/shiny", target="_blank", "Cal Poly Statistics Dept Shiny Series"),align="right", style = "font-size: 8pt")
),
mainPanel(plotOutput("ruinPlot",width="100%"),
verbatimTextOutput("summary"), HTML("<hr style='height: 2px; color: #de7008; background-color: #df7109; border: none;'>"),
p("An application:"),
p("In the casino game of blackjack, the house edge dictates that a player will win any given hand against the house
with an approximate probability of 0.495 (assuming that the player is playing perfect `basic strategy,' and
is not employing any card-counting techniques)."),
p("Suppose
that a player sits at a blackjack table and plans to bet $1 on every hand, and is willing to lose X dollars overall, but will quit if he is
up by Y dollars from his initial fortune, at any moment. Using the inputs above, we can find a simulated probability that he will leave a winner, by putting in X as the initial fortune
of Player A, Y as the initial fortune of Player B (the amount that the player wishes to win from the casino), and 0.495 as the probability that Player A wins
on any given turn, and then running a large number of games (e.g. 100)."),
p("It is rare to find a casino that will actually allow you to bet only $1 on a hand of blackjack; to consider larger bet sizes, we simply need
to divide X and Y by the bet size before inputting the initial fortunes.")
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment