-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path04-Random-variables.Rmd
1826 lines (1552 loc) · 63.7 KB
/
04-Random-variables.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Random variables {#random-variables}
```{r setup4, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
prompt = FALSE,
tidy = TRUE,
collapse = TRUE)
library("tidyverse")
library("gridExtra")
```
Economics is a mostly *quantitative* field: its outcomes can be usually
described using numbers: price, quantity, interest rates, unemployment rates,
GDP, etc. Statistics is also a quantitative field: every statistic is a number
calculated from data. When a random outcome is described by a number, we call
that number a ***random variable***. We can use probability theory to describe
and model random variables.
This chapter will introduce the basic terminology and mathematical tools for
working with simple random variables.
::: {.goals data-latex=""}
**Chapter goals**
In this chapter, we will learn how to:
1. Define a random variable in terms of a random outcome.
2. Determine the support and range of a random variable.
3. Calculate and interpret the PDF of a discrete random variable.
4. Calculate and interpret the CDF of a discrete random variable.
5. Calculate interval probabilities from the CDF.
6. Calculate the expected value of a discrete random variable from its PDF.
7. Calculate a quantile from the CDF.
8. Calculate the variance of a discrete random variable from its PDF.
9. Calculate the variance from expected values.
9. Calculate the standard deviation from the variance.
10. Calculate the expected value for a linear function of a random variable.
11. Calculate the variance and standard deviation for a linear function of a
random variable.
12. Standardize a random variable.
13. Use standard discrete probability distributions:
- Bernoulli
- binomial
- discrete uniform.
:::
To prepare for this chapter, please review the chapter on
[probability and random events](#probability-and-random-events) and the section on
[sequences and summations](#sequences-and-summations) in the math appendix.
## Defining a random variable {#introduction-to-random-variables}
A ***random variable*** is a number whose value depends on a random outcome. The
idea here is that we are going to use a random variable to describe some (but
not necessarily every) aspect of the outcome.
::: example
**Random variables in roulette**
Here are a few random variables we could define in a roulette game:
- The number the ball lands on:
\begin{align}
b = b(\omega) = \omega
\end{align}
- An indicator for whether a bet on red wins:
\begin{align}
r = r(\omega)
= I(\omega \in RedWins)
= \begin{cases}
1 & \textrm{if } \omega \in RedWins \\
0 & \textrm{if } \omega \notin RedWins \\
\end{cases}
\end{align}
- The player's net profits from \$1 bet on red:
\begin{align}
y_{Red} = y_{Red}(\omega)
= \begin{cases}
1 & \textrm{ if } \omega \in RedWins \\
-1 & \textrm{ if } \omega \in RedWins^c
\end{cases}
\end{align}
That is, a player who bets \$1 on red wins \$1 if the ball lands on red
and loses \$1 if the ball lands anywhere else.
- The player's net profits from \$1 bet on 14:
\begin{align}
y_{14} = y_{14}(\omega)
= \begin{cases}
35 & \textrm{ if } \omega = 14 \\
-1 & \textrm{ if } \omega \neq 14
\end{cases}
\end{align}
That is, a player who bets \$1 on 14 wins \$35 if the ball lands on 14
and loses \$1 if the ball lands anywhere else.
All of these random variables are defined in terms of the underlying outcome
$\omega$.
:::
A random variable is always a function of the original outcome, but for
convenience, we usually leave its dependence on the original outcome implicit,
and write it as if it were an ordinary variable.
### Implied probabilities {#probability-distributions}
Since every random variable is a number, we can define its sample space as the
set of real numbers $\mathbb{R}$.
We can also define events for a random variable. For example, we can define
events like:
- $x = 1$.
- $x \leq 1$.
- $x \in \{1, 3, 5\}$.
- $x \in A$ (for some arbitrary set $A$)
for any random variable $x$.
Finally, each random variable has a probability distribution, which can be
derived from the probability distribution of the underlying outcome. That is,
let $\omega \in \Omega$ be some random outcome, and let $x = x(\omega)$
be some random variable that depends on that outcome. Then the probability
that $x$ is in some set $A$ is:
\begin{align}
\Pr(x \in A) = \Pr(\{\omega \in \Omega: x(\omega) \in A\})
\end{align}
Again, this definition looks complicated but it is not. We learned in
Section \@ref(calculating-probabilities) how to calculate the
probability of an event by adding up the probabilities of its elementary
events. So we can just:
1. List the outcomes (elementary events) that imply the event $x \in A$.
2. Calculate the probability of each outcome.
3. Add up the probabilities.
As in Section \@ref(calculating-probabilities), we can summarize this procedure
with a formula:
\begin{align}
\Pr(x \in A) &= \sum_{s \in A} \Pr(\omega = s) \\
&= \sum_{s \in \Omega} \Pr(\omega = s)I(s \in A)
\end{align}
and we can demonstrate the procedure by example.
::: example
**Probability distributions for roulette**
Assume we have a fair roulette game. Since each of our random variables is a
function of the outcome $\omega$, we can derive their probability distributions
from the probability distribution of $\omega$:
- The probability distribution for $b$ is:
\begin{align}
\Pr(b = 0) &= \Pr(\omega = 0) = 1/37 \approx 0.027 \\
\Pr(b = 1) &= \Pr(\omega = 1) = 1/37 \approx 0.027 \\
\vdots \nonumber \\
\Pr(b = 36) &= \Pr(\omega = 36) = 1/37 \approx 0.027 \\
\Pr(b \notin \{0,1,\ldots,36\}) &= 0 \\
\end{align}
- The probability distribution for $r$ is:
\begin{align}
\Pr(r = 1) &= \Pr(\omega \in RedWins) = 18/37 \approx 0.486 \\
\Pr(r = 0) &= \Pr(\omega \notin RedWins) = 19/37 \approx 0.514 \\
\Pr(r \notin \{0,1\}) &= 0
\end{align}
- The probability distribution for $y_{14}$ is:
\begin{align}
\Pr(y_{14} = 35) &= \Pr(\omega = 14) = 1/37 \approx 0.027 \\
\Pr(y_{14} = -1) &= \Pr(\omega \neq 14) = 36/37 \approx 0.973 \\
\Pr(y_{14} \notin \{-1,35\}) &= 0
\end{align}
Notice that these random variables are related to each other since they all
depend on the same underlying outcome $\omega$. Section \@ref(multiple-random-variables)
will explain how we can describe and analyze those relationships.
:::
### The support {#the-support}
The ***support*** of a random variable $x$ is the smallest[^401] set
$S_x \subset \mathbb{R}$ such that $\Pr(x \in S_x) = 1$.
[^401]: Technically, it is the smallest *closed* set, but let's ignore that.
In plain language, the support is the set of all values in the sample space that
might actually happen.
::: example
**The support in roulette**
The sample space of $b$ is $\mathbb{R}$ and the support of $b$ is
$S_{b} = \{0,1,2,\ldots,36\}$.
The sample space of $r$ is $\mathbb{R}$ and the support of
$r$ is $S_{r} = \{0,1\}$.
The sample space of $y_{14}$ is $\mathbb{R}$ and the support of
$y_{14}$ is $S_{14} = \{-1,35\}$.
Notice that these random variables all have the same sample space
($\mathbb{R}$) but not the same support.
:::
The random variables we will consider in this chapter have ***discrete***
support. That is, the support is a set of isolated points each of which has
a strictly positive probability. In most examples, the support will also have
a ***finite*** number of elements. All finite sets are also discrete, but
it is also possible for a discrete set to have an infinite number of elements.
For example, the set of positive integers $\{1,2,3,\ldots\}$ is discrete but
not finite.
Some random variables have a support that is continuous rather than discrete.
Chapter \@ref(more-on-random-variables) will cover continuous random variables.
## The PDF and CDF {#the-pdf-and-cdf}
### The PDF {#the-pdf-of-a-discrete-random-variable}
We can describe the probability distribution of a random variable with a
function called its ***probability density function (PDF)***.
The PDF of a discrete random variable $x$ is defined as:
\begin{align}
f_x(a) = \Pr(x = a)
\end{align}
where $a$ is any number (the value of $a$ does *not* need to be in the support).
By convention, we typically use a lower-case $f$ to represent a PDF, and we
use the subscript when needed to clarify which specific random variable we are
talking about.
The probability distribution and PDF of a random variable are closely related
but distinct. The probability distribution is a function that gives the
probability $\Pr(x \in A)$ for any event (set) $A$, while the PDF is a function
that gives the gives the probability $\Pr(x = a)$ for any number $a$.
Since $x = a$ is an event, you can derive the PDF directly from the probability
distribution.
::: example
**The PDF in roulette**
The random variables $b$, $r$ and $y_{14}$ are all discrete, and each has its
own PDF:
\begin{align}
f_b(a) &= \Pr(b = a) = \begin{cases}
1/37 & \textrm{if } a \in \{0,1,\ldots,36\} \\
0 & \textrm{otherwise} \\
\end{cases} \\
f_{r}(a) &= \Pr(r = a) = \begin{cases}
19/37 & \textrm{if } a = 0 \\
18/37 & \textrm{if } a = 1 \\
0 & \textrm{otherwise} \\
\end{cases} \\
f_{14}(a) &= \Pr(y_{14} = a) = \begin{cases}
36/37 & \textrm{if } a = -1 \\
1/37 & \textrm{if } a = 35 \\
0 & \textrm{otherwise} \\
\end{cases} \\
\end{align}
Figure \@ref(fig:RoulettePDF) below shows these three PDFs, plotted for each
integer between -10 and 40.
:::
```{r RoulettePDF, fig.cap = "*PDFs for the roulette example*"}
RoulettePDF <- tibble(a = seq(from=-10, to=40),
fb = c(rep(0, 10), rep(1/37, 37), rep(0, 4)),
fr = c(rep(0, 10), 19/37, 18/37, rep(0, 39)),
fred = c(rep(0, 9), 19/37, 0, 18/37, rep(0, 39)),
f14 = c(rep(0, 9), 36/37, rep(0, 35), 1/37, rep(0,5)))
p1 <- ggplot(data = RoulettePDF, mapping = aes(x = a)) +
geom_point(aes(y=fb), col = "blue") +
xlab("a") +
ylab("f_b(a)") +
ylim(0,0.1) +
geom_text(x = 4,
y = 2/37,
label = "f_b(a)",
col = "blue")
p2 <- ggplot(data = RoulettePDF, mapping = aes(x = a)) +
geom_point(aes(y=fr), col = "red") +
xlab("a") +
ylab("f_r(a)") +
ylim(0,1) +
geom_text(x = 4,
y = 18/37,
label = "f_r(a)",
col = "red")
p3 <- ggplot(data = RoulettePDF, mapping = aes(x = a)) +
geom_point(aes(y=f14), col = "orange") +
xlab("a") +
ylab("f_14(a)") +
ylim(0,1) +
geom_text(x = 2,
y = 36/37,
label = "f_14(a)",
col = "orange")
grid.arrange(p1,p2,p3,top="Probability density function (PDF)")
```
You can also derive the probability distribution from the PDF. Again, we can
make use of the idea of calculating event probabilities by adding up outcome
probabilities. To calculate the probability that a random variable $x$
is in some set $A$, follow three steps:
1. List all values in $A$.
2. Use the PDF to calculate the probability of each value.
3. Add up the probabilities.
As in Section \@ref(calculating-probabilities), we can summarize this procedure
with a formula:
\begin{align}
\Pr(x \in A) &= \sum_{s \in A} \Pr(x = s) \\
&= \sum_{s \in S_x} f_x(s)I(s \in A)
\end{align}
and we can demonstrate the procedure by example.
::: example
**Some event probabilities in roulette**
Since the outcome in roulette is discrete, we can calculate any event
probability by adding up the probabilities of the event's component outcomes.
The probability of the event $b \leq 1$ can be calculated by following these
steps:
1. *Write down the formula*, substituting in the appropriate variable names:
\begin{align}
\Pr(b \leq 1) &= \sum_{s \in S_b}f_b(s)I(s \leq 1)
\end{align}
2. *Find the support* and substitute it in the formula:
\begin{align}
\Pr(b \leq 1) &= \sum_{s=0}^{36}f_b(s)I(s \leq 1)
\end{align}
2. *Expand out the summation*:
\begin{align}
\Pr(b \leq 1)
&= \begin{aligned}[t]
& f_b(0) I(0 \leq 1)
+ f_b(1) I(1 \leq 1)
+ f_b(2) I(2 \leq 1) \\
&+ \cdots
+ f_b(36) I(36 \leq 1) \\
\end{aligned}
\end{align}
2. *Find the PDF* and substitute it into the formula:
\begin{align}
\Pr(b \leq 1)
&= \begin{aligned}[t]
& (1/37)*1
+ (1/37)*1
+ (1/37)*0 \\
&+ \cdots
+ (1/37)*0 \\
\end{aligned} \\
&= 2/37
\end{align}
The probability of the event $b \in Even$ can also be calculated by following
these steps:
\begin{align}
\Pr(b \in Even) &= \sum_{s=0}^{36}f_b(s)I(s \in Even) \\
&= \begin{aligned}[t]
& \underbrace{f_b(0)}_{1/37} \underbrace{I(0 \in Even)}_{0}
+ \underbrace{f_b(1)}_{1/37} \underbrace{I(1 \in Even)}_{0}
+ \underbrace{f_b(2)}_{1/37} \underbrace{I(2 \in Even)}_{1} \\
&+ \cdots
+ \underbrace{f_b(36)}_{1/37} \underbrace{I(36 \in Even)}_{1} \\
\end{aligned} \\
&= 18/37
\end{align}
Remember that zero is not counted as an even number in roulette, so it is not
in the event $Even$.
:::
The PDF of a discrete random variable therefore provides a compact but
complete way of summarizing its probability distribution. It has several
general properties:
1. It is always between zero and one:
\begin{align}
0 \leq f_x(a) \leq 1
\end{align}
since it is a probability.
2. It sums up to one over the support:
\begin{align}
\sum_{a \in S_x} f_x(a) = \Pr(x \in S_x) = 1
\end{align}
since the support has probability one by definition.
3. It is strictly positive for all values in the support:
\begin{align}
a \in S_x \implies f_x(a) > 0
\end{align}
since the support is the *smallest* set that has probability one.
You can confirm that examples above all satisfy these properties.
### The CDF {#the-cdf}
Another way to describe the probability distribution of a random variable is
with a function called its ***cumulative distribution function (CDF)***.
The CDF is a little less intuitive than the PDF, but it has the advantage that
it always has the same definition whether the random variable is discrete,
continuous, or even some combination of the two.
The CDF of the random variable $x$ is the function
$F_x:\mathbb{R} \rightarrow [0,1]$ defined by:
\begin{align}
F_x(a) = Pr(x \leq a)
\end{align}
where $a$ is any number. By convention, we typically use an upper-case $F$ to
indicate a CDF, and we use the subscript to indicate what random variable we are
talking about.
We can construct the CDF of a discrete random variable by just adding up the
PDF. That is, for any value of $a$ we can calculate $F_x(a)$ by following
three steps:
1. Find every value $s$ in the support of $x$ that is less than or equal to
$a$.
2. Calculate the probability of each value using the PDF.
3. Add up the probabilities.
Expressed as a formula, this procedure is:
\begin{align}
F_x(a) &= \Pr(x \leq a) \\
&= \sum_{s \in S_x} f_x(s)I(s \leq a)
\end{align}
Again, the formula looks complicated but implementation is simple in practice.
::: example
**Deriving the CDF of $y_{14}$**
We will derive the CDF $F_{14}(\cdot)$ of the random variable $y_{14}$
using the formula above and following this step-by-step procedure:
1. *Write down the formula*, substituting in the appropriate variable names:
\begin{align}
F_{14}(a) &= \Pr(y_{14} \leq a) \\
&= \sum_{s \in S_{14}} f_{14}(s)I(s \leq a)
\end{align}
2. *Find the support* and substitute it into the formula. In this case, we
earlier found that the support of $y_{14}$ is $S_{14} = \{-1, 35\}$ so:
\begin{align}
F_{14}(a) &= \sum_{s \in \{-1, 35\} } f_{14}(s)I(s \leq a)
\end{align}
3. *Expand out the summation*:
\begin{align}
F_{14}(a) &= f_{14}(-1)I(-1 \leq a) + f_{14}(35)I(35 \leq a)
\end{align}
If you don't know how to do this, review the material on summations
in the Math Appendix.
4. *Find the PDF* and substitute it into the formula. In this case, we earlier
found that $f_{14}(-1) = 36/37 \approx 0.973$ and
$f_{14}(35) = 1/37 \approx 0.027$, so:
\begin{align}
F_{14}(a) &= 36/37 * I(-1 \leq a) + 1/37 * I(35 \leq a) \\
&\approx 0.973 * I(-1 \leq a) + 0.027 * I(1 \leq a) \\
\end{align}
5. *Try out a few values for $a$*. You can try as many values as you like until
you get a feel for what this function looks like:
\begin{align}
F_{14}(-100) &\approx 0.973 * \underbrace{I (-1 \leq -100)}_{=0} + 0.027 * \underbrace{I(35 \leq -100)}_{=0} \\
&= 0 \\
F_{14}(-2) &\approx 0.973 * \underbrace{I (-1 \leq -2)}_{=0} + 0.027 * \underbrace{I(35 \leq -2)}_{=0} \\
&= 0 \\
F_{14}(-1) &\approx 0.973 * \underbrace{I (-1 \leq -1)}_{=1} + 0.027 * \underbrace{I(35 \leq -1)}_{=0} \\
&\approx 0.973 \\
F_{14}(0) &\approx 0.973 * \underbrace{I (-1 \leq 0)}_{=1} + 0.027 * \underbrace{I(35 \leq 0)}_{=0} \\
&\approx 0.973 \\
F_{14}(35) &\approx 0.973 * \underbrace{I (-1 \leq 35)}_{=1} + 0.027 * \underbrace{I(35 \leq 35)}_{=1} \\
&= 1 \\
F_{14}(100) &\approx 0.973 * \underbrace{I (-1 \leq 100)}_{=1} + 0.027 * \underbrace{I(35 \leq 100)}_{=1} \\
&= 1
\end{align}
6. *Summarize your results* in a clear and simple formula. Case notation is a
good way to do this:
\begin{align}
F_{14}(a) &\approx \begin{cases}
0 & a < -1 \\
0.973 & -1 \leq a < 35 \\
1 & a \geq 35 \\
\end{cases}
\end{align}
Figure \@ref(fig:RouletteCDF1) below shows this CDF. As you can see, it starts
off at zero, jumps up at the values $-1$ and $35$ and then stays at one after
that.
:::
```{r RouletteCDF1, fig.cap = "*A CDF for the roulette example*"}
RouletteCDF <- RoulettePDF %>%
mutate (Fb = cumsum(fb)) %>%
mutate (Fred = cumsum(fred))%>%
mutate (F14 = cumsum(f14))
ggplot(data = RouletteCDF, mapping = aes(x = a)) +
geom_step(aes(y=F14), col = "orange") +
xlab("a") +
ylab("F(a)") +
geom_text(x = 8,
y = 32/37,
label = "F_14(a)",
col = "orange") +
labs(title = "Cumulative distribution function (CDF)",
subtitle = "Roulette - net player profits for a bet on 14",
caption = "",
tag = "")
```
I have given a specific formula and step-by-step instructions here, but once
you have done a few of these calculations, you will probably be able to handle
most cases without explicitly following each step. If you are stuck, come back
to these instructions.
::: example
**More CDFs for roulette**
We can follow the same procedure to derive CDFs for the other random variables:
- The CDF of $b$ is:
\begin{align}
F_b(a) = \begin{cases}
0 & a < 0 \\
1/37 & 0 \leq a < 1 \\
2/37 & 1 \leq a < 2 \\
\vdots & \vdots \\
36/37 & 35 \leq a < 36 \\
1 & a \geq 36 \\
\end{cases}
\end{align}
- The CDF of $y_{14}$ is:
\begin{align}
F_{14}(a) = \begin{cases}
0 & a < -1 \\
36/37 & -1 \leq a < 35 \\
1 & a \geq 35 \\
\end{cases}
\end{align}
:::
If you plot a CDF you will notice it follows certain patterns.
::: example
**CDF properties**
Figure \@ref(fig:RouletteCDF) below graphs the CDFs from the previous example.
Notice that they show a few common properties:
- The CDF runs from zero to one, and never leaves that range.
- The CDF never goes down, it only goes up or stays the same.
- The CDF has a distinctive "stair-step" shape, jumping up at each point in
the support, and flat between those points,
In fact, the CDF of *any* discrete random variable has these properties.
:::
```{r RouletteCDF, fig.cap = "*CDFs for the roulette example*"}
RouletteCDF <- RoulettePDF %>%
mutate (Fb = cumsum(fb)) %>%
mutate (Fr = pmax(0.01,pmin(cumsum(fr),0.99))) %>%
mutate (Fred = cumsum(fred))%>%
mutate (F14 = cumsum(f14))
ggplot(data = RouletteCDF, mapping = aes(x = a)) +
geom_step(aes(y=Fb), col = "blue") +
geom_step(aes(y=Fr), col = "red") +
xlab("a") +
ylab("F(a)") +
geom_text(x = 7,
y = 4/37,
label = "F_b(a)",
col = "blue") +
geom_text(x = 3,
y = 18/37,
label = "F_r(a)",
col = "red") +
labs(title = "Cumulative distribution function (CDF)",
subtitle = "Roulette - number ball lands on (b), red wins indicator (r) ",
caption = "Note: values slightly modified at far left and far right to make both lines visible",
tag = "")
```
All CDFs have the following properties:
1. The CDF is a *probability,* That is:
\begin{align}
0 \leq F_x(a) \leq 1
\end{align}
for any number $a$.
2. The CDF is *non-decreasing*. That is:
\begin{align}
F_x(a) \leq F_x(b)
\end{align}
for any two numbers $a$ and $b$ such that $a \leq b$.
3. The CDF *runs from zero to one*. That is, it is zero or close to zero for
low values of $a$, and one or close to one for high values of $a$. We can use
limits to give precise meaning to the broad terms "close", "low", and "high":
\begin{align}
\lim_{a \rightarrow -\infty} F_x(a) = \Pr(x \leq -\infty) &= 0 \\
\lim_{a \rightarrow \infty} F_x(a) = \Pr(x \leq \infty) &= 1
\end{align}
You can review the section on [limits](#limits) in the math appendix if you
do not follow the notation.
In addition, all discrete random variables have the stair-step shape.
In addition to constructing the CDF from the PDF, we can also go the other way,
and construct the PDF of a discrete random variable from its CDF. Each little
jump in the CDF is a point in the support, and the size of the jump is exactly
equal to the PDF.
::: {.fyi data-latex=""}
In more formal mathematics, the formula for deriving the PDF of a discrete
random variable from its CDF would be written:
\begin{align}
f_x(a) = F_x(a) - \lim_{\epsilon \rightarrow 0} F_x(a-|\epsilon|)
\end{align}
but we can just think of it as the size of the jump.
:::
The table below summarizes the relationship between the probability
distribution, the PDF, and the CDF. Since each of these functions provides
a complete description of the associated random variable, we can choose to work
with whichever is most convenient in a given application, and we can easily
convert between them as needed.
| Description | Probability distribution | PDF | CDF |
|:------------|:------------------:|:------------:|:---------------:|
| Notation | $\Pr(A)$ | $f_x(a)$ | $F_x(a)$ |
| Argument | An event (set) $A$ | A number $a$ | A number $a$ |
| Value | $\Pr(x \in A)$ | $\Pr(x = a)$ | $\Pr(x \leq a)$ |
| Fully describes distribution? | Yes | Yes | Yes |
### Interval probabilities {#interval-probabilities}
We can use the CDF to calculate the probability that $x$ lies in any interval.
That is, let $L$ and $H$ be any two numbers such that $L < H$. Then the
probability that $x$ is between $L$ and $H$ is given by:
\begin{align}
F_x(H) - F_x(L) &= \Pr(x \leq H) - \Pr(x \leq L) \\
&= \Pr((x \leq L) \cup (L < x \leq H)) - \Pr(x \leq L) \\
&= \Pr(x \leq L) + \Pr(L < x \leq H) - \Pr(x \leq L) \\
&= \Pr(L < x \leq H)
\end{align}
Notice that we have to be a little careful here to distinguish between the
strict inequality $<$ and the weak inequality $\leq$, because it is always
possible for $x$ to be exactly equal to $L$ or $H$. We can use the PDF as
needed to adjust for those possibilities:
\begin{align}
\Pr(L < x \leq H) &= F_x(H) - F_x(L) \\
\Pr(L \leq x \leq H) &= \Pr(x = L) + \Pr(L < x \leq H) \\
&= f_x(a) + F_x(b) - F_x(a) \\
\Pr(L < x < H) &= \Pr(L < x \leq H) - \Pr(x = H) \\
&= F_x(H) - F_x(L) - f_x(H) \\
\Pr(L \leq x < H) &= \Pr(x = L) + \Pr(L < x \leq H) - \Pr(x = H) \\
&= f_x(L) + F_x(H) - F_x(L) - f_x(H)
\end{align}
::: example
**Calculating interval probabilities**
Consider the CDF for $b$ derived above. Then:
\begin{align}
\Pr(b \leq 36) &= F_b(36) \\
&= 1 \\
\Pr(1 < b \leq 36) &= F_b(36) - F_b(1) \\
&= 1 - 2/37 \\
&= 35/37
\end{align}
Note that the placement of the $<$ and $\leq$ are important here.
What if we want $\Pr(1 \leq b \leq 36)$ instead? We can split that event into
two disjoint events $(b = 1)$ and $(1 < b \leq 36)$ and apply the axioms of
probability:
\begin{align}
\Pr(1 \leq b \leq 36) &= \Pr( (b = 1) \cup (1 < b \leq 36) ) \\
&= \Pr(b = 1) + \Pr(1 < b \leq 36) \\
&= f_b(1) + F_b(36) - F_b(1) \\
&= 1/37 + 1 - 2/37 \\
&= 36/37
\end{align}
We can use similar methods to determine $\Pr(1 < b < 36)$ or
$\Pr(1 \leq b < 36)$.
:::
### Range and mode {#range}
The ***range*** of a random variable is the interval from its lowest possible
value to its highest possible value. It can be caclulated from the support:
\begin{align}
range(x) &= [\min(S_x), \max(S_x)]
\end{align}
or from the PDF or CDF.
:::example
**The range in roulette**
The support of $y_{Red}$ is $\{-1,1\}$ so its range is $[-1,1]$.
The support of $y_{14}$ is $\{-1,35\}$ so its range is $[-1,35]$.
The support of $b$ is $\{0,1,2,\ldots,36\}$ so its range is $[0,36]$.
:::
The ***mode*** of a discrete random variable is (loosely speaking) its most
likely value. That is, it is the number $a$ that maximizes $\Pr(x=a)$.
A random variable can have more than one mode, in which case the mode is
a set of numbers.
:::example
**The mode in roulette**
The mode of $r$ is $0$, since $f_{r}(0) = 0.514 > 0.486 = f_{r}(1)$.
The mode of $y_{Red}$ is $-1$, since $f_{Red}(-1) = 0.514 > 0.486 = f_{Red}(1)$.
The mode of $y_{14}$ is $-1$, since $f_{14}(-1) = 0.973 > 0.027 = f_{14}(35)$.
The mode of $b$ is the set $\{0,1,2,\ldots,36\}$ since each of those values has
the same probability.
:::
::: {.fyi data-latex=""}
The mathematical language defining the mode is:
\begin{align}
mode(x) &= \textrm{arg }\max_{a \in S_x} f_x(a)
\end{align}
:::
## The expected value {#the-expected-value}
The ***expected value*** of a random variable $x$ is written $E(x)$. When $x$
is discrete, it is defined as:
\begin{align}
E(x) = \sum_{a \in S_x} a\Pr(x=a) = \sum_{a \in S_x} af_x(a)
\end{align}
The expected value is also called the ***mean***, the ***population mean***
or the ***expectation*** of the random variable.
The formula for the expected value may look difficult if you are not used to the
notation, but it is actually quite simple to calculate: just multiply each
possible value by its probability, and then add it all up.
::: example
**Calculating the expected value of $y_{14}$**
In previous examples, we found the support and PDF of $y_{14}$. We can
find its expected value by following these steps:
1. *Write down the formula*, substituting in the correct variable names:
\begin{align}
E(y_{14}) &= \sum_{a \in S_{14}} a f_{14}(a)
\end{align}
2. *Find the support* and substitute:
\begin{align}
E(y_{14}) &= \sum_{a \in \{-1,35\}} a f_{14}(a)
\end{align}
3. *Expand out the summation*:
\begin{align}
E(y_{14}) &= -1 * f_{14}(-1) + 35 * f_{14}(35)
\end{align}
4. *Find the PDF* and substitute:
\begin{align}
E(y_{14}) &= -1 * 36/37 + 35 * 1/37 \\
&= -1/37 \\
&\approx -0.027
\end{align}
:::
The expected value of a random variable is an important concept, and has several
closely related interpretations:
- As a ***weighted average*** of its possible values, That is, it is like an
average but instead of each value receiving equal weight it receives a weight
equal to the probability that we will observe that value.
- As a measure of ***central tendency***, i.e., a typical or representative
value for the random variable. Other measures of central tendency include
the [median](#the-median) and the [mode](#range).
- As a ***prediction*** for the value of the random variable. That is, we want
to predict/guess its value, and we want our guess to be as close as possible
to the actual (not-yet-known) value. The median and mode are also used for
prediction; the best prediction method depends on what you mean by "as close
as possible":
- The mode is the most likely to be *exactly* correct (prediction error
of zero).
- The median tends to produce the lowest *absolute* prediction error.
- The expected value tends to produce the lowest *squared* prediction error.
We can use whichever of these interpretations makes sense for a given
application.
::: example
**Interpreting the expected value of $y_{14}$**
As described earlier, we can think of the expected value as describing a
typical or predicted value for the random variable. In this case, our
result can be interpreted as a prediction of the house advantage: the player
can expect to lose approximately 2.7 cents per dollar bet on 14, and the house
can expect to gain approximately 2.7 cents per dollar bet on 14.
:::
We can follow the same procedures to find and interpret the expected value of
any discrete random variable.
::: example
**More expected values in roulette**
The support of $b$ is $\{0,1,2\ldots,36\}$ and its PDF is the $f_b(\cdot)$
function we calculated earlier. So its expected value is:
\begin{align}
E(b) &= \sum_{a \in S_b} a f_b(a) \\
&= \sum_{a \in \{0,1,2\ldots,36\}} a f_b(a) \\
&= 0*\underbrace{f_b(0)}_{1/37} + 1*\underbrace{f_b(1)}_{1/37} + \cdots 36*\underbrace{f_b(36)}_{1/37} \\
&= \frac{1 + 2 + \cdots + 36}{37} \\
&= 18
\end{align}
The support of $r$ is $\{0,1\}$ and its PDF is the $f_r(\cdot)$ function we
calculated earlier. So its expected value is:
\begin{align}
E(r) &= \sum_{a \in S_r} a f_r(a) \\
&= \sum_{a \in \{0,1\}} a f_r(a) \\
&= 0*\underbrace{f_r(0)}_{19/37} + 1*\underbrace{f_r(1)}_{18/37} \\
&= 18/37 \\
&\approx 0.486
\end{align}
:::
## Quantiles and their relatives {#the-properties-of-a-random-variable}
You have probably heard of the median, and you may have heard of percentiles.
These are special cases of a group of numbers called the ***quantiles***
of a distribution.
### Quantiles and percentiles {#quantiles-and-percentiles}
Let $q$ be any number *strictly* between zero and one. Then the $q$
***quantile*** of a random variable $x$ is defined as:
\begin{align}
F_x^{-1}(q) &= \min\{a \in S_X: \Pr(x \leq a) \geq q\} \\
&= \min\{a \in S_x: F_x(a) \geq q\}
\end{align}
where $F_x(\cdot)$ is the CDF of $x$. The quantile function $F_x^{-1}(\cdot)$
is also called the ***inverse CDF*** of $x$. The $q$ quantile of a distribution
is also called the $100q$ ***percentile***; for example the 0.75 quantile of
$x$ is also called the 75th percentile of $x$.
We can use the formula above to find quantiles from the CDF. The procedure is
easier to follow if we do it graphically.
::: example
**Quantiles in roulette**
We can find the 0.25, 0.5, and 0.75 quantiles of the random variable $r$ by
following these steps:
1. *Write down the CDF*:
\begin{align}
F_{r}(a) = \begin{cases}
0 & a < 0 \\
19/37 \approx 0.514 & 0 \leq a < 1 \\
1 & a \geq 1 \\ \end{cases}
\end{align}
2. *Plot the CDF*, as in Figure \@ref(fig:RouletteQuantiles).
3. *Find the quantile* by applying the definition or by finding the value on the
graph where the CDF crosses $q$.
- For example, the 0.25 quantile (25th percentile) is defined as:
\begin{align}
F_{r}^{-1}(0.25) &= \min\{a \in S_x: F_{r}(a) \geq 0.25\} \\
&= \min \{0, 1\} \\
&= 0
\end{align}
or we can draw the blue dashed line marked "0.25 quantile" and see that it
hits the red line at $a = 0$.
- Applying the same method, we can find that the 0.5 quantile (50th
percentile) is also zero.
- Following this method again, we find the 0.75 quantile (75th percentile) by
seeing that the red line crosses the blue dashed line marked "0.75 quantile"
at $a = 1$, or we can apply the definition:
\begin{align}
F_{r}^{-1}(0.75) &= \min\{a \in S_x: F_{r}(a) \geq 0.75\} \\
&= \min \{1\} \\
&= 1
\end{align}
Either method will work.
:::
```{r RouletteQuantiles, fig.cap = "*Deriving quantiles from the CDF*"}
ggplot(data = filter(RouletteCDF,a<=20), mapping = aes(x = a)) +
geom_step(aes(y=Fr), col = "red") +
xlab("a") +
ylab("F(a)") +
annotate("text",
x = 17,
y = 0.9,
label = "F_r(a)",
col = "red") +
annotate("text",
x = 5.5,
y = 0.25,
label = "0.25 quantile = 0",
col = "blue") +
annotate("text",
x = 6.0,
y = 0.5,
label = "0.5 quantile (median) = 0",
col = "blue") +
annotate("text",
x = 6.5,
y = 0.75,
label = "0.75 quantile = 1",
col = "blue") +
geom_segment(x=-10,
xend=0,
y=0.25,
yend=0.25,
col="blue",
linetype = "dashed") +
geom_segment(x=-10,
xend=0,
y=0.5,
yend=0.5,
col="blue",
linetype = "dashed") +
geom_segment(x=-10,
xend=1,
y=0.75,
yend=0.75,
col="blue",
linetype = "dashed") +
labs(title = "Cumulative distribution function (CDF)",
subtitle = "Roulette - indicator for red wins (r)",
caption = "",
tag = "")
```
The formula for the quantile function may look intimidating, but it can be
constructed by just "flipping" the axes of the CDF. This is why the quantile
function is also called the inverse CDF.
::: example
**The whole quantile function**
We can use the same ideas as in the previous example to show that $F_{r}^{-1}(q)$ is
equal to $0$ for any $q$ between $0$ and $19/37$, and equal to $1$ for any $q$
between $19/37$ and $1$. But what is the value of $F^{-1}_{r}(19/37)$? To
figure that out we will need to carefully apply the definition:
\begin{align}
F_{r}^{-1}(19/37) &= \min\{a \in S_x: F_{r}(a) \geq 19/37\} \\
&= \min \{-1,1\} \\
&= -1
\end{align}
So the full quantile function can be written in case form as:
\begin{align}
F_{r}^{-1}(q) &= \begin{cases}
0 & 0 < q \leq 19/37 \\
1 & 19/37 < q < 1 \\
\end{cases}
\end{align}
and we can plot it as in Figure \@ref(fig:RouletteQuantFunction) below. Notice
that the quantile function looks just like the CDF, but with the horizontal and
vertical axes flipped.
:::
```{r RouletteQuantFunction, fig.cap = "*Quantile function for $r$*"}
RouletteQuant <- tibble(a = c(0.0001,19/37,0.9999),
qred = c(0,1,1))
ggplot(data = RouletteQuant, mapping = aes(x = a)) +
geom_step(aes(y=qred),col = "red") +
xlab("quantile") +
ylab("value of r at this quantile") +
geom_text(x = 0.8,
y = 0.9,
label = "{F^{-1}}(quantile)",
col = "red",
parse = TRUE) +
labs(title = "Quantile function (inverse CDF)",
subtitle = "Roulette - indicator for red wins (r)",
caption = "",
tag = "")
```
### Median {#the-median}
The ***median*** of a random variable is its 0.5 quantile or 50th percentile.
::: example
**The median in roulette**
The median of $r$ is just its 0.5 quantile or 50th percentile:
\begin{align}
median(r) = F_{r}^{-1}(0.5) = 0
\end{align}
:::
Like the expected value, the median is often interpreted as a measure of central