-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHW4_Li.Rmd
267 lines (217 loc) · 8.72 KB
/
HW4_Li.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
---
title: "HW4_LI"
author: "Senhao Li"
date: "March 20, 2018"
output:
word_document: default
---
# Part 1) Binomial distribution
#Suppose a pitcher in Baseball has 50% chance of getting a strike-out.Using the binomial distribution,
## a) Compute and plot the probability distribution for striking out the next 6 batters.
```{r}
striking_a <- dbinom(0:6,size=6,prob=0.5)
striking_a
#plot the PMF of striking out the 6 batters
plot(0:6, striking_a, type="h",xaxt = "n",
xlab="Times of striking out",ylab="PMF of striking out", ylim = c(0, 0.4),main="Probability distribution of striking out (p=50%)")
abline(h=0)
points(0:6, striking_a, pch=19)
axis(side = 1, at = 0:6, labels = TRUE)
```
## b) Plot the CDF for the above
```{r}
CDF <- pbinom(0:6, size = 6, prob = 0.5) #Calculate the CDF of striking out for 6 batters
plot(0:6, CDF, type="h",xaxt = "n",
xlab="Times of striking out",ylab="CDF", ylim = c(0, 1),main="Cumulative distribution of striking out (p=50%)")
abline(h=0)
points(0:6, CDF, pch=19)
axis(side = 1, at = 0:6, labels = TRUE)
```
## c) Repeat a) and b) if the pitcher has 70% chance of getting a strike-out.
```{r}
striking_c <- dbinom(0:6,size=6,prob=0.7)
striking_c
#plot the PMF of striking out the 6 batters if prob=0.7
plot(0:6, striking_c, type="h",xaxt = "n",
xlab="Times of striking out",ylab="striking_c", ylim = c(0, 0.6),main="Probability distribution of striking out (p=70%)")
abline(h=0)
points(0:6, striking_c, pch=19)
axis(side = 1, at = 0:6, labels = TRUE)
#calculate and plot the CDF if the striking out prob is 0.7
CDF_c <- pbinom(0:6, size = 6, prob = 0.7)
plot(0:6, CDF_c, type="h",xaxt = "n",
xlab="Times of striking out",ylab="CDF when probability is 0.7", ylim = c(0, 1),main="Cumulative distribution of striking out (p=70%)")
abline(h=0)
points(0:6, CDF_c, pch=19)
axis(side = 1, at = 0:6, labels = TRUE)
```
## d) Repeat a) and b) if the pitcher has 30% chance of getting a strike-out
```{r}
striking_d <- dbinom(0:6,size=6,prob=0.3)
striking_d
#plot the PMF of striking out the 6 batters if prob=0.3
plot(0:6, striking_d, type="h",xaxt = "n",
xlab="Times of striking out",ylab="striking_d", ylim = c(0, 0.6),main="Probability distribution of striking out (p=30%)")
abline(h=0)
points(0:6, striking_d, pch=19)
axis(side = 1, at = 0:6, labels = TRUE)
#calculate and plot the CDF if the striking out prob is 0.7
CDF_d <- pbinom(0:6, size = 6, prob = 0.3)
plot(0:6, CDF_d, type="h",xaxt = "n",
xlab="Times of striking out",ylab="CDF when probability is 0.3", ylim = c(0, 1),main="Cumulative distribution of striking out (p=30%)")
abline(h=0)
points(0:6, CDF_d, pch=19)
axis(side = 1, at = 0:6, labels = TRUE)
```
## e) Infer from the shape of the distributions.
````{r}
#When the striking-out probability is 50%, the PMF is symmetric. Thus, the probability of getting 3 times of striking out for the next 6 batters is the highest, and the probabilities of getting 0 or 6 times of striking out for the next 6 batters are equally the lowest.
#When the striking-out probability is 70%, the PMF is left-skewed. Thus, the probability of getting 4 times of striking out for the next 6 batters is the highest, and the probability of getting 0 times of striking out for the next 6 batters is the lowest.
#When the striking-out probability is 30%, the PMF is right-skewed. Thus, the probability of getting 2 times of striking out for the next 6 batters is the highest, and the probability of getting 6 times of striking out for the next 6 batters is the lowest.
```
#Part 2) Binomial distribution
#Suppose that 80% of the flights arrive on time. Using the binomial distribution,
## a) What is the probability that four flights will arrive on time in the next 10 flights?
```{r}
n2 <- 10;p2 <- 0.8
flight_4 <- dbinom(4,size=n2,prob=p2) #calculating the PMF of 4 flights that are on time
flight_4
```
## b) What is the probability that four or fewer flights will arrive on time in the next 10 flights?
```{r}
flight_01234 <- dbinom(0:4, size=n2,prob=p2)#calculating the PMF of 4 or fewer flights that are on time
flight_01234
```
## c) Compute the probability distribution for flight arriving in time for the next 10 flights.
```{r}
flight_0to10 <- dbinom(0:10,size=n2,prob=p2)
flight_0to10
```
## d) Show the PMF and the CDF for the next 10 flights.
```{r}
plot(0:10,flight_0to10,type="h",xaxt = "n",
xlab="Numbers of flights on time",ylab="Probability", ylim = c(0, 0.5),main="Probability distribution of flights on time (p=80%)")
abline(h=0)
points(0:10, flight_0to10, pch=19)
axis(side = 1, at = 0:10, labels = TRUE)
#calculate and plot the CDF of the 10 flights on time
CDF_flight <- pbinom(0:10, size=n2,prob=p2)
plot(0:10,CDF_flight,type="h",xaxt = "n",
xlab="Numbers of flights on time",ylab="Cumulative Probability", ylim = c(0, 1),main="Cumulative distribution of flights on time (p=80%)")
abline(h=0)
points(0:10, CDF_flight, pch=19)
axis(side = 1, at = 0:10, labels = TRUE)
```
#Part 3) Poisson distribution
#Suppose that on average 10 cars drive up to the teller window at your bank between 3 PM and 4 PM and the random variable has a Poisson distribution. During this time period,
## a) What is the probability of serving exactly 3 cars?
```{r}
dpois(3, lambda=10)
```
## b) What is the probability of serving at least 3 cars?
```{r}
1-ppois(2,lambda=10)
```
## c) What is the probability of serving between 2 and 5 cars (inclusive)?
```{r}
ppois(5,lambda=10)-ppois(2,lambda=10)
#or calculate the probability by summing up the PMF
sum(dpois(c(3,4,5),lambda=10))
```
## d) Calculate and plot the PMF for the first 20 cars.
```{r}
car20 <- dpois(1:20,lambda=10)
plot(car20, type="h", xlab="Numbers of cars", ylab="PMF")
abline(h=0)
points(1:20,car20,pch=19)
axis(side = 1, at=c(0,5,10,15,20), labels=T)
```
#Part 4) Uniform distribution
#Suppose that your exams are graded using a uniform distribution between 60 and 100 (both inclusive).
##a) What is the probability of scoring i) 60? ii) 80? iii) 100?
```{r}
dunif(60,60,100)
dunif(80,60,100)
dunif(100,60,100)
#the probabilities of scoring 60, 80 and 100 are all equal to 0.025
```
##b) What is the mean and standard deviation of this distribution?
```{r}
grade <- seq(60,100,0.01)
grade_prob <- dunif(grade,60,100)
mean(grade_prob)
sd(grade_prob)
```
##c) What is the probability of getting a score of at most 70?
```{r}
punif(70,60,100)
```
##d) What is the probability of getting a score greater than 80 (use the lower.tail option)?
```{r}
punif(80,60,100,lower.tail = F)
```
##e) What is the probability of getting a score between 90 and 100 (both inclusive)?
```{r}
punif(100,60,100)-punif(90,60,100)
```
#Part5) Normal distribution
#Suppose that visitors at a theme park spend an average of $100 on souvenirs. Assume that the money spent is normally distributed with a standard deviation of $10.
## a) Show the PDF of this distribution covering the three standard deviations on either side of the mean.
```{r}
a5 <- seq(60,140)
pdf_a5 <- dnorm(a5, mean=100, sd=10)
plot(a5, pdf_a5, type="l", col="red",xaxt="n",xlab="Spending",ylab="PDF",main="PMF of spending on souvenirs")
axis(side = 1, at = seq(60,140,10),
labels = TRUE)
```
## b) What is the probability that a randomly selected visitor will spend more than $120?
```{r}
pnorm(120,100,10,lower.tail=F)
```
## c) What is the probability that a randomly selected visitor will spend between $80 and $90 (inclusive)?
```{r}
pnorm(90,100,10)-pnorm(80,100,10)
```
## d) What are the probabilities of spending within one standard deviation, two standard deviations, and three standard deviations, respectively?
###within one sd:
```{r}
pnorm(110,100,10)-pnorm(90,100,10)
```
###within two sd:
```{r}
pnorm(120,100,10)-pnorm(80,100,10)
```
###within three sd:
```{r}
pnorm(130,100,10)-pnorm(70,100,10)
```
## e) Between what two values will the middle 90% of the money spent will fall?
```{r}
qnorm(0.05,100,10)
qnorm(0.95,100,10)
```
## f) Show a plot for 10,000 visitors using the above distribution.
```{r}
plot(table(round((rnorm(10000,100,10)))),type="h",xaxt="n",xlab="Spending",ylab="Frequency",main="Histgram of 10,000 visitors")
axis(side=1, at=seq(60,140,10),labels=T)
```
# Part6) Exponential distribution (15 points)
# Suppose your cell phone provider's customer support receives calls at the rate of 18 per hour.
## a) What is the probability that the next call will arrive within 2 minutes?
```{r}
pexp(2/60,rate=18)
```
## b) What is the probability that the next call will arrive within 5 minutes?
```{r}
pexp(5/60,rate=18)
```
## c) What is the probability that the next call will arrive between 2 minutes and 5 minutes (both inclusive)?
```{r}
pexp(5/60,rate=18)-pexp(2/60,rate=18)
```
## d) Show the CDF of this distribution.
```{r}
d5 <- seq(0,1,by=1/60)
cdf <- pexp(d5,rate=18)
plot(d5,cdf,type="l",col="red",xlab="Time",main="CDF of Exponential distribution")
```