-
Notifications
You must be signed in to change notification settings - Fork 18
/
06-probability.Rmd
464 lines (360 loc) · 11.1 KB
/
06-probability.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# Probability
## Load packages, load data, set theme
Let's load the packages that we need for this chapter.
```{r, message=FALSE}
library("knitr") # for rendering the RMarkdown file
library("kableExtra") # for nicely formatted tables
library("arrangements") # fast generators and iterators for creating combinations
library("DiagrammeR") # for drawing diagrams
library("tidyverse") # for data wrangling
```
Set the plotting theme.
```{r}
theme_set(theme_classic() +
theme(text = element_text(size = 20)))
opts_chunk$set(comment = "",
fig.show = "hold")
```
## Counting
Imagine that there are three balls in an urn. The balls are labeled 1, 2, and 3. Let's consider a few possible situations.
```{r}
balls = 1:3 # number of balls in urn
ndraws = 2 # number of draws
# order matters, without replacement
permutations(balls, ndraws)
# order matters, with replacement
permutations(balls, ndraws, replace = T)
# order doesn't matter, with replacement
combinations(balls, ndraws, replace = T)
# order doesn't matter, without replacement
combinations(balls, ndraws)
```
I've generated the figures below using the `DiagrammeR` package. It's a powerful package for drawing diagrams in R. See information on how to use the DiagrammeR package [here](https://rich-iannone.github.io/DiagrammeR/).
```{r, echo=FALSE, fig.cap="Drawing two marbles out of an urn __with__ replacement."}
grViz("
digraph dot{
# general settings for all nodes
node [
shape = circle,
style = filled,
color = black,
label = ''
fontname = 'Helvetica',
fontsize = 24,
fillcolor = lightblue
]
# edges between nodes
edge [color = black]
0 -> {1 2 3}
1 -> {11 12 13}
2 -> {21 22 23}
3 -> {31 32 33}
# labels for each node
0 [fillcolor = 'black', width = 0.1]
1 [label = '1']
2 [label = '2']
3 [label = '3']
11 [label = '1']
12 [label = '2']
13 [label = '3']
21 [label = '1']
22 [label = '2']
23 [label = '3']
31 [label = '1']
32 [label = '2']
33 [label = '3']
# direction in which arrows are drawn (from left to right)
rankdir = LR
}
")
```
```{r, echo=FALSE, fig.cap="Drawing two marbles out of an urn __without__ replacement."}
grViz("
digraph dot{
# general settings for all nodes
node [
shape = circle,
style = filled,
color = black,
label = ''
fontname = 'Helvetica',
fontsize = 24,
fillcolor = lightblue
]
# edges between nodes
edge [color = black]
0 -> {1 2 3}
1 -> {12 13}
2 -> {21 23}
3 -> {31 32}
# labels for each node
0 [fillcolor = 'black', width = 0.1]
1 [label = '1']
2 [label = '2']
3 [label = '3']
12 [label = '2']
13 [label = '3']
21 [label = '1']
23 [label = '3']
31 [label = '1']
32 [label = '2']
# direction in which arrows are drawn (from left to right)
rankdir = LR
}
")
```
## The random secretary
A secretary types four letters to four people and addresses the four envelopes. If he inserts the letters at random, each in a different envelope, what is the probability that exactly three letters will go into the right envelope?
```{r}
df.letters = permutations(x = 1:4, k = 4) %>%
as_tibble(.name_repair = ~ str_c("person_", 1:4)) %>%
mutate(n_correct = (person_1 == 1) +
(person_2 == 2) +
(person_3 == 3) +
(person_4 == 4))
df.letters %>%
summarize(prob_3_correct = sum(n_correct == 3) / n())
```
```{r}
ggplot(data = df.letters,
mapping = aes(x = n_correct)) +
geom_bar(aes(y = stat(count)/sum(count)),
color = "black",
fill = "lightblue") +
scale_y_continuous(labels = scales::percent,
expand = c(0, 0)) +
labs(x = "number correct",
y = "probability")
```
## Flipping a coin many times
```{r, fig.cap='A demonstration of the law of large numbers.'}
# Example taken from here: http://statsthinking21.org/probability.html#empirical-frequency
set.seed(1) # set the seed so that the outcome is consistent
nsamples = 50000 # how many flips do we want to make?
# create some random coin flips using the rbinom() function with
# a true probability of 0.5
df.samples = tibble(trial_number = seq(nsamples),
outcomes = rbinom(nsamples, 1, 0.5)) %>%
mutate(mean_probability = cumsum(outcomes) / seq_along(outcomes)) %>%
filter(trial_number >= 10) # start with a minimum sample of 10 flips
ggplot(data = df.samples,
mapping = aes(x = trial_number, y = mean_probability)) +
geom_hline(yintercept = 0.5, color = "gray", linetype = "dashed") +
geom_line() +
labs(x = "Number of trials",
y = "Estimated probability of heads") +
theme_classic() +
theme(text = element_text(size = 20))
```
## Clue guide to probability
```{r}
who = c("ms_scarlet", "col_mustard", "mrs_white",
"mr_green", "mrs_peacock", "prof_plum")
what = c("candlestick", "knife", "lead_pipe",
"revolver", "rope", "wrench")
where = c("study", "kitchen", "conservatory",
"lounge", "billiard_room", "hall",
"dining_room", "ballroom", "library")
df.clue = expand_grid(who = who,
what = what,
where = where)
df.suspects = df.clue %>%
distinct(who) %>%
mutate(gender = ifelse(test = who %in% c("ms_scarlet", "mrs_white", "mrs_peacock"),
yes = "female",
no = "male"))
```
```{r}
df.suspects %>%
arrange(desc(gender)) %>%
kable() %>%
kable_styling("striped", full_width = F)
```
### Conditional probability
```{r}
# conditional probability (via rules of probability)
df.suspects %>%
summarize(p_prof_plum_given_male =
sum(gender == "male" & who == "prof_plum") /
sum(gender == "male"))
```
```{r}
# conditional probability (via rejection)
df.suspects %>%
filter(gender == "male") %>%
summarize(p_prof_plum_given_male =
sum(who == "prof_plum") /
n())
```
### Law of total probability
```{r, echo=FALSE}
grViz("
digraph dot{
# general settings for all nodes
node [
shape = circle,
style = filled,
color = black,
label = ''
fontname = 'Helvetica',
fontsize = 9,
fillcolor = lightblue,
fixedsize=true,
width = 0.8
]
# edges between nodes
edge [color = black,
fontname = 'Helvetica',
fontsize = 10]
1 -> 2 [label = 'p(female)']
1 -> 3 [label = 'p(male)']
2 -> 4 [label = 'p(revolver | female)']
3 -> 4 [label = 'p(revolver | male)']
# labels for each node
1 [label = 'Gender?']
2 [label = 'If female\nuse revolver?']
3 [label = 'If male\nuse revolver?']
4 [label = 'Revolver\nused?']
rankdir='LR'
}"
)
```
## Probability operations
```{r}
# Make a deck of cards
df.cards = tibble(suit = rep(c("Clubs", "Spades", "Hearts", "Diamonds"), each = 8),
value = rep(c("7", "8", "9", "10", "Jack", "Queen", "King", "Ace"), 4))
```
```{r}
# conditional probability: p(Hearts | Queen) (via rules of probability)
df.cards %>%
summarize(p_hearts_given_queen =
sum(suit == "Hearts" & value == "Queen") /
sum(value == "Queen"))
```
```{r}
# conditional probability: p(Hearts | Queen) (via rejection)
df.cards %>%
filter(value == "Queen") %>%
summarize(p_hearts_given_queen = sum(suit == "Hearts")/n())
```
## Bayesian reasoning explained
```{r, echo=FALSE}
grViz("
digraph dot{
# general settings for all nodes
node [
shape = circle,
style = filled,
color = black,
label = ''
fontname = 'Helvetica',
fontsize = 10,
fillcolor = lightblue,
fixedsize=true,
width = 0.8
]
# edges between nodes
edge [color = black,
fontname = 'Helvetica',
fontsize = 10]
1 -> 2 [label = 'ill']
1 -> 3 [label = 'healthy']
2 -> 4 [label = 'test +']
2 -> 5 [label = 'test -']
3 -> 6 [label = 'test +']
3 -> 7 [label = 'test -']
# labels for each node
1 [label = '10000\npeople']
2 [label = '100']
3 [label = '9900']
4 [label = '95']
5 [label = '5']
6 [label = '495']
7 [label = '9405']
rankdir='LR'
}"
)
```
## Getting Bayes right matters
### Bayesian reasoning example
```{r}
# prior probability of the disease
p.D = 0.0001
# sensitivity of the test
p.T_given_D = 0.999
# specificity of the test
p.notT_given_notD = 0.999
p.T_given_notD = (1 - p.notT_given_notD)
# posterior given a positive test result
p.D_given_T = (p.T_given_D * p.D) / ((p.T_given_D * p.D) + (p.T_given_notD * (1-p.D)))
p.D_given_T
```
### Bayesian reasoning example (COVID rapid test)
https://pubmed.ncbi.nlm.nih.gov/34242764/#:~:text=The%20overall%20sensitivity%20of%20the,%25%20CI%2024.4%2D65.1).
```{r}
# prior probability of the disease
p.D = 0.1
# sensitivity covid rapid test
p.T_given_D = 0.653
# specificity of covid rapid test
p.notT_given_notD = 0.999
p.T_given_notD = (1 - p.notT_given_notD)
# posterior given a positive test result
p.D_given_T = (p.T_given_D * p.D) / ((p.T_given_D * p.D) + (p.T_given_notD * (1-p.D)))
# posterior given a negative test result
p.D_given_notT = ((1-p.T_given_D) * p.D) / (((1-p.T_given_D) * p.D) + ((1-p.T_given_notD) * (1-p.D)))
str_c("Probability of COVID given a positive test: ", round(p.D_given_T * 100, 1), "%")
str_c("Probability of COVID given a negative test: ", round(p.D_given_notT * 100, 1), "%")
```
### Most people in the hospital are vaccinated
```{r}
# probability of being vaccinated
p.V = 0.8
# likelihood of hospital
p.H_given_V = 0.2
p.H_given_notV = 0.5
# posterior probability
p.V_given_H = (p.H_given_V * p.V) / ((p.H_given_V * p.V) + (p.H_given_notV * (1-p.V)))
p.V_given_H
```
## Building a Bayesis
### Dice example
```{r}
# prior
p.four = 0.5
p.six = 0.5
# possibilities
df.possibilities = tibble(observation = 1:6,
p.four = c(rep(1/4, 4), rep(0, 2)),
p.six = c(rep(1/6, 6)))
# data
# data = c(4)
# data = c(4, 2, 1)
data = c(4, 2, 1, 3, 1)
# data = c(4, 2, 1, 3, 1, 5)
# likelihood
p.data_given_four = prod(df.possibilities$p.four[data])
p.data_given_six = prod(df.possibilities$p.six[data])
# posterior
p.four_given_data = (p.data_given_four * p.four) /
((p.data_given_four * p.four) +
(p.data_given_six * p.six))
p.four_given_data
```
Given this data $d$ = [`r data`], there is a `r round(p.four_given_data * 100)`% chance that the four sided die was rolled rather than the six sided die.
## Additional resources
### Cheatsheets
- [Probability cheatsheet](figures/probability.pdf)
### Books and chapters
- [Probability and Statistics with examples using R](http://www.isibang.ac.in/~athreya/psweur/)
- [Learning statistics with R: Chapter 9 Introduction to probability](https://learningstatisticswithr-bookdown.netlify.com/probability.html#probstats)
### Misc
- [Bayes' theorem in three panels](https://www.tjmahr.com/bayes-theorem-in-three-panels/)
- [Statistics 110: Probability; course at Harvard](https://projects.iq.harvard.edu/stat110)
- [Bayes theorem and making probability intuitive](https://www.youtube.com/watch?v=HZGCoVF3YvM&feature=youtu.be)
## Session info
Information about this R session including which version of R was used, and what packages were loaded.
```{r, echo=F}
sessionInfo()
```