Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Gambling Machine Puzzle
- ### Author: Corey Chivers
- ### http://wordplay.blogs.nytimes.com/2013/03/04/machine/
- ### original code adapted by: annoporci (R newbie), 25 March 2013.
- ### purpose: use continuous uniform distribution on reals instead of discrete distribution on integers
- ### Doesn't seem to work as intended...
- ### Refine computations
- ### Initialize
- rm(list=ls())
- xmin = 69
- xmax = 72
- step = 0.05
- df <- data.frame()
- for( N in list(10,100,1000,10000,100000) )
- #for( N in list(1000000) ) # extra large
- { ### start loop over sample size -N-
- vector.cutoff <- numeric(length=0)
- vector.payoff <- numeric(length=0)
- for( cutoff in seq(xmin, xmax, by = step) )
- { ### start loop over strategies -cutoff-
- set.seed(123456789)
- x <- runif(N, min = 0, max = 100)
- y <- runif(N, min = 0, max = 100)
- payoff <- numeric(N)
- for(i in 1:N)
- { ### start loop over grid values -i-
- ### Correct guess payoff = y
- if( (x[i]>cutoff & y[i]<x[i]) | (x[i]<=cutoff & y[i]>x[i]) )
- payoff[i] <- y[i]
- ### Wrong guess payoff = 0
- if( (x[i]>cutoff & y[i]>x[i]) | (x[i]<=cutoff & y[i]<x[i]) )
- payoff[i] < -0
- ### Tie payoff = 0
- if(x[i] == y[i])
- payoff[i] <- 0
- } ### end loop over grid values -i-
- ### Expected Payoff All Contingencies Considered
- #print(paste(cutoff,mean(payoff)))
- vector.cutoff <- c(vector.cutoff,cutoff)
- vector.payoff <- c(vector.payoff,mean(payoff))
- print(vector.payoff)
- dfN <- data.frame(vector.cutoff, vector.payoff, stringsAsFactors = FALSE)
- colnames(dfN) <- c("Cutoff","Payoff")
- } ### end loop over strategies -cutoff-
- # export pdf plot
- pdfname <- paste0("gamble-payoff-sample-size-",N,".pdf")
- pdf(pdfname)
- par(cex=0.8) # cex = character expansion factor
- plot(dfN, main = paste( "samples of size", N )
- , xlab = 'Cutoff', ylab = 'Payoff'
- , pch = 20, col = "red", axes = FALSE )
- axis(1, pos = min(vector.payoff))
- axis(2, pos = min(vector.cutoff), las = 1)
- segments(0, max(vector.payoff), which.max(vector.payoff), max(vector.payoff), lty = "dashed")
- segments(which.max(vector.payoff), min(vector.payoff), which.max(vector.payoff), max(vector.payoff), lty = "dashed")
- dev.off()
- assign(paste0("df",N),dfN)
- df <- c(df,dfN)
- print( get(paste0("df",N)) )
- save(dfN, file = paste0("df",N,".RData"))
- } ### end loop over sample size -N-
- # clean-up
- library(gdata)
- # keep only objects with names starting in df
- keep(list = ls(pattern = "^df", envir = .GlobalEnv), all = TRUE, sure = TRUE)
- rm(dfN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement