Flashtalk - Monte Carlo Techniques in R

This is a companion presentation to FT-MonteCarloinR.pdf, which describes the basics of building a Monte Carlo method package using the R programming language. This publication provides examples to support the material in the .pdf document.

Problem Examples

Nate Silver's 538 site uses Monte Carlo methods to bridge the gap between his statewide election models and control of the Senate and Presidency.

Here is an example from the 2012 Presidential election, when his blog was still on the New York times.

Note that in spite of a large number of iterations, the Law of Large Numbers does not apply to this model.

In Dealer's Choice Poker, game variations are designed to confuse human intuition about winning hand probabilities but the random elements of the game (shuffling the deck, dealing cards) are mechanical and easy to model. This makes best hand analysis of Dealer's Choice poker an excellent candidate for Monte Carlo methods.

This is a graphic developed with Monte Carlo methods to analyze a variant of a variant (my poker playing friends were creative)

Model Example - wildpoker package

The wprules function just formats the game rules from data frame wpsupportedgames in a readable fashion.

The wpgame function was designed to play any supported poker variant and provide metrics, with enough detail to allow debugging the underlying internal functions.

Here's how the basic model data frame and functions look interactively

``````str(wpsupportedgames)
``````
``````## Error in str(wpsupportedgames): object 'wpsupportedgames' not found
``````
``````wprules("Omaha Hi-Lo")
``````
``````## GAME : Omaha Hi-Lo (Community, max players = 8)
##
## DEAL : 4 hole 5 community
##
## BETS : blind & left of dealer leads - cards 4 7 8 9
##
## WILD : None
##
## MAIN : High - Use exactly 2 hole cards and 3 community cards to make hand (can be different cards for High and Low)
##
## SPLIT: Low - 8-High or lower to win - Ace low ignore Straight or Flush
##
## NOTES: First 2 players bet without looking at cards (small blind and big blind).
``````
``````set.seed(100)
wpgame("Omaha Hi-Lo", 4, "Deuces")
``````
``````## \$table
## \$table\$Extra
## [1] "None"
##
## \$table\$Community
##                PC
## "5C AH 10S 9C 6D"
##
## \$table\$Players
##                 P1                 P2                 P3
## "#5D #2Dw #3S #4H" "#JD #10C #KS #AC" "#KD #9H #2Sw #QS"
##                 P4
##  "#KH #4D #4S #6C"
##
##
## \$showdown
##    maintype         mainhand mainscore splittype       splithand
## P1   3-Kind 5D 5C 2Dw AH 10S      3274    6-High AH 2Dw 3S 5C 6D
## P2   2-Pair AC AH 10C 10S 9C      2859    NoHand AH 5C 6D 10C JD
## P3   3-Kind 9H 9C 2Sw AH 10S      3474    NoHand AH 2Sw 5C 6D 9H
## P4     Pair  6C 6D AH KH 10S      1337    NoHand  AH 4S 5C 6C 9C
##    splitscore winmain winsplit potpct
## P1        311       0        1    0.5
## P2        572       0        0    0.0
## P3        464       1        0    0.5
## P4        466       0        0    0.0
##
## \$summary
##          game          wild   winmainhand  winsplithand
## "Omaha Hi-Lo"      "Deuces"      "3-Kind"      "6-High"
##
## \$detail
## mwscore msscore  mstype swscore ssscore  sstype  wcdeck  wcdeal    pnum
##    3474       1       2     311       1       1       4       4       4
##   mppct   sppct   bppct
##     100     100       0
``````

Monte Carlo Function Example - wildpoker package

The wpstats function starts with a straightforward iterator that just captures key information from wpgame. This data is not saved unless the raw parameter is set to TRUE.

``````head(wpstats("Seven Stud Hi-Lo", 4, "Deuces", numdeal = 500, seed = 100, raw = TRUE))
``````
``````##               game players   wild   mwtype  swtype mwscore msscore mstype
## 1 Seven Stud Hi-Lo       4 Deuces   4-Kind  6-High    7263       1      1
## 2 Seven Stud Hi-Lo       4 Deuces Straight  7-High    4013       1      1
## 3 Seven Stud Hi-Lo       4 Deuces   4-Kind MainWon    7214       1      1
## 4 Seven Stud Hi-Lo       4 Deuces   4-Kind  8-High    7506       1      2
## 5 Seven Stud Hi-Lo       4 Deuces   5-Kind  7-High    9600       1      1
## 6 Seven Stud Hi-Lo       4 Deuces    Flush  8-High    5050       1      1
##   swscore ssscore sstype mppct sppct bppct wcdeck wcdeal
## 1     311       1      1   100   100   100      4      4
## 2     365       1      1   100   100     0      4      4
## 3       0       1      1   100     0     0      4      4
## 4     418       1      1   100   100     0      4      4
## 5     366       1      1   100   100     0      4      4
## 6     411       1      2   100   100   100      4      4
``````

With this raw data, it organizes the data into a complex list designed to support several styles of visualization.

``````wpstats("Seven Stud Hi-Lo", 4, "Deuces", numdeal = 500, seed = 100)
``````
``````## \$game
##              ngame            players             wcards
## "Seven Stud Hi-Lo"                "4"           "Deuces"
##
## \$stats
## numdeal    seed  wcdeck
##     500     100       4
##
## \$pmain
## \$pmain[[1]]
##     HighCard  Pair 2-Pair 3-Kind Straight Flush FullHouse 4-Kind StrFlush
## 1          0 0.000  0.032  0.080    0.296 0.492     0.684  0.912     0.98
## 2          0 0.000  0.006  0.028    0.062 0.024     0.028  0.012     0.00
## 3          0 0.006  0.000  0.006    0.006 0.000     0.000  0.000     0.00
## 4          0 0.006  0.000  0.000    0.000 0.000     0.000  0.000     0.00
## tie        0 0.002  0.000  0.000    0.004 0.002     0.000  0.000     0.00
##     5-Kind
## 1        1
## 2        0
## 3        0
## 4        0
## tie      0
##
##
## \$cmain
## \$cmain[[1]]
##     HighCard Pair 2-Pair 3-Kind Straight Flush FullHouse 4-Kind StrFlush
## 1          0    0      9     21       91    62        83    100       28
## 2          0    0      3     14       31    12        14      6        0
## 3          0    3      0      3        3     0         0      0        0
## 4          0    3      0      0        0     0         0      0        0
## tie        0    1      0      0        2     1         0      0        0
##     5-Kind
## 1       10
## 2        0
## 3        0
## 4        0
## tie      0
##
##
## \$wmain
## \$wmain[[1]]
##   HighCard Pair 2-Pair 3-Kind Straight Flush FullHouse 4-Kind StrFlush
## 4        0    7     12     38      127    75        97    106       28
##   5-Kind
## 4     10
##
##
## \$psplit
## \$psplit[[1]]
##     MainWon 8-High 7-High 6-High 5-High
## 1     0.334  0.562  0.816  0.942      1
## 2     0.000  0.036  0.012  0.000      0
## 3     0.000  0.002  0.000  0.000      0
## 4     0.000  0.000  0.000  0.000      0
## tie   0.004  0.000  0.000  0.000      0
##
##
## \$csplit
## \$csplit[[1]]
##     MainWon 8-High 7-High 6-High 5-High
## 1       167    112    108     57     29
## 2         0     18      6      0      0
## 3         0      1      0      0      0
## 4         0      0      0      0      0
## tie       2      0      0      0      0
##
##
## \$wsplit
## \$wsplit[[1]]
##   MainWon 8-High 7-High 6-High 5-High
## 4     169    131    114     57     29
##
##
## \$potpct
## potval
##    25    50    75   100
## 0.001 0.431 0.002 0.566
``````

Visualization Example - wildpoker package

wpgraphs and interactive use

The wpgraphs function is designed to use a “gstat” object, the output of the wpstats program. Interactively you can go straight from calculation to visualization.

In practice, a useful number of iterations of wpstats takes too long for rapid comparisons between options and games, so legal player values from 2-8 and fixed wildcard variants from 0-7 are precalculated for seed = 52 and numdeal = 1000 and can be called directly.

The 4x4 graph shown here is the default, but the individual Confidence and Hands Won can be split out separately

Feeding wpstats directly into wpgraphs

``````wpgraphs(wpstats("Seven Stud Hi-Lo", 4, "Deuces", numdeal = 500, seed = 100))
``````

Calling a precalculated function (numdeal = 1000, seed = 52)

``````wpgraphs(ngame = "Seven Stud Hi-Lo", players = 4, wcnum = 4)
``````

wildpoker shiny application

This application is the actual decision support system, allowing a user to rapidly compare different game variants, player and wildcard combinations, with easy access to the basic rules and using their preferred graphic options.

https://solbergb.shinyapps.io/wildpoker/