Simulation provides a straight forward way of approximately probabilities. For simulating a Game of chance of coin tossing, one simulates a particular random experiment (coin tossing, dice roll, and/or card drawing) a large number of times, and a probability of an outcome is approximated by the relative frequency of the outcome in the repeated experiments.

The use of simulation experiments to better understand probability patterns is called the **Monte Carlo Method**.

### Practical Example: Simulating Coin Tossing Experiment

Let person “A” and person “B” play a simple game involving repeated tosses of a fair coin. In a given toss, if head is observed, “A” wins $1 from “B”; otherwise if tail is tossed, “A” gives $1 to “B”. If “A” starts with zero dollars, we are interested in his fortune as the game is played for 50 tosses.

For the above scenario, one can simulate this game using the R function “sample()”. A’s winning on a particular toss will be $1 or -$1 with equal probability. His winning on 50 repeated tosses can be considered to be a sample of size 50 selected with replacement from the set {$1, -$1}.

option(width=60) sample(c(-1, 1), size = 50, relapce = T) # output [1] -1 1 1 -1 -1 1 -1 1 1 -1 1 -1 1 1 -1 -1 1 -1 -1 -1 1 1 1 -1 -1 [26] 1 -1 1 -1 1 -1 -1 1 1 1 -1 -1 1 -1 -1 -1 1 -1 1 1 -1 1 -1 1 1

One can graphical represent the outcome, as coded below. Note that the results will different for each compilation of the code as samples are drawn randomly.

results <- sample( c(-1, 1), size = 50,replace = TRUE) x = table(results) names(x) = c("loss", "win") barplot(x)

Suppose “A” is interested in his cumulative winnings as he plays this game. One needs to score his individual toss winnings in the variable $win$. The function “`cumsum()`

” will compute the cumulative winnings of the individuals values and the cumulative values are stored in “`cam.win`

“.

win = sample(c(-1, 1), size = 50, replace = T) cum.win = cumsum(win) # Output for different execution [1] -1 -2 -1 -2 -1 0 1 0 -1 0 1 0 1 2 1 2 1 2 3 4 5 6 7 6 5 [26] 4 5 4 5 6 7 6 7 8 9 8 7 6 5 4 5 4 3 2 1 0 -1 -2 -3 -4 [1] 1 2 1 0 1 2 1 2 1 2 1 2 3 4 5 4 5 6 7 8 7 8 7 6 7 6 5 4 5 4 3 4 3 4 5 4 3 2 [39] 3 4 3 4 3 2 3 4 5 6 5 4

Extending and plotting the sequence of cumulative winnings for 4 game. For four games, the win/loss score is plotted in four combined (2 by 2) graphs, to visualize the situation in all four games.

par(mfrow = c(2, 2))

for (j in 1:4){

win = sample( c(-1, 1), size = 50, replace = TRUE)

plot(cumsum(win), type = “l”, ylim = c(-15, 15))

abline(h=0)

}

The horizontal line in each graph is drawn at break-even. The points above the horizontal line show the win situation while the information below the horizontal line shows the loss to the player.

One can make a customize function about the situation discussed above. For example,

# customized function winloss <- function (n=50){ win = sample(c(-1,1), size = n, replace = T) sum(win) } # Insights about win/ loss situation F = replicate(1000, winloss() ) table(F) # output F -22 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 3 6 7 22 28 52 60 78 82 128 93 103 95 72 50 48 34 24 8 3 20 22 24 1 2 1 par(mfrow = c(1, 1) ) plot(table(F))