Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. martingale <- function(max_round = 1000, init_token = 100, bid = 1,
  2. rouletee_prob = c(19/37, 18/37)) {
  3. ## max_round: number of bidding
  4. ## init_token: initial # of chip
  5. ## bid: bidding chip when winning
  6. ## rouletee_prob: c(win, lose)
  7.  
  8. if (sum(rouletee_prob) != 1) stop("sum of 'rouletee_prob' must be 1")
  9.  
  10. capital = init_token ## current # of chip
  11. PL <- 0 # the profit vector of the game
  12.  
  13. i=1
  14. while (capital >= bid && i <= max_round) { # the requirement of bidding on the table
  15. rout = sample(0:1, size = 1, prob = rouletee_prob, replace = T) # rouletee simulation
  16.  
  17. if(rout == 1) {
  18. PL[i] = bid ## win "bid" chips
  19. bid = 1 # bidding 1 chip in the next game
  20. } else {
  21. PL[i] = -bid # lossing "bid" chips
  22. bid=bid*2 # double bid
  23. }
  24.  
  25. capital = capital + PL[i] ## the capital
  26. i=i+1
  27. }
  28.  
  29. init_token + cumsum(PL)
  30. }
  31.  
  32. ## Plot
  33. plot(martingale(),
  34. lwd=3,font=2,
  35. type="l", col="red",
  36. xlab="The # of Bidding",
  37. ylab="Cumulative Chips",
  38. main="Martingale")
  39. abline(h=init_token,col="green",lwd=2)
  40.  
  41.  
  42. ## Simulation
  43. survival_round <- integer(10000)
  44. final_token <- integer(10000)
  45. for (i in 1:10000) {
  46. mar_result <- martingale(max_round = 80, init_token = 10)
  47. survival_round[i] <- length(mar_result)
  48. final_token[i] <- tail(mar_result, n=1)
  49. }
  50.  
  51. hist(survival_round, breaks=100)
  52. hist(final_token, breaks=100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement