Skip to main content

Solutions Manual Probability, Statistics, and Data A Fresh Approach Using R, 1st Edition by Speegle

Page 1

Solutions Manual for Probability, Statistics, and Data A Fresh Approach Using R, 1e by Darrin Speegle, Bryan Clair (All Chapters)

1 Data in R – Solutions Problem 1 a. 2 4 6; b. 6 10 12; c. 5 Problem 2 a. 1 2 3 (result not stored into x). b. 2 4 6 c. 1 2 3 (result not stored into x) d. 2 3 4 Problem 3 1:10 ## [1] 1 1:10 * 2

2

3

4

## [1] 1:10ˆ2

4

6

8 10 12 14 16 18 20

2 20 38 56 74 92

3 21 39 57 75 93

3

4

5

6

2

3

4

5

2

## [1] ## [19] ## [37] ## [55] ## [73] ## [91] 1:10 + 1 ##

[1]

1 19 37 55 73 91 2

5

6

4 22 40 58 76 94

7

8

9 10

5 23 41 59 77 95

6 24 42 60 78 96

7 25 43 61 79 97

7

8

9 10 11

6

7

8

8 26 44 62 80 98

9 10 27 28 45 46 63 64 81 82 99 100

11 29 47 65 83

12 30 48 66 84

13 31 49 67 85

14 32 50 68 86

15 33 51 69 87

16 34 52 70 88

17 35 53 71 89

1:(10 * 2) ##

[1]

1

9 10 11 12 13 14 15 16 17 18 19 20

rep(c(1,1,2), times = 2) ## [1] 1 1 2 1 1 2 seq(from = 0, to = 10, length.out = 5) ## [1]

0.0

2.5

5.0

7.5 10.0

Problem 4 p <- seq(0,1,0.2) plot(p,p*(1-p),type='l')

1

18 36 54 72 90


0.15

1. Data in R – Solutions

0.00

p * (1 − p)

2

0.0

0.2

0.4

0.6

0.8

1.0

0.6

0.8

1.0

p

0.15 0.00

p * (1 − p)

p <- seq(0,1,0.01) plot(p,p*(1-p),type='l')

0.0

0.2

0.4 p

Problem 5 sum((1:100)ˆ2) ## [1] 338350 Problem 6 x <- seq(from = 10, to = 30, by = 2) length(x) ## [1] 11 x[2] ## [1] 12 x[1:5] ## [1] 10 12 14 16 18 x[1:3*2] ## [1] 12 16 20 x[1:(3*2)] ## [1] 10 12 14 16 18 20


1. Data in R – Solutions

3

x > 25 ##

[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

TRUE

TRUE

TRUE

x[x > 25] ## [1] 26 28 30 x[-1] ##

[1] 12 14 16 18 20 22 24 26 28 30

x[-1:-3] ## [1] 16 18 20 22 24 26 28 30 Problem 7 mean(rivers) ## [1] 591.1844 sd(rivers) ## [1] 493.8708 hist(rivers)

80 40 0

Frequency

Histogram of rivers

0

1000

2000

3000

4000

rivers summary(rivers) ## ##

Min. 1st Qu. 135.0 310.0

Median 425.0

Mean 3rd Qu. 591.2 680.0

Max. 3710.0

max(rivers) ## [1] 3710 min(rivers) ## [1] 135 rivers[rivers > 1000] ## [1] 1459 1450 1243 2348 1171 3710 2315 2533 1306 1054 1270 1885 1100 1205 1038 ## [16] 1770


4

1. Data in R – Solutions

Problem 8 str(airquality) ## 'data.frame': ## $ Ozone : int ## $ Solar.R: int ## $ Wind : num ## $ Temp : int ## $ Month : int ## $ Day : int

153 obs. of 6 variables: 41 36 12 18 NA 28 23 19 8 NA ... 190 118 149 313 NA NA 299 99 19 194 ... 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ... 67 72 74 62 56 66 65 59 61 69 ... 5 5 5 5 5 5 5 5 5 5 ... 1 2 3 4 5 6 7 8 9 10 ...

153 observations of 6 variables, named Ozone, Solar.R, Wind, Temp, Month, Day. They are all type int except for Wind, which is type num. Ozone, Solar.R, and Temp would be more natural as num types. Month and Day could be stored as factor types. Problem 9 a. table(state.region) ## state.region ## Northeast ## 9

South North Central 16 12

West 13

b. state.name[state.area < 10000] ## [1] "Connecticut" "Delaware" ## [5] "New Hampshire" "New Jersey"

"Hawaii" "Rhode Island"

"Massachusetts" "Vermont"

"Mazda RX4 Wag" "Datsun 710" "Merc 280" "Merc 280C" "Toyota Corolla" "Fiat X1-9"

"Merc 240D" "Fiat 128" "Volvo 142E"

c. state.name[which.min(state.center$y)] ## [1] "Florida" Problem 10 row.names(mtcars)[mtcars$gear == 4] ## ## ##

[1] "Mazda RX4" [5] "Merc 230" [9] "Honda Civic"

row.names(mtcars)[mtcars$gear == 4 & mtcars$am == 1] ## [1] "Mazda RX4" ## [5] "Honda Civic"

"Mazda RX4 Wag" "Datsun 710" "Toyota Corolla" "Fiat X1-9"

"Fiat 128" "Volvo 142E"

row.names(mtcars)[mtcars$gear == 4 | mtcars$am == 1] ## [1] "Mazda RX4" ## [5] "Merc 230" ## [9] "Honda Civic" ## [13] "Lotus Europa" ## [17] "Volvo 142E"

"Mazda RX4 Wag" "Datsun 710" "Merc 280" "Merc 280C" "Toyota Corolla" "Fiat X1-9" "Ford Pantera L" "Ferrari Dino"

mean(mtcars$mpg[mtcars$carb == 2])

"Merc 240D" "Fiat 128" "Porsche 914-2" "Maserati Bora"


1. Data in R – Solutions

5

## [1] 22.4 Problem 11 mtcars$am <- factor(mtcars$am, levels = c(0, 1), labels = c("auto", "manual")) table(mtcars$am) ## ## ##

auto manual 19 13

table(mtcars$am[mtcars$mpg > 25]) ## ## ##

auto manual 0 6

Problem 12 a. 54 observations of 3 variables; b. Beef, Meat, Poultry; c. 645; d. 190 max(hot_dogs[hot_dogs$type == "Beef","calories"]) ## [1] 190 Problem 13 a. 70 obs. of 6 variables. class and trade are type Factor. sober, drinks, and n are type int. wage is type num. b. Profession #23, factory worker, is the lowest paid. c. 604 workers were surveyed d. The average wage of all 604 workers was 24.60 shillings/week. library(HistData) which.min(DrinksWages$wage) ## [1] 23 DrinksWages[23,] ## class trade sober drinks wage n ## 23 A factory worker 1 3 12 4 sum(DrinksWages$n) ## [1] 604 sum(DrinksWages$wage * DrinksWages$n)/604 ## [1] 24.59782 Problem 14 library(Lahman) a. 102816 obs. of 22 variables c,d,e with base R: max(Batting$X3B) ## [1] 36


6

1. Data in R – Solutions

Batting[which.max(Batting$X3B),c("playerID","yearID","X3B")] ## playerID yearID X3B ## 13805 wilsoch01 1912 36 recent<-Batting[Batting$yearID >= 1960,c("playerID","yearID","X3B")] recent[which.max(recent$X3B),] ## playerID yearID X3B ## 89181 grandcu01 2007 23 wilsoch01 hit 36 triples in 1912. Since 1960, It’s grandcu01 (Curtis Granderson) with 23 in 2007. Alternately, c,d,e with dplyr library: head(arrange(Batting,desc(X3B)),1) ## playerID yearID stint teamID lgID G AB R H X2B X3B HR RBI SB CS BB SO ## 1 wilsoch01 1912 1 PIT NL 152 583 80 175 19 36 11 95 16 NA 35 67 ## IBB HBP SH SF GIDP ## 1 NA 2 23 NA NA recent <- filter(Batting, yearID > 1960) head(arrange(recent,desc(X3B)),1) ## playerID yearID stint teamID lgID G AB R H X2B X3B HR RBI SB CS BB ## 1 grandcu01 2007 1 DET AL 158 612 122 185 38 23 23 74 26 1 52 ## SO IBB HBP SH SF GIDP ## 1 141 3 5 5 2 3 Problem 15 a. 803 pass b. 44.76% pass c. table(bechdel$year) d. 2010; which.max(table(bechdel$year)) e. 5 f. bechdel[bechdel$binary == "PASS",] g. bechdel[!is.na(bechdel$domgross),]


2 Probability – Solutions Problem 1 The probability that one die is twice the other is 1/6. Problem 2 a. P (difference = 0) = 1/6 b. P (difference = 4) = 1/9 Problem 3 a. The sample space consists of the following 15 ordered pairs: (1,2), (1,3), (1,4), (1,5), (1,6), (2,3), (2,4), (2,5), (2,6), (3,4), (3,5), (3,6), (4,5), (4,6), (5,6). b. E = {(1, 4), (2, 3)}. c. P (E) = 2/15. d. P (F ) = 0. Problem 4 a. About 11 percent. mean(replicate(10000, { sum(sample(1:8, 2, TRUE)) == 8 })) ## [1] 0.1092 b. About 23 percent. mean(replicate(10000, { any(sample(1:8, 2, TRUE) == 2) })) ## [1] 0.2282 Problem 5 a. 0.80; b. 0.47; c. 0.61; d. 0.013 mmdist <- c(0.14, 0.13, 0.20, 0.12, 0.20, 0.21) mmcolors <- c('Yel','Red','Org','Brn','Grn','Blu') mean(replicate(10000,any(sample(mmcolors,4,replace=TRUE,prob=mmdist)=='Blu'))) ## [1] 0.6049 mean(replicate(10000,anyDuplicated(sample(mmcolors,6,replace=TRUE,prob=mmdist)) == 0)) ## [1] 0.0135 Problem 6 About 0.065. mean(replicate(10000,{ bag <- sample(mmcolors,30,replace=TRUE,prob=mmdist); blueCount <- sum(bag == 'Blu'); orangeCount <- sum(bag == 'Org'); (blueCount >= 9) & (orangeCount >= 6) })) ## [1] 0.0647

7


8

2. Probability – Solutions

Problem 7 About 38%. P (same) = P (OO) + P (AA) + P (BB) + P (ABAB) = 0.452 + 0.402 + 0.112 + 0.042 = 0.3762. Or simulate: types <- c("O", "A", "B", "AB") p <- c(0.45, 0.40, 0.11, 0.04) mean(replicate(10000,sample(types,1,prob=p) == sample(types,1,prob=p))) ## [1] 0.3813 Problem 8 mean(replicate(10000,sum(sample(1:6,2,replace=TRUE)) == 10)) ## [1] 0.0878 3/36 ## [1] 0.08333333 Problem 9 mean(replicate(10000,sum(sample(0:1,7,replace=TRUE)) == 3)) ## [1] 0.2719 choose(7,3)/128 ## [1] 0.2734375 Problem 10 About 0.56. mean(replicate(10000,{roll <- sum(sample(1:6,5,replace=TRUE)); roll >= 15 & roll <= 20})) ## [1] 0.5519 Problem 11 mean(replicate(10000, {dieRoll <- sample(1:6, 20, replace = TRUE); 20 %in% cumsum(dieRoll)})) ## [1] 0.289 Problem 12

0 1000

rolls <- replicate(10000,sum(sample(1:6,2,replace=TRUE))) barplot(table(rolls))

2

3

4

5

6

7

8

rolls <- replicate(10000,5+sample(c(2,6),1)) barplot(table(rolls))

9

10

11

12


9

0 3000

2. Probability – Solutions

7

11

0 1000

rolls <- replicate(10000,sample(c(1,2,2,3,3,4),1)+sample(c(1,3,4,5,6,8),1)) barplot(table(rolls))

2

3

4

5

6

7

8

9

10

11

12

A sum of two Sicherman dice has the same probability distribution as a sum of two normal dice. Problem 13 If you happen to be born on day 255, like I was: mean(replicate(10000,any(sample(1:365,199,replace=TRUE)==255))) ## [1] 0.4201 1-(364/365)ˆ199 ## [1] 0.420711 Problem 14 num_hours <- 24 * 365 sim_data <- replicate(10000, { birth_hours <- sample(1:num_hours, 100, T) anyDuplicated(birth_hours) > 0 }) mean(sim_data) ## [1] 0.4296 The probability is about 43 percent! Problem 15 The probability is approximately 0.12. most_with_same_birthday <- replicate(10000,max(table(sample(1:365,50,replace=TRUE)))) mean(most_with_same_birthday >= 3) ## [1] 0.121 Problem 16 sum(replicate(100000,length(unique(sample(1:20,100,replace=TRUE))) < 20))/100000 ## [1] 0.11546 Gives (with rounding) 0.113 consistently Problem 17 a. 0.048; b. 0.060


10

2. Probability – Solutions

deck <- rep(c(2:10,10,10,10,11),4) deals <- replicate(100000, sum(sample(deck,2))) mean(deals == 21) ## [1] 0.04828 mean(deals == 19) ## [1] 0.06084 Problem 18 Between .04 and .05. About .047. pp <- replicate(10000, { x1 <- sample(1:1000, 1) x2 <- sample(1:x1, 1) x3 <- sample(1:x2, 1) x4 <- sample(1:x3, 1) return(x4 == 1 && x3 > 1) }) mean(pp) ## [1] 0.0451 Problem 19 About .019. vowels <- c("A","E","I","O","U") mean(replicate(100000,{ hand <- sample(fosdata::scrabble$piece,7); length(intersect(hand,vowels)) == 0 })) ## [1] 0.01895 1 Problem 20 a. 12 ≈ 0.083 b. 16 ≈ 0.167 c. 12

1 2 Problem 21 a. 15 ≈ 0.067 b. 15 ≈ 0.133 c. 12

Problem 22 Disjoint: AD, CD; Independent: AB, BD Problem 23 a. A and C are disjoint. b. B and C are independent. c. P (B|A) = 1. Problem 24 Since B is contained in A ∪ B, P (A ∪ B|B) =

P ((A ∪ B) ∩ B) P (B) = =1 P (B) P (B)

Problem 25 #Solution Needed Problem 26 P (A) = 1/6, P (B) ≈ .42 and P (A ∩ B) ≈ .11. A and B are not independent since P (A ∩ B) 6= P (A)P (B). prob_a <- mean(replicate(10000, { die_roll <- sample(1:6, 3, T) die_roll[1] >= max(die_roll[2:3]) })) prob_a ## [1] 0.4237


2. Probability – Solutions

11

prob_ab <- mean(replicate(10000, { die_roll <- sample(1:6, 3, T) die_roll[1] >= max(die_roll[2:3]) && die_roll[1] == 5 })) prob_ab ## [1] 0.1144 p(1−p) = 21 . Problem 27 The probability is 2p(1−p)

Problem 28 a. 0.934; b. 0.063. Problem 29

0.9 0.5

p^2 + (1 − p)^2

p <- seq(0,1,.05); plot(p,pˆ2 + (1-p)ˆ2, type='l')

0.0

0.2

0.4

0.6

0.8

1.0

p a. 1/2; b. p2 + (1 − p)2 ; d. P (same) = 0.505, P (diff) = 0.495. e. Call same. f. Yes, closer to even. Problem 30 There is only a 40.5% chance a person testing positive has the disease. Let D be the event “has disease” and + be the event “tests positive”. Given P (+|D) = 0.999, P (−|not D) = 0.97 and P (D) = 0.02. First, P (+|not D) = 0.03 so the law of total probability gives P (+) = 0.999 · 0.02 + 0.03 · 0.98 = 0.04938

Now Bayes’ rule gives

P (D|+) = P (+|D) ·

P (D) 0.02 = 0.999 · ≈ 0.405 P (+) 0.04938

Problem 31 Let R be the event “red marble” and B1 and B2 be the events corresponding to the boxes. a. With the law of total probability: P (R) = P (R|B1 )P (B1 ) + P (R|B2 )P (B2 ) =

31 22 1 + = 73 73 3

b. Using Bayes’ rule: P (B1 |R) = P (R|B1 ) Problem 32 a. About 84%.

P (B1 ) 3 1/3 3 = = ≈ 0.249 P (R) 7 1/3 7


12

2. Probability – Solutions

mean(replicate(20000, { box <- sample(0:9, 1); marblesInBox <- c(rep("R", box), rep("B", 9 - box)); marblesDrawn <- sample(marblesInBox, 3, TRUE); if (all(marblesDrawn == "R")) { sample(marblesInBox, 1) == "R" } else NA }), na.rm = TRUE) ## [1] 0.83779 Here is another approach. Let B be the event “three red marbles drawn” and A the event “four red marbles drawn”. Estimate P (B) and P (A ∩ B) to get P (A|B). This is somewhat less accurate for the same number of trials. probB <- mean(replicate(10000, { box <- sample(0:9, 1); marblesInBox <- c(rep("R", box), rep("B", 9 - box)); marblesDrawn <- sample(marblesInBox, 3, TRUE); all(marblesDrawn == "R") })) #Prob of B probAB <- mean(replicate(10000, { box <- sample(0:9, 1); marblesInBox <- c(rep("R", box), rep("B", 9 - box)); marblesDrawn <- sample(marblesInBox, 4, TRUE); all(marblesDrawn == "R") } )) #Prob of A cap B probAB/probB ## [1] 0.8216944 b. About 36%. mean(replicate(20000, { box <- sample(0:9, 1); marblesInBox <- c(rep("R", box), rep("B", 9 - box)); marblesDrawn <- sample(marblesInBox, 3, TRUE); if (all(marblesDrawn == "R")) { box == 9 # TRUE if box 9, FALSE otherwise } else NA # don't count this trial }), na.rm = TRUE) ## [1] 0.3653223 Problem 33 10 4 = 210. Problem 34 93 = 84. Problem 35

a. 66 = 46656. b. 6! = 720. c. 720/46656 = 5/324 ≈ 0.015.

Problem 36


2. Probability – Solutions a. 10 210. 6 = b. 54 52 = 50. c. 50/210 ≈ 0.238

13


3 Discrete Random Variables – Solutions Problem 1 a. 1/4 + 1/2 + 1/8 + 1/8 = 1. b. P (X ≥ 2) = 1/4. c. P (X ≥ 2|X ≥ 1) = 1/3. d. P (X ≥ 2 ∪ X ≥ 1) = P (X ≥ 1) = 3/4 Problem 2

X <- sample(0:3, 10000, prob=c(1/4,1/2,1/8,1/8), replace=TRUE) mean(X == 1) # part a ## [1] 0.4947 table(X)/10000

# part b

## X ## 0 1 2 3 ## 0.2497 0.4947 0.1322 0.1234 P Problem 3 Need 1 = x p(x) = C/4 + C/2 + C = 7C/4 so C = 4/7.

Problem 4 For example, p(x) = 1/2 when x = ±1 and p(x) = 0 otherwise. Problem 5 E[X] = 0 · 41 + 1 · 12 + 2 · 18 + 3 · 18 = 89 = 1.125

1 1 11 1 + · · · + 10 · 10 = 10 (1 + 2 + · · · + 10) = 55 Problem 6 E[X] = 1 · 10 10 = 2 = 5.5

X <- sample(1:10, 10000, replace=TRUE) mean(X) ## [1] 5.5062 Problem 7 Exactly: products <- sort(rep(1:6,6)) * (1:6) sum(products)/36

# clever way to get all 36 products

## [1] 12.25 Or by simulation: mean( replicate(10000, prod(sample(1:6,2,replace=TRUE)))) ## [1] 12.1701 Problem 8 If X is the product, X = 2, X = 3, and X = 6 are all equally likely. Then E(X) = 13 2 + 31 3 + 13 6 = 11 3 90 900 10 + 2 · 1000 + 3 · 1000 = 2.89. Problem 9 The expected number of digits is 1 · 1000

Problem 10

a. X can take two values, 1 and 3. p(1) = .952 = 0.9025, p(3) = 1 − .952 = 0.0975. b. E[X] = 1 · 0.9025 + 3 · 0.0975 = 1.195.

c.Using simulations:

15


16

3. Discrete Random Variables – Solutions

npool <- 90 # number of people in the pool sim_data <- replicate(10000, { tests <- sample(c("pos", "neg"), npool, TRUE, prob = c(5, 95)) ifelse(any(tests == "pos"), 1+npool, 1) }) table(sim_data)/10000 ## sim_data ## 1 91 ## 0.01 0.99 mean(sim_data) ## [1] 90.1 d. Pooled testing uses less tests. In this setting pooled testing uses less tests with the pool size 87 or less, although the advantage diminishes after a pool size of 18. Problem 11 a. Expected value of $1 bet on red is 1 ∗ (18/38) + (−1) ∗ (20/38) = −2/38 ≈ −0.053. b. Expected value of let it ride, once, is 3 ∗ (18/38)2 + (−1) ∗ (1 − (18/38)2 ) = −0.102.

Problem 12 a. Bet doubling, you can lose 6 in a row. Seven losses in a row and you are out of money. b. The probability you do not win $1 is (20/38)7 = 0.0112. The probability you win $1 is 1 − 0.0112 = 0.9888. You are about 99% likely to win a dollar with bet doubling! c. Expected value is 1 ∗ 0.9888 + (−127) ∗ 0.0112 = −0.432. This is not good - you expect to lose, on average, 43 cents every time you play this strategy. d. Let X be the number of wins before you go broke. X ∼ Geom(0.0112), so the expected value is E(X) = (1 − p)/p = 0.9888/0.0112 ≈ 88.4 plays before you go broke. Problem 13 This code uses some tools that are a little advanced for this chapter. library(plyr) suppressMessages(suppressWarnings(library(dplyr)))

n <- 10 #flips to sample k <- 1 #count flips after k-in-a-row successes trials <- rdply(10000, { shots <- sample(0:1,n,replace=TRUE) after <- shots[diff(c(rep(0,k+1),cumsum(shots)),k) == k] table(factor(after, levels=0:1), useNA="no") }) trials <- mutate(trials, hit = `1`, miss = `0`, count = hit + miss, p = hit/count) %>% select(hit,miss,count,p) mean(trials$p,na.rm=TRUE) ## [1] 0.4425184 Problem 14 a. $1 straight: 0.60 b. $1 front pair: 0.60 c. $1 back pair: 0.60 d. $6 6-way: 3.60 (since the bet costs $6, this is a return of 0.60 on the dollar) e. $3 3-way: 1.80 (since the bet costs $3, this is a return of 0.60 on the dollar) f. $1 1-off: (1 × 300 + 6 × 29 + 12 × 4 + 8 × 9)/1000 = 0.594 (this is the worst bet, you don’t get your sixty cents on the dollar)


3. Discrete Random Variables – Solutions

17

Problem 15 E[X] = k1 (1 + 2 + · · · + k) = k+1 2 .

Problem 16 Expected score is 5. Probability of 10 or more correct is:

sum(dbinom(10:20,20,.25)) ## [1] 0.01386442 Problem 17 Expected number is 9.1 shots. Probability of at least 8 made is: sum(dbinom(8:10,10,.91)) ## [1] 0.94596 Problem 18 dgeom(19,.09) ## [1] 0.01499785 Problem 19 0.047*262 #a ## [1] 12.314 1 - pbinom(41,262,0.047) #b ## [1] 4.627632e-12 0.5*305 #c ## [1] 152.5 1 - pbinom(210,305,0.5) #d ## [1] 8.791412e-12 It seems likely that Dream was cheating. Problem 20 Let X be a geometric rv with success probability p. Let q = 1 − p be the failure probability. Begin with a geometric series in q: ∞ X

qx =

x=0

1 1−q

Take the derivative of both sides with respect to q: ∞ X

x=0

and a second time:

∞ X

x=0 2

Multiply both sides by pq :

xq x−1 =

1 (1 − q)2

x(x − 1)q x−2 =

∞ X

x=0

x(x − 1)pq x =

2 (1 − q)3 2pq 2 (1 − q)3


18

3. Discrete Random Variables – Solutions

Replace 1 − q with p and we have shown: E[X(X − 1)] =

2q 2 p2

Now E[X] = q/p so E[X 2 ] = E[X(X − 1)] + E[X] =

2q 2 qp 2q 2 + qp + = p2 p2 p2

Finally, var(X) = E[X 2 ] − E[X]2 =

q 2 + qp q(q + p) q 2q 2 + qp q 2 − 2 = = = 2. 2 p p p2 p2 p

The standard deviation is then σ(X) =

√

1−p p

Problem 21  1/4    1/2 p(y) =  1/8    1/8

 1/4    1/2 p(u) =  1/8    1/8

Problem 22 a. 23; b. -5.

  1/2 p(v) = 3/8   1/8

y = −1 y=0 y=1 y=2 u=0 u=1 u=4 u=9 v=0 v=1 v=2

Problem 23 pmf <- c(1/4, 1/2, 1/8, 1/8) mu <- sum((0:3)*pmf) # mu = 9/8 v <- sum( ((0:3) - mu)ˆ2 * pmf ) # v is the variance The variance is 0.859375, the sd is 0.9270248. Problem 24 # a pmf <- c(1:6,5:1)/36 # b mu <- sum(2:12 * pmf) mu # mean


3. Discrete Random Variables – Solutions

19

## [1] 7 sd <- sqrt(sum((2:12)ˆ2 * pmf) - muˆ2) # sd sd ## [1] 2.415229 # c sdˆ2 ## [1] 5.833333 2*(35/12) ## [1] 5.833333 Problem 25 a. 9 = Var(X) = E[X 2 ] − 4 so E[X 2 ] = 13. b. E[(2X − 1)2 ] = E[4X 2 − 4X + 1] = 4(13) − 4(2) + 1 = 45 c. Var(2X − 1) = E[(2X − 1)2 ] − E[2X − 1]2 = 45 − 9 − 36.

Problem 26

E[(X − µX )(Y − µY )] = E[XY − XµY − Y µX + µX µY ] =

= E[XY ] − E[XµY ] − E[Y µX ] + µX µY = E[XY ] − µX µY − µX µY + µX µY

= E[XY ] − E[X]E[Y ] = cov(X, Y ) Problem 27 a. Var(X) = 100(.2)(.8) = 16 and Var(Y ) = 40 ∗ .5 ∗ .5 = 10. b. The value should be 26. X <- rbinom(100000,100,.2) Y <- rbinom(100000,40,.5) var(X + Y) ## [1] 25.93344

Problem 28 There are four outcomes of the experiment, HH, HT, TH, TT. All three variables have probability 1/2 of being either 0 or 1. Every pair of values for two of the variables occurs on exactly one outcome of the experiment: • (X, Y ) are (0,0) when the outcome is TT, (0,1) when TH, (1,0) when HT, and (1,1) when HH. • (X, Z) are (0,0) when the outcome is TH, (0,1) when TT, (1,0) when HT, and (1,1) when HH. • (Y, Z) are (0,0) when the outcome is HT, (0,1) when TT, (1,0) when TH, and (1,1) when HH. Since each pair has probability 1/4 and 1/4 = 1/2 · 1/2, each pair of variables is independent. However, P (X = 0, Y = 0, Z = 0) = 0 since it’s impossible for the first and second toss to both be tails while also demanding that the tosses don’t match. Since P (X = 0)P (Y = 0)P (Z = 0) = 1/8 6= 0, the variables are not mutually independent. Problem 29


20

The mean is E Problem 30

h

X−µ σ

i

3. Discrete Random Variables – Solutions = σ1 (E[X] − µ) = 0. The sd is σ X−µ = σ1 (σ(X − µ)) = 1. σ

a. Expect √ 110 to support. b. σ = 200 ∗ .55 ∗ .45/200 ≈ 0.0352. Margin of error is 7.04%. c. Probability that poll claims Prop A will fail is 8.87%. pbinom(100,200,.55) ## [1] 0.08870062 d. For 2% margin of error, solve 2

p .55 ∗ .44/n = 0.02 to get n = 2420.

Problem 31 The mean and standard deviation are both 1.

correct <- replicate(10000,sum(sample(1:27) == 1:27)) mean(correct) ## [1] 1.0018 sd(correct) ## [1] 1.00881 Here is an exact computation for the expected value. There are n! possible draws from the hat. Person 1 gets their number correct (n − 1)! of these. The same goes for person 2, and so on. Then the total number of correct assignments across all n! draws is (n − 1)! + · · · + (n − 1)! = n(n − 1)! = n!. So the average number of correct assignments is n!/n! = 1. Problem 32

a. Let Xi be the participant’s score on guess i. On the ith trial, the participant has 1/12 chance of scoring 1 and 2/12 chance of scoring 3/4. Their expected score for one guess is then E[Xi ] = 1/12 + 6/48 = 5/24 ≈ 0.208. The expected score on 36 guesses is 36 · 5/24 = 15/2 = 7.5. b. For a single guess: E[Xi2 ] = 1 · 1/12 + (3/4)2 · (2/12) = 17/96. Then Var(Xi ) = 17/96 − (5/24)2 = 77/576. Since each√trial is independent, the variance for the score after 36 guesses is 77/16 so the sd is 77/4 ≈ 2.19.

Problem 33

0.20 0.10 0.00

dpois(0:15, 3.9)

plot(0:15,dpois(0:15,3.9))

0

5

10 0:15

b. 3 is most likely c. a = 3.

15


3. Discrete Random Variables – Solutions

21

d. b = 2. Problem 34 The maximum difference is about 0.0014 when x = 2. approx_err <- abs(dpois(0:200,2)-dbinom(0:200,200,2/200)) which.max(approx_err)-1 ## [1] 2 max(approx_err) ## [1] 0.001362443 2

var(N e) e var(N ) λ Problem 35 var(I) E[I] = E[N e] = eE[N ] = e λ = e.

Problem 36 P∞ λx −λ P∞ x P∞ x a. = e−λ x=0 λx! = e−λ eλ = 1, since x=0 λx! is the Taylor series of eλ . x=0 x! e P∞ P∞ P∞ x x λx b. E[X] = x=0 x λx! e−λ = e−λ x=1 (x−1)! = λe−λ x=0 λx! = λ. P∞ P∞ P∞ x λx λx −λ c. E[X(X − 1)] = = e−λ x=1 (x−2)! = λ2 e−λ x=0 λx! = λ2 . x=0 x(x − 1) x! e Therefore, by E[X 2 ] = λ2 + λ and Var(X) = E[X 2 ] − E[X]2 = λ2 + λ − λ2 = λ. Problem 37

a. The number of failures before n successes is the number of failures before the first success plus the number of failures between first and second success, and so on. Pn Pn p p b. From part a, E[X] = i=1 E[Xi ] = n 1−p and Var(X) = i=1 Var(Xi ) = n (1−p) 2.

Problem 38

np λ = (1−p) Var(X) = λ ≤ 1−p 2 = Var(Y ).

Problem 39 scrabble <- fosdata::scrabble vowels <- c('A','E','I','O','U') num_vowels <- replicate(10000,{ hand <- sample(scrabble$piece, 7) sum(hand %in% vowels) }) mean(num_vowels == 7) ## [1] 0.0015 mean(num_vowels <= 2) ## [1] 0.3732 mean(num_vowels) ## [1] 2.9478 sd(num_vowels) ## [1] 1.274691 Problem 40 We can take a large sample of number of rolls with the following code.


22

3. Discrete Random Variables – Solutions

sim_data <- replicate(10000, { cur_value <- 1000 num_rolls <- 1 while(cur_value != 1) { cur_value <- sample(1:cur_value, 1) num_rolls <- num_rolls + 1 } num_rolls }) a. The expected value is about 9.5 mean(sim_data) ## [1] 9.4748 b. Here is an approximate pmf. table(sim_data)/10000 ## sim_data ## 2 3 4 5 6 7 8 9 10 11 12 ## 0.0011 0.0063 0.0224 0.0465 0.0780 0.1173 0.1306 0.1366 0.1243 0.1024 0.0798 ## 13 14 15 16 17 18 19 20 21 22 23 ## 0.0588 0.0369 0.0255 0.0137 0.0097 0.0047 0.0023 0.0014 0.0007 0.0007 0.0001 ## 25 ## 0.0002 c. The probability that player 1 wins is the probability that it takes an even number of rolls to get to 1, and is about 1/2. mean(sim_data %% 2 == 0) ## [1] 0.4936


4 Continuous Random Variables – Solutions Problem 1 a. 3/4; b. 4/5 Problem 2 C = 3/2. Problem 3 For each of the following functions, decide whether the function is a valid pdf, a valid cdf or neither.   0≤x≤2 1 a. h(x) = −1 2 ≤ x ≤ 3 Not pdf or cdf, has negative values.   0 otherwise b. h(x) = sin(x) + 1 Not pdf, doesn’t integrate to 1. Not cdf because not increasing. ( 2 1 − e−x x ≥ 0 c. h(x) = Not pdf, doesn’t integrate to 1. Yes cdf. Increasing, and 0 x<0 has correct ( limits. 2 2xe−x x ≥ 0 d. h(x) = Yes pdf. Nonnegative and integrates to 1. Not cdf because 0 x<0 not increasing. Problem 4 One possible answer: f (x) = 2x for 0 ≤ x ≤ 1 and f (x) = 0 otherwise. It is not possible for f (x) > 1 for all x. Problem 5 It is not possible. limx→∞ f (x) = 1 if f is a cdf but the limit is 0 for a pdf. Problem 6 R1 a. 0 3(1 − x)2 dx = 1, and f (x) ≥ 0 for all x. R1 b. µ(X) = 0 x · 3(1 − x)2 dx = 1/4. R1 var(X) = 0 x2 · 3(1 − x)2 dx − (1/4)2 = 0.0375. σ(X) ≈ 0.194. c. 7/8 d. (19/64)/(27/64) = 19/27 ≈ .704.

Problem 7 Var(2X + 1) = 4Var(X) = 12.

Problem 8 a. 1; b. 2; c. a = 0; d. A little bit less than 1/2. Problem 9 x<-seq(-20,20,.01) plot(x,dnorm(x,1,1), type="l") lines(x,dnorm(x,1,10), type="l") lines(x,dnorm(x,-4,1), type="l")

23


0.0

0.2

0.4

4. Continuous Random Variables – Solutions

dnorm(x, 1, 1)

24

−20

0

10

20

x Problem 10 a. pnorm(5,1,2)-pnorm(3,1,2) ## [1] 0.1359051 b.

0.10

0.20

# tricky - could just shade by hand

0.00

dnorm(x, 1, 2)

x <- seq(-6,8,.1) plot(x,dnorm(x,1,2),type="l") x <- seq(3,5,.1) polygon(c(3,x,5),c(0,dnorm(x,1,2),0),col="grey")

−6

−2

2 4 6 8 x

c. Largest when a = 0.

Problem 11 pnorm(85, 80, 5, lower.tail = FALSE) ## [1] 0.1586553 1 - pbinom(3, 10, 0.1586553) ## [1] 0.05974509 Problem 12 x <- seq(4536,5536,10); plot(x,dnorm(x,5036,122), type="l")


25

0.0000 0.0020

dnorm(x, 5036, 122)

4. Continuous Random Variables – Solutions

4600

5000

5400

x b. About 0.384. c. 5236.7 lbs. Problem 13 X <- rnorm(100000,0,2) Y <- rnorm(100000,0,1) var(X+3*Y) ## [1] 13.12761 This is close to Var(X) + 9Var(Y ) = 13. Problem 14 Z ∞

e

−∞

= So

R∞

2

−∞

−x2

2 Z ∞ Z ∞ Z ∞Z ∞ 2 2 2 2 dx = e−x dx e−y dy = e−(x +y ) dx dy

Z 2π Z ∞

e−x dx =

0

√

0

−∞

2

−∞

e−r r dr dθ =

Z 2π 0

π.

Problem 15

0.8 0.4 0.0

dunif(x)

x <- seq(-1,2,.01) plot(x,dunif(x),type="l")

−1.0

0.0

1.0 x

plot(x,punif(x),type="l")

2.0

−∞

2 1 dθ = − e−r 2 r=0

∞

−∞

Z 2π 0

1 dθ = π 2


4. Continuous Random Variables – Solutions

0.0

0.4

punif(x)

0.8

26

−1.0

0.0

1.0

2.0

x Problem 16

1.0 0.0

dexp(x, 2)

2.0

x<-seq(-.1,5,.01) plot(x,dexp(x,2), type="l") lines(x,dexp(x,1/2), type="l", lty=2) # dashed line

0

1

2

3

4

5

x Problem 17 a. 4 b. P (a ≤ X ≤ a + 1) is maximized when a = 0. The mean is not in [0, 1].

0.05 0.15 0.25

dexp(xvals, 1/4)

xvals <- seq(0,10,.1) plot(xvals,dexp(xvals, 1/4),type='l')

0

2

4

6

8

10

xvals Problem 18 The probability mom gets a text on any given day is 1/6. The probability she gets a text on 3 consecutive days is (1/6)3 = 1/216 ≈ 0.46%


4. Continuous Random Variables – Solutions

27

Problem 19 component1 <- rexp(10000,1/5) component2 <- rexp(10000,1/5) system.fail <- (component1 < 10) & (component2 < 10) mean(system.fail) ## [1] 0.7462 Alternately, P (system fail) = P (c1 < 10 ∩ c2 < 10) = P (c1 < 10) · P (c2 < 10). So: pexp(10,1/5)ˆ2

## [1] 0.7476451 Problem 20 Let X ∼ unif(a, b). E[X] = (a + b)/2. Then var(X) =

Z b a

(x −

b−a 2 (b − a)2 ) dx = . 2 12

Problem 21 a. The statement says that the probability of waiting a additional time, given that you’ve already waited b, is the same as the probability of waiting a from the beginning. In other words, the fact that you’ve waited b doesn’t help: the variable has no “memory” of how long you have waited so far. Ra b. P (X > a) = 1 − 0 λe−λx dx = e−aλ . c.

P (X > a+b | X > b) =

P (X > a + b) e−(a+b)λ P (X > a + b ∩ X > b) = = = = e−aλ = P (X > a). P (X > b) P (X > b) e−bλ

Problem 22 Part a: Y is binomial. P (Y = 3) ≈ 0.155. Expect 10/6 ≈ 1.67 sixes. V ar(Y ) = 50/36 ≈ 1.39. Part b: U is poisson. P (U = 2) ≈ 0.27, E(U ) = 2, V ar(U ) = 2.

Part c: X is uniform. The mean of X is 30 seconds.

Part d: X is exponential. The mean of X is 1/5 hour or 12 minutes. 10 minutes is 1/6 hour, and P (X ≤ 1/6) = 0.565. Part e: X is geometric. The mean of X is 1. P (X ≤ 3) ≈ 0.94.

Part f: X is normal. The mean of X is well known to be 98.6◦ F . The standard deviation is probably around 1◦ F , since 100◦ F is considered a fever. Problem 23 X is uniform with mean 15 minutes. Problem 24 F (z) = P (Z ≤ z) = P (X1 ≤ z)∩(X2 ≤ z)∩(X3 ≤ z) = P (X1 ≤ z)·P (X2 ≤ z)·P (X3 ≤ z) ( z 3 z ∈ [0, 1] For z ∈ [0, 1], P (Xi ≤ z) = z, so F (z) = . 0 otherwise Problem 25


28

4. Continuous Random Variables – Solutions

pexp(4, 1/4) ## [1] 0.6321206 Sixty-three percent change the meeting will start in 4 or fewer minutes. b. pexp(5, 1/4, lower.tail = FALSE) ## [1] 0.2865048 Twenty-nine percent chance the meeting will start in exactly five minutes. Problem 26 n <- 4 mean(replicate(10000, { centers <- sort(runif(n)) all(diff(centers) <= .5) && (centers[n] - centers[1] > .5) })) ## [1] 0.5021 The probability that 4 intervals of length 1/2 placed randomly inside of [0, 1] union to form an interval of length of at least 1 is 1/2. Problem 27 sim_data <- replicate(10000, { location <- runif(1) angle <- runif(1, 0, pi) location + abs(1/4 * sin(angle)) > 1 || location - abs(1/4 * sin(angle)) < 0 }) abs(1/mean(sim_data) - pi) ## [1] 0.07598873 Problem 28 Test if sides of length a, b, c make a triangle by checking if the longest edge c is shorter than the sum of the two shorter edges a + b. Since our stick has length 1, a + b + c = 1 this means that c < a + b ⇐⇒ c < 1 − c or c < 1/2. mean(replicate(10000,{ breaks <- c(0,sort(runif(2)),1) lengths <- diff(breaks) longedge <- max(lengths) longedge < 1/2 })) ## [1] 0.2512 The exact answer is 1/4.


Turn static files into dynamic content formats.

Create a flipbook
Solutions Manual Probability, Statistics, and Data A Fresh Approach Using R, 1st Edition by Speegle by digitaldownload87 - Issuu