-
Notifications
You must be signed in to change notification settings - Fork 5
/
Rolling_The_Die_Likelihood.Rmd
101 lines (77 loc) · 5.61 KB
/
Rolling_The_Die_Likelihood.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
title: "Rolling The Die With Trevor"
author: "Lisa Berghäuser, Melanie Fischer"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Basic Considerations for Trevor's Dice Problem
A basic consideration for the simulation of number of dice rolls needed to attain an eye sum of *SumDice = 17* is that the lowest possible number of dice rolls is 3 (6 eyes + 6 eyes + 5 eyes) and the highest is 17 (with each of the 17 dice rolls yielding 1 eye). Therefore, simulated experiments of 3 to 17 dice rolls can be analysed in order to gain information on the likelihood distribution of attaining a result of exactly 17 eyes with a given number of dice rolls. The expected value is 5.
## Proposed Solution 1
*set.seed* ensures that the experiment is repeatable, including the random numbers.
The vector *Times* with the min and max times of rolling the dice is used for the loop in the first proposed solution. *Resultvec* will be a vector with the same length as *Times*. With *sample* we simulate a random integer between 1 and 6 (equivalent of rolling a dice once), with the number of iterations that *Times* give (min 3, max 17). *sum* sums the result and the number of eyes for each experiment is written into the *Resultvec* at the related place in the vector (, i.e. the sum of the eyes with 3 times rolling at first place of the vector). The right place is identified with *match*, as the index i loops from 3 to 17 and the results must be written into the vector in places 1:15, respectively. The outer for loop defines, how often the experiment is repeated, in this case 1,000 times. The result is saved as *Resultmatrix*, where the *Resultvec* is appended in each iteration.
```{r warning=FALSE}
set.seed(666)
SumDice <- 17
Times <- c(3:SumDice)
Resultvec <- array(dim = length(Times))
rm("Resultmatrix")
for ( j in c( 1:1e4 ) ){
for ( i in Times ){
SumDie <- sum( sample( 1:6, i, replace = T ) )
Resultvec[ match( x = i, table = Times ) ] <- SumDie
}
if( exists( "Resultmatrix" ) ){
Resultmatrix <- cbind( Resultmatrix, Resultvec )
}else{
Resultmatrix <- Resultvec
}
}
```
The plot shows the histograms for each times of rolling the dice in the experiment. The histograms show a wider pink bar for the most frequent occuring sum of eyes. This value is also given in the subtitle of each plot: the sum of eyes that is mostly rolled in each experiment. You can see from the plots, that if you roll the dice 5 times you are most likely to get a sum of eyes that equals 17. This equals the expected value.
```{r, echo=FALSE, out.width = '100%'}
par(mfrow=c(3,5))
for( Zeile in c( 1:( dim( Resultmatrix)[1] ) ) ){
Resulttable <- table( Resultmatrix[ Zeile, ] )
Resultdf <- as.data.frame( Resulttable )
RDdf <- Resultdf$Var1[ match( x = max( Resulttable ), table = Resulttable ) ]
rolleddice <- as.numeric( levels( RDdf ) )[ RDdf ]
plot(Resulttable,
main = paste( "Rolled", Times[Zeile], " times" ),
col = "hotpink",
xlab = paste( "Most rolled sum:", rolleddice ),
ylab = "Frequency"
)
index <- match( x = rolleddice, table = Resulttable )
abline( v = rolleddice, col = "pink", lwd = 6 )
}
```
## Proposed Solution 2
As we also use a for loop in the second solution, we as well have to create an empty vector *resultsVector* into which the results of the calculation in the for loop are subsequently written.
Creating a for loop with the variable *diceRolls*, which takes the integer values of 3 to 17 (potential number of dice rolled to attain a eye sum of 17, see above). Creating a matrix *diceRollsMatrix* with its number of collums representing the number of simulated dice rolls. The matrix is filled with integer values of 1 to 6 (= simulated results of rolling a die) with the function sample(). The row sums of each matrix are calculated (function rowSums()). Then the number of calculated row sums which equal 17 (== 17) are calculated with the sum()-function into *res*. The results of the calculation are added to *resultsVector*. *message* generates a message which informs on the number of dice rolls and the occurrence frequency of exactly 17 eyes as their eye count by using *diceRolls* and *res*.
```{r}
resultsVector <- c()
for (diceRolls in Times) {
diceRollsMatrix <- matrix(c(
sample( 1:6, 100000*diceRolls, replace=TRUE ) ),
ncol = diceRolls, byrow = TRUE
)
res <- sum( rowSums( diceRollsMatrix ) == SumDice )
resultsVector <- c( resultsVector, res )
message( "We roll the die ", diceRolls, " times and obtained ", res,
" out of 100000 experiments exactly ", SumDice, " eyes." )
}
```
### Histogram for Coding Solution 2
In order to illustrate the frequency distribution of dice rolls needed to attain an eye count of 17, the afore-shown results are plotted into one histogram. This is implemented by using the plot() command, setting an integer sequence of the values 3 to 17 (i.e. the number of dice rolls) as x-axis and the resulting vector containing the frequencies of dice rolls with exactly 17 eye sums in a simulated 100,000 experiments as y-axis. The histogram clearly illustrates that rolling five dice yields the greatest frequency of exactly 17 eye counts. This finding is consistent with the known empirical value of 3.5 eyes per rolled die, as 17/3.5 = 4.9.
```{r Plot, echo = FALSE}
plot(x = c(3:17),
y = resultsVector,
xlab = "Number of rolled dice",
ylab = "Frequency",
type = "h",
main = "How many dice do you have to roll to obtain exactly 17 eyes?",
col = "red",
lwd = 5)
```