-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy path02-ch2.Rmd
1596 lines (1118 loc) · 79.6 KB
/
02-ch2.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
# Probability Theory {#pt}
This chapter reviews some basic concepts of probability theory and demonstrates how they can be
applied in `r ttcode("R")`.
Most of the statistical functionalities in base `r ttcode("R")` are collected in the `r ttcode("stats")` package. It provides simple functions which compute descriptive measures and facilitate computations involving a variety of probability distributions. It also contains more sophisticated routines that, enable the user to estimate a large number of models based on the same data or help to conduct extensive simulation studies. `r ttcode("stats")` is part of the base distribution of `r ttcode("R")`, meaning that it is installed by default, so there is no need to run `install.packages("stats")` or `library("stats")`. Simply execute `library(help = "stats")` in the console to view the documentation and a complete list of all functions gathered in `r ttcode("stats")`. For most packages a documentation that can be viewed within *RStudio* is available. Documentation can be invoked using the `r ttcode("?")` operator, for example, upon execution of `?stats` the documentation of the `r ttcode("stats")` package is shown in the help tab of the bottom-right pane.
Subsequently, our focus is on (some of) the probability distributions handled by `r ttcode("R")` and how to use the relevant functions to solve simple problems. Afterwards, we will review some core concepts of probability theory. Among other things, you will learn how to draw random numbers, how to compute densities, probabilities, quantiles and alike. As we shall see, it is very convenient to do these computations in R.
## Random Variables and Probability Distributions
Let us briefly review some basic concepts of probability theory.
- The mutually exclusive results of a random process are called the *outcomes*. 'Mutually exclusive' means that only one of the possible outcomes can be observed.
- We refer to the *probability* of an outcome as the proportion that the outcome occurs in the long run, that is, if the experiment is repeated many times.
- The set of all possible outcomes of a random variable is called the *sample space*.
- An *event* is a subset of the sample space and consists of one or more outcomes.
These ideas are unified in the concept of a *random variable* which is a numerical summary of random outcomes. Random variables can be *discrete* or *continuous*.
- Discrete random variables have discrete outcomes, e.g., $0$ and $1$.
- A continuous random variable may take on a continuum of possible values.
### Probability Distributions of Discrete Random Variables {-}
A typical example for a discrete random variable $D$ is the result of a dice
roll: in terms of a random experiment this is nothing but randomly selecting a
sample of size $1$ from a set of numbers which are mutually exclusive outcomes.
Here, the sample space is $\{1,2,3,4,5,6\}$ and we can think of many different
events, e.g., 'the observed outcome lies between $2$ and $5$'.
A basic function to draw random samples from a specified set of elements is the function `r ttcode("sample()")`, see `?sample`. We can use it to simulate the random outcome of a dice roll. Let's roll the dice!
```{r, echo = T, eval = T, message = F, warning = F}
sample(1:6, size=1)
```
The probability distribution of a discrete random variable is the list of all possible values of the variable and their probabilities that sum to $1$. The cumulative probability distribution function gives the probability that the random variable is less than or equal to a particular value.
For the dice roll, the probability distribution and the cumulative probability distribution are summarized in Table \@ref(tab:pdist).
```{r pdist, echo=FALSE, purl=FALSE}
pdfdata <- rbind("Outcome"=as.character(1:6), "Probability"=c("1/6","1/6","1/6","1/6","1/6","1/6"), "Cumulative Probability"=c("1/6","2/6","3/6","4/6","5/6","1"))
knitr::kable(pdfdata, format = my_output, caption = "PDF and CDF of a Dice Roll")
```
We can easily plot both functions using `r ttcode("R")`. Since the probability is equal to $1/6$ for each outcome, we can set up the `r ttcode("probability")` vector using the function `r ttcode("rep()")`, which replicates a given value a specified number of times.
```{r, eval = T, message = F, warning = F, fig.align='center', fig.pos="h"}
# generate the vector of probabilities
probability <- rep(1/6, 6)
# plot the probabilities
plot(probability,
xlab = "Outcomes",
ylab="Probability",
main = "Probability Distribution",
pch=20)
```
For the cumulative probability distribution we need the cumulative probabilities, i.e., we need the cumulative sums of the vector `r ttcode("probability")`. These sums can be computed using `r ttcode("cumsum()")`.
```{r, echo = T, eval = T, message = F, warning = F, fig.align='center'}
# generate the vector of cumulative probabilities
cum_probability <- cumsum(probability)
# plot the probabilites
plot(cum_probability,
xlab = "Outcomes",
ylab="Cumulative Probability",
main = "Cumulative Probability Distribution",
pch=20)
```
### Bernoulli Trials {-}
The set of elements from which `r ttcode("sample()")` draws outcomes does not have to consist of numbers
only. We might as well simulate coin tossing with outcomes $H$ (heads) and $T$
(tails).
```{r, echo = T, eval = T, message = F, warning = F}
sample(c("H", "T"), 1)
```
The result of a single coin toss is a *Bernoulli* distributed random variable, i.e., a variable with two possible distinct outcomes.
Imagine you are about to toss a coin $10$ times in a row and wonder how likely it
is to end up with a $5$ times heads. This is a typical example of what we call a *Bernoulli experiment* as it consists of $n=10$ Bernoulli trials that are independent of each other and we are interested in the likelihood of observing $k=5$ successes $(H)$ that occur with probability $p=0.5$ (assuming a fair coin) in each trial. Note that the order of the outcomes does not matter here.
It is a [well known result](https://en.wikipedia.org/wiki/Binomial_distribution) that the number of successes $k$ in a Bernoulli experiment follows a binomial distribution. We denote this as
$$k \sim B(n,p).$$
The probability of observing $k$ successes in the experiment $B(n,p)$ is given by
$$f(k)=P(k)=\begin{pmatrix}n\\ k \end{pmatrix} \cdot p^k \cdot
(1-p)^{n-k}=\frac{n!}{k!(n-k)!} \cdot p^k \cdot (1-p)^{n-k},$$
as $\begin{pmatrix}n\\ k \end{pmatrix}$ the binomial coefficient.
In `r ttcode("R")`, we can solve problems like the one stated above by means of the function `r ttcode("dbinom()")` which calculates $P(k\vert n, p)$ the probability of the binomial distribution given the parameters `r ttcode("x")` ($k$),
`r ttcode("size")` ($n$), and `r ttcode("prob")` ($p$), see `?dbinom`. Let us compute $P(k=5\vert n = 10, p = 0.5)$ (we write this as $P(k=5)$).
```{r, echo = T, eval = T, message = F, warning = F}
dbinom(x = 5,
size = 10,
prob = 0.5)
```
We conclude that $P(k=5)$, the probability of observing Head $k=5$ times when tossing a fair coin $n=10$ times is about $24.6\%$.
Now assume we are interested in $P(4 \leq k \leq 7)$, i.e., the probability of
observing $4$, $5$, $6$ or $7$ successes for $B(10, 0.5)$. This may be computed by providing a vector as the argument `r ttcode("x")` in our call of `r ttcode("dbinom()")` and summing up using `r ttcode("sum()")`.
```{r, echo = T, eval = T, message = F, warning = F}
# compute P(4 <= k <= 7) using 'dbinom()'
sum(dbinom(x = 4:7, size = 10, prob = 0.5))
```
An alternative approach is to use `r ttcode("pbinom()")`, the distribution function of the binomial distribution to compute $$P(4 \leq k \leq 7) = P(k \leq 7) - P(k\leq3 ).$$
```{r, echo = T, eval = T, message = F, warning = F}
# compute P(4 <= k <= 7) using 'pbinom()'
pbinom(size = 10, prob = 0.5, q = 7) - pbinom(size = 10, prob = 0.5, q = 3)
```
The probability distribution of a discrete random variable is nothing but a list
of all possible outcomes that can occur and their respective probabilities. In the coin tossing example, we have $11$ possible outcomes for $k$.
```{r, echo = T, eval = T, message = F, warning = F}
# set up vector of possible outcomes
k <- 0:10
k
```
To visualize the probability distribution function of $k$ we may therefore do the following:
```{r, echo = T, eval = T, message = F, warning = F, fig.align='center'}
# assign the probabilities
probability <- dbinom(x = k,
size = 10,
prob = 0.5)
# plot the outcomes against their probabilities
plot(x = k,
y = probability,
ylab="Probability",
main = "Probability Distribution Function",
pch=20)
```
In a similar fashion we may plot the cumulative distribution function of $k$ by
executing the following code chunk:
```{r, echo = T, eval = T, message = F, warning = F, fig.align='center'}
# compute cumulative probabilities
prob <- pbinom(q = k,
size = 10,
prob = 0.5)
# plot the cumulative probabilities
plot(x = k,
y = prob,
ylab="Probability",
main = "Cumulative Distribution Function",
pch=20)
```
### Expected Value, Mean and Variance {-}
The expected value of a random variable is, loosely, the long-run average value of its outcomes when the number of repeated trials is large. For a discrete random variable, the
expected value is computed as a weighted average of its possible outcomes
whereby the weights are the related probabilities. This is formalized in Key
Concept 2.1.
```{r, eval = my_output == "html", results='asis', echo=FALSE, purl=FALSE}
cat('
<div class = "keyconcept" <div class = "keyconcept" id="KC2.1">
<h3 class = "right"> Key Concept 2.1 </h3>
<h3 class= "left"> Expected Value and the Mean </h3>
<p> Suppose the random variable $Y$
takes on $k$ possible values, $y_1, \\dots, y_k$, where $y_1$ denotes the first
value, $y_2$ denotes the second value, and so forth, and that the probability
that $Y$ takes on $y_1$ is $p_1$, the probability that $Y$ takes on $y_2$ is
$p_2$ and so forth. The expected value of $Y$, $E(Y)$ is defined as
$$ E(Y) = y_1 p_1 + y_2 p_2 + \\cdots + y_k p_k = \\sum_{i=1}^k y_i p_i, $$
where the notation $\\sum_{i=1}^k y_i p_i$ means "the sum of $y_i$ $p_i$ for $i$
running from $1$ to $k$". The expected value of $Y$ is also called the mean of $Y$
or the expectation of $Y$ and is denoted by $\\mu_Y$.
</p>
</div>')
```
```{r, eval = my_output == "latex", results='asis', echo=FALSE, purl=FALSE}
cat('\\begin{keyconcepts}[Expected Value and the Mean]{2.1}
Suppose the random variable $Y$
takes on $k$ possible values, $y_1, \\dots, y_k$, where $y_1$ denotes the first
value, $y_2$ denotes the second value, and so forth, and that the probability
that $Y$ takes on $y_1$ is $p_1$, the probability that $Y$ takes on $y_2$ is
$p_2$ and so forth. The expected value of $Y$, $E(Y)$ is defined as
$$ E(Y) = y_1 p_1 + y_2 p_2 + \\cdots + y_k p_k = \\sum_{i=1}^k y_i p_i, $$
where the notation $\\sum_{i=1}^k y_i p_i$ means \\"the sum of $y_i$ $p_i$ for $i$
running from $1$ to $k$\\". The expected value of $Y$ is also called the mean of $Y$
or the expectation of $Y$ and is denoted by $\\mu_Y$.
\\end{keyconcepts}')
```
In the dice example, the random variable, $D$ say, takes on $6$ possible values
$d_1 = 1, d_2 = 2, \dots, d_6 = 6$. Assuming a fair die, each of the $6$ outcomes occurs with a probability of $1/6$. It is therefore easy to calculate the exact value of $E(D)$ by hand:
$$ E(D) = \frac{1}{6} \sum_{i=1}^6 d_i = 3.5. $$
$E(D)$ is simply the average of the natural numbers from $1$ to $6$ since all weights $p_i$ are $1/6$. This can be easily calculated using the function `r ttcode("mean()")` which computes the arithmetic mean of a numeric vector.
```{r, echo = T, eval = T, message = F, warning = F}
# compute mean of natural numbers from 1 to 6
mean(1:6)
```
An example of sampling with replacement is rolling a dice three times in a row.
```{r, eval = T, message = F, warning = F}
# set seed for reproducibility
set.seed(1)
# rolling a dice three times in a row
sample(1:6, 3, replace = T)
```
Note that every call of `sample(1:6, 3, replace = T)` gives a different outcome since we draw with replacement at random. To allow you to reproduce the results of computations that involve random numbers, we will use `set.seed()` to set R's random number generator to a specific state. You should check that it actually works: set the seed in your R session to 1 and verify that you obtain the same three random numbers!
```{block2, randomseed, type='rmdknit'}
Sequences of random numbers generated by R are pseudo-random numbers, i.e., they are not "truly" random but approximate the properties of sequences of random numbers. Since this approximation is good enough for our purposes we refer to pseudo-random numbers as random numbers throughout this book.
In general, sequences of random numbers are generated by functions called "pseudo-random number generators" (PRNGs). The PRNG in R works by performing some operation on a deterministic value. Generally, this value is the previous number generated by the PRNG. However, the first time the PRNG is used, there is no previous value. A "seed" is the first value of a sequence of numbers --- it initializes the sequence. Each seed value will correspond to a different sequence of values. In R a seed can be set using <tt>set.seed()</tt>.
This is convenient for us:
If we provide the same seed twice, we get the same sequence of numbers twice. Thus, setting a seed before executing R code which involves random numbers makes the outcome reproducible!
```
Of course, we could also consider a much bigger number of trials, let's say $10000$.
By doing so, it would be pointless to simply print the results to the console since, by
default, `r ttcode("R")` displays up to $1000$ entries of large vectors and omits the
remainder (give it a try). Eyeballing the numbers does not reveal much.
Instead, let us calculate the sample average of the outcomes using `r ttcode("mean()")` and see if the result comes close to the expected value $E(D)=3.5$.
```{r, eval = T, message = F, warning = F}
# set seed for reproducibility
set.seed(1)
# compute the sample mean of 10000 dice rolls
mean(sample(1:6,
10000,
replace = T))
```
We find the sample mean to be fairly close to the expected value. This result will be discussed in Chapter \@ref(RSATDOSA) in more detail.
Other frequently encountered measures are the variance and the standard deviation. Both are measures of the *dispersion* of a random variable.
```{r, eval = my_output == "html", results='asis', echo=FALSE, purl=FALSE}
cat('<div class = "keyconcept" id="KC2.2">
<h3 class = "right"> Key Concept 2.2 </h3>
<h3 class= "left"> Variance and Standard Deviation </h3>
<p>
The variance of the discrete random variable $Y$, denoted by $\\sigma^2_Y$, is
$$ \\sigma^2_Y = \\text{Var}(Y) = E\\left[(Y-\\mu_y)^2\\right] = \\sum_{i=1}^k (y_i - \\mu_y)^2 p_i. $$
The standard deviation of $Y$ is $\\sigma_Y$, the square root of the variance. The units of the standard deviation are the same as the units of $Y$.
</p>
</div>')
```
```{r, eval = my_output == "latex", results='asis', echo=FALSE, purl=FALSE}
cat('\\begin{keyconcepts}[Variance and Standard Deviation]{2.2}
The variance of the discrete \\textit{random variable} $Y$, denoted by $\\sigma^2_Y$, is
$$ \\sigma^2_Y = \\text{Var}(Y) = E\\left[(Y-\\mu_Y)^2\\right] = \\sum_{i=1}^k (y_i - \\mu_Y)^2 p_i. $$
The standard deviation of $Y$ is $\\sigma_Y$, the square root of the variance. The units of the standard deviation are the same as the units of $Y$.
\\end{keyconcepts}')
```
The variance as defined in Key Concept 2.2, being a population quantity, *is not* implemented as a function in R. Instead we have the function `r ttcode("var()")` which computes the *sample variance*
$$ s^2_Y = \frac{1}{n-1} \sum_{i=1}^n (y_i - \overline{y})^2. $$
Remember that $s^2_Y$ is different from the so called *population variance* of a discrete random variable $Y$,
$$ \text{Var}(Y) = \frac{1}{N} \sum_{i=1}^N (y_i - \mu_Y)^2. $$
since it measures how the $n$ observations in the sample are dispersed around the sample average $\overline{y}$. Instead, $\text{Var}(Y)$ measures the dispersion of the whole population ($N$ members) around the population mean $\mu_Y$. The difference becomes clear when we look at our dice rolling example. For $D$ we have
$$ \text{Var}(D) = \frac{1}{6} \sum_{i=1}^6 (d_i - 3.5)^2 = 2.92 $$
which is obviously different from the result of $s^2$ as computed by `r ttcode("var()")`.
```{r, echo = 1, eval = T, message = F, warning = F}
var(1:6)
```
The sample variance as computed by `r ttcode("var()")` is an *estimator* of the population variance. You may check this using the widget below.
```{r, echo=FALSE, results='asis', purl=FALSE}
write_html(playground = T)
```
### Probability Distributions of Continuous Random Variables {-}
Since a continuous random variable takes on a continuum of possible values, we
cannot use the concept of a probability distribution as used for discrete random
variables. Instead, the probability distribution of a continuous random variable
is summarized by its *probability density function* (PDF).
The cumulative probability distribution function (CDF) for a continuous random variable is defined just as in the discrete case. Hence, the CDF of a continuous random variables states the probability that the random variable is less than or equal to a particular value.
For completeness, we present revisions of Key Concepts 2.1 and 2.2 for the continuous case.
```{r, eval = my_output == "html", results='asis', echo=FALSE, purl=FALSE}
cat('<div class = "keyconcept" id="KC2.3">
<h3 class = "right"> Key Concept 2.3 </h3>
<h3 class= "left"> Probabilities, Expected Value and Variance of a Continuous Random Variable </h3>
<p>
Let $f_Y(y)$ denote the probability density function of $Y$. The probability that $Y$ falls between $a$ and $b$ where $a < b$ is
$$ P(a \\leq Y \\leq b) = \\int_a^b f_Y(y) \\mathrm{d}y. $$
We further have that $P(-\\infty \\leq Y \\leq \\infty) = 1$ and therefore $\\int_{-\\infty}^{\\infty} f_Y(y) \\mathrm{d}y = 1$.
As for the discrete case, the expected value of $Y$ is the probability weighted average of its values. Due to continuity, we use integrals instead of sums. The expected value of $Y$ is defined as
$$ E(Y) = \\mu_Y = \\int y f_Y(y) \\mathrm{d}y. $$
The variance is the expected value of $(Y - \\mu_Y)^2$. We thus have
$$\\text{Var}(Y) = \\sigma_Y^2 = \\int (y - \\mu_Y)^2 f_Y(y) \\mathrm{d}y.$$
</p>
</div>')
```
```{r, eval = my_output == "latex", results='asis', echo=FALSE, purl=FALSE}
cat('
\\begin{keyconcepts}[Probabilities\\comma Expected Value and Variance of a Continuous Random Variable]{2.3}
Let $f_Y(y)$ denote the probability density function of $Y$. The probability that $Y$ falls between $a$ and $b$ where $a < b$ is
$$ P(a \\leq Y \\leq b) = \\int_a^b f_Y(y) \\mathrm{d}y. $$
We further have that $P(-\\infty \\leq Y \\leq \\infty) = 1$ and therefore $\\int_{-\\infty}^{\\infty} f_Y(y) \\mathrm{d}y = 1$.
As for the discrete case, the expected value of $Y$ is the probability weighted average of its values. Due to continuity, we use integrals instead of sums. The expected value of $Y$ is defined as
$$ E(Y) = \\mu_Y = \\int y f_Y(y) \\mathrm{d}y. $$
The variance is the expected value of $(Y - \\mu_Y)^2$. We thus have
$$\\text{Var}(Y) = \\sigma_Y^2 = \\int (y - \\mu_Y)^2 f_Y(y) \\mathrm{d}y.$$
\\end{keyconcepts}')
```
Let us discuss an example:
Consider the continuous random variable $X$ with PDF
$$ f_X(x) = \frac{3}{x^4}, x>1. $$
* We can show analytically that the integral of $f_X(x)$ over the real line equals $1$.
\begin{align}
\int f_X(x) \mathrm{d}x =& \int_{1}^{\infty} \frac{3}{x^4} \mathrm{d}x \\
=& \lim_{t \rightarrow \infty} \int_{1}^{t} \frac{3}{x^4} \mathrm{d}x \\
=& \lim_{t \rightarrow \infty} -x^{-3} \rvert_{x=1}^t \\
=& -\left(\lim_{t \rightarrow \infty}\frac{1}{t^3} - 1\right) \\
=& 1
\end{align}
* The expectation of $X$ can be computed as follows:
\begin{align}
E(X) = \int x \cdot f_X(x) \mathrm{d}x =& \int_{1}^{\infty} x \cdot \frac{3}{x^4} \mathrm{d}x \\
=& - \frac{3}{2} x^{-2} \rvert_{x=1}^{\infty} \\
=& -\frac{3}{2} \left( \lim_{t \rightarrow \infty} \frac{1}{t^2} - 1 \right) \\
=& \frac{3}{2}
\end{align}
* Note that the variance of $X$ can be expressed as $\text{Var}(X) = E(X^2) - E(X)^2$. Since $E(X)$ has been computed in the previous step, we seek $E(X^2)$:
\begin{align}
E(X^2)= \int x^2 \cdot f_X(x) \mathrm{d}x =& \int_{1}^{\infty} x^2 \cdot \frac{3}{x^4} \mathrm{d}x \\
=& -3 x^{-1} \rvert_{x=1}^{\infty} \\
=& -3 \left( \lim_{t \rightarrow \infty} \frac{1}{t} - 1 \right) \\
=& 3
\end{align}
So we have shown that the area under the curve equals one, that the expectation is $E(X)=\frac{3}{2}$ and we found the variance to be $\text{Var}(X) = \frac{3}{4}$. However, this was tedious and, as we shall see, an analytic approach is not applicable for some PDFs, e.g., if integrals have no closed form solutions.
Luckily, `r ttcode("R")` also enables us to easily find the results derived above. The tool we use for this is the function `r ttcode("integrate()")`. First, we have to define the functions we want to calculate integrals for as `r ttcode("R")` functions, i.e., the PDF $f_X(x)$ as well as the expressions $x\cdot f_X(x)$ and $x^2\cdot f_X(x)$.
```{r, echo = T, eval = T, message = F, warning = F}
# define functions
f <- function(x) 3 / x^4
g <- function(x) x * f(x)
h <- function(x) x^2 * f(x)
```
Next, we use `r ttcode("integrate()")` and set lower and upper limits of integration to $1$ and $\infty$ using arguments `r ttcode("lower")` and `r ttcode("upper")`. By default, `r ttcode("integrate()")` prints the result along with an estimate of the approximation error to the console. However, the outcome is not a numeric value one can readily do further calculation with. In order to get only a numeric value of the integral, we need to use the `r ttcode("$")` operator in conjunction with the `r ttcode("value")`. The `r ttcode("$")` operator is used to extract elements by name from an object of type `r ttcode("list")`.
```{r, echo = T, eval = T, message = F, warning = F}
# compute area under the density curve
area <- integrate(f,
lower = 1,
upper = Inf)$value
area
# compute E(X)
EX <- integrate(g,
lower = 1,
upper = Inf)$value
EX
# compute Var(X)
VarX <- integrate(h,
lower = 1,
upper = Inf)$value - EX^2
VarX
```
Although there is a wide variety of distributions, the ones most often
encountered in econometrics are the normal, chi-squared, Student $t$ and $F$
distributions. Therefore we will discuss some core `r ttcode("R")` functions that allow us to do
calculations involving densities, probabilities and quantiles of these
distributions.
Every probability distribution that `r ttcode("R")` handles has four basic functions whose names consist of a prefix followed by a root name. As an example, take the normal distribution. The root name of all four functions associated with the normal distribution is `r ttcode("norm")`. The four prefixes are
- `r ttcode("d")` for "density" - probability function / probability density function
- `r ttcode("p")` for "probability" - cumulative distribution function
- `r ttcode("q")` for "quantile" - quantile function (inverse cumulative distribution function)
- `r ttcode("r")` for "random" - random number generator
Thus, for the normal distribution we have the `r ttcode("R")` functions `r ttcode("dnorm()")`, `r ttcode("pnorm()")`, `r ttcode("qnorm()")` and `r ttcode("rnorm()")`.
### The Normal Distribution {-}
Perhaps the most important probability distribution considered here is the normal
distribution. This is not least due to the special role of the standard normal distribution and the Central Limit Theorem which is to be treated shortly. Normal distributions are symmetric and bell-shaped. A normal distribution is characterized by its mean $\mu$ and its standard deviation $\sigma$, concisely expressed by
$\mathcal{N}(\mu,\sigma^2)$. The normal distribution has the PDF
\begin{align}
f(x) = \frac{1}{\sqrt{2 \pi} \sigma} \exp{-(x - \mu)^2/(2 \sigma^2)}.
\end{align}
For the standard normal distribution we have $\mu=0$ and $\sigma=1$. Standard normal variates are often denoted by $Z$. Usually, the standard normal PDF is denoted by $\phi$ and the standard normal CDF is denoted by $\Phi$. Hence,
$$ \phi(c) = \Phi'(c) \ \ , \ \ \Phi(c) = P(Z \leq c) \ \ , \ \ Z \sim \mathcal{N}(0,1).$$ Note that the notation X $\sim$ Y reads as "X is distributed as Y". In `r ttcode("R")`, we can conveniently obtain densities of normal distributions using the function `r ttcode("dnorm()")`. Let us draw a plot of the standard normal density function using `r ttcode("curve()")` together with `r ttcode("dnorm()")`.
```{r, echo = T, eval = T, message = F, warning = F, fig.align='center'}
# draw a plot of the N(0,1) PDF
curve(dnorm(x),
xlim = c(-3.5, 3.5),
ylab = "Density",
main = "Standard Normal Density Function")
```
We can obtain the density at different positions by passing a vector to `r ttcode("dnorm()")`.
```{r, echo = T, eval = T, message = F, warning = F}
# compute density at x=-1.96, x=0 and x=1.96
dnorm(x = c(-1.96, 0, 1.96))
```
Similar to the PDF, we can plot the standard normal CDF using `r ttcode("curve()")`. We can use `r ttcode("dnorm()")` for this but it is much more convenient to rely on `r ttcode("pnorm()")`.
```{r, echo = T, eval = T, message = F, warning = F, fig.align='center'}
# plot the standard normal CDF
curve(pnorm(x),
xlim = c(-3.5, 3.5),
ylab = "Probability",
main = "Standard Normal Cumulative Distribution Function")
```
We can also use `r ttcode("R")` to calculate the probability of events associated with a standard normal variate.
Let us say we are interested in $P(Z \leq 1.337)$. For some continuous random variable $Z$ on $[-\infty,\infty]$ with density $g(x)$ we would have to determine $G(x)$, the anti-derivative of $g(x)$ so that
$$ P(Z \leq 1.337 ) = G(1.337) = \int_{-\infty}^{1.337} g(x) \mathrm{d}x. $$
If $Z \sim \mathcal{N}(0,1)$, we have $g(x)=\phi(x)$. There is no analytic solution to the integral above. Fortunately, `r ttcode("R")` offers good approximations. The first approach makes use of the function `r ttcode("integrate()")` which allows to solve one-dimensional integration problems using a numerical method. For this, we first define the function we want to compute the integral of as an `r ttcode("R")` function `r ttcode("f")`. In our example, `r ttcode("f")` is the standard normal density function and hence takes a single argument `r ttcode("x")`. Following the definition of $\phi(x)$ we define `r ttcode("f")` as
```{r, echo = T, eval = T, message = F, warning = F}
# define the standard normal PDF as an R function
f <- function(x) {
1/(sqrt(2 * pi)) * exp(-0.5 * x^2)
}
```
Let us check if this function computes standard normal densities by passing a vector.
```{r, echo = T, eval = T, message = F, warning = F}
# define a vector of reals
quants <- c(-1.96, 0, 1.96)
# compute densities
f(quants)
# compare to the results produced by 'dnorm()'
f(quants) == dnorm(quants)
```
The results produced by `r ttcode("f()")` are indeed equivalent to those given by `r ttcode("dnorm()")`.
Next, we call `r ttcode("integrate()")` on `r ttcode("f()")` and specify the arguments `r ttcode("lower")` and `r ttcode("upper")`, the lower and upper limits of integration.
```{r, echo = T, eval = T, message = F, warning = F}
# integrate f()
integrate(f,
lower = -Inf,
upper = 1.337)
```
We find that the probability of observing $Z \leq 1.337$ is about $90.94\%$.
A second and much more convenient way is to use the function `r ttcode("pnorm()")`, the standard normal cumulative distribution function.
```{r, echo = T, eval = T, message = F, warning = F}
# compute the probability using pnorm()
pnorm(1.337)
```
The result matches the outcome of the approach using `r ttcode("integrate()")`.
Let us discuss some further examples:
A commonly known result is that $95\%$ probability mass of a standard normal distribution lies in the interval $[-1.96, 1.96]$, that is, in a distance of about $2$ standard deviations to the mean. We can easily confirm this by calculating $$ P(-1.96 \leq Z \leq 1.96) = 1-2\times P(Z \leq -1.96), $$ due to symmetry of the standard normal PDF. Thanks to `r ttcode("R")`, we can abandon the table of the standard normal CDF found in many other textbooks and instead solve this fast by using `r ttcode("pnorm()")`.
```{r, echo = T, eval = T, message = F, warning = F}
# compute the probability
1 - 2 * (pnorm(-1.96))
```
To make statements about the probability of observing outcomes of $Y$ in some specific range it is more convenient when we standardize first as shown in Key Concept 2.4.
```{r, eval = my_output == "html", results='asis', echo=FALSE, purl=FALSE}
cat('<div class = "keyconcept" id="KC2.4">
<h3 class = "right"> Key Concept 2.4 </h3>
<h3 class = "left"> Computing Probabilities Involving Normal Random Variables </h3>
<p>
Suppose $Y$ is normally distributed with mean $\\mu$ and variance $\\sigma^2$: $$Y
\\sim \\mathcal{N}(\\mu, \\sigma^2)$$ Then $Y$ is standardized by subtracting it from its mean and then dividing it by its standard deviation: $$ Z = \\frac{Y -\\mu}{\\sigma}. $$ Let $c_1$
and $c_2$ denote two numbers whereby $c_1 < c_2$ and further $d_1 = (c_1 - \\mu)
/ \\sigma$ and $d_2 = (c_2 - \\mu)/\\sigma$. Then
\\begin{align*}
P(Y \\leq c_2) =& \\, P(Z \\leq d_2) = \\Phi(d_2), \\\\
P(Y \\geq c_1) =& \\, P(Z \\geq d_1) = 1 - \\Phi(d_1), \\\\
P(c_1 \\leq Y \\leq c_2) =& \\, P(d_1 \\leq Z \\leq d_2) = \\Phi(d_2) - \\Phi(d_1).
\\end{align*}
</p>
</div>')
```
```{r, eval = my_output == "latex", results='asis', echo=FALSE, purl=FALSE}
cat('\\begin{keyconcepts}[Computing Probabilities Involving Normal Random Variables]{2.4}
Suppose $Y$ is normally distributed with mean $\\mu$ and variance $\\sigma^2$: $$Y
\\sim \\mathcal{N}(\\mu, \\sigma^2)$$ Then $Y$ is standardized by subtracting it from its mean and then dividing it by its standard deviation: $$ Z = \\frac{Y -\\mu}{\\sigma}. $$ Let $c_1$
and $c_2$ denote two numbers whereby $c_1 < c_2$ and further $d_1 = (c_1 - \\mu)
/ \\sigma$ and $d_2 = (c_2 - \\mu)/\\sigma$. Then
\\begin{align*}
P(Y \\leq c_2) =& \\, P(Z \\leq d_2) = \\Phi(d_2), \\\\
P(Y \\geq c_1) =& \\, P(Z \\geq d_1) = 1 - \\Phi(d_1), \\\\
P(c_1 \\leq Y \\leq c_2) =& \\, P(d_1 \\leq Z \\leq d_2) = \\Phi(d_2) - \\Phi(d_1).
\\end{align*}
\\end{keyconcepts}')
```
Now consider a random variable $Y$ with $Y \sim \mathcal{N}(5, 25)$. `r ttcode("R")` functions that handle the normal distribution can perform the standardization. If we are interested in $P(3 \leq Y \leq 4)$ we can use `r ttcode("pnorm()")` and adjust for a mean and/or a standard deviation that deviate from $\mu=0$ and $\sigma = 1$ by specifying the arguments `r ttcode("mean")` and `r ttcode("sd")` accordingly. **Attention**: the argument `r ttcode("sd")` requires the standard deviation, not the variance!
```{r, echo = T, eval = T, message = F, warning = F}
pnorm(4, mean = 5, sd = 5) - pnorm(3, mean = 5, sd = 5)
```
An extension of the normal distribution in a univariate setting is the multivariate normal distribution. The joint PDF of two random normal variables $X$ and $Y$ is given by
\begin{align}
\begin{split}
g_{X,Y}(x,y) =& \, \frac{1}{2\pi\sigma_X\sigma_Y\sqrt{1-\rho_{XY}^2}} \\
\cdot & \, \exp \left\{ \frac{1}{-2(1-\rho_{XY}^2)} \left[ \left( \frac{x-\mu_x}{\sigma_X} \right)^2 - 2\rho_{XY}\left( \frac{x-\mu_X}{\sigma_X} \right)\left( \frac{y-\mu_Y}{\sigma_Y} \right) + \left( \frac{y-\mu_Y}{\sigma_Y} \right)^2 \right] \right\}.
\end{split} (\#eq:bivnorm)
\end{align}
Equation \@ref(eq:bivnorm) contains the bivariate normal PDF. It is somewhat hard to gain insights from this complicated expression. Instead, let us consider the special case where $X$ and $Y$ are uncorrelated standard normal random variables with densities $f_X(x)$ and $f_Y(y)$ with joint normal distribution. We then have the parameters $\sigma_X = \sigma_Y = 1$, $\mu_X=\mu_Y=0$ (due to marginal standard normality) and $\rho_{XY}=0$ (due to independence). The joint density of $X$ and $Y$ then becomes
$$ g_{X,Y}(x,y) = f_X(x) f_Y(y) = \frac{1}{2\pi} \cdot \exp \left\{ -\frac{1}{2} \left[x^2 + y^2 \right] \right\}, \tag{2.2} $$
the PDF of the bivariate standard normal distribution. The widget below provides an interactive three-dimensional plot of (<a href="#mjx-eqn-2.2">2.2</a>).
```{r, echo=F, purl=FALSE}
library("knitr")
library("devtools")
url<-"https://plot.ly/~mca_unidue/22.embed?width=550&height=550?showlink=false"
plotly_iframe <- paste("<center><iframe scrolling='no' seamless='seamless' style='border:none' src='", url,
"/800/1200' width='600' height='400'></iframe></center>", sep = "")
```
`r I(plotly_iframe)`
```{r, echo=FALSE, results='asis', purl=FALSE}
if (my_output=="latex"){
cat("\\begin{center}\\textit{This interactive part of the book is only available in the HTML version.}\\end{center}")
}
```
By moving the cursor over the plot you can see that the density is rotationally invariant, i.e., the density at $(a, b)$ solely depends on the distance of $(a, b)$ to the origin: geometrically, regions of equal density are edges of concentric circles in the XY-plane, centered at $(\mu_X = 0, \mu_Y = 0)$.
The normal distribution has some remarkable characteristics. For example, for two jointly normally distributed variables $X$ and $Y$, the conditional expectation function is linear: one can show that $$ E(Y\vert X) = E(Y) + \rho \frac{\sigma_Y}{\sigma_X} (X - E(X)). $$ The interactive widget below shows standard bivariate normally distributed sample data along with the conditional expectation function $E(Y\vert X)$ and the marginal densities of $X$ and $Y$. All elements adjust accordingly as you vary the parameters.
```{r, echo=FALSE, results='asis', purl=FALSE}
if (my_output=="html"){
cat('
<center>
<iframe height="880" width="770" frameborder="0" scrolling="no" src="bivariatenormalv4.html"></iframe>
</center>
')
} else {
cat("\\begin{center}\\textit{This interactive part of the book is only available in the HTML version.}\\end{center}")
}
```
<a name="chisquare"></a>
### The Chi-Squared Distribution {-}
The chi-squared distribution is another distribution relevant in econometrics. It is often needed when testing special types of hypotheses frequently encountered when dealing with regression models.
The sum of $M$ squared independent standard normal distributed random variables follows a chi-squared distribution with $M$ degrees of freedom:
\begin{align*}
Z_1^2 + \dots + Z_M^2 = \sum_{m=1}^M Z_m^2 \sim \chi^2_M \ \ \text{with} \ \ Z_m \overset{i.i.d.}{\sim} \mathcal{N}(0,1). (\#eq:chisq)
\end{align*}
A $\chi^2$ distributed random variable with $M$ degrees of freedom has expectation $M$, mode at $M-2$ for $M \geq 2$ and variance $2 \cdot M$. For example, for
$$ Z_1,Z_2,Z_3 \overset{i.i.d.}{\sim} \mathcal{N}(0,1), $$
it holds that
$$ Z_1^2+Z_2^2+Z_3^2 \sim \chi^2_3. \tag{2.3} $$
Using the code below, we can display the PDF and the CDF of a $\chi^2_3$ random variable in a single plot. This is achieved by setting the argument `r ttcode("add = TRUE")` in the second call of `r ttcode("curve()")`. Further we adjust limits of both axes using `r ttcode("xlim")` and `r ttcode("ylim")` and choose different colors to make both functions better distinguishable. The plot is completed by adding a legend with help of `r ttcode("legend()")`.
```{r, echo = T, eval = T, message = F, warning = F, fig.align='center'}
# plot the PDF
curve(dchisq(x, df = 3),
xlim = c(0, 10),
ylim = c(0, 1),
col = "blue",
ylab = "",
main = "p.d.f. and c.d.f of Chi-Squared Distribution, M = 3")
# add the CDF to the plot
curve(pchisq(x, df = 3),
xlim = c(0, 10),
add = TRUE,
col = "red")
# add a legend to the plot
legend("topleft",
c("PDF", "CDF"),
col = c("blue", "red"),
lty = c(1, 1))
```
Since the outcomes of a $\chi^2_M$ distributed random variable are always positive, the support of the related PDF and CDF is $\mathbb{R}_{\geq0}$.
As expectation and variance depend (solely!) on the degrees of freedom, the distribution's shape changes drastically if we vary the number of squared standard normals that are summed up. This relation is often depicted by overlaying densities for different $M$, see the <a href="https://en.wikipedia.org/wiki/Chi-squared_distribution">Wikipedia Article</a>.
We reproduce this here by plotting the density of the $\chi_1^2$ distribution on the interval $[0,15]$ with `r ttcode("curve()")`. In the next step, we loop over degrees of freedom $M=2,...,7$ and add a density curve for each $M$ to the plot. We also adjust the line color for each iteration of the loop by setting `r ttcode("col = M")`. At last, we add a legend that displays degrees of freedom and the associated colors.
```{r, echo = T, eval = T, message = F, warning = F, fig.align='center'}
# plot the density for M=1
curve(dchisq(x, df = 1),
xlim = c(0, 15),
xlab = "x",
ylab = "Density",
main = "Chi-Square Distributed Random Variables")
# add densities for M=2,...,7 to the plot using a 'for()' loop
for (M in 2:7) {
curve(dchisq(x, df = M),
xlim = c(0, 15),
add = T,
col = M)
}
# add a legend
legend("topright",
as.character(1:7),
col = 1:7 ,
lty = 1,
title = "D.F.")
```
Increasing the degrees of freedom shifts the distribution to the right (the mode becomes larger) and increases the dispersion (the distribution's variance grows).
### The Student t Distribution {-#thetdist}
<a name="tdist"></a>
Let $Z$ be a standard normal variate, $W$ a $\chi^2_M$ random variable and further assume that $Z$ and $W$ are independent. Then it holds that
$$ \frac{Z}{\sqrt{W/M}} =:X \sim t_M $$
and $X$ follows a *Student $t$ distribution* (or simply $t$ distribution) with $M$ degrees of freedom.
Similar to the $\chi^2_M$ distribution, the shape of a $t_M$ distribution depends on $M$. $t$ distributions are symmetric, bell-shaped and look similar to a normal distribution, especially when $M$ is large. This is not a coincidence: for a sufficiently large $M$, the $t_M$ distribution can be approximated by the standard normal distribution. This approximation works reasonably well for $M\geq 30$. As we will illustrate later by means of a small simulation study, the $t_{\infty}$ distribution *is* the standard normal distribution.
A $t_M$ distributed random variable $X$ has an expectation if $M>1$ and it has a variance if $M>2$.
\begin{align}
E(X) =& 0, \ M>1 \\
\text{Var}(X) =& \frac{M}{M-2}, \ M>2
\end{align}
Let us plot some $t$ distributions with different $M$ and compare them to the standard normal distribution.
```{r, echo = T, eval = T, message = F, warning = F, fig.align='center'}
# plot the standard normal density
curve(dnorm(x),
xlim = c(-4, 4),
xlab = "x",
lty = 2,
ylab = "Density",
main = "Densities of t Distributions")
# plot the t density for M=2
curve(dt(x, df = 2),
xlim = c(-4, 4),
col = 2,
add = T)
# plot the t density for M=4
curve(dt(x, df = 4),
xlim = c(-4, 4),
col = 3,
add = T)
# plot the t density for M=25
curve(dt(x, df = 25),
xlim = c(-4, 4),
col = 4,
add = T)
# add a legend
legend("topright",
c("N(0, 1)", "M=2", "M=4", "M=25"),
col = 1:4,
lty = c(2, 1, 1, 1))
```
The plot illustrates what has been said in the previous paragraph: as the degrees of freedom increase, the shape of the $t$ distribution comes closer to that of a standard normal bell curve. Already for $M=25$, we find little difference to the standard normal density. If $M$ is small, we find the distribution to have heavier tails than a standard normal, i.e., it has a "fatter" bell shape.
### The F Distribution {-}
Another ratio of random variables important to econometricians is the ratio of two independent $\chi^2$ distributed random variables that are divided by their degrees of freedom $M$ and $n$. The quantity
$$ \frac{W/M}{V/n} \sim F_{M,n} \ \ \text{with} \ \ W \sim \chi^2_M \ \ , \ \ V \sim \chi^2_n , $$
follows an $F$ distribution with numerator degrees of freedom $M$ and denominator degrees of freedom $n$, denoted $F_{M,n}$. The distribution was first derived by George Snedecor but was named in honor of [Sir Ronald Fisher](https://en.wikipedia.org/wiki/Ronald_Fisher).
By definition, the support of both PDF and CDF of an $F_{M,n}$ distributed random variable is $\mathbb{R}_{\geq0}$.
Say we have an $F$ distributed random variable $Y$ with numerator degrees of freedom $3$ and denominator degrees of freedom $14$ and are interested in $P(Y \geq 2)$. This can be computed with the help of the function `r ttcode("pf()")`. By setting the argument `r ttcode("lower.tail")` to `r ttcode("FALSE")` we ensure that `r ttcode("R")` computes $1- P(Y \leq 2)$, i.e, the probability mass in the tail right of $2$.
```{r, echo = T, eval = T, message = F, warning = F}
pf(2, df1 = 3, df2 = 14, lower.tail = F)
```
We can visualize this probability by drawing a line plot of the related density and adding a color shading with `r ttcode("polygon()")`.
```{r, echo = T, eval = T, message = F, warning = F, fig.align='center'}
# define coordinate vectors for vertices of the polygon
x <- c(2, seq(2, 10, 0.01), 10)
y <- c(0, df(seq(2, 10, 0.01), 3, 14), 0)
# draw density of F_{3, 14}
curve(df(x ,3 ,14),
ylim = c(0, 0.8),
xlim = c(0, 10),
ylab = "Density",
main = "Density Function")
# draw the polygon
polygon(x, y, col = "orange")
```
The $F$ distribution is related to many other distributions. An important special case encountered in econometrics arises if the denominator degrees of freedom are large such that the $F_{M,n}$ distribution can be approximated by the $F_{M,\infty}$ distribution which turns out to be simply the distribution of a $\chi^2_M$ random variable divided by its degrees of freedom $M$,
$$ W/M \sim F_{M,\infty} \ \ , \ \ W \sim \chi^2_M. $$
```{r, echo=FALSE, results='asis', purl=FALSE}
if (my_output=="html"){
cat('
<iframe src="DCL/playground.html" frameborder="0" scrolling="no" style="width:100%;height:340px"></iframe>
')
}
```
## Random Sampling and the Distribution of Sample Averages {#RSATDOSA}
To clarify the basic idea of random sampling, let us jump back to the dice rolling example:
Suppose we are rolling the dice $n$ times. This means we are interested in the outcomes of random $Y_i, \ i=1,...,n$ which are characterized by the same distribution. Since these outcomes are selected randomly, they are *random variables* themselves and their realizations will differ each time we draw a sample, i.e., each time we roll the dice $n$ times. Furthermore, each observation is randomly drawn from the same population, that is, the numbers from $1$ to $6$, and their individual distribution is the same. Hence $Y_1,\dots,Y_n$ are identically distributed.
Moreover, we know that the value of any of the $Y_i$ does not provide any information on the remainder of the outcomes. In our example, rolling a six as the first observation in our sample does not alter the distributions of $Y_2,\dots,Y_n$: all numbers are equally likely to occur. This means that all $Y_i$ are also independently distributed. Thus $Y_1,\dots,Y_n$ are independently and identically distributed (*i.i.d.*).
The dice example uses this most simple sampling scheme. That is why it is called *simple random sampling*. This concept is summarized in Key Concept 2.5.
```{r, eval = my_output == "html", results='asis', echo=FALSE, purl=FALSE}
cat('
<div class = "keyconcept" id="KC2.5">
<h3 class = "right"> Key Concept 2.5 </h3>
<h3 class = "left"> Simple Random Sampling and i.i.d. Random Variables </h3>
<p>
In simple random sampling, $n$ objects are drawn at random from a population. Each object is equally likely to end up in the sample. We denote the value of the random variable $Y$ for the $i^{th}$ randomly drawn object as $Y_i$. Since all objects are equally likely to be drawn and the distribution of $Y_i$ is the same for all $i$, the $Y_i, \\dots, Y_n$ are independently and identically distributed (i.i.d.). This means the distribution of $Y_i$ is the same for all $i=1,\\dots,n$ and $Y_1$ is distributed independently of $Y_2, \\dots, Y_n$ and $Y_2$ is distributed independently of $Y_1, Y_3, \\dots, Y_n$ and so forth.
</p>
</div>')
```
```{r, eval = my_output == "latex", results='asis', echo=FALSE, purl=FALSE}
cat('\\begin{keyconcepts}[Simple Random Sampling and i.i.d. Random Variables]{2.5}
In simple random sampling, $n$ objects are drawn at random from a population. Each object is equally likely to end up in the sample. We denote the value of the random variable $Y$ for the $i^{th}$ randomly drawn object as $Y_i$. Since all objects are equally likely to be drawn and the distribution of $Y_i$ is the same for all $i$, the $Y_i, \\dots, Y_n$ are independently and identically distributed (i.i.d.). This means the distribution of $Y_i$ is the same for all $i=1,\\dots,n$ and $Y_1$ is distributed independently of $Y_2, \\dots, Y_n$ and $Y_2$ is distributed independently of $Y_1, Y_3, \\dots, Y_n$ and so forth.
\\end{keyconcepts}')
```
What happens if we consider functions of the sample data? Consider the example of rolling a dice two times in a row once again. A sample now consists of two independent random draws from the set $\{1,2,3,4,5,6\}$. It is apparent that any function of these two random variables, e.g. their sum, is also random. Convince yourself by executing the code below several times.
```{r, echo = T, eval = T, message = F, warning = F}
sum(sample(1:6, 2, replace = T))
```
Clearly, this sum, let us call it $S$, is a random variable as it depends on randomly drawn summands. For this example, we can completely enumerate all outcomes and hence write down the theoretical probability distribution of our function of the sample data $S$:
We face $6^2=36$ possible pairs. Those pairs are
\begin{align*}
&(1,1) (1,2) (1,3) (1,4) (1,5) (1,6) \\
&(2,1) (2,2) (2,3) (2,4) (2,5) (2,6) \\
&(3,1) (3,2) (3,3) (3,4) (3,5) (3,6) \\
&(4,1) (4,2) (4,3) (4,4) (4,5) (4,6) \\
&(5,1) (5,2) (5,3) (5,4) (5,5) (5,6) \\
&(6,1) (6,2) (6,3) (6,4) (6,5) (6,6)
\end{align*}
Thus, possible outcomes for $S$ are
$$ \left\{ 2,3,4,5,6,7,8,9,10,11,12 \right\} . $$
Enumeration of outcomes yields
\begin{align}
P(S) =
\begin{cases}
1/36, \ & S = 2 \\
2/36, \ & S = 3 \\
3/36, \ & S = 4 \\
4/36, \ & S = 5 \\
5/36, \ & S = 6 \\
6/36, \ & S = 7 \\
5/36, \ & S = 8 \\
4/36, \ & S = 9 \\
3/36, \ & S = 10 \\
2/36, \ & S = 11 \\
1/36, \ & S = 12
\end{cases}
\end{align}
We can also compute $E(S)$ and $\text{Var}(S)$ as stated in Key Concept 2.1 and Key Concept 2.2.
```{r, echo = T, eval = T, message = F, warning = F}
# Vector of outcomes
S <- 2:12
# Vector of probabilities
PS <- c(1:6, 5:1) / 36
# Expectation of S
ES <- sum(S * PS)
ES
# Variance of S
VarS <- sum((S - c(ES))^2 * PS)
VarS
```
So the distribution of $S$ is known. It is also evident that its distribution differs considerably from the marginal distribution, i.e, the distribution of a single dice roll's outcome, $D$ . Let us visualize this using bar plots.
```{r, echo = T, eval = T, message = F, warning = F, fig.align='center'}
# divide the plotting area into one row with two columns
par(mfrow = c(1, 2),cex.main=1)
# plot the distribution of S
barplot(PS,
ylim = c(0, 0.2),
xlab = "S",
ylab = "Probability",
col = "steelblue",
space = 0,
main = "Sum of Two Dice Rolls")
# plot the distribution of D
probability <- rep(1/6, 6)
names(probability) <- 1:6
barplot(probability,
ylim = c(0, 0.2),
xlab = "D",
col = "steelblue",
space = 0,
main = "Outcome of a Single Dice Roll")
```
Many econometric procedures deal with averages of sampled data. It is typically assumed that observations are drawn randomly from a larger, unknown population. As demonstrated for the sample function $S$, computing an average of a random sample has the effect that the average is a random variable itself. This random variable in turn has a probability distribution, called the sampling distribution. Knowledge about the sampling distribution of the average is therefore crucial for understanding the performance of econometric procedures.
The *sample average* of a sample of $n$ observations $Y_1, \dots, Y_n$ is
$$ \overline{Y} = \frac{1}{n} \sum_{i=1}^n Y_i = \frac{1}{n} (Y_1 + Y_2 + \cdots + Y_n). $$
$\overline{Y}$ is also called the sample mean.
### Mean and Variance of the Sample Mean {-}
Suppose that $Y_1,\dots,Y_n$ are i.i.d. and denote $\mu_Y$ and $\sigma_Y^2$ as the mean and the variance of the $Y_i$. Then we have that
$$ E(\overline{Y}) = E\left(\frac{1}{n} \sum_{i=1}^n Y_i \right) = \frac{1}{n} E\left(\sum_{i=1}^n Y_i\right) = \frac{1}{n} \sum_{i=1}^n E\left(Y_i\right) = \frac{1}{n} \cdot n \cdot \mu_Y = \mu_Y $$
and
\begin{align*}
\text{Var}(\overline{Y}) =& \text{Var}\left(\frac{1}{n} \sum_{i=1}^n Y_i \right) \\
=& \frac{1}{n^2} \sum_{i=1}^n \text{Var}(Y_i) + \frac{1}{n^2} \sum_{i=1}^n \sum_{j=1, j\neq i}^n \text{cov}(Y_i,Y_j) \\
=& \frac{\sigma^2_Y}{n} \\
=& \sigma_{\overline{Y}}^2.
\end{align*}
The second summand vanishes since $\text{cov}(Y_i,Y_j)=0$ for $i\neq j$ due to independence. Consequently, the standard deviation of the sample mean is given by $$\sigma_{\overline{Y}} = \frac{\sigma_Y}{\sqrt{n}}.$$
It is worthwhile to mention that these results hold irrespective of the underlying distribution of the $Y_i$.
#### The Sampling Distribution of $\overline{Y}$ when $Y$ Is Normally Distributed {-}
If $Y_1,\dots,Y_n$ are i.i.d. draws from a normal distribution with mean $\mu_Y$ and variance $\sigma_Y^2$, the following holds for their sample average $\overline{Y}$:
$$ \overline{Y} \sim \mathcal{N}(\mu_Y, \sigma_Y^2/n) \tag{2.4} $$
For example, if a sample $Y_i$ with $i=1,\dots,10$ is drawn from a standard normal distribution with mean $\mu_Y = 0$ and variance $\sigma_Y^2=1$ we have
$$ \overline{Y} \sim \mathcal{N}(0,0.1).$$
We can use `r ttcode("R")`'s random number generation facilities to verify this result. The basic idea is to simulate outcomes of the true distribution of $\overline{Y}$ by repeatedly drawing random samples of 10 observations from the $\mathcal{N}(0,1)$ distribution and computing their respective averages. If we do this for a large number of repetitions, the simulated data set of averages should quite accurately reflect the theoretical distribution of $\overline{Y}$ if the theoretical result holds.
The approach sketched above is an example of what is commonly known as *Monte Carlo Simulation* or *Monte Carlo Experiment*. To perform this simulation in `r ttcode("R")`, we proceed as follows:
1. Choose a sample size `r ttcode("n")` and the number of samples to be drawn, `r ttcode("reps")`.
2. Use the function `r ttcode("replicate()")` in conjunction with `r ttcode("rnorm()")` to draw `r ttcode("n")` observations from the standard normal distribution `r ttcode("rep")` times.
**Note**: the outcome of `r ttcode("replicate()")` is a matrix with dimensions `r ttcode("n")` $\times$ `r ttcode("rep")`. It contains the drawn samples as *columns*.
3. Compute sample means using `r ttcode("colMeans()")`. This function computes the mean of each column, i.e., of each sample and returns a vector.
```{r, echo = T, eval = T, message = F, warning = F}
# set sample size and number of samples
n <- 10
reps <- 10000
# perform random sampling
samples <- replicate(reps, rnorm(n)) # 10 x 10000 sample matrix
# compute sample means
sample.avgs <- colMeans(samples)
```
We then end up with a vector of sample averages. You can check the vector property of `r ttcode("sample.avgs")`:
```{r, echo = T, eval = T, message = F, warning = F}
# check that 'sample.avgs' is a vector
is.vector(sample.avgs)
# print the first few entries to the console
head(sample.avgs)
```
A straightforward approach to examine the distribution of univariate numerical data is to plot it as a histogram and compare it to some known or assumed distribution. By default, `r ttcode("hist()")` will give us a frequency histogram, i.e., a bar chart where observations are grouped into ranges, also called bins. The ordinate reports the number of observations falling into each of the bins. Instead, we want it to report density estimates for comparison purposes. This is achieved by setting the argument `r ttcode("freq = FALSE")`. The number of bins is adjusted by the argument `r ttcode("breaks")`.
Using `r ttcode("curve()")`, we overlay the histogram with a red line, the theoretical density of a $\mathcal{N}(0, 0.1)$ random variable. Remember to use the argument `r ttcode("add = TRUE")` to add the curve to the current plot. Otherwise `r ttcode("R")` will open a new graphic device and discard the previous plot!^[*Hint:* `r ttcode("T")` and `r ttcode("F")` are alternatives for `r ttcode("TRUE")` and `r ttcode("FALSE")`.]
```{r, echo = T, eval = T, message = F, warning = F, fig.align='center'}
# Plot the density histogram
hist(sample.avgs,
ylim = c(0, 1.4),
col = "steelblue" ,
freq = F,