<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Probability on Stats and R</title>
    <link>https://statsandr.com/tags/probability/</link>
    <description>Recent content in Probability on Stats and R</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Wed, 06 Dec 2023 00:00:00 +0000</lastBuildDate>
    
	<atom:link href="https://statsandr.com/tags/probability/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>What is the probability that two persons have the same initials?</title>
      <link>https://statsandr.com/blog/what-is-the-probability-that-two-persons-have-the-same-initials/</link>
      <pubDate>Wed, 06 Dec 2023 00:00:00 +0000</pubDate>
      
      <guid>https://statsandr.com/blog/what-is-the-probability-that-two-persons-have-the-same-initials/</guid>
      <description>

&lt;div id=&#34;TOC&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#introduction&#34; id=&#34;toc-introduction&#34;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#how-likely-is-it&#34; id=&#34;toc-how-likely-is-it&#34;&gt;How likely is it?&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#for-our-team&#34; id=&#34;toc-for-our-team&#34;&gt;For our team&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#for-teams-of-different-sizes&#34; id=&#34;toc-for-teams-of-different-sizes&#34;&gt;For teams of different sizes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#verification&#34; id=&#34;toc-verification&#34;&gt;Verification&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#for-our-team-1&#34; id=&#34;toc-for-our-team-1&#34;&gt;For our team&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#for-teams-of-different-sizes-1&#34; id=&#34;toc-for-teams-of-different-sizes-1&#34;&gt;For teams of different sizes&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#conclusion&#34; id=&#34;toc-conclusion&#34;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img src=&#34;images/what-is-the-probability-that-two-persons-have-the-same-initials.jpeg&#34; style=&#34;width:100.0%&#34; /&gt;&lt;/p&gt;
&lt;div id=&#34;introduction&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;Last week, I joined a team to work on a collaborative project. The team was already established for a few months, with several scientists working together on the project. For simplicity, they used to sign documents, mention colleagues in emails, etc. with their initials (the first letter of their first name followed by the first letter of their last name).&lt;/p&gt;
&lt;p&gt;A couple of days after joining the project, when I needed to sign my first document with my initials, we realized that another person in the team had the exact same initials than me.&lt;/p&gt;
&lt;p&gt;This was not really an issue, as we decided that I would write my initials backward, that is, “SA” instead of “AS”, and the other person would keep signing with “AS” as usual.&lt;/p&gt;
&lt;p&gt;It could have stopped here. However, the idea to write a post about this rather trivial anecdote came to me when the team leader claimed, in the middle of a meeting: “That’s very unfortunate that you two have the same initials! What are the chances of this happening to us?!”.&lt;/p&gt;
&lt;p&gt;We spent a couple of minutes trying to estimate this probability, which in the end were mostly based on our intuitions rather than on a formal calculation. This piqued my curiosity.&lt;/p&gt;
&lt;p&gt;Given that the project we are working on requires the use of simulations, I decided to focus on answering this question via simulations in R. That being said, as for most simulations, it is a good practice to verify these results. This is done using &lt;a href=&#34;https://statsandr.com/blog/the-9-concepts-and-formulas-in-probability-that-every-data-scientist-should-know/&#34;&gt;probability theory&lt;/a&gt;. This comparison will allow to assess the truthfulness of results obtained through simulations.&lt;/p&gt;
&lt;p&gt;Furthermore, I thought that it would be a nice way to illustrate methods not often presented in my posts: for loops, replications and writing functions in R.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;how-likely-is-it&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;How likely is it?&lt;/h1&gt;
&lt;p&gt;Before answering the question raised by the team leader, there are three things to note:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;Although the team leader was curious to know the probability that &lt;em&gt;exactly two persons&lt;/em&gt; have the same initials, we are actually more interested in the probability that &lt;em&gt;at least two persons&lt;/em&gt; have the same initials (as the problem also occurs if more than two persons within a team have the same initials).&lt;/li&gt;
&lt;li&gt;The team consists of 8 people.&lt;/li&gt;
&lt;li&gt;We restrict ourselves to two-letters initials (the first letter being the first letter of the first name, the second letter being the first letter of the last name). This means that middle names are not taken into account, and only the first letter is considered for compound names.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In this post, we will show how to compute this probability:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;in our context, that is, for a team of 8 persons, and&lt;/li&gt;
&lt;li&gt;for completeness, for teams of all sizes from 2 to 100 persons.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As stated in the introduction, we will compute these probabilities first through simulations and then through probability theory.&lt;/p&gt;
&lt;div id=&#34;for-our-team&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;For our team&lt;/h2&gt;
&lt;p&gt;We start by creating a vector of size 8, corresponding to the initials of a team of 8 persons randomly sampled among all 26 letters of the Latin alphabet:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# number of persons
n_persons &amp;lt;- 8

# create vector of initials
initials &amp;lt;- replicate(
  n = n_persons, # number of replications
  paste0(sample(LETTERS, size = 1), sample(LETTERS, size = 1)) # sample letters
)

# display initials
initials&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;UJ&amp;quot; &amp;quot;MN&amp;quot; &amp;quot;XD&amp;quot; &amp;quot;CY&amp;quot; &amp;quot;BB&amp;quot; &amp;quot;ZB&amp;quot; &amp;quot;CU&amp;quot; &amp;quot;HQ&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# are there duplicates?
any(duplicated(initials))&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] FALSE&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As we can see, everyone has different initials in this simulated team of 8 persons, but this will not always be the case.&lt;/p&gt;
&lt;p&gt;To estimate, via simulations, how likely is that at least two persons have the same initials among the team, we need to replicate this vector of 8 sampled initials a large number of times (say 1,000 replications):&lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# number of replications
reps &amp;lt;- 1000

# create and save replications
dat &amp;lt;- replicate(
  n = reps, # number of replications
  replicate(n_persons, paste0(sample(LETTERS, size = 1), sample(LETTERS, size = 1)))
)

# dimensions
dim(dat)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1]    8 1000&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# display first 4 simulated teams
dat[, 1:4]&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##      [,1] [,2] [,3] [,4]
## [1,] &amp;quot;VA&amp;quot; &amp;quot;BU&amp;quot; &amp;quot;LU&amp;quot; &amp;quot;PT&amp;quot;
## [2,] &amp;quot;JG&amp;quot; &amp;quot;SM&amp;quot; &amp;quot;HM&amp;quot; &amp;quot;OL&amp;quot;
## [3,] &amp;quot;BY&amp;quot; &amp;quot;NA&amp;quot; &amp;quot;VJ&amp;quot; &amp;quot;OT&amp;quot;
## [4,] &amp;quot;RT&amp;quot; &amp;quot;CM&amp;quot; &amp;quot;WT&amp;quot; &amp;quot;YT&amp;quot;
## [5,] &amp;quot;PS&amp;quot; &amp;quot;CT&amp;quot; &amp;quot;NB&amp;quot; &amp;quot;QJ&amp;quot;
## [6,] &amp;quot;MG&amp;quot; &amp;quot;KR&amp;quot; &amp;quot;SV&amp;quot; &amp;quot;US&amp;quot;
## [7,] &amp;quot;PL&amp;quot; &amp;quot;SN&amp;quot; &amp;quot;PN&amp;quot; &amp;quot;XW&amp;quot;
## [8,] &amp;quot;NJ&amp;quot; &amp;quot;BR&amp;quot; &amp;quot;DD&amp;quot; &amp;quot;ZC&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The result is a matrix of 8 rows and 1000 columns, where:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;each rows corresponds to the sampled initials of a person, and&lt;/li&gt;
&lt;li&gt;each column corresponds to one simulated team of 8 people.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For better readability, we rename:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the row names as &lt;code&gt;M1&lt;/code&gt; to &lt;code&gt;M8&lt;/code&gt;, corresponding to persons 1 to 8, and&lt;/li&gt;
&lt;li&gt;the column names as &lt;code&gt;T1&lt;/code&gt; to &lt;code&gt;T1000&lt;/code&gt;, corresponding to teams 1 to 1000.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# rename rows
rownames(dat) &amp;lt;- paste0(&amp;quot;M&amp;quot;, 1:n_persons)

# rename columns
colnames(dat) &amp;lt;- paste0(&amp;quot;T&amp;quot;, 1:reps)

# display first 4 simulated teams
dat[, 1:4]&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##    T1   T2   T3   T4  
## M1 &amp;quot;VA&amp;quot; &amp;quot;BU&amp;quot; &amp;quot;LU&amp;quot; &amp;quot;PT&amp;quot;
## M2 &amp;quot;JG&amp;quot; &amp;quot;SM&amp;quot; &amp;quot;HM&amp;quot; &amp;quot;OL&amp;quot;
## M3 &amp;quot;BY&amp;quot; &amp;quot;NA&amp;quot; &amp;quot;VJ&amp;quot; &amp;quot;OT&amp;quot;
## M4 &amp;quot;RT&amp;quot; &amp;quot;CM&amp;quot; &amp;quot;WT&amp;quot; &amp;quot;YT&amp;quot;
## M5 &amp;quot;PS&amp;quot; &amp;quot;CT&amp;quot; &amp;quot;NB&amp;quot; &amp;quot;QJ&amp;quot;
## M6 &amp;quot;MG&amp;quot; &amp;quot;KR&amp;quot; &amp;quot;SV&amp;quot; &amp;quot;US&amp;quot;
## M7 &amp;quot;PL&amp;quot; &amp;quot;SN&amp;quot; &amp;quot;PN&amp;quot; &amp;quot;XW&amp;quot;
## M8 &amp;quot;NJ&amp;quot; &amp;quot;BR&amp;quot; &amp;quot;DD&amp;quot; &amp;quot;ZC&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We now need to compute, among the 1000 teams simulated, how many of them have at least two persons with the same initials:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# transform to data frame
dat &amp;lt;- as.data.frame(dat)

# save which teams have duplicates
duplicates &amp;lt;- rep(NA, reps) # create empty vector
for (i in 1:reps) { # for loop over i from 1 to 1,000
  duplicates[i] &amp;lt;- any(duplicated(dat[, i])) # save results TRUE/FALSE in duplicates vector
}

# count how many teams have duplicates
sum(duplicates)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 41&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, for each column of our data frame &lt;code&gt;dat&lt;/code&gt; (from the first to the 1000th column), we ask whether there are duplicates or not. This is done repeatedly over all columns thanks to a for loop. For each column, the result is &lt;code&gt;TRUE&lt;/code&gt; if there are duplicates, otherwise it is &lt;code&gt;FALSE&lt;/code&gt;. The result of each iteration is saved in the &lt;code&gt;duplicates&lt;/code&gt; vector. As &lt;code&gt;TRUE = 1&lt;/code&gt; and &lt;code&gt;FALSE = 0&lt;/code&gt; in R, we can then count how many columns (and thus teams) have duplicates by summing the number of &lt;code&gt;TRUE&lt;/code&gt; in the &lt;code&gt;duplicates&lt;/code&gt; vector.&lt;/p&gt;
&lt;p&gt;As we can see from the output above, among the 1000 simulated teams, 41 of them have duplicates, that is, 41 of them have at least two persons with the same initials.&lt;/p&gt;
&lt;p&gt;Therefore, based on the simulations, we can expect the probability that at least two persons with the same initials in a team of 8 persons to be close to 4.1%.&lt;/p&gt;
&lt;p&gt;This is a good starting point. Notice, however, that I wrote close to 4.1% because this probability will vary each time it is computed via simulations.&lt;/p&gt;
&lt;p&gt;For instance, if we repeat the exact same process a second time:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# create and save replications
dat &amp;lt;- replicate(
  n = reps, # number of replications
  replicate(n_persons, paste0(sample(LETTERS, size = 1), sample(LETTERS, size = 1)))
)

# transform to data frame
dat &amp;lt;- as.data.frame(dat)

# save which teams have duplicates
duplicates &amp;lt;- rep(NA, reps) # create empty vector
for (i in 1:reps) { # for loop over i from 1 to 1,000
  duplicates[i] &amp;lt;- any(duplicated(dat[, i])) # save results in the duplicates vector (as TRUE/FALSE)
}

# count how many teams have duplicates
sum(duplicates)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 44&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We now find a probability of 4.4%. This is not an error, but it is due to randomness when sampling initials.&lt;/p&gt;
&lt;p&gt;Luckily, we can make the computation of this probability more robust thanks to replications. Intuitively, it works as follows. We repeat the same computation multiple times, giving us a range of possible probabilities. This allows us to assess the uncertainty of our result, and understand how the probability might vary due to taking different random samples of initials.&lt;/p&gt;
&lt;p&gt;So the goal is to compute our probability multiple times (say 100 times), and see its distribution.&lt;/p&gt;
&lt;p&gt;To repeat the same computation multiple times, it is best to write a function in order to avoid copy pasting the same code over and over. So we first write a function (called &lt;code&gt;initials&lt;/code&gt;) which computes the probability that at least two persons share the same initials among a team of &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; people:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;initials &amp;lt;- function(n_persons, reps = 1000) {
  # simulate data
  dat &amp;lt;- as.data.frame(replicate(
    reps,
    replicate(n_persons, paste0(sample(LETTERS, size = 1), sample(LETTERS, size = 1)))
  ))

  # save which teams have duplicates
  duplicates &amp;lt;- rep(NA, reps)
  for (i in 1:reps) {
    duplicates[i] &amp;lt;- any(duplicated(dat[, i]))
  }

  # proportion of teams with duplicates
  return(mean(duplicates))
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A function in R requires to include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the parameters inside &lt;code&gt;()&lt;/code&gt;, and&lt;/li&gt;
&lt;li&gt;the computation inside &lt;code&gt;{}&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We can then use our function to compute the probability that at least two persons share the same initials among a team of 8 people. And we combine it with the &lt;code&gt;replicate()&lt;/code&gt; function to compute this probability 100 times.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# compute and save probabilities
probs &amp;lt;- replicate(100, initials(n_persons = 8))

# display probabilities
probs&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   [1] 0.032 0.037 0.040 0.043 0.033 0.042 0.039 0.047 0.045 0.038 0.052 0.042
##  [13] 0.042 0.040 0.023 0.044 0.041 0.039 0.036 0.048 0.041 0.037 0.027 0.030
##  [25] 0.052 0.038 0.043 0.035 0.038 0.045 0.047 0.044 0.030 0.036 0.036 0.048
##  [37] 0.038 0.045 0.044 0.034 0.031 0.043 0.045 0.034 0.049 0.047 0.051 0.036
##  [49] 0.051 0.040 0.043 0.044 0.038 0.049 0.043 0.050 0.035 0.043 0.048 0.038
##  [61] 0.041 0.044 0.039 0.045 0.033 0.057 0.036 0.043 0.041 0.041 0.041 0.041
##  [73] 0.038 0.044 0.031 0.034 0.049 0.041 0.040 0.034 0.032 0.036 0.049 0.047
##  [85] 0.048 0.038 0.038 0.037 0.036 0.037 0.043 0.040 0.026 0.049 0.046 0.044
##  [97] 0.048 0.038 0.026 0.029&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we visualize the distribution of these 100 probabilities thanks to a histogram and a boxplot (with the &lt;a href=&#34;https://statsandr.com/blog/graphics-in-r-with-ggplot2/&#34;&gt;{ggplot2} package&lt;/a&gt;):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# visualize distribution of the computed probabilities
# build and save plots
library(ggplot2)

p1 &amp;lt;- ggplot(mapping = aes(x = probs)) +
  geom_histogram(color = &amp;quot;black&amp;quot;, fill = &amp;quot;steelblue&amp;quot;, bins = 8) +
  labs(
    x = &amp;quot;Probabilities&amp;quot;,
    y = &amp;quot;Frequencies&amp;quot;
  ) +
  scale_x_continuous(labels = scales::percent) # format x-axis in %

p2 &amp;lt;- ggplot(mapping = aes(x = probs)) +
  geom_boxplot(color = &amp;quot;black&amp;quot;, fill = &amp;quot;steelblue&amp;quot;) +
  labs(x = &amp;quot;Probabilities&amp;quot;) +
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank()
  ) +
  scale_x_continuous(labels = scales::percent) # format x-axis in %

# combine plots
library(patchwork)

p1 + p2&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/what-is-the-probability-that-two-persons-have-the-same-initials/index_files/figure-html/unnamed-chunk-8-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;These two plots show that the probability that at least two persons share the same initials among a team of 8 people is most likely between 3.5% and 4.5%.&lt;/p&gt;
&lt;p&gt;For the record, during the meeting at the root of all this thinking, most of us thought that it was much less likely. Indeed, I believe we were tempted to compute the probability that someone who joins the team has “AS” as initials. This is indeed much less likely, as the probability is only &lt;span class=&#34;math inline&#34;&gt;\(\frac{1}{26} \times \frac{1}{26} \simeq 0.15\%\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;However, this does not take into account the fact:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;that the newcomer can have the same initials as any other person, and&lt;/li&gt;
&lt;li&gt;that it is not only the newcomer who can have the same initials as another person (2 people already working in the team when the newcomer arrives could have the same initials as well).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you are puzzled by this finding, I recommend reading about the &lt;a href=&#34;https://en.wikipedia.org/wiki/Birthday_problem&#34; target=&#34;_blank&#34;&gt;birthday’s paradox&lt;/a&gt;. The birthday’s paradox states that the probability of two people sharing the same birthday becomes surprisingly high with a relatively small group of individuals. In practice, in a group of just 23 people, there is a greater than 50% chance that at least two individuals share the same birthday, illustrating our counterintuitive intuitions about the likelihood of such coincidences. This phenomenon arises due to the multitude of possible birthday pairs within the group, similar to the multitude of possible pairs if initials within a team.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;for-teams-of-different-sizes&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;For teams of different sizes&lt;/h2&gt;
&lt;p&gt;We are now interested in computing this probability not just for a team of 8 persons, but for teams of different sizes. We can do this with the help of our function defined earlier.&lt;/p&gt;
&lt;p&gt;For the illustration, let’s compute the probability that at least two persons have the same initials, for teams of 2 and up to 100 persons:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# set lower and upper bounds of number of persons
min_persons &amp;lt;- 2
max_persons &amp;lt;- 100

# create empty vector of probabilities
probs &amp;lt;- rep(NA, length(min_persons:max_persons))

# compute and save probabilities for teams of size 2 to 100
for (i in min_persons:max_persons) {
  probs[i] &amp;lt;- initials(n_persons = i)
}

# display probabilities
probs&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   [1]    NA 0.001 0.005 0.013 0.012 0.019 0.036 0.040 0.047 0.057 0.074 0.083
##  [13] 0.103 0.128 0.158 0.166 0.178 0.215 0.232 0.260 0.275 0.296 0.300 0.329
##  [25] 0.357 0.392 0.405 0.405 0.439 0.478 0.495 0.536 0.535 0.563 0.578 0.599
##  [37] 0.653 0.656 0.686 0.693 0.715 0.711 0.767 0.760 0.786 0.784 0.814 0.817
##  [49] 0.825 0.826 0.842 0.845 0.867 0.893 0.901 0.920 0.919 0.911 0.917 0.942
##  [61] 0.950 0.951 0.946 0.947 0.969 0.959 0.965 0.964 0.977 0.984 0.977 0.977
##  [73] 0.985 0.978 0.986 0.981 0.989 0.991 0.989 0.988 0.992 0.993 0.994 0.996
##  [85] 0.997 0.995 0.994 0.999 0.999 0.999 1.000 0.999 0.997 1.000 0.999 0.999
##  [97] 0.999 1.000 1.000 0.999&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We are left with storing these probabilities together with the number of persons in the team in a data frame:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# create data frame with saved probabilities and number of persons
dat_plot_sim &amp;lt;- data.frame(
  n_persons = (min_persons - 1):max_persons,
  prob = probs
)

# display first 6 rows
head(dat_plot_sim)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   n_persons  prob
## 1         1    NA
## 2         2 0.001
## 3         3 0.005
## 4         4 0.013
## 5         5 0.012
## 6         6 0.019&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Of course, two people having the same initials in a team of 1 (if we can call this a team…) is impossible.&lt;/p&gt;
&lt;p&gt;An event which is impossible has a probability equal to 0. We thus impute this probability in our data frame, in the first row:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# set proba = 0 when n_person = 1
dat_plot_sim[1, 2] &amp;lt;- 0

# display first 6 rows
head(dat_plot_sim)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   n_persons  prob
## 1         1 0.000
## 2         2 0.001
## 3         3 0.005
## 4         4 0.013
## 5         5 0.012
## 6         6 0.019&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we visualize these probabilities in function of the number of persons in the team:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# visualize probabilities
ggplot(dat_plot_sim) +
  aes(x = n_persons, y = probs) +
  geom_line(linewidth = 1) +
  labs(
    x = &amp;quot;# of persons in the team&amp;quot;,
    y = &amp;quot;Probability&amp;quot;,
    title = &amp;quot;What is the probability that at least 2 persons have the same initials?&amp;quot;
  ) +
  scale_y_continuous(labels = scales::percent) # format y-axis in %&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/what-is-the-probability-that-two-persons-have-the-same-initials/index_files/figure-html/unnamed-chunk-12-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;From the plot above, we see that the probability that at least two persons have the same initials reaches 50% when the team exceeds around 30 people.&lt;/p&gt;
&lt;p&gt;Moreover, notice that this probability becomes close to 100% when the team reaches around 75 people.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;verification&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Verification&lt;/h1&gt;
&lt;p&gt;For the sake of completeness, we now compare results obtained through simulations with results obtained from probability theory.&lt;a href=&#34;#fn2&#34; class=&#34;footnote-ref&#34; id=&#34;fnref2&#34;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;We first define the function that will be used to compare results found above:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# define function
have_same &amp;lt;- function(s, n) {
  sample_space &amp;lt;- s
  probability &amp;lt;- 1
  for (i in 0:(n - 1)) {
    probability &amp;lt;- probability * (sample_space - i) / sample_space
  }
  1 - probability
}&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;for-our-team-1&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;For our team&lt;/h2&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# number of possible two-letter initials
n_initials &amp;lt;- 26^2

# apply function
have_same(n_initials, n_persons)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.0407218&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The probability that at least two persons have the same initials in a team of 8 is 4.07%. This is close to the probability found with simulations, and within the range of 3.5%–4.5%.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;for-teams-of-different-sizes-1&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;For teams of different sizes&lt;/h2&gt;
&lt;p&gt;We now compute the probability for teams between 1 and 100 persons:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# compute and save probabilities for teams between 1 and 100 persons
probs &amp;lt;- vector(length = max_persons)
for (i in 1:max_persons) {
  probs[i] &amp;lt;- have_same(n_initials, i)
}

# create data frame with saved probabilities and number of persons
dat_plot_theory &amp;lt;- data.frame(
  n_persons = (min_persons - 1):max_persons,
  prob = probs
)

# display first 6 rows
head(dat_plot_theory)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##   n_persons        prob
## 1         1 0.000000000
## 2         2 0.001479290
## 3         3 0.004433493
## 4         4 0.008851688
## 5         5 0.014716471
## 6         6 0.022004071&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, we visualize these probabilities in function of the number of persons in the team:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# visualize probabilities
ggplot(dat_plot_theory) +
  aes(x = n_persons, y = probs) +
  geom_line(linewidth = 1) +
  labs(
    x = &amp;quot;# of persons in the team&amp;quot;,
    y = &amp;quot;Probability&amp;quot;,
    title = &amp;quot;What is the probability that at least 2 persons have the same initials?&amp;quot;
  ) +
  scale_y_continuous(labels = scales::percent) # format y-axis in %&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/what-is-the-probability-that-two-persons-have-the-same-initials/index_files/figure-html/unnamed-chunk-16-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;For an easier comparison, we plot probabilities found thanks to simulations and thanks to probability theory on the same plot:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# combine the two data frames into one and add the method as variable
dat_plot_sim$Method &amp;lt;- &amp;quot;Simulations&amp;quot;
dat_plot_theory$Method &amp;lt;- &amp;quot;Theory&amp;quot;
dat_plot_all &amp;lt;- rbind(dat_plot_sim, dat_plot_theory)

# visualize probabilities on same plot
ggplot(dat_plot_all) +
  aes(x = n_persons, y = prob, color = Method) +
  geom_line(linewidth = 1) +
  labs(
    x = &amp;quot;# of persons in the team&amp;quot;,
    y = &amp;quot;Probability&amp;quot;,
    title = &amp;quot;What is the probability that at least 2 persons have the same initials?&amp;quot;
  ) +
  scale_y_continuous(labels = scales::percent) # format y-axis in %&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/what-is-the-probability-that-two-persons-have-the-same-initials/index_files/figure-html/unnamed-chunk-17-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The plot above shows that results using probability theory are relatively similar to results obtained through simulations, indicating that the simulations are trustworthy.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;conclusion&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;The initial question, raised during a meeting, was “What is the probability that, among our team consisting of 8 persons, two have the same initials?”.&lt;/p&gt;
&lt;p&gt;In this post, we first showed how to compute this probability through simulations in R. Secondly, we verified the veracity of the simulations thanks to probability theory. Furthermore, we illustrated how for loops, replications and writing a function can be used in R to answer a probability problem.&lt;/p&gt;
&lt;p&gt;As a side note, it is important to keep in mind that in this post, we assumed the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All letters of the alphabet had the same probability of occurring, meaning that all pairs of initials were equally probable. This is probably not the case in reality, as a first and last name starting both with X is not as probable as a first and last name starting respectively with M and K. This bias could be limited by specifying different weights when sampling initials.&lt;/li&gt;
&lt;li&gt;We restricted ourselves to two-letters initials. Therefore, for compound first or last names, only the first letter is considered. Middle names are also not taken into account.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Last but not least, note that you will find slightly different results than mine, even if you use the exact same code. This is due to randomness. To replicate results as shown in this post, use &lt;code&gt;set.seed(6)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Thanks for reading.&lt;/p&gt;
&lt;p&gt;As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;footnotes footnotes-end-of-document&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;You can always use a larger number of replications, but in our case the final result is similar with more replications, and the aim of the post is more to show the development than the final answer.&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn2&#34;&gt;&lt;p&gt;I thank Richard for writing the first version of the code used for the verifications.&lt;a href=&#34;#fnref2&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>Koh-Lanta 2022: the ambassadors probability problem</title>
      <link>https://statsandr.com/blog/koh-lanta-2022-ambassadors-probability-problem/</link>
      <pubDate>Mon, 16 May 2022 00:00:00 +0000</pubDate>
      
      <guid>https://statsandr.com/blog/koh-lanta-2022-ambassadors-probability-problem/</guid>
      <description>

&lt;div id=&#34;TOC&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#introduction&#34; id=&#34;toc-introduction&#34;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#before-2022&#34; id=&#34;toc-before-2022&#34;&gt;Before 2022&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#in-2022&#34; id=&#34;toc-in-2022&#34;&gt;In 2022&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#probabilities-computation-in-r&#34; id=&#34;toc-probabilities-computation-in-r&#34;&gt;Probabilities computation in R&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#first-draw&#34; id=&#34;toc-first-draw&#34;&gt;First draw&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#second-draw&#34; id=&#34;toc-second-draw&#34;&gt;Second draw&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#third-draw&#34; id=&#34;toc-third-draw&#34;&gt;Third draw&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#game-limited-to-3-draws&#34; id=&#34;toc-game-limited-to-3-draws&#34;&gt;Game limited to 3 draws&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#game-limited-to-5-draws&#34; id=&#34;toc-game-limited-to-5-draws&#34;&gt;Game limited to 5 draws&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#game-limited-to-100-draws&#34; id=&#34;toc-game-limited-to-100-draws&#34;&gt;Game limited to 100 draws&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#game-limited-to-the-number-of-necessary-draws&#34; id=&#34;toc-game-limited-to-the-number-of-necessary-draws&#34;&gt;Game limited to the number of necessary draws&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#final-winning-probabilities&#34; id=&#34;toc-final-winning-probabilities&#34;&gt;Final winning probabilities&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#visual-representations&#34; id=&#34;toc-visual-representations&#34;&gt;Visual representations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#coded-into-a-function&#34; id=&#34;toc-coded-into-a-function&#34;&gt;Coded into a function&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#conclusion&#34; id=&#34;toc-conclusion&#34;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img src=&#34;images/koh-lanta-2022-ambassadors-probability-problem.jpeg&#34; style=&#34;width:100.0%&#34; /&gt;&lt;/p&gt;
&lt;div id=&#34;introduction&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Introduction&lt;/h1&gt;
&lt;p&gt;There is a popular TV show broadcasted in France and the french-speaking part of Belgium called “&lt;strong&gt;Koh-Lanta&lt;/strong&gt;”.&lt;/p&gt;
&lt;p&gt;In this show, several adventurers are dropped off on a desert island with almost no food nor equipment (just a personal backpack with their clothes and a small portion of rice). They must learn to survive on the hostile island by building their hut, finding water and food, etc.&lt;/p&gt;
&lt;p&gt;Each season, adventurers are divided into teams (called tribes), and the teams compete against each other in games involving ability, strength, thinking and endurance. Every other game, the winning tribe receives some food or survival equipment (something to fish or something to make a fire, for instance). The losing tribe receives nothing. For the other half of the games, each member of the losing tribe has to elect an adventurer. The adventurer with the most votes leaves the show definitely. The winning tribe goes back to its island with all of its members.&lt;/p&gt;
&lt;p&gt;At some point during the show, the two competing tribes are grouped together into one single tribe, and it continues this time with each adventurer competing against each other (so they play individually). The winner of the show is the last one to “survive”.&lt;/p&gt;
&lt;p&gt;The 2022 season started with 24 adventurers. Just before being grouped together, each tribe has to select an adventurer in the opposing tribe. The two selected adventurers become ambassadors of their tribe. The two ambassadors must then go to another island in order to choose the adventurer who is going to leaves the show definitely. The adventurer selected by the two ambassadors will not be part of the reunification of the two tribes and her adventure stops there.&lt;/p&gt;
&lt;p&gt;Of course, both ambassadors want to eliminate a member of the opposing tribe (to arrive at the reunification with the most allies). If the two ambassadors cannot agree, they have to play a game that will determine which of the 2 ambassadors must leave the show. This game is entirely based on luck. For the rest of the article, we call this game the &lt;em&gt;ambassadors’ game&lt;/em&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;before-2022&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Before 2022&lt;/h1&gt;
&lt;p&gt;Here were the rules of the ambassadors’ game &lt;em&gt;before&lt;/em&gt; the 2022 season:&lt;/p&gt;
&lt;p&gt;There are two identical urns, one in front of each ambassador. Each urn contains exactly &lt;strong&gt;one black ball&lt;/strong&gt; and &lt;strong&gt;one white ball&lt;/strong&gt;. Each ambassador has to draw a ball among the 2 from his urn. Both urns are of course closed, so no one sees which ball is picked (nor which one is not picked). The winner of the game (remember that the winner stays in the show, the loser has to leave definitely) is the one who picks a white ball while the other ambassador draws a black ball. If both ambassadors draw the same ball (both black or both white), the balls are put back in the urns and the game start over (with the exact same conditions) until the two ambassadors draw a ball of different color.&lt;/p&gt;
&lt;p&gt;For each draw, there are thus four possible results:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;Ambassador from tribe A draws a white ball and ambassador from tribe B draws a black ball: ambassador from tribe A wins.&lt;/li&gt;
&lt;li&gt;Ambassador from tribe A draws a black ball and ambassador from tribe B draws a white ball: ambassador from tribe B wins.&lt;/li&gt;
&lt;li&gt;Both ambassadors draw a black ball: the game start over.&lt;/li&gt;
&lt;li&gt;Both ambassadors draw a white ball: the game start over.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let’s compute the probability for each result to occur. Since the two events are &lt;a href=&#34;https://statsandr.com/blog/the-9-concepts-and-formulas-in-probability-that-every-data-scientist-should-know/#independence-of-two-events&#34;&gt;independent&lt;/a&gt; (the fact that ambassador A draws a white ball does not change the probability for ambassador B to draw a white ball makes the two events independent), we can multiply the probabilities to compute the joint probability of the two events.&lt;/p&gt;
&lt;p&gt;With &lt;span class=&#34;math inline&#34;&gt;\(P_A\)&lt;/span&gt; (&lt;span class=&#34;math inline&#34;&gt;\(P_B\)&lt;/span&gt;) denoting the probability that ambassador from tribe A (B) draws a white ball, we have:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(P_A \cdot (1 - P_B) = 0.5 \cdot 0.5 = 0.25\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\((1 - P_A) \cdot P_B = 0.5 \cdot 0.5 = 0.25\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\((1 - P_A) \cdot (1 - P_B) = 0.5 \cdot 0.5 = 0.25\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(P_A \cdot P_B = 0.5 \cdot 0.5 = 0.25\)&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The sum of the 4 probabilities gives 1 (i.e., 100%), which makes sense since it covers all possible outcomes.&lt;/p&gt;
&lt;p&gt;This means that, &lt;em&gt;on the first draw&lt;/em&gt;, each ambassador has a probability of 25% to win the game (outcome 1 for ambassador A, outcome 2 for ambassador B).&lt;/p&gt;
&lt;p&gt;Of course, since the game is repeated until there is a winner, probabilities for outcomes 3 and 4 tend, in the long run, to decrease until it becomes null (0%). If this statement is not straightforward to you, think about it like this: if you play that game with your friend up to 100 times, what is the probability that there is still no winner, meaning that you and your friend drew the same ball (never a different color) 100 times in a row. You conceive that it is highly unlikely.&lt;/p&gt;
&lt;p&gt;In this context, since both urns are identical and the game is played indefinitely until there is a winner, ambassadors have exactly the same probability of winning that game. It is indeed a 50-50 chance for each of them.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;in-2022&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;In 2022&lt;/h1&gt;
&lt;p&gt;Of course, I would not write an article about that ambassadors’ game if it is was this easy and straightforward.&lt;/p&gt;
&lt;p&gt;The 2022 season differs from the previous ones in the sense that for each game, the losing tribe receives an additional punishment (called a curse in the show). As a consequence, this year, the two urns at the ambassadors’ game were &lt;strong&gt;not&lt;/strong&gt; identical.&lt;/p&gt;
&lt;p&gt;To give you some context, the red tribe won against the yellow tribe in the last game before the ambassadors’ negotiation. The punishment for the yellow tribe was the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The yellow tribe (with its ambassador Colin) had an urn with &lt;strong&gt;2 black balls&lt;/strong&gt; and 1 white ball.&lt;/li&gt;
&lt;li&gt;The red tribe (with its ambassador Louana) had an urn with &lt;strong&gt;1 black ball&lt;/strong&gt; and 1 white ball.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is indeed a punishment for the yellow tribe since the game is not fair anymore: the ambassador of the red tribe clearly has a higher chance of winning that game compared to the ambassador of the yellow tribe.&lt;/p&gt;
&lt;p&gt;For the curious among you, here is how it happened:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The two ambassadors were informed by the presenter Denis Brogniart about the composition of the urns for each tribe.&lt;/li&gt;
&lt;li&gt;Knowing that the odds were not in his favour, Colin (ambassador of the yellow tribe), chose not to play the game.&lt;a href=&#34;#fn1&#34; class=&#34;footnote-ref&#34; id=&#34;fnref1&#34;&gt;&lt;sup&gt;1&lt;/sup&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;They finally agreed on the name of the adventurer who would leave the show (an adventurer of the yellow tribe).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You guessed it by now, the reason for writing this article is of course not to explain you what actually happened—the news sites do it better and much faster than me. The reason is that I wanted to compute the chance of winning the game for each ambassador if they had not agreed on an adventurer to eliminate.&lt;a href=&#34;#fn2&#34; class=&#34;footnote-ref&#34; id=&#34;fnref2&#34;&gt;&lt;sup&gt;2&lt;/sup&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Moreover, I wanted to compute these probabilities in R through simulation. And why not, reuse the code in case organizers of Koh-Lanta decide to change the rules again in the future.&lt;/p&gt;
&lt;p&gt;Even if you do not watch Koh-Lanta (because it is not broadcasted in your country, or you do not like the show), it could be of interest to those of you who want to see:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;how a real life example can be transferred into R,&lt;/li&gt;
&lt;li&gt;and how a &lt;code&gt;for loop&lt;/code&gt; and a function can be used to answer the initial question.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;probabilities-computation-in-r&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Probabilities computation in R&lt;/h1&gt;
&lt;p&gt;For the remaining of this article, we denote:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(p_c\)&lt;/span&gt;, the probability that Colin (ambassador of the yellow tribe) draws a white ball,&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(p_l\)&lt;/span&gt;, the probability that Louana (ambassador of the red tribe) draws a white ball,&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(q_c\)&lt;/span&gt;, the probability that Colin draws a black ball,&lt;/li&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(q_l\)&lt;/span&gt;, the probability that Louana draws a black ball.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Based on the composition of the urns given by Denis Brogniart, we have:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;p_c &amp;lt;- 1 / 3
p_l &amp;lt;- 1 / 2
q_c &amp;lt;- (1 - p_c)
q_l &amp;lt;- (1 - p_l)&lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;first-draw&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;First draw&lt;/h2&gt;
&lt;p&gt;To start easy, let’s first compute the winning probabilities for each ambassador &lt;em&gt;on the first draw only&lt;/em&gt;. Remember that to have a winner, balls must be of different colors.&lt;/p&gt;
&lt;p&gt;Louana wins if and only if Colin draws a black ball and Louana draws a white ball:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Louana winning on first draw
l_win &amp;lt;- q_c * p_l
l_win&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.3333333&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Louana has a 33.33% chance of winning on the first draw.&lt;/p&gt;
&lt;p&gt;On the other hand, Colin wins if and only if Louana draws a black ball and Colin draws a white ball:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Colin winning on first draw
c_win &amp;lt;- q_l * p_c
c_win&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.1666667&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Colin has a 16.67% chance of winning on the first draw.&lt;/p&gt;
&lt;p&gt;You can already see that the game is in favour of Louana, as expected. Let’s see now how it evolves when playing several times.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;second-draw&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Second draw&lt;/h2&gt;
&lt;p&gt;To win exactly on the second draw, it must be a tie on the first draw.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Louana winning on second draw
tie &amp;lt;- (p_c * p_l) + (q_c * q_l)

l_win2 &amp;lt;- tie * l_win
l_win2&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.1666667&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Colin winning on second draw
c_win2 &amp;lt;- tie * c_win
c_win2&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.08333333&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Louana has a 16.67% chance of winning on the second draw.&lt;/li&gt;
&lt;li&gt;Colin has a 8.33% chance of winning on the second draw.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;third-draw&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Third draw&lt;/h2&gt;
&lt;p&gt;To win exactly on the third draw, it must be a tie on the first two draws.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Louana winning on third draw
l_win3 &amp;lt;- (tie^2) * l_win
l_win3&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.08333333&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Colin winning on third draw
c_win3 &amp;lt;- (tie^2) * c_win
c_win3&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.04166667&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Louana has a 8.33% chance of winning on the third draw.&lt;/li&gt;
&lt;li&gt;Colin has a 4.17% chance of winning on the third draw.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A pattern seems to emerge in the code.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;game-limited-to-3-draws&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Game limited to 3 draws&lt;/h2&gt;
&lt;p&gt;We can already compute the probabilities of winning for each ambassador as if the game was limited to three draws, by summing the probabilities for each of the first three draws:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Louana winning on draw 1, 2 or 3
l_win_tot &amp;lt;- l_win + l_win2 + l_win3
l_win_tot&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.5833333&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Colin winning on draw 1, 2 or 3
c_win_tot &amp;lt;- c_win + c_win2 + c_win3
c_win_tot&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.2916667&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Louana has a 58.33% chance of winning if the game is limited to three draws.&lt;/li&gt;
&lt;li&gt;Colin has a 29.17% chance of winning if the game is limited to three draws.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;game-limited-to-5-draws&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Game limited to 5 draws&lt;/h2&gt;
&lt;p&gt;Now if we compute the probabilities as if the game was limited to 5 draws and generalize the computation to see the pattern even more clearly, we have:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Louana winning
l_win_tot &amp;lt;- ((tie^0) * l_win) +
  ((tie^1) * l_win) +
  ((tie^2) * l_win) +
  ((tie^3) * l_win) +
  ((tie^4) * l_win)

l_win_tot&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.6458333&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Colin winning
c_win_tot &amp;lt;- ((tie^0) * c_win) +
  ((tie^1) * c_win) +
  ((tie^2) * c_win) +
  ((tie^3) * c_win) +
  ((tie^4) * c_win)

c_win_tot&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.3229167&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Louana has a 64.58% chance of winning if the game is limited to 5 draws.&lt;/li&gt;
&lt;li&gt;Colin has a 32.29% chance of winning if the game is limited to 5 draws.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;game-limited-to-100-draws&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Game limited to 100 draws&lt;/h2&gt;
&lt;p&gt;We could continue like this for a long time, but for now let’s compute it for up to 100 draws, using a &lt;code&gt;for loop&lt;/code&gt;. Using a &lt;code&gt;for loop&lt;/code&gt; is necessary here in order to avoid to copy-paste our computations a hundred times.&lt;/p&gt;
&lt;p&gt;For the ease of illustration, we compute only the probability of winning for Louana. We will show later on how the probability for Colin can easily be computed.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;n_draws &amp;lt;- 100 # number of draws
l_win_tot &amp;lt;- c() # set empty vector

for (i in 1:n_draws) {
  l_win_tot[i] &amp;lt;- ((tie^(i - 1)) * l_win) # prob of Louana winning up to n_draws
  print(paste0(&amp;quot;Draw &amp;quot;, i, &amp;quot;: &amp;quot;, sum(l_win_tot))) # print sum of winning up to n_draws
}&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Draw 1: 0.333333333333333&amp;quot;
## [1] &amp;quot;Draw 2: 0.5&amp;quot;
## [1] &amp;quot;Draw 3: 0.583333333333333&amp;quot;
## [1] &amp;quot;Draw 4: 0.625&amp;quot;
## [1] &amp;quot;Draw 5: 0.645833333333333&amp;quot;
## [1] &amp;quot;Draw 6: 0.65625&amp;quot;
## [1] &amp;quot;Draw 7: 0.661458333333333&amp;quot;
## [1] &amp;quot;Draw 8: 0.6640625&amp;quot;
## [1] &amp;quot;Draw 9: 0.665364583333333&amp;quot;
## [1] &amp;quot;Draw 10: 0.666015625&amp;quot;
## [1] &amp;quot;Draw 11: 0.666341145833333&amp;quot;
## [1] &amp;quot;Draw 12: 0.66650390625&amp;quot;
## [1] &amp;quot;Draw 13: 0.666585286458333&amp;quot;
## [1] &amp;quot;Draw 14: 0.6666259765625&amp;quot;
## [1] &amp;quot;Draw 15: 0.666646321614583&amp;quot;
## [1] &amp;quot;Draw 16: 0.666656494140625&amp;quot;
## [1] &amp;quot;Draw 17: 0.666661580403646&amp;quot;
## [1] &amp;quot;Draw 18: 0.666664123535156&amp;quot;
## [1] &amp;quot;Draw 19: 0.666665395100911&amp;quot;
## [1] &amp;quot;Draw 20: 0.666666030883789&amp;quot;
## [1] &amp;quot;Draw 21: 0.666666348775228&amp;quot;
## [1] &amp;quot;Draw 22: 0.666666507720947&amp;quot;
## [1] &amp;quot;Draw 23: 0.666666587193807&amp;quot;
## [1] &amp;quot;Draw 24: 0.666666626930237&amp;quot;
## [1] &amp;quot;Draw 25: 0.666666646798452&amp;quot;
## [1] &amp;quot;Draw 26: 0.666666656732559&amp;quot;
## [1] &amp;quot;Draw 27: 0.666666661699613&amp;quot;
## [1] &amp;quot;Draw 28: 0.66666666418314&amp;quot;
## [1] &amp;quot;Draw 29: 0.666666665424903&amp;quot;
## [1] &amp;quot;Draw 30: 0.666666666045785&amp;quot;
## [1] &amp;quot;Draw 31: 0.666666666356226&amp;quot;
## [1] &amp;quot;Draw 32: 0.666666666511446&amp;quot;
## [1] &amp;quot;Draw 33: 0.666666666589056&amp;quot;
## [1] &amp;quot;Draw 34: 0.666666666627862&amp;quot;
## [1] &amp;quot;Draw 35: 0.666666666647264&amp;quot;
## [1] &amp;quot;Draw 36: 0.666666666656965&amp;quot;
## [1] &amp;quot;Draw 37: 0.666666666661816&amp;quot;
## [1] &amp;quot;Draw 38: 0.666666666664241&amp;quot;
## [1] &amp;quot;Draw 39: 0.666666666665454&amp;quot;
## [1] &amp;quot;Draw 40: 0.66666666666606&amp;quot;
## [1] &amp;quot;Draw 41: 0.666666666666364&amp;quot;
## [1] &amp;quot;Draw 42: 0.666666666666515&amp;quot;
## [1] &amp;quot;Draw 43: 0.666666666666591&amp;quot;
## [1] &amp;quot;Draw 44: 0.666666666666629&amp;quot;
## [1] &amp;quot;Draw 45: 0.666666666666648&amp;quot;
## [1] &amp;quot;Draw 46: 0.666666666666657&amp;quot;
## [1] &amp;quot;Draw 47: 0.666666666666662&amp;quot;
## [1] &amp;quot;Draw 48: 0.666666666666664&amp;quot;
## [1] &amp;quot;Draw 49: 0.666666666666666&amp;quot;
## [1] &amp;quot;Draw 50: 0.666666666666666&amp;quot;
## [1] &amp;quot;Draw 51: 0.666666666666666&amp;quot;
## [1] &amp;quot;Draw 52: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 53: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 54: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 55: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 56: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 57: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 58: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 59: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 60: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 61: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 62: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 63: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 64: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 65: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 66: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 67: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 68: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 69: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 70: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 71: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 72: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 73: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 74: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 75: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 76: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 77: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 78: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 79: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 80: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 81: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 82: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 83: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 84: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 85: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 86: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 87: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 88: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 89: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 90: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 91: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 92: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 93: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 94: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 95: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 96: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 97: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 98: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 99: 0.666666666666667&amp;quot;
## [1] &amp;quot;Draw 100: 0.666666666666667&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The probabilities found up to draw 1, 3 and 5 are coherent with what we found in the previous sections. Moreover, we see that from draw 52 onwards, the probability of Louana winning remains constant at 66.67%. This is referred as the limit.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;game-limited-to-the-number-of-necessary-draws&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Game limited to the number of necessary draws&lt;/h2&gt;
&lt;p&gt;Without printing the results of the above &lt;code&gt;for loop&lt;/code&gt;, we do not know how many draws are necessary to reach the limit.&lt;/p&gt;
&lt;p&gt;Let’s now try to include the information about the number of necessary draws in order to avoid computing unnecessary draws:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;n_draws &amp;lt;- 9999 # initial number of draws, set intentionally to a high number
p_tie &amp;lt;- c() # set empty vector for prob of ties
l_win_tot &amp;lt;- c() # set empty vector for prob of Louana winning

# find number of necessary draws:
for (i in 1:n_draws) {
  p_tie[i] &amp;lt;- tie^i # prob of tie for each draw
  limit_ndraws &amp;lt;- sum(p_tie &amp;gt; 2.2e-16) # number of necessary draws
}

# compute Louana winning probabilities with the smallest number of necessary draws
for (i in 1:limit_ndraws) {
  l_win_tot[i] &amp;lt;- ((tie^(i - 1)) * l_win) # prob of Louana winning up to limited number of draws
  print(paste0(&amp;quot;Draw &amp;quot;, i, &amp;quot;: &amp;quot;, sum(l_win_tot))) # sum of winning up to limited number of draws
}&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Draw 1: 0.333333333333333&amp;quot;
## [1] &amp;quot;Draw 2: 0.5&amp;quot;
## [1] &amp;quot;Draw 3: 0.583333333333333&amp;quot;
## [1] &amp;quot;Draw 4: 0.625&amp;quot;
## [1] &amp;quot;Draw 5: 0.645833333333333&amp;quot;
## [1] &amp;quot;Draw 6: 0.65625&amp;quot;
## [1] &amp;quot;Draw 7: 0.661458333333333&amp;quot;
## [1] &amp;quot;Draw 8: 0.6640625&amp;quot;
## [1] &amp;quot;Draw 9: 0.665364583333333&amp;quot;
## [1] &amp;quot;Draw 10: 0.666015625&amp;quot;
## [1] &amp;quot;Draw 11: 0.666341145833333&amp;quot;
## [1] &amp;quot;Draw 12: 0.66650390625&amp;quot;
## [1] &amp;quot;Draw 13: 0.666585286458333&amp;quot;
## [1] &amp;quot;Draw 14: 0.6666259765625&amp;quot;
## [1] &amp;quot;Draw 15: 0.666646321614583&amp;quot;
## [1] &amp;quot;Draw 16: 0.666656494140625&amp;quot;
## [1] &amp;quot;Draw 17: 0.666661580403646&amp;quot;
## [1] &amp;quot;Draw 18: 0.666664123535156&amp;quot;
## [1] &amp;quot;Draw 19: 0.666665395100911&amp;quot;
## [1] &amp;quot;Draw 20: 0.666666030883789&amp;quot;
## [1] &amp;quot;Draw 21: 0.666666348775228&amp;quot;
## [1] &amp;quot;Draw 22: 0.666666507720947&amp;quot;
## [1] &amp;quot;Draw 23: 0.666666587193807&amp;quot;
## [1] &amp;quot;Draw 24: 0.666666626930237&amp;quot;
## [1] &amp;quot;Draw 25: 0.666666646798452&amp;quot;
## [1] &amp;quot;Draw 26: 0.666666656732559&amp;quot;
## [1] &amp;quot;Draw 27: 0.666666661699613&amp;quot;
## [1] &amp;quot;Draw 28: 0.66666666418314&amp;quot;
## [1] &amp;quot;Draw 29: 0.666666665424903&amp;quot;
## [1] &amp;quot;Draw 30: 0.666666666045785&amp;quot;
## [1] &amp;quot;Draw 31: 0.666666666356226&amp;quot;
## [1] &amp;quot;Draw 32: 0.666666666511446&amp;quot;
## [1] &amp;quot;Draw 33: 0.666666666589056&amp;quot;
## [1] &amp;quot;Draw 34: 0.666666666627862&amp;quot;
## [1] &amp;quot;Draw 35: 0.666666666647264&amp;quot;
## [1] &amp;quot;Draw 36: 0.666666666656965&amp;quot;
## [1] &amp;quot;Draw 37: 0.666666666661816&amp;quot;
## [1] &amp;quot;Draw 38: 0.666666666664241&amp;quot;
## [1] &amp;quot;Draw 39: 0.666666666665454&amp;quot;
## [1] &amp;quot;Draw 40: 0.66666666666606&amp;quot;
## [1] &amp;quot;Draw 41: 0.666666666666364&amp;quot;
## [1] &amp;quot;Draw 42: 0.666666666666515&amp;quot;
## [1] &amp;quot;Draw 43: 0.666666666666591&amp;quot;
## [1] &amp;quot;Draw 44: 0.666666666666629&amp;quot;
## [1] &amp;quot;Draw 45: 0.666666666666648&amp;quot;
## [1] &amp;quot;Draw 46: 0.666666666666657&amp;quot;
## [1] &amp;quot;Draw 47: 0.666666666666662&amp;quot;
## [1] &amp;quot;Draw 48: 0.666666666666664&amp;quot;
## [1] &amp;quot;Draw 49: 0.666666666666666&amp;quot;
## [1] &amp;quot;Draw 50: 0.666666666666666&amp;quot;
## [1] &amp;quot;Draw 51: 0.666666666666666&amp;quot;
## [1] &amp;quot;Draw 52: 0.666666666666667&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div id=&#34;final-winning-probabilities&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Final winning probabilities&lt;/h2&gt;
&lt;p&gt;Remember that the initial question was:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;What is the probability of winning the game for each ambassador?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The probability that Louana wins the game can easily be extracted as follows:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;sum(l_win_tot)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.6666667&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And since the game only stops when there is a winner, the sum of the winning probabilities for Louana and Colin must be equal to 1, so the probability that Colin wins the game is:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;c_win_tot &amp;lt;- 1 - sum(l_win_tot)
c_win_tot&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.3333333&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;To summarize:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Louana has 66.67% chance to win the ambassadors’ game.&lt;/li&gt;
&lt;li&gt;Colin has 33.33% chance to win the ambassadors’ game.&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div id=&#34;visual-representations&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Visual representations&lt;/h2&gt;
&lt;p&gt;To visualize the probabilities for each ambassador, we miss the probabilities of winning for Colin so let’s compute them first:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;c_win_tot &amp;lt;- c() # set empty vector for prob of Colin winning

# compute Colin winning probabilities with the smallest number of necessary draws
for (i in 1:limit_ndraws) {
  c_win_tot[i] &amp;lt;- ((tie^(i - 1)) * c_win) # prob of Colin winning
  print(paste0(&amp;quot;Draw &amp;quot;, i, &amp;quot;: &amp;quot;, sum(c_win_tot))) # print sum of winning
}&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;Draw 1: 0.166666666666667&amp;quot;
## [1] &amp;quot;Draw 2: 0.25&amp;quot;
## [1] &amp;quot;Draw 3: 0.291666666666667&amp;quot;
## [1] &amp;quot;Draw 4: 0.3125&amp;quot;
## [1] &amp;quot;Draw 5: 0.322916666666667&amp;quot;
## [1] &amp;quot;Draw 6: 0.328125&amp;quot;
## [1] &amp;quot;Draw 7: 0.330729166666667&amp;quot;
## [1] &amp;quot;Draw 8: 0.33203125&amp;quot;
## [1] &amp;quot;Draw 9: 0.332682291666667&amp;quot;
## [1] &amp;quot;Draw 10: 0.3330078125&amp;quot;
## [1] &amp;quot;Draw 11: 0.333170572916667&amp;quot;
## [1] &amp;quot;Draw 12: 0.333251953125&amp;quot;
## [1] &amp;quot;Draw 13: 0.333292643229167&amp;quot;
## [1] &amp;quot;Draw 14: 0.33331298828125&amp;quot;
## [1] &amp;quot;Draw 15: 0.333323160807292&amp;quot;
## [1] &amp;quot;Draw 16: 0.333328247070312&amp;quot;
## [1] &amp;quot;Draw 17: 0.333330790201823&amp;quot;
## [1] &amp;quot;Draw 18: 0.333332061767578&amp;quot;
## [1] &amp;quot;Draw 19: 0.333332697550456&amp;quot;
## [1] &amp;quot;Draw 20: 0.333333015441895&amp;quot;
## [1] &amp;quot;Draw 21: 0.333333174387614&amp;quot;
## [1] &amp;quot;Draw 22: 0.333333253860474&amp;quot;
## [1] &amp;quot;Draw 23: 0.333333293596904&amp;quot;
## [1] &amp;quot;Draw 24: 0.333333313465118&amp;quot;
## [1] &amp;quot;Draw 25: 0.333333323399226&amp;quot;
## [1] &amp;quot;Draw 26: 0.33333332836628&amp;quot;
## [1] &amp;quot;Draw 27: 0.333333330849806&amp;quot;
## [1] &amp;quot;Draw 28: 0.33333333209157&amp;quot;
## [1] &amp;quot;Draw 29: 0.333333332712452&amp;quot;
## [1] &amp;quot;Draw 30: 0.333333333022892&amp;quot;
## [1] &amp;quot;Draw 31: 0.333333333178113&amp;quot;
## [1] &amp;quot;Draw 32: 0.333333333255723&amp;quot;
## [1] &amp;quot;Draw 33: 0.333333333294528&amp;quot;
## [1] &amp;quot;Draw 34: 0.333333333313931&amp;quot;
## [1] &amp;quot;Draw 35: 0.333333333323632&amp;quot;
## [1] &amp;quot;Draw 36: 0.333333333328483&amp;quot;
## [1] &amp;quot;Draw 37: 0.333333333330908&amp;quot;
## [1] &amp;quot;Draw 38: 0.333333333332121&amp;quot;
## [1] &amp;quot;Draw 39: 0.333333333332727&amp;quot;
## [1] &amp;quot;Draw 40: 0.33333333333303&amp;quot;
## [1] &amp;quot;Draw 41: 0.333333333333182&amp;quot;
## [1] &amp;quot;Draw 42: 0.333333333333258&amp;quot;
## [1] &amp;quot;Draw 43: 0.333333333333295&amp;quot;
## [1] &amp;quot;Draw 44: 0.333333333333314&amp;quot;
## [1] &amp;quot;Draw 45: 0.333333333333324&amp;quot;
## [1] &amp;quot;Draw 46: 0.333333333333329&amp;quot;
## [1] &amp;quot;Draw 47: 0.333333333333331&amp;quot;
## [1] &amp;quot;Draw 48: 0.333333333333332&amp;quot;
## [1] &amp;quot;Draw 49: 0.333333333333333&amp;quot;
## [1] &amp;quot;Draw 50: 0.333333333333333&amp;quot;
## [1] &amp;quot;Draw 51: 0.333333333333333&amp;quot;
## [1] &amp;quot;Draw 52: 0.333333333333333&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We create a dataset with the probabilities for both ambassadors:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;dat &amp;lt;- data.frame(
  Draw = rep(1:limit_ndraws, 2),
  Probability = c(cumsum(l_win_tot), cumsum(c_win_tot)),
  Ambassador = c(rep(&amp;quot;Louana&amp;quot;, limit_ndraws), rep(&amp;quot;Colin&amp;quot;, limit_ndraws))
)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can now visualize these probabilities up to the number of necessary draws:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# load package
library(ggplot2)

# plot
ggplot(dat) +
  aes(x = Draw, y = Probability, colour = Ambassador) +
  geom_line(linewidth = 2L) +
  labs(
    y = &amp;quot;Probability of winning&amp;quot;,
    caption = &amp;quot;Source: Koh-Lanta 2022&amp;quot;
  ) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0, 1)) +
  theme_minimal()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/koh-lanta-2022-ambassadors-probability-problem/index_files/figure-html/unnamed-chunk-14-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;But I believe the most appropriate plot to answer the initial question is with a &lt;a href=&#34;https://statsandr.com/blog/descriptive-statistics-in-r/#barplot&#34;&gt;barplot&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# create dataset
dat_barplot &amp;lt;- data.frame(
  Ambassador = c(&amp;quot;Louana&amp;quot;, &amp;quot;Colin&amp;quot;),
  Probability = c(sum(l_win_tot), sum(c_win_tot))
)

# plot
ggplot(data = dat_barplot, aes(x = Ambassador, y = Probability)) +
  geom_bar(stat = &amp;quot;identity&amp;quot;, fill = &amp;quot;steelblue&amp;quot;) +
  labs(
    y = &amp;quot;Probability of winning&amp;quot;,
    caption = &amp;quot;Source: Koh-Lanta 2022&amp;quot;
  ) +
  scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0, 1)) +
  theme_minimal()&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/koh-lanta-2022-ambassadors-probability-problem/index_files/figure-html/unnamed-chunk-15-1.png&#34; alt=&#34;&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;coded-into-a-function&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Coded into a function&lt;/h2&gt;
&lt;p&gt;Let’s try to implement this problem in a function to be able to reuse it with other initial probabilities.&lt;/p&gt;
&lt;p&gt;With:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;p_a&lt;/code&gt; and &lt;code&gt;p_b&lt;/code&gt; denoting, respectively, the probability that ambassador A and ambassador B draw a white ball,&lt;/li&gt;
&lt;li&gt;&lt;code&gt;n_draws&lt;/code&gt; denoting the maximum number of draws that is allowed (default = 9999),&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;we have the following function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ambassadors_game &amp;lt;- function(p_a, p_b, n_draws = 9999) {
  q_a &amp;lt;- (1 - p_a)
  q_b &amp;lt;- (1 - p_b)

  a_win &amp;lt;- q_b * p_a
  b_win &amp;lt;- q_a * p_b

  tie &amp;lt;- (p_a * p_b) + (q_a * q_b)

  p_tie &amp;lt;- c() # set empty vector for prob of ties
  a_win_tot &amp;lt;- c() # set empty vector for prob of A winning
  b_win_tot &amp;lt;- c() # set empty vector for prob of B winning

  # find number of necessary draws:
  for (i in 1:n_draws) {
    p_tie[i] &amp;lt;- tie^i # prob of tie for each draw
    limit_ndraws &amp;lt;- sum(p_tie &amp;gt; 2.2e-16) # number of necessary draws
  }

  # compute A and B winning probabilities with the smallest number of necessary draws:
  for (i in 1:limit_ndraws) {
    a_win_tot[i] &amp;lt;- ((tie^(i - 1)) * a_win) # prob of A winning up to limited number of draws
    b_win_tot[i] &amp;lt;- ((tie^(i - 1)) * b_win) # prob of B winning up to limited number of draws
  }

  # save P(A), P(B) and number of necessary draws:
  res &amp;lt;- list(
    &amp;quot;p_a&amp;quot; = sum(a_win_tot),
    &amp;quot;p_b&amp;quot; = sum(b_win_tot),
    &amp;quot;ndraws&amp;quot; = limit_ndraws
  )
  # print results:
  return(res)
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We test the function to see if it matches results found above.&lt;/p&gt;
&lt;p&gt;First, if both ambassadors have identical urns as it was the case before the 2022 season:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ambassadors_game(
  p_a = 1 / 2,
  p_b = 1 / 2
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## $p_a
## [1] 0.5
## 
## $p_b
## [1] 0.5
## 
## $ndraws
## [1] 52&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The game is indeed fair, with a 50% chance of winning for each ambassador.&lt;/p&gt;
&lt;p&gt;Second, with the urns presented to Louana and Colin, but for the first draw only (setting arbitrarily that Louana is ambassador A and Colin is ambassador B):&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# first draw only
ambassadors_game(
  p_a = 1 / 2,
  p_b = 1 / 3,
  n_draws = 1
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## $p_a
## [1] 0.3333333
## 
## $p_b
## [1] 0.1666667
## 
## $ndraws
## [1] 1&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Third, still with the urns presented to Louana and Colin, but for a game limited to exactly 3 and 5 draws:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# up to 3 draws
ambassadors_game(
  p_a = 1 / 2,
  p_b = 1 / 3,
  n_draws = 3
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## $p_a
## [1] 0.5833333
## 
## $p_b
## [1] 0.2916667
## 
## $ndraws
## [1] 3&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# up to 5 draws
ambassadors_game(
  p_a = 1 / 2,
  p_b = 1 / 3,
  n_draws = 5
)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## $p_a
## [1] 0.6458333
## 
## $p_b
## [1] 0.3229167
## 
## $ndraws
## [1] 5&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And now, the final verification with the real situation of Louana and Colin in Koh-Lanta 2022:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Koh-Lanta 2022 situation
out &amp;lt;- ambassadors_game(
  p_a = 1 / 2,
  p_b = 1 / 3
)

out&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## $p_a
## [1] 0.6666667
## 
## $p_b
## [1] 0.3333333
## 
## $ndraws
## [1] 52&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All results match the ones presented above.&lt;/p&gt;
&lt;p&gt;(Note that the probabilities for each ambassador can be extracted as follows:)&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# prob ambassador A
out$p_a&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.6666667&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# prob ambassador B
out$p_b&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.3333333&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;conclusion&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;The initial question, coming from the television show Koh-Lanta, was “what is the probability of winning for each participant if they play the ambassadors’ game?”.&lt;/p&gt;
&lt;p&gt;In this article, we have shown how to compute these probabilities. Moreover, we have illustrated the process of how a simple probability problem could be generalized to suit many real life situations, and how a &lt;code&gt;for loop&lt;/code&gt; and a function could be used to implement a real life situation into R.&lt;/p&gt;
&lt;p&gt;Last but not least, I would like to focus on something Denis Brogniart (the well-known presenter of the show) said just after the two ambassadors came back to the island to announce their choice to the other adventurers. He mentioned that, due to the punishment afflicted to the yellow tribe, there was a difference of chances of 16% between Louana and Colin.&lt;/p&gt;
&lt;p&gt;This comes naturally from the following two situations:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;First situation:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Louana has a 50% chance of picking a white ball.&lt;/li&gt;
&lt;li&gt;Colin has a 33.33% chance of picking a white ball.&lt;/li&gt;
&lt;li&gt;The difference is 50 - 33.33 = 16.67%, truncated to 16%.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Or, if the game is limited to the first draw only:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Second situation:&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Louana has 33.33% chance of winning the game.&lt;/li&gt;
&lt;li&gt;Colin has a 16.67% chance of winning the game.&lt;/li&gt;
&lt;li&gt;The difference is 33.33 - 16.67 = 16.66%, truncated to 16%.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It is true that, seeing the problem from those two angles, the difference is ~16%. Denis Brogniart is right in mentioning this 16% difference.&lt;/p&gt;
&lt;p&gt;However, the difference of chances between the two ambassadors is larger &lt;em&gt;if we see the game from a broader perspective&lt;/em&gt;. The rules of the game say that it stops only when there is a winner. From that point of view, as demonstrated above:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Louana has 66.67% chance of winning the game.&lt;/li&gt;
&lt;li&gt;Colin has 33.33% chance of winning the game.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In this situation, the difference in probability of winning between the 2 ambassadors is 66.67 - 33.33 = 33.34%!&lt;/p&gt;
&lt;p&gt;I must admit that I was not expecting such a large difference between the two ambassadors. Seeing the ambassadors’ game from that perspective looks different to what we were told, or to what we thought before actually computing the probabilities. (Denis Brogniart, if you happen to read this, feel free to let me know whether this relatively large difference was intended or not.)&lt;/p&gt;
&lt;p&gt;For those of you who do not watch the show: upon the return of the ambassadors on the island with all adventurers, Colin has been heavily criticized by the members of his tribe. His decision not to play the ambassadors’ game (and the decision to eliminate one member of his tribe with the aim of saving himself) was seen as a betrayal. As a consequence of this, he was eliminated by the reunited tribe directly after that episode.&lt;/p&gt;
&lt;p&gt;I am going to conclude this article with the following question:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If you were an ambassador in the 2022 Koh-Lanta season and given that now you know the exact probabilites of winning for each tribe, what would you have done?&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Thanks for reading.&lt;/p&gt;
&lt;p&gt;As always, if you have any question related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&#34;footnotes footnotes-end-of-document&#34;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&#34;fn1&#34;&gt;&lt;p&gt;We will never know if he would have played the game if the chances of winning were equal for both ambassadors. Since I started to watch this television show, I have never seen any ambassadors’ negotiation leading to the ambassadors’ game, they all ended with the designation of an adventurer.&lt;a href=&#34;#fnref1&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li id=&#34;fn2&#34;&gt;&lt;p&gt;And to be honest, I wanted to compute these probabilities because while I was watching the show with my girlfriend, she looked at me and asked “what are the probabilities for each of them?”. I am now able to give her a precise answer.&lt;a href=&#34;#fnref2&#34; class=&#34;footnote-back&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description>
    </item>
    
    <item>
      <title>The 9 concepts and formulas in probability that every data scientist should know</title>
      <link>https://statsandr.com/blog/the-9-concepts-and-formulas-in-probability-that-every-data-scientist-should-know/</link>
      <pubDate>Tue, 03 Mar 2020 00:00:00 +0000</pubDate>
      
      <guid>https://statsandr.com/blog/the-9-concepts-and-formulas-in-probability-that-every-data-scientist-should-know/</guid>
      <description>

&lt;div id=&#34;TOC&#34;&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#what-is-probability&#34; id=&#34;toc-what-is-probability&#34;&gt;What is probability?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#a-probability-is-always-between-0-and-1&#34; id=&#34;toc-a-probability-is-always-between-0-and-1&#34;&gt;1. A probability is always between 0 and 1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#compute-a-probability&#34; id=&#34;toc-compute-a-probability&#34;&gt;2. Compute a probability&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#complement-of-an-event&#34; id=&#34;toc-complement-of-an-event&#34;&gt;3. Complement of an event&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#union-of-two-events&#34; id=&#34;toc-union-of-two-events&#34;&gt;4. Union of two events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#intersection-of-two-events&#34; id=&#34;toc-intersection-of-two-events&#34;&gt;5. Intersection of two events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#independence-of-two-events&#34; id=&#34;toc-independence-of-two-events&#34;&gt;6. Independence of two events&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#conditional-probability&#34; id=&#34;toc-conditional-probability&#34;&gt;7. Conditional probability&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#bayes-theorem&#34; id=&#34;toc-bayes-theorem&#34;&gt;Bayes’ theorem&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#example&#34; id=&#34;toc-example&#34;&gt;Example&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#accuracy-measures&#34; id=&#34;toc-accuracy-measures&#34;&gt;8. Accuracy measures&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#false-negatives&#34; id=&#34;toc-false-negatives&#34;&gt;False negatives&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#false-positives&#34; id=&#34;toc-false-positives&#34;&gt;False positives&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#sensitivity&#34; id=&#34;toc-sensitivity&#34;&gt;Sensitivity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#specificity&#34; id=&#34;toc-specificity&#34;&gt;Specificity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#positive-predictive-value&#34; id=&#34;toc-positive-predictive-value&#34;&gt;Positive predictive value&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#negative-predictive-value&#34; id=&#34;toc-negative-predictive-value&#34;&gt;Negative predictive value&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#counting-techniques&#34; id=&#34;toc-counting-techniques&#34;&gt;9. Counting techniques&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#multiplication&#34; id=&#34;toc-multiplication&#34;&gt;Multiplication&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#example-1&#34; id=&#34;toc-example-1&#34;&gt;Example&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#permutation&#34; id=&#34;toc-permutation&#34;&gt;Permutation&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#example-2&#34; id=&#34;toc-example-2&#34;&gt;Example&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#by-hand&#34; id=&#34;toc-by-hand&#34;&gt;By hand&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#in-r&#34; id=&#34;toc-in-r&#34;&gt;In R&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#combination&#34; id=&#34;toc-combination&#34;&gt;Combination&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#example-3&#34; id=&#34;toc-example-3&#34;&gt;Example&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;#by-hand-1&#34; id=&#34;toc-by-hand-1&#34;&gt;By hand&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#in-r-1&#34; id=&#34;toc-in-r-1&#34;&gt;In R&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;#conclusion&#34; id=&#34;toc-conclusion&#34;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/the-7-formulas-in-probability-that-every-data-scientist-should-know_files/the-9-concepts-and-formulas-in-probability-that-every-data-scientist-should-know.jpeg&#34; style=&#34;width:100.0%&#34; /&gt;&lt;/p&gt;
&lt;div id=&#34;what-is-probability&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;What is probability?&lt;/h1&gt;
&lt;p&gt;A probability is a number that reflects the &lt;strong&gt;chance that a particular event will occur&lt;/strong&gt;. In other words, it quantifies (on a scale from 0 to 1, or from 0% to 100%) &lt;strong&gt;how likely an event is to occur&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Probability is a branch of mathematics that provides models to describe random processes. These mathematical tools allow to establish theoretical models for random phenomena and to use them to make predictions. Like every model, the probabilistic model is a simplification of the world. However, the model is useful as soon as it captures the essential features.&lt;/p&gt;
&lt;p&gt;In this article, we present 9 fundamental formulas and concepts in probability that every data scientist should understand and master in order to appropriately handle any project involving probabilities.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;a-probability-is-always-between-0-and-1&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;1. A probability is always between 0 and 1&lt;/h1&gt;
&lt;p&gt;The probability of an event is always between 0 and 1 (or 0% and 100%). If we denote the probability that an event A (which could be any event) occurs by &lt;span class=&#34;math inline&#34;&gt;\(P(A)\)&lt;/span&gt;, we have:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[0 \le P(A) \le 1\]&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If an event is impossible: &lt;span class=&#34;math inline&#34;&gt;\(P(A) = 0\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;If an event is certain: &lt;span class=&#34;math inline&#34;&gt;\(P(A) = 1\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For example, throwing a 7 with a standard six-sided dice (with faces ranging from 1 to 6) is impossible so its probability is equal to 0. Throwing head &lt;em&gt;or&lt;/em&gt; tail with a coin is certain, so its probability is equal to 1.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;compute-a-probability&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;2. Compute a probability&lt;/h1&gt;
&lt;p&gt;If the elements of a sample space (the set of all possible results of a randomized experiment) are equiprobable (= all elements have the same probability), then the probability of an event occurring is equal to the number of favourable cases (number of ways it can happen) divided by the number of possible cases (total number of outcomes):&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(A) = \frac{\text{number of favourable cases}}{\text{number of possible cases}}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;For example, all numbers of a six-sided dice are equiprobable since they all have the same probability of occurring. The probability of rolling a 3 with a dice is thus&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(3) = \frac{\text{number of favourable cases}}{\text{number of possible cases}} = \frac{1}{6}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;because there is only one favourable case (there is only one face with a 3 on it), and there are 6 possible cases (because there are 6 faces altogether).&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;complement-of-an-event&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;3. Complement of an event&lt;/h1&gt;
&lt;p&gt;The probability of the complement (or opposite) of an event is:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(\text{not A}) = P(\bar{A}) = 1 - P(A)\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;For instance, the probability of &lt;em&gt;not&lt;/em&gt; throwing a 3 with a dice is:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(\bar{A}) = 1 - P(A) = 1 - \frac{1}{6} = \frac{5}{6}\]&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;union-of-two-events&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;4. Union of two events&lt;/h1&gt;
&lt;p&gt;The probability of the union of two events is the probability of either occurring:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{align}
P(\text{A or B)} &amp;amp;= P(A \cup B) \\
&amp;amp;= P(A) + P(B) - P(A \cap B)
\end{align}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Suppose that the probability of a fire breaking out in two houses in a given year is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;in house A: 60%, so &lt;span class=&#34;math inline&#34;&gt;\(P(A) = 0.6\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;in house B: 45%, so &lt;span class=&#34;math inline&#34;&gt;\(P(B) = 0.45\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;in at least one of the two houses: 80%, so &lt;span class=&#34;math inline&#34;&gt;\(P(A \cup B) = 0.8\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Graphically we have&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/the-7-formulas-in-probability-that-every-data-scientist-should-know_files/figure-html/unnamed-chunk-1-1.png&#34; width=&#34;100%&#34; style=&#34;display: block; margin: auto;&#34; /&gt;&lt;/p&gt;
&lt;p&gt;The probability of a fire breaking out in house A &lt;strong&gt;or&lt;/strong&gt; house B is&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(A \cup B) = P(A) + P(B) - P(A \cap B)\]&lt;/span&gt;
&lt;span class=&#34;math display&#34;&gt;\[= 0.6 + 0.45 - 0.25 = 0.8\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;By summing &lt;span class=&#34;math inline&#34;&gt;\(P(A)\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(P(B)\)&lt;/span&gt;, the intersection of A and B, i.e. &lt;span class=&#34;math inline&#34;&gt;\(P(A \cap B)\)&lt;/span&gt;, is counted twice. This is the reason we subtract it to count it only once.&lt;/p&gt;
&lt;p&gt;If two events are mutually exclusive (i.e., two events that cannot occur simultaneously), the probability of both events occurring &lt;span class=&#34;math inline&#34;&gt;\(P(A \cap B)\)&lt;/span&gt; is equal to 0, so the above formula becomes&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(A \cup B) = P(A) + P(B)\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;For example, the event “rolling a 3” and the event “rolling a 6” on a six-sided dice are two mutually exclusive events since they cannot both occur at the same time. Since their joint probability is equal to 0, the probability of rolling a 3 or 6 on a six-sided dice is&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(3 \cup 6) = P(3) + P(6) = \frac{1}{6} + \frac{1}{6} = \frac{1}{3}\]&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;intersection-of-two-events&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;5. Intersection of two events&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;If two events are independent&lt;/strong&gt;, the probability of the intersection of the two events (i.e., the joint probability) is the probability of the two events occurring:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(\text{A and B)} = P(A \cap B) = P(A) \cdot P(B)\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;For instance, if two coins are flipped, the probability of both coins being tails is&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(T_1 \cap T_2) = P(T_1) \cdot P(T_2) = \frac{1}{2} \cdot \frac{1}{2} = \frac{1}{4}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;where &lt;span class=&#34;math inline&#34;&gt;\(T_1\)&lt;/span&gt; (&lt;span class=&#34;math inline&#34;&gt;\(T_2\)&lt;/span&gt;) denotes the event that the first (second) coin is tail.&lt;/p&gt;
&lt;p&gt;Note that &lt;span class=&#34;math inline&#34;&gt;\(P(A \cap B) = P(B \cap A)\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;If two events are mutually exclusive, their joint probability is equal to 0:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(A \cap B) = 0\]&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;independence-of-two-events&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;6. Independence of two events&lt;/h1&gt;
&lt;p&gt;Another important concept in probability is the independence of two events. Formally, the events A and B are independent if and only if&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(A \cap B) = P(A) \cdot P(B)\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;If the equality holds, the two events are said to be independent, otherwise the two events are said to be dependent.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In the example of the two coins:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(T_1 \cap T_2) = \frac{1}{4}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(T_1) \cdot P(T_2) = \frac{1}{2} \cdot \frac{1}{2} = \frac{1}{4}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;so the following equality holds&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(T_1 \cap T_2) = P(T_1) \cdot P(T_2) = \frac{1}{4}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The two events are thus independent, denoted &lt;span class=&#34;math inline&#34;&gt;\(T_1{\perp\!\!\!\perp}T_2\)&lt;/span&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In the example of the fire breaking out in two houses (see &lt;a href=&#34;https://statsandr.com/blog/the-9-concepts-and-formulas-in-probability-that-every-data-scientist-should-know/#union-of-two-events&#34;&gt;section 4&lt;/a&gt;):&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(A \cap B) = 0.25\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;and&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(A) \cdot P(B) = 0.6 \cdot 0.45 = 0.27\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;so the following equality does not hold&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(A \cap B) \ne P(A) \cdot P(B)\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The two events are thus dependent (or not independent), denoted &lt;span class=&#34;math inline&#34;&gt;\(A \not\!\perp\!\!\!\perp B\)&lt;/span&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;conditional-probability&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;7. Conditional probability&lt;/h1&gt;
&lt;p&gt;Suppose two events A and B and &lt;span class=&#34;math inline&#34;&gt;\(P(B) &amp;gt; 0\)&lt;/span&gt;. The conditional probability of A given (knowing) B is the likelihood of event A occurring given that event B has occurred:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(A | B) = \frac{P(A \cap B)}{P(B)}\]&lt;/span&gt;
&lt;span class=&#34;math display&#34;&gt;\[= \frac{P(B \cap A)}{P(B)} \text{ (since } P(A \cap B) = P(B \cap A))\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Note that, in general, the probability of A given B is not equal to the probability of B given A, that is, &lt;span class=&#34;math inline&#34;&gt;\(P(A | B) \ne P(B | A)\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;From the formula of the conditional probability, we can derive the multiplicative law:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(A | B) = \frac{P(A \cap B)}{P(B)} \text{ (Eq. 1)}\]&lt;/span&gt;
&lt;span class=&#34;math display&#34;&gt;\[P(A | B) \cdot P(B) = \frac{P(A \cap B)}{P(B)} \cdot P(B)\]&lt;/span&gt;
&lt;span class=&#34;math display&#34;&gt;\[P(A | B) \cdot P(B) = P(A \cap B) \text{ (multiplicative law)}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;If two events are independent, &lt;span class=&#34;math inline&#34;&gt;\(P(A \cap B) = P(A) \cdot P(B)\)&lt;/span&gt;, and:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(P(B) &amp;gt; 0\)&lt;/span&gt;, the conditional probability becomes&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(A | B) = \frac{P(A \cap B)}{P(B)}\]&lt;/span&gt;
&lt;span class=&#34;math display&#34;&gt;\[P(A | B) = \frac{P(A) \cdot P(B)}{P(B)}\]&lt;/span&gt;
&lt;span class=&#34;math display&#34;&gt;\[P(A | B) = P(A) \text{ (Eq. 2)}\]&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span class=&#34;math inline&#34;&gt;\(P(A) &amp;gt; 0\)&lt;/span&gt;, the conditional probability becomes&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(B | A) = \frac{P(B \cap A)}{P(A)}\]&lt;/span&gt;
&lt;span class=&#34;math display&#34;&gt;\[P(B | A) = \frac{P(B) \cdot P(A)}{P(A)}\]&lt;/span&gt;
&lt;span class=&#34;math display&#34;&gt;\[P(B | A) = P(B) \text{ (Eq. 3)}\]&lt;/span&gt;
Equations 2 and 3 mean that knowing that one event occurred does not influence the probability of the outcome of the other event. This is in fact the definition of the independence: if knowing that one event occurred does not help to predict (does not influence) the outcome of the other event, the two events are by essence independent.&lt;/p&gt;
&lt;div id=&#34;bayes-theorem&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Bayes’ theorem&lt;/h2&gt;
&lt;p&gt;From the formulas of the conditional probability and the multiplicative law, we can derive the Bayes’ theorem:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{align}
P(B | A) &amp;amp;= \frac{P(B \cap A)}{P(A)} \\
&amp;amp; \text{(from conditional probability)} \\
&amp;amp;= \frac{P(A \cap B)}{P(A)} \\
&amp;amp; \text{(since } P(A \cap B) = P(B \cap A)) \\
&amp;amp;= \frac{P(A | B) \cdot P(B)}{P(A)} \\
&amp;amp; \text{ (from multiplicative law)}
\end{align}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;which is equivalent to&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{align}
P(B | A) &amp;amp;= \frac{P(B | A) \cdot P(A)}{P(B)} \\
&amp;amp; \text{(Bayes&amp;#39; theorem)}
\end{align}\]&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;example&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Example&lt;/h2&gt;
&lt;p&gt;In order to illustrate the conditional probability and the Bayes’ theorem, suppose the following problem:&lt;/p&gt;
&lt;p&gt;In order to determine the presence of a disease in a person, a blood test is performed. When a person has the disease, the test can reveal the disease in 80% of cases. When the disease is not present, the test is negative in 90% of cases. Experience has shown that the probability of the disease being present is 10%. A researcher would like to know the probability that an individual has the disease &lt;em&gt;given that the result of the test is positive&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;To answer this question, the following events are defined:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;P: the test result is positive&lt;/li&gt;
&lt;li&gt;D: the person has the disease&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Moreover, we use a tree diagram to illustrate the statement:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/the-7-formulas-in-probability-that-every-data-scientist-should-know_files/Screenshot%202020-03-03%20at%2013.54.24.png&#34; style=&#34;width:100.0%&#34; /&gt;&lt;/p&gt;
&lt;p&gt;(The sum of all 4 scenarios must be equal to 1 since these 4 scenarios cover all possible cases.)&lt;/p&gt;
&lt;p&gt;We are looking for the probability that an individual has the disease given that the result of the test is positive, &lt;span class=&#34;math inline&#34;&gt;\(P(D | P)\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Following the formula of the conditional probability (Eq. 1) we have:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(A | B) = \frac{P(A \cap B)}{P(B)}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In terms of our problem:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(D | P) = \frac{P(D \cap P)}{P(P)}\]&lt;/span&gt;
&lt;span class=&#34;math display&#34;&gt;\[P(D | P) = \frac{0.08}{P(P)} \text{ (Eq. 4)}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;From the tree diagram, we can see that a positive test result is possible under two scenarios:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;when a person has the disease, or&lt;/li&gt;
&lt;li&gt;when the person does not actually have the disease (because the test is not always correct).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In order to find the probability of a positive test result, &lt;span class=&#34;math inline&#34;&gt;\(P(P)\)&lt;/span&gt;, we need to sum up those two scenarios:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{align}
P(P) &amp;amp;= P(D \cap P) + P(\bar{D} \cap P) \\
&amp;amp;= 0.08 + 0.09 \\
&amp;amp;= 0.17
\end{align}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Eq. 4 then becomes&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P(D | P) = \frac{0.08}{0.17} = 0.4706\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The probability of having the disease given that the result of the test is positive is only 47.06%. This means that in this specific case (with the same percentages), an individual with a positive test has less than 1 chance out of 2 of having the disease!&lt;/p&gt;
&lt;p&gt;This relatively small percentage is due to the facts that the disease is quite rare (only 10% of the population is affected) and that the test is not always correct (sometimes it detects the disease although it is not present, and sometimes it does not detect it although it is present).&lt;/p&gt;
&lt;p&gt;As a consequence, a higher percentage of healthy people have a positive result (9%) compared to the percentage of people who have a positive result and who actually have the disease (8%). This explains why several diagnostic tests are often performed before announcing the diagnosis, especially for rare diseases.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;accuracy-measures&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;8. Accuracy measures&lt;/h1&gt;
&lt;p&gt;Based on the example of the disease and the diagnostic test presented above, we explain the most common accuracy measures:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;False negative (number and rate)&lt;/li&gt;
&lt;li&gt;False positive (number and rate)&lt;/li&gt;
&lt;li&gt;Sensitivity&lt;/li&gt;
&lt;li&gt;Specificity&lt;/li&gt;
&lt;li&gt;Positive predictive value&lt;/li&gt;
&lt;li&gt;Negative predictive value&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Before diving into the details of these accuracy measures, here is an overview of the measures and the tree diagram with the labels added for each of the 4 scenarios:&lt;/p&gt;
&lt;div class=&#34;figure&#34;&gt;
&lt;img src=&#34;https://statsandr.com/blog/the-7-formulas-in-probability-that-every-data-scientist-should-know_files/the-7-concepts-and-formulas-in-probability-that-every-data-scientist-should-know.png&#34; style=&#34;width:100.0%&#34; alt=&#34;&#34; /&gt;
&lt;p class=&#34;caption&#34;&gt;Adapted from Wikipedia&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img src=&#34;https://statsandr.com/blog/the-7-formulas-in-probability-that-every-data-scientist-should-know_files/Screenshot%202020-03-03%20at%2015.53.19.png&#34; style=&#34;width:100.0%&#34; /&gt;&lt;/p&gt;
&lt;div id=&#34;false-negatives&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;False negatives&lt;/h2&gt;
&lt;p&gt;The false negatives (FN) are the number of people incorrectly labeled as &lt;strong&gt;not&lt;/strong&gt; having the disease or the condition, when in reality it is present. It is like telling a women who is 7 months pregnant that she is not pregnant.&lt;/p&gt;
&lt;p&gt;From the tree diagram, we have:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[FN = P(D \cap \bar{P}) = 0.02\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Moreover, the false negative &lt;strong&gt;rate&lt;/strong&gt; (&lt;em&gt;FNR&lt;/em&gt;) is defined as&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{align}
FNR &amp;amp;= \frac{FN}{FN + TP} \\
&amp;amp;= P(\bar{P} | D) \\
&amp;amp;= \frac{P(\bar{P} \cap D)}{P(D)} \\
&amp;amp;= \frac{0.02}{0.08 + 0.02} \\
&amp;amp;= 0.2
\end{align}\]&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;false-positives&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;False positives&lt;/h2&gt;
&lt;p&gt;The false positives (FP) are the number of people incorrectly labeled as having the disease or the condition, when in reality it is &lt;strong&gt;not&lt;/strong&gt; present. It is like telling a man he is pregnant.&lt;/p&gt;
&lt;p&gt;From the tree diagram, we have:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[FP = P(\bar{D} \cap P) = 0.09\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Moreover, the false positive &lt;strong&gt;rate&lt;/strong&gt; (&lt;em&gt;FPR&lt;/em&gt;) is defined as&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{align}
FPR &amp;amp;= \frac{FP}{FP + TN} \\
&amp;amp;= P(P | \bar{D}) \\
&amp;amp;= \frac{P(P \cap \bar{D})}{P(\bar{D})} \\
&amp;amp;= \frac{0.09}{0.09 + 0.81} \\
&amp;amp;= 0.1
\end{align}\]&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;sensitivity&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Sensitivity&lt;/h2&gt;
&lt;p&gt;The sensitivity of a test, also referred as the recall, measures the ability of a test to detect the condition when the condition is present (the percentage of sick people who are correctly identified as having the disease):&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[ Sensitivity = \frac{TP}{TP + FN}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;where &lt;em&gt;TP&lt;/em&gt; is the true positives.&lt;/p&gt;
&lt;p&gt;From the tree diagram, we have:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{align}
Sensitivity &amp;amp;= \frac{TP}{TP + FN} \\
&amp;amp;= P(P|D) \\
&amp;amp;= 0.8
\end{align}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Note also that &lt;span class=&#34;math inline&#34;&gt;\(1 - sensitivity = FNR\)&lt;/span&gt; (false negative rate).&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;specificity&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Specificity&lt;/h2&gt;
&lt;p&gt;The specificity of a test measures the ability of a test to correctly exclude the condition when the condition is absent (the percentage of healthy people who are correctly identified as not having the disease):&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[Specificity = \frac{TN}{TN + FP}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;where &lt;em&gt;TN&lt;/em&gt; is the true negatives.&lt;/p&gt;
&lt;p&gt;From the tree diagram, we have:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{align}
Specificity &amp;amp;= \frac{TN}{TN + FP} \\
&amp;amp;= P(\bar{P} | \bar{D}) \\
&amp;amp;= 0.9
\end{align}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Note also that &lt;span class=&#34;math inline&#34;&gt;\(1 - specificity = FPR\)&lt;/span&gt; (false positive rate).&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;positive-predictive-value&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Positive predictive value&lt;/h2&gt;
&lt;p&gt;The positive predictive value, also referred as the precision, is the proportion of positives that correspond to the presence of the condition, so the proportions of positive results that are true positive results:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[PPV = \frac{TP}{TP+FP}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;From the tree diagram, we have:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{align}
PPV &amp;amp;= \frac{TP}{TP+FP} \\
&amp;amp;= P(D | P) \\
&amp;amp;= \frac{P(D \cap P)}{P(P)} \\
&amp;amp;= \frac{0.08}{0.08+0.09} \\
&amp;amp;= 0.4706
\end{align}\]&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;negative-predictive-value&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Negative predictive value&lt;/h2&gt;
&lt;p&gt;The negative predictive value is the proportion of negatives that correspond to the absence of the condition, so the proportions of negative results that are true negative results:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[NPV = \frac{TN}{TN + FN}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;From the tree diagram, we have:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{align}
NPV &amp;amp;= \frac{TN}{TN + FN} \\
&amp;amp;= P(\bar{D} | \bar{P}) \\
&amp;amp;= \frac{P(\bar{D} \cap \bar{P})}{P(\bar{P})} \\
&amp;amp;= \frac{0.81}{0.81+0.02} \\
&amp;amp;= 0.9759
\end{align}\]&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;counting-techniques&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;9. Counting techniques&lt;/h1&gt;
&lt;p&gt;In order to use the formula in &lt;a href=&#34;https://statsandr.com/blog/the-9-concepts-and-formulas-in-probability-that-every-data-scientist-should-know/#compute-a-probability&#34;&gt;section 2&lt;/a&gt;, one must know how to count the number of possible elements (both for favorable and possible cases).&lt;/p&gt;
&lt;p&gt;There are 3 main counting techniques in probability:&lt;/p&gt;
&lt;ol style=&#34;list-style-type: decimal&#34;&gt;
&lt;li&gt;Multiplication&lt;/li&gt;
&lt;li&gt;Permutation&lt;/li&gt;
&lt;li&gt;Combination&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;See below how to count the number of possible elements in case of equiprobable results.&lt;/p&gt;
&lt;div id=&#34;multiplication&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Multiplication&lt;/h2&gt;
&lt;p&gt;The multiplication rule is as follows:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\#(A \times B) = (\#A) \times (\#B)\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;where &lt;span class=&#34;math inline&#34;&gt;\(\#\)&lt;/span&gt; is the number of elements.&lt;/p&gt;
&lt;div id=&#34;example-1&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Example&lt;/h3&gt;
&lt;p&gt;In a restaurant, a customer has to choose a starter, a main course and a dessert. The restaurant offers 2 starters, 3 main courses and 2 desserts. How many different choices are possible?&lt;/p&gt;
&lt;p&gt;There are &lt;span class=&#34;math inline&#34;&gt;\(2 \cdot 3 \cdot 2 = 12\)&lt;/span&gt; different possible choices.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;permutation&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Permutation&lt;/h2&gt;
&lt;p&gt;The number of permutations is as follows:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{align}
P^r_n &amp;amp;= n \times (n - 1) \times \cdots \times (n - r + 1) \\
&amp;amp;= \frac{n !}{(n - r)!}
\end{align}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;with &lt;span class=&#34;math inline&#34;&gt;\(r\)&lt;/span&gt; the length, &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; the number of elements and &lt;span class=&#34;math inline&#34;&gt;\(r \le n\)&lt;/span&gt;. Note that &lt;span class=&#34;math inline&#34;&gt;\(0! = 1\)&lt;/span&gt; and &lt;span class=&#34;math inline&#34;&gt;\(k! = k \times (k - 1) \times (k - 2) \times \cdots \times 2 \times 1\)&lt;/span&gt; if &lt;span class=&#34;math inline&#34;&gt;\(k = 1, 2, \dots\)&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The order is important in permutations!&lt;/p&gt;
&lt;div id=&#34;example-2&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Example&lt;/h3&gt;
&lt;p&gt;Count the permutations of length 2 of the set &lt;span class=&#34;math inline&#34;&gt;\(A = \{a, b, c, d\}\)&lt;/span&gt;, without a letter being repeated. How many permutations do you find?&lt;/p&gt;
&lt;div id=&#34;by-hand&#34; class=&#34;section level4&#34;&gt;
&lt;h4&gt;By hand&lt;/h4&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[P^4_2 = \frac{4!}{(4-2)!} = \frac{4\cdot3\cdot2\cdot1}{2\cdot1} = 12\]&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;in-r&#34; class=&#34;section level4&#34;&gt;
&lt;h4&gt;In R&lt;/h4&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(gtools)

x &amp;lt;- c(&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;, &amp;quot;d&amp;quot;)

# See all different permutations
perms &amp;lt;- permutations(
  n = 4, r = 2, v = x,
  repeats.allowed = FALSE
)
perms&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;##       [,1] [,2]
##  [1,] &amp;quot;a&amp;quot;  &amp;quot;b&amp;quot; 
##  [2,] &amp;quot;a&amp;quot;  &amp;quot;c&amp;quot; 
##  [3,] &amp;quot;a&amp;quot;  &amp;quot;d&amp;quot; 
##  [4,] &amp;quot;b&amp;quot;  &amp;quot;a&amp;quot; 
##  [5,] &amp;quot;b&amp;quot;  &amp;quot;c&amp;quot; 
##  [6,] &amp;quot;b&amp;quot;  &amp;quot;d&amp;quot; 
##  [7,] &amp;quot;c&amp;quot;  &amp;quot;a&amp;quot; 
##  [8,] &amp;quot;c&amp;quot;  &amp;quot;b&amp;quot; 
##  [9,] &amp;quot;c&amp;quot;  &amp;quot;d&amp;quot; 
## [10,] &amp;quot;d&amp;quot;  &amp;quot;a&amp;quot; 
## [11,] &amp;quot;d&amp;quot;  &amp;quot;b&amp;quot; 
## [12,] &amp;quot;d&amp;quot;  &amp;quot;c&amp;quot;&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Count the number of permutations
nrow(perms)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 12&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;combination&#34; class=&#34;section level2&#34;&gt;
&lt;h2&gt;Combination&lt;/h2&gt;
&lt;p&gt;The number of combinations is as follows:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[\begin{align}
C^r_n &amp;amp;= \frac{P^r_n}{r!} \\
&amp;amp;= \frac{n !}{r!(n - r)!} \\
&amp;amp;= {n \choose r} \\
&amp;amp;= \frac{n}{r} \times \frac{n - 1}{r - 1} \times \dots \times \frac{n - r + 1}{1}
\end{align}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;with &lt;span class=&#34;math inline&#34;&gt;\(r\)&lt;/span&gt; the length, &lt;span class=&#34;math inline&#34;&gt;\(n\)&lt;/span&gt; the number of elements and &lt;span class=&#34;math inline&#34;&gt;\(r \le n\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;The order is &lt;strong&gt;not&lt;/strong&gt; important in combinations!&lt;/p&gt;
&lt;div id=&#34;example-3&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;Example&lt;/h3&gt;
&lt;p&gt;In a family of 5 children, what is the probability that there are 3 girls and 2 boys? Assume that the probabilities of giving birth to a girl and a boy are equal.&lt;/p&gt;
&lt;div id=&#34;by-hand-1&#34; class=&#34;section level4&#34;&gt;
&lt;h4&gt;By hand&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Count of 3 girls and 2 boys (favourable cases): &lt;span class=&#34;math inline&#34;&gt;\(C^3_5 = {5 \choose 3} = \frac{5!}{3!(5-3)!} = 10\)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Count of possible cases: &lt;span class=&#34;math inline&#34;&gt;\(2^5 = 32\)&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span class=&#34;math inline&#34;&gt;\(\Rightarrow P(3 \text{ girls and 2 boys}) = \frac{\text{# of favourable cases}}{\text{# of possible cases}}\)&lt;/span&gt; &lt;span class=&#34;math display&#34;&gt;\[= \frac{10}{32} = 0.3125\]&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;in-r-1&#34; class=&#34;section level4&#34;&gt;
&lt;h4&gt;In R&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;Count of 3 girls and 2 boys:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;choose(n = 5, k = 3)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 10&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Count of possible cases:&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;2^5&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 32&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Probability of 3 girls and 2 boys:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;choose(n = 5, k = 3) / 2^5&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.3125&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&#34;conclusion&#34; class=&#34;section level1&#34;&gt;
&lt;h1&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;Thanks for reading.&lt;/p&gt;
&lt;p&gt;I hope this article helped you to understand the most important formulas and concepts from probability theory.&lt;/p&gt;
&lt;p&gt;As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.&lt;/p&gt;
&lt;/div&gt;
</description>
    </item>
    
  </channel>
</rss>