-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHW02Key.Rmd
242 lines (136 loc) · 8.03 KB
/
HW02Key.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
---
title: "Homework02"
author: "Key"
date: "10/9/2018"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Homework 02
### Soccer teams [10]
1. How many different soccer teams with 11 players can be made from a team of 15, assuming that all players can play any position?
This is a case for combinations without replacement because the order does not matter and one player cannot appear twice in the team.
```{r, message= FALSE}
library(arrangements)
ncombinations(k = 11, n = 15, replace = FALSE)
```
$$C_{11}^{15} = `r ncombinations(k = 11, n = 15)`$$
library(arrangements)
ncombinations()
2. How many teams can be made if there are only 3 players that can be goalies?
We need to multiply the number of goalies times the number of teams of 10 that can be made with the other 14 players.
```{r}
ncombinations(k = 10, n = 14) * ncombinations(k = 1, n = 3)
```
$$C_{10}^{14} \times C_{3}^{1} = `r ncombinations(k = 10, n = 14) * ncombinations(k = 1, n = 3)`$$
### Four aces [5]
What is the probability of getting 4 aces in a hand of poker with 5 cards out of a deck of 52?
The total number of hands is $$C_{52}^{5} = `r ncombinations(k = 5, n = 52)`$$.
The total number of hands with 4 aces is $$C_{4}^{4} \times C_{48}^{1} = `r ncombinations(k = 1, n = 48)`$$.
```{r}
(P4Aces <- ncombinations(k = 1, n = 48) * ncombinations(k = 4, n = 4) / ncombinations(k = 5, n = 52))
```
### Coin flips [25]
The R package `prob` has a function `tosscoin` that allows us to simulate coin tosses. By specifying the argument `makespace = TRUE` we get all the set of possible outcomes (sample space) and their probabilities using the principle of symmetry. All outcomes have equal probability if the coin has equal probability 0.50 of heads or tails.
We show an example where we toss 4 coins and then you are asked to repeat the calculations for 5 coins. The argument `times` in the `tosscoin` function refers to how many coins are tossed each time. Each set of 4 tosses is an ordered set with 4 elements selected with replacement from a set of two elements, H and T.
Because H and T can be "reused" the selection is with replacement, and order matters. The nature and number of the outcomes can be listed with the `tosscoin` function.
<br>
```{r HwCoinToss1, message = FALSE}
library(prob)
(SampleSpace <- tosscoin(times = 4, makespace = TRUE))
# Number of rows is number of outcomes.
nrow(SampleSpace)
# Number of H's in each outcome
SampleSpace$nH <- as.numeric(SampleSpace$toss1 == "H") +
as.numeric(SampleSpace$toss2 == "H") +
as.numeric(SampleSpace$toss3 == "H") +
as.numeric(SampleSpace$toss4 == "H")
# Define event NoHeads, OneHead, TwoHeads, etc.
NoHeads <- subset(SampleSpace, nH == 0)
OneHead <- subset(SampleSpace, nH == 1)
TwoHeads <- subset(SampleSpace, nH == 2)
ThreeHeads <- subset(SampleSpace, nH == 3)
# Calculate probabilities
Prob(NoHeads) # simply the number of rows without Head over the total 1/16
Prob(OneHead)
Prob(TwoHeads)
# Make frequency table for all events
nHfreq <- as.data.frame(table(SampleSpace$nH))
# Rename columns
names(nHfreq) <- c("nHeads", "AbsFreq")
nHfreq$RelFreq <- nHfreq$AbsFreq / sum(nHfreq$AbsFreq)
pander::pander(nHfreq)
```
<br>
Five coins labeled A-E are flipped together and the results are listed in order A-E. For example A=H, B=H, C=T, D=H, E=H, would be listed as HHTHH.
Follow the code in the example above to answer the following questions. You may want to copuy parts of the code, paste it below and modify it to adjust for the number of coins tossed.
1. What Combinatorics case is this? How many different outcomes are there for tossing 5 coins?
This is a case of permutations with replacement because the order matters, and sampling is with replacement.
Number of permutations is 2^5^ = 32
Also can be calculated as:
nrow(tosscoin(times = 5, makespace = TRUE)) = `r nrow(tosscoin(times = 5, makespace = TRUE))`
2. List all possible outcomes of the random experiment.
```{r}
(SampleSpace <- tosscoin(times = 5, makespace = TRUE))
```
3. For each outcome, calculate the number of H's in the sequence and add to the table.
```{r}
# Number of H's in each outcome
SampleSpace$nH <- as.numeric(SampleSpace$toss1 == "H") +
as.numeric(SampleSpace$toss2 == "H") +
as.numeric(SampleSpace$toss3 == "H") +
as.numeric(SampleSpace$toss4 == "H") +
as.numeric(SampleSpace$toss5 == "H")
```
4. Make a frequency table that shows the number of H's and the absolute and relative frequency for each of all possible outcomes.
```{r}
(nHfreq <- as.data.frame(table(SampleSpace$nH)))
# Rename columns
names(nHfreq) <- c("nHeads", "AbsFreq")
nHfreq$RelFreq <- nHfreq$AbsFreq / sum(nHfreq$AbsFreq)
pander::pander(nHfreq)
```
5. How many different ways can you get exactly 3 H's in 5 tosses? Answer both using the frequency table and the correct Combinatorics formula. Consider that there are 5 "slots" or positions where the 3 heads can occur. Think that you are choosing three slots out of 5, and that once a slot is taken, it's taken.
$C_3^5 = \frac{5!}{(5 - 3)! 3!} = \frac{5 \times 4 \times 3}{3 \times 2 \times 1} = 10$
```{r}
ncombinations(k = 3, n = 5)
```
### Marginal and Joint probabilities [24]
A large number of zebu (*Bos indicus*) and taurine (*Bos taurus*) cattle were tested under high temperature conditions. Their body temperature was classified as Normal, Moderately high and Very high. The following table represents the probabilities of each species and condition for the population.
<br>
| Body Temp. | B. indicus | B. taurus |
|-----------:|:----------:|:---------:|
| Normal | 0.30 | 0.20 |
| Moderate | 0.15 | ____ |
| Very High | 0.05 | 0.10 |
<br>
Answer the following questions using R as a calculator or type in the calculations and the result you geyt using a regular calculator.
1. Calculate the missing probability
$P(\text{B. taurus & Moderate}) = 1 - 0.30 - 0.20 - 0.15 - 0.05 - 0.10 = 0.20$
2. Calculate the marginal probabilities for species and temperature.
$P(\text{B. indicus}) = 0.50 \qquad P(\text{B. taurus}) = 0.50$
$P(\text{Normal}) = 0.50 \qquad P(\text{Moderate}) = 0.35 \qquad P(\text{Very High}) = 0.15$
3. What is the probability that a randomly chosen animal has very high temperature?
0.15
4. Are body temperature and species independent? Why? Show calculation.
Body temperature and species are not independent because the joint probabilities are not equal to the product of the marginal probabilities.
$P(\text{B. indicus & Very High}) = 0.05 \ne P(\text{B. indicus}) \ P(\text{Very High}) = 0.50 * 0.15$
### Conditional Probability and Bayes' rule [15]
Use the table for cattle temperature above.
1. What is the probability that a randomly selected animal from that population is *B. indicus*?
$P(\text{B. indicus}) = 0.50$
2. What is the probability that a randomly selected animal from that population who has very high temperature is *B. indicus*?
$P(\text{B. indicus|Very High}) = 0.05/0.15 = 1/3$
3. Assuming that the propensity to have elevated temperatures under stress remains the same, but that we now have a population with 80% *B.indicus*, what is the probability that a randomly selected animal from that population who has very high temperature is *B. indicus*? Hint: P(B. indicus = 0.80, but the conditional probabilities remain the same).
New prior: $P(\text{B. indicus}) = 0.80$
Bayes' rule:
$$P(\text{B. indicus|Very High}) = \frac{P(\text{B. indicus}) \times P(\text{Very High|B. indicus})}{ P(\text{Very High})} \\[30pt]
= \frac{P(\text{B. indicus}) \times P(\text{Very High|B. indicus})}{P(\text{Very High|B. indicus}) \times P(\text{B. indicus}) + P(\text{Very High|B. taurus}) \times P(\text{B. taurus})} \\[30pt]
= \frac{0.80 \times (0.05/0.50)}{(0.05/0.50) \times 0.80 + (0.10/0.50) \times 0.20} = 2/3$$
### Answer 14 questions in Top Hat [14]
Go to the Top hat site and answer the 14 questions assigned as part of Homework02. Each question in Top Hat is worth 1 point.
### Knit this file [7]
Knit the file and submit both the .Rmd and the .html files created.