Advertisement
Guest User

Gambling Machine revisited

a guest
Mar 25th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.70 KB | None | 0 0
  1. ### Gambling Machine Puzzle
  2. ### Author: Corey Chivers
  3. ### http://wordplay.blogs.nytimes.com/2013/03/04/machine/
  4. ### original code adapted by: annoporci (R newbie), 25 March 2013.
  5. ### purpose: use continuous uniform distribution on reals instead of discrete distribution on integers
  6.  
  7. ### Doesn't seem to work as intended...
  8.  
  9. ### Refine computations
  10. ### Initialize
  11. rm(list=ls())
  12. xmin = 69
  13. xmax = 72
  14. step = 0.05
  15. df <- data.frame()
  16.  
  17. for( N in list(10,100,1000,10000,100000) )
  18. #for( N in list(1000000) ) # extra large
  19.   { ### start loop over sample size -N-
  20.     vector.cutoff <- numeric(length=0)
  21.     vector.payoff <- numeric(length=0)
  22.    
  23.     for( cutoff in seq(xmin, xmax, by = step) )
  24.     { ### start loop over strategies -cutoff-
  25.       set.seed(123456789)
  26.       x <- runif(N, min = 0, max = 100)
  27.       y <- runif(N, min = 0, max = 100)
  28.       payoff <- numeric(N)
  29.      
  30.       for(i in 1:N)
  31.         { ### start loop over grid values -i-
  32.           ### Correct guess payoff = y
  33.           if( (x[i]>cutoff & y[i]<x[i]) | (x[i]<=cutoff & y[i]>x[i]) )
  34.             payoff[i] <- y[i]
  35.          
  36.           ### Wrong guess payoff = 0
  37.           if( (x[i]>cutoff & y[i]>x[i]) | (x[i]<=cutoff & y[i]<x[i]) )
  38.             payoff[i] < -0
  39.          
  40.           ### Tie payoff =  0
  41.           if(x[i] == y[i])
  42.             payoff[i] <- 0
  43.         } ### end loop over grid values -i-
  44.      
  45.       ### Expected Payoff All Contingencies Considered
  46.       #print(paste(cutoff,mean(payoff)))
  47.       vector.cutoff <- c(vector.cutoff,cutoff)
  48.       vector.payoff <- c(vector.payoff,mean(payoff))
  49.       print(vector.payoff)
  50.       dfN <- data.frame(vector.cutoff, vector.payoff, stringsAsFactors = FALSE)
  51.       colnames(dfN) <- c("Cutoff","Payoff")
  52.      
  53.     } ### end loop over strategies -cutoff-
  54.  
  55.   # export pdf plot
  56.   pdfname <- paste0("gamble-payoff-sample-size-",N,".pdf")
  57.   pdf(pdfname)
  58.   par(cex=0.8) # cex = character expansion factor
  59.   plot(dfN, main = paste( "samples of size",  N )
  60.        , xlab = 'Cutoff', ylab = 'Payoff'
  61.        , pch = 20, col = "red", axes = FALSE )
  62.   axis(1, pos = min(vector.payoff))
  63.   axis(2, pos = min(vector.cutoff), las = 1)
  64.   segments(0, max(vector.payoff), which.max(vector.payoff), max(vector.payoff), lty = "dashed")
  65.   segments(which.max(vector.payoff), min(vector.payoff), which.max(vector.payoff), max(vector.payoff), lty = "dashed")
  66.   dev.off()
  67.  
  68.   assign(paste0("df",N),dfN)
  69.   df <- c(df,dfN)
  70.   print( get(paste0("df",N)) )
  71.   save(dfN, file = paste0("df",N,".RData"))
  72.  
  73.   } ### end loop over sample size -N-
  74.  
  75.  
  76. # clean-up
  77. library(gdata)
  78. # keep only objects with names starting in df
  79. keep(list = ls(pattern = "^df", envir = .GlobalEnv), all = TRUE, sure = TRUE)
  80. rm(dfN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement