# Detecting an Unfair Die with Bayes’ Theorem

(This article was first published on The Chemical Statistician » R programming, and kindly contributed to R-bloggers)

#### Introduction

I saw an interesting problem that requires Bayes’ Theorem and some simple R programming while reading a bioinformatics textbook.  I will discuss the math behind solving this problem in detail, and I will illustrate some very useful plotting functions to generate a plot from R that visualizes the solution effectively.

#### The Problem

The following question is a slightly modified version of Exercise #1.2 on Page 8 in “Biological Sequence Analysis” by Durbin, Eddy, Krogh and Mitchison.

An occasionally dishonest casino uses 2 types of dice.  Of its dice, 97% are fair but 3% are unfair, and a “five” comes up 35% of the time for these unfair dice.  If you pick a die randomly and roll it, how many “fives”  in a row would you need to see before it was most likely that you had picked an unfair die?”

#### Translating the Problem into Math

To translate this problem into the language of probability,

• let $R$ denote the outcome of rolling any die.  The possible outcomes are ${1, 2, 3, 4 , 5, 6}$.
• let $D$ denote whether the die is fair or not.  The possible outcomes are ${\text{Fair, Not Fair}}$.
• let $X$ denote the number of consecutive times a “five” is observed after rolling a die.  The possible outcomes are all non-negative integers.

We know that

• $P(\text{D = Fair}) = 0.97$
• $P(\text{D = Unfair)} = 0.03$
• $P(\text{R = 5} | \text{D = Fair}) = 1/6$
• $P(\text{R = 5} | \text{D = Unfair}) = 0.35$

The die that you picked is either fair or unfair, regardless of how many times you roll the die.  Thus, given that we observe $\text{X = x}$ consecutive fives, a sensible way to decide that a die is unfair is if the probability of the die being fair given $\text{X = x}$ is higher than the probability of the die being unfair given $\text{X = x}$.  Mathematically, we seek the value of $\text{x}$ such that

$P(\text{D = Fair} | \text{X = x}) > 0.5. \ \ \ \ \ (1)$

To see why this equation formulates our problem accurately, let’s assume that it is true.  Combining Equation $(1)$ and the fact that $D$ is a Bernoulli random variable,

$P(\text{D = Fair} | \text{X = x}) = 1 - P(\text{D = Unfair} | \text{X = x}) > 0.5$

$1 - 0.5 = 0.5 > P(\text{D = Unfair} | \text{X = x})$

$P(\text{D = Fair} | \text{X = x}) > 0.5 > P(\text{D = Unfair} | \text{X = x})$

$P(\text{D = Fair} | \text{X = x}) > P(\text{D = Unfair} | \text{X = x})$

and this is the premise of the question.

#### Using Bayes’ Theorem

To calculate $P(\text{D = Fair} | \text{X})$, let’s use Bayes’ Theorem.  I always “work out” Bayes’ Theorem by

1. stating the definition of conditional probability,
2. applying the law of total probability.

To apply the definition of conditional probability for our problem,

$P(\text{D = Fair} | \text{X = x}) = P(\text{D = Fair} \cap \text{X = x}) \ \div \ P(\text{X = x})$.

Now, re-write the joint probability in the numerator by applying the definition of conditional probability again, but reversing the conditionality.

$P(\text{D = Fair} | \text{X = x}) = P(\text{X = x} | \text{D = Fair})P(\text{D = Fair}) \ \div \ P(\text{X = x})$.

Finally, apply the law of total probability to the denominator.  (Because of the difficulties of writing LaTeX in WordPress, the entire denominator is written on the second line.  Please excuse the awkward line placement.)

$P(\text{D = Fair} | \text{X = x}) = P(\text{X = x} | \text{D = Fair})P(\text{D = Fair}) \ \div$

$[P(\text{X = x} | \text{D = Fair})P(\text{D = Fair}) + P(\text{X = x} | \text{D = Unfair})P(\text{D = Unfair})]$

By Equation $(1)$, set 0.5 to be less than the above equation.

$0.5 < P(\text{X = x} | \text{D = Fair})P(\text{D = Fair}) \ \div$

$[P(\text{X = x} | \text{D = Fair})P(\text{D = Fair}) + P(\text{X = x} | \text{D = Unfair})P(\text{D = Unfair})] \ \ \ (2)$

#### Solving for X

To solve the problem, we need to find the minimum value of $\text{x}$ such that Inequality $(2)$ is true.  This inequality cannot be solved analytically, so I computed the right-hand side of $(2)$ for various possible values of $\text{x}$ in R.  Below is the resulting plot.  In addition to plot(), many functions were used to generate it.  I have used text() to add custom text inside the plot, abline() to add a custom line, and axis() to add custom tick marks before, but this is the first time that I have used mtext() to add a custom tick label to the vertical axis.  I encourage you to study the code carefully to learn all of the useful functions and build your own plot with custom axes, tick marks, and tick labels.

Notice that I needed to use the axis() function twice to add all of the tick marks that I wanted.  For some reason, I could not add ’0′ and ’1′ among the first set of tick marks, but I could add them separately in a second calling of axis().

I also found a document by Tian Zheng from Columbia University that lists the names of various colours in R.  It turns out that there are many more colours than the standard ones that I usually use from the colours of a rainbow!

As the plot shows, if you see 5 or more consecutive “fives”, you have reason to suspect that the die is unfair.

##### Detecting an Unfair Die with Bayes' Theorem
##### By Eric Cai - The Chemical Statistician

# set the possible values of the number of consecutive "fives" observed from rolling a die
x = 1:20

# calculate the probability of the die being unfair given the observed number of consecutive "fives"

# export the plot as a PNG image file
png('INSERT YOUR DIRECTORY PATH HERE/unfair die plot.png')

# plot the calculated probabilities
# notice that the "yaxt = 'n'" option suppresses the verticle axis; I want to add my own custom axis later
plot(x, prob.loaded, col=ifelse(x == 5, "forestgreen", "black"), ylab = 'P(Die is Fair | X)', ylim = c(-0.05, 1.05), xlab = 'X - Number of Consecutive Fives Observed', yaxt='n', main = "Using Bayes' Theorem to Detect an Unfair Die")

# add a custom horizontal line to show where P(Die = Unfair | X = x) = 0.5
abline(0.5, 0, col = 'firebrick1')

# add a custom tick mark with the colour 'firebrick1'
# the label is left intentionally blank, because its colour can only be black
# use mtext() to set the tick label's colour
axis(2, at = c(0.5), col = 'firebrick1', labels = ' ')

# add a custom tick label at P(Die = Unfair | X = x) = 0.5
# distinguish it with the colour 'firebrick1'
mtext(text = '0.5', side = 2, line = 1, col = "firebrick1")

# add text to denote the point of interest
text(9, 0.55, 'P(Die is loaded | X = 5) = 0.5581', col = 'forestgreen')

# add some other tick marks to show the other values along the vertical axis
axis(2, at = c(0.1, 0.3, 0.7, 0.9), labels = c('0.1', '0.3', '0.7', '0.9'))
axis(2, at = c(0, 1), labels = c('0', '1'))

dev.off()

#### Reference

Durbin, R. (Ed.). (1998). Biological sequence analysis: probabilistic models of proteins and nucleic acids. Cambridge university press.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...