Created
November 28, 2014 15:12
-
-
Save phewson/2ce8aae3e09a58b8ce3d to your computer and use it in GitHub Desktop.
Parrondo's Paradox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shinyServer(function(input, output) { | |
gameA <- function(p){ | |
WinLose <- sample(c(1,-1), size = 1, prob=c(p,(1-p))) | |
} | |
gameB <- function(CurrentCapital, p1, p2){ | |
WinLose <- 0 | |
if ((CurrentCapital %% 3) == 0){ | |
WinLose <- sample(c(1,-1), size = 1, prob= c(p1, (1-p1))) }else{ | |
WinLose <- sample(c(1,-1), size = 1, prob=c(p2, (1-p2)))} | |
return(WinLose) | |
} | |
gameC <- function(LocalCopyCapital, p, p1, p2, gamma){ | |
switcher <- sample(c("A","B"), 1, prob = c(gamma, (1-gamma))) | |
if(switcher == "A"){ | |
WinLose <- gameA(p) | |
} else { | |
WinLose <- gameB(LocalCopyCapital, p1, p2) | |
} | |
return(WinLose) | |
} | |
#output$SimGameA <- renderPlot({ | |
output$SimGame <- renderPlot({ | |
par(mfrow = c(2,2)) | |
MyCurrentCapitalA <- matrix(0, input$SimLength,input$replicates) | |
for (j in 1:input$replicates){ | |
MyCurrentCapitalA[1,j] <- input$MyInitialCapital | |
for (i in 2:input$SimLength){ | |
if(MyCurrentCapitalA[i-1,j] > 0){ | |
WinLose <- gameA(input$p) | |
MyCurrentCapitalA[i,j] <- MyCurrentCapitalA[i-1,j]+WinLose | |
}else{MyCurrentCapitalA[i,j]<- 0} | |
} | |
} | |
matplot(MyCurrentCapitalA, type = "l", xlab = "Iteration", ylab = "Capital", main = "Game A") | |
#}) | |
#output$SimGameB <- renderPlot({ | |
MyCurrentCapitalB <- matrix(0, input$SimLength,input$replicates) | |
for (j in 1:input$replicates){ | |
MyCurrentCapitalB[1,j] <- input$MyInitialCapital | |
for (i in 2:input$SimLength){ | |
if(MyCurrentCapitalB[i-1,j] > 0){ | |
WinLose <- gameB(MyCurrentCapitalB[i-1,j], input$p1, input$p2) | |
MyCurrentCapitalB[i,j] <- MyCurrentCapitalB[i-1,j]+WinLose | |
}else{MyCurrentCapitalB[i,j]<- 0} | |
} | |
} | |
matplot(MyCurrentCapitalB, type = "l", xlab = "Iteration", ylab = "Capital", main = "Game B") | |
#}) | |
#output$SimGameC <- renderPlot({ | |
MyCurrentCapitalC <- matrix(0, input$SimLength,input$replicates) | |
for (j in 1:input$replicates){ | |
MyCurrentCapitalC[1,j] <- input$MyInitialCapital | |
for (i in 2:input$SimLength){ | |
if(MyCurrentCapitalC[i-1,j] > 0){ | |
WinLose <- gameC(MyCurrentCapitalC[i-1,j], input$p, input$p1, input$p2, input$gamma) | |
MyCurrentCapitalC[i,j] <- MyCurrentCapitalC[i-1,j]+WinLose | |
}else{MyCurrentCapitalC[i,j]<- 0} | |
} | |
} | |
matplot(MyCurrentCapitalC, type = "l", xlab = "Iteration", ylab = "Capital", main = "Game C") | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
# Define UI for application that draws a histogram | |
shinyUI(fluidPage( | |
# Application title | |
titlePanel("Parrondo's Paradox"), | |
# Sidebar with a slider input for the number of bins | |
sidebarLayout( | |
sidebarPanel( | |
sliderInput("p", "p for each Game A:", | |
min = 0.01, | |
max = 0.99, | |
value = 0.45, step = 0.01), | |
sliderInput("p1", "p1 for each Game B:", | |
min = 0.01, | |
max = 0.99, | |
value = 0.01, step = 0.01), | |
sliderInput("p2", "p2 for each Game B:", | |
min = 0.01, | |
max = 0.99, | |
value = 0.9, step = 0.01), | |
sliderInput("gamma", "gamma for each Game C:", | |
min = 0.01, | |
max = 0.99, | |
value = 0.5, step = 0.01), | |
sliderInput("SimLength", "Length of run:", | |
min = 100, | |
max = 5000, | |
value = 500, step = 100), | |
sliderInput("MyInitialCapital", "Initial capital:", | |
min = 10, | |
max = 200, | |
value = 20, step = 5), | |
sliderInput("replicates", "Number of replicates", | |
min=5, max = 100, step = 5, value=5) | |
), | |
# Show a plot of the generated distribution | |
mainPanel( | |
plotOutput("SimGame")#, | |
#plotOutput("SimGameB"), | |
#plotOutput("SimGameC") | |
) | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment