1 Pre-Requistes : Setup R environment for DATA 606.

See https://data606.net/assignments/homework/ for more information. Chapter 3 - Distributions of Random Variables Practice: 3.1 (see normalPlot), 3.3, 3.17 (use qqnormsim from lab 3), 3.21, 3.37, 3.41 Graded: 3.2 (see normalPlot), 3.4, 3.18 (use qqnormsim from lab 3), 3.22, 3.38, 3.42

1.1 Install R packages as per DATA606Spring2019-master/R/Setup.R

Refer to “Getting Started with R” in https://data606.net/post/


2 Exercises

2.1 Question#3.2 Area under the curve, Part II.

What percent of a standard normal distribution N(?? =0, ?? = 1) is found in each region? Be sure to draw a graph.

Z=(x?????)/??

2.1.1 (a) Z > -1.13

mu <- 0
sd <- 1
Z <- -1.13
x <- Z * sd + mu
x
## [1] -1.13
P <- 1 - pnorm(Z, mean = mu, sd = sd) # Finding probability for Z > -1.13
P
## [1] 0.8707619
par(mfrow=c(1,3))
visualize.norm(stat=x, mu=mu, sd=sd, section="upper")
shadeDist(x, lower.tail = FALSE)
normalPlot(mean = mu, sd = sd, bounds=(c(x,Inf)), tails = FALSE)

2.1.2 (b) Z < 0.18

mu <- 0
sd <- 1
Z <- 0.18
x <- Z * sd + mu
x
## [1] 0.18
P <- pnorm(Z, mean = 0, sd = 1) # Finding probability for Z < 0.18
P
## [1] 0.5714237
par(mfrow=c(1,3))
visualize.norm(stat=x, mu=mu, sd=sd, section="lower")
shadeDist(x, lower.tail = TRUE)
normalPlot(mean = mu, sd = sd, bounds=(c(-Inf,x)), tails = FALSE)

2.1.3 (c) Z > 8

mu <- 0
sd <- 1
Z <- 8
x <- Z * sd + mu
x
## [1] 8
P <- 1 - pnorm(Z, mean = mu, sd = sd) # Finding probability for Z > 8
P
## [1] 6.661338e-16
par(mfrow=c(1,3))
visualize.norm(stat=x, mu=mu, sd=sd, section="upper")
shadeDist(x, lower.tail = FALSE)
normalPlot(mean = mu, sd = sd, bounds=(c(x,Inf)), tails = FALSE)

2.1.4 (d) |Z| < 0.5

Z < 0.5
-Z < 0.5 => Z > -0.5
-0.5 < Z < 0.5
mu <- 0
sd <- 1
Z <- 0.5
x <- Z * sd + mu
x
## [1] 0.5
x1 <- pnorm(-Z, mean = mu, sd = sd)
x2 <- pnorm(Z, mean = mu, sd = sd)
P <- x2 - x1 # Finding probability for |Z| < 0.5 = -Z < 0.5 < Z
P
## [1] 0.3829249
par(mfrow=c(1,3))
visualize.norm(stat=c(-Z,Z), mu=mu, sd=sd, section="bounded")
shadeDist(c(-Z,Z), lower.tail = FALSE)
normalPlot(mean = mu, sd = sd, bounds=(c(-Z,Z)), tails = FALSE)


2.2 Question#3.4 Triathlon times, Part I

In triathlons, it is common for racers to be placed into age and gender groups. Friends Leo and Mary both completed the Hermosa Beach Triathlon, where Leo competed in the Men, Ages 30 - 34 group while Mary competed in the Women, Ages 25 - 29 group. Leo completed the race in 1:22:28 (4948 seconds), while Mary completed the race in 1:31:53 (5513 seconds). Obviously Leo finished faster, but they are curious about how they did within their respective groups. Can you help them? Here is some information on the performance of their groups: . The finishing times of the Men, Ages 30 - 34 group has a mean of 4313 seconds with a standard deviation of 583 seconds. . The finishing times of the Women, Ages 25 - 29 group has a mean of 5261 seconds with a standard deviation of 807 seconds. . The distributions of finishing times for both groups are approximately Normal. Remember: a better performance corresponds to a faster finish.

2.2.1 (a) Write down the short-hand for these two normal distributions.

Leo Men group: Ages=30-34; Mean=4313s; SD=583s; RaceTime=4948s
Mary Women group: Ages=25-29; Mean=5261s; SD=807s; RaceTime=5513s

N(??=4313, s=583) -> Men, Ages 30 - 34 N(??=5261, s=807) -> Women, Ages 25 - 29

2.2.2 (b) What are the Z-scores for Leo’s and Mary’s finishing times? What do these Z-scores tell you?

The Z score of an observation is the number of standard deviations it falls above or below the mean.

LMgX <- 4948
LMgU <- 4313
LMgS <- 583
LMgZ <- (LMgX-LMgU)/LMgS
paste0('Leo Men group Z score is ',LMgZ,', the number of standard deviation is above the mean.')
## [1] "Leo Men group Z score is 1.08919382504288, the number of standard deviation is above the mean."
MWgX <- 5513
MWgU <- 5261
MWgZ <-  (5513-5261)/807
paste0('Mary Women group Z score is ',MWgZ,', the number of standard deviation is above the mean.')
## [1] "Mary Women group Z score is 0.312267657992565, the number of standard deviation is above the mean."

2.2.3 (c) Did Leo or Mary rank better in their respective groups? Explain your reasoning.

paste('Mary had a lower Z score of', MWgZ,'than Leo Z score of', LMgZ,' which would mean that Mary got faster time than Leo within their respective groups.')
## [1] "Mary had a lower Z score of 0.312267657992565 than Leo Z score of 1.08919382504288  which would mean that Mary got faster time than Leo within their respective groups."

2.2.4 (d) What percent of the triathletes did Leo finish faster than in his group?

paste0('Percent of the triathletes who finished slower than Leo in his Men group ', (1-pnorm(LMgZ))*100, '%')
## [1] "Percent of the triathletes who finished slower than Leo in his Men group 13.803421070203%"

2.2.5 (e) What percent of the triathletes did Mary finish faster than in her group?

paste0('Percent of the triathletes who finished slower than Mary in her Women group ', (1-pnorm(MWgZ))*100, '%')
## [1] "Percent of the triathletes who finished slower than Mary in her Women group 37.74185585735%"

2.2.6 (f) If the distributions of finishing times are not nearly normal, would your answers to parts (b) - (e) change?

Explain your reasoning.

The part b and c would not change, because the ranking will not change and hence Zscore will still reflect above or below mean. But the part d and e will change due to changes in density and percentiles, “Pnorm” only used for normal distribution.


2.3 Question#3.18 Heights of female college students.

Below are heights of 25 female college students. 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 54,55,56,56,57,58,58,59,60,60,60,61,61,62,62,63,63,63,64,65,65,67,67,69,73

heights <- c(54,55,56,56,57,58,58,59,60,60,60,61,61,62,62,63,63,63,64,65,65,67,67,69,73)
summary(heights)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   54.00   58.00   61.00   61.52   64.00   73.00
glimpse(heights)
##  num [1:25] 54 55 56 56 57 58 58 59 60 60 ...

2.3.1 (a) The mean height is 61.52 inches with a standard deviation of 4.58 inches.

Use this information to determine if the heights approximately follow the 68-95-99.7% Rule.

mean <- 61.52
sd <- 4.58

#prove 68% rule
lb1 <- mean - sd
ub1 <- mean + sd
cat(paste0('68% rule calculation as :'),
    paste0('1. (pnorm(ub1, mean, sd) - pnorm(lb1, mean, sd)) = ',(pnorm(ub1, mean, sd) - pnorm(lb1, mean, sd))),
    paste0('2. (1-2*pnorm(ub1, mean, sd, lower=FALSE))= ', (1-2*pnorm(ub1, mean, sd, lower=FALSE))),
    paste0('4. (quantile(heights,0.68)) = ', quantile(heights,0.68)),sep='\n')
## 68% rule calculation as :
## 1. (pnorm(ub1, mean, sd) - pnorm(lb1, mean, sd)) = 0.682689492137086
## 2. (1-2*pnorm(ub1, mean, sd, lower=FALSE))= 0.682689492137087
## 4. (quantile(heights,0.68)) = 63
#prove 95% rule
lb2 <- mean - (sd*2)
ub2 <- mean + (sd*2)
cat(paste0('95% rule calculation as :'),
    paste0('1. pnorm(ub2, mean, sd) - pnorm(lb2, mean, sd) = ',(pnorm(ub2, mean, sd) - pnorm(lb2, mean, sd))),
    paste0('2. (1-2*pnorm(ub2, mean, sd, lower=FALSE))= ', (1-2*pnorm(ub2, mean, sd, lower=FALSE))),
    paste0('3. (quantile(heights,0.95)) = ', quantile(heights,0.95)),sep='\n')
## 95% rule calculation as :
## 1. pnorm(ub2, mean, sd) - pnorm(lb2, mean, sd) = 0.954499736103642
## 2. (1-2*pnorm(ub2, mean, sd, lower=FALSE))= 0.954499736103642
## 3. (quantile(heights,0.95)) = 68.6
#prove 99.7% rule
lb3 <- mean - (sd*3)
ub3 <- mean + (sd*3)
cat(paste0('99.7% rule calculation as :'),
    paste0('1. pnorm(ub3, mean, sd) - pnorm(lb3, mean, sd) = ',(pnorm(ub3, mean, sd) - pnorm(lb3, mean, sd))),
    paste0('2. (1-2*pnorm(ub3, mean, sd, lower=FALSE))= ', (1-2*pnorm(ub3, mean, sd, lower=FALSE))),
    paste0('3. (quantile(heights,0.997)) = ', quantile(heights,0.997)),sep='\n')
## 99.7% rule calculation as :
## 1. pnorm(ub3, mean, sd) - pnorm(lb3, mean, sd) = 0.99730020393674
## 2. (1-2*pnorm(ub3, mean, sd, lower=FALSE))= 0.99730020393674
## 3. (quantile(heights,0.997)) = 72.712

Hence, these heights approximately follow the 68-95-99.7% Rule since:
(1) 63% of the data are within 1 standard deviation of the mean.
(2) 68.6% of the data are within 2 standard deviation of the mean.
(3) 72.7% of the data are within 3 standard deviation of the mean.

2.3.2 (b) Do these data appear to follow a normal distribution?

Explain your reasoning using the graphs provided below.

qqnormsim(heights)

par(mfrow=c(2,2))

qqnorm(heights)
qqline(heights, col = 2)

histPlot(heights, main="Histogram")

hist(heights, probability = TRUE, xlab = "Heights", ylim = c(0, 0.1))
x <- 50:75
y <- dnorm(x = x, mean = mean, sd = sd)
lines(x = x, y = y, col = "blue")
abline(v = mean, col = "red")

hist(heights, freq=F, col ="red", ylim=c(0,0.1))
curve(dnorm(x, 61.52, 4.58),min(heights), max(heights), add=T, col="blue")

The distriubtion is unimodal, symmetric and similar to normal. The theoretical quantiles are also keeping the trend of lines. The normal curve depicts the bell shaped curve of the distribution in histogram. The data on the normal probability fall on the line, apart from one outlier at the end of the line resulting in slight right skewness


2.4 Question#3.22 Defective rate.

A machine that produces a special type of transistor (a component of computers) has a 2% defective rate. The production is considered a random process where each transistor is independent of the others.

#  1 2 3 4 5 6 7 8 9 Defect ...... Probaility of 1 defect in 10
# In this case, 9 is the number of success until we find a defective part
defectRate <- 0.02
successRate <- (1-defectRate)

2.4.1 (a) What is the probability that the 10th transistor produced is the first with a defect?

paste('probability that the 10th transistor produced is the first with a defect =',((successRate^9)*(defectRate^1)))
## [1] "probability that the 10th transistor produced is the first with a defect = 0.016674955242603"
paste('Calculating probability of 10th transistor produced is the first with a defect =',dgeom(9, defectRate))
## [1] "Calculating probability of 10th transistor produced is the first with a defect = 0.016674955242603"

2.4.2 (b) What is the probability that the machine produces no defective transistors in a batch of 100?

paste('probability that the machine produces no defective transistors in a batch of 100 =',((successRate^100)*(defectRate^0)))
## [1] "probability that the machine produces no defective transistors in a batch of 100 = 0.132619555894753"
paste('Calculating probability of 0 defect in nbatch of 100 transistors produced =',dbinom(0, 100, defectRate))
## [1] "Calculating probability of 0 defect in nbatch of 100 transistors produced = 0.132619555894753"
#pgeom(100, defectRate, lower.tail = FALSE)

2.4.3 (c) On average, how many transistors would you expect to be produced before the first with a defect? What is the standard deviation?

paste('probability how many transistors would you expect to be produced before the first with a defect =',(1/defectRate))
## [1] "probability how many transistors would you expect to be produced before the first with a defect = 50"
paste('standard deviation =',sqrt(successRate/defectRate^2))
## [1] "standard deviation = 49.4974746830583"

2.4.4 (d) Another machine that also produces transistors has a 5% defective rate where each transistor is produced independent of the others.

On average how many transistors would you expect to be produced with this machine before the first with a defect? What is the standard deviation?

newDefectRate <- 0.05
newSuccessRate <- (1-newDefectRate)
paste('probability how many transistors would you expect to be produced before the first with a defect =',(1/newDefectRate))
## [1] "probability how many transistors would you expect to be produced before the first with a defect = 20"
paste('standard deviation =',sqrt(newSuccessRate/newDefectRate^2))
## [1] "standard deviation = 19.4935886896179"

2.4.5 (e) Based on your answers to parts (c) and (d), how does increasing the probability of an event affect the mean and standard deviation of the wait time until success?

When the probability of defective rate increases, the expected number iterations of product having first defect decreases i.e, it gets to a defect faster. And the normal distribution would become narrow due to decrease in standard deviation and in mean.


2.5 Question#3.38 Male children.

While it is often assumed that the probabilities of having a boy or a girl are the same, the actual probability of having a boy is slightly higher at 0.51. Suppose a couple plans to have 3 kids.

boyRate <- 0.51
girlRate <- 1-boyRate

2.5.1 (a) Use the binomial model to calculate the probability that two of them will be boys.

paste('probability of having 2 boys out of 3 =', (girlRate^(3-2)) * (boyRate^2) * (choose(3,2)))
## [1] "probability of having 2 boys out of 3 = 0.382347"
possible3KidPermutation <- factorial(3) # Permutations (ggg, bgg, bbg, bbb) / Combinations (ggg, bgg, gbg, ggb, bgb, bbg, gbb, bbb)
possible2BoyPermutation <- factorial(2) # bbg, bgb, gbb
paste('Calculating probability of having 2 boys out of 3 =',(girlRate^(3-2)) * (boyRate^2) * (possible3KidPermutation/possible2BoyPermutation))
## [1] "Calculating probability of having 2 boys out of 3 = 0.382347"

2.5.2 (b) Write out all possible orderings of 3 children, 2 of whom are boys.

Use these scenarios to calculate the same probability from part (a) but using the addition rule for disjoint outcomes. Confirm that your answers from parts (a) and (b) match.

paste('Possible combinations of having 2 boys out of 3 =', sum((boyRate*boyRate*girlRate),(boyRate*girlRate*boyRate),(girlRate*boyRate*boyRate)))
## [1] "Possible combinations of having 2 boys out of 3 = 0.382347"

2.5.3 (c) If we wanted to calculate the probability that a couple who plans to have 8 kids will have 3 boys,

briefly describe why the approach from part (b) would be more tedious than the approach from part (a).

To calculate the probability of 8 kids having 3 boys, there would be 56 combinations to be calculated, hence calculations of part(b) would be more tedious for all possible scenarios of atleast 3 boys out of 8 kids than atleast 2 boys out of 3 kids.


2.6 Question#3.42 Serving in volleyball.

A not-so-skilled volleyball player has a 15% chance of making the serve, which involves hitting the ball so it passes over the net on a trajectory such that it will land in the opposing team’s court. Suppose that her serves are independent of each other.

serveRate <- 0.15
failRate <- 1-0.15

2.6.1 (a) What is the probability that on the 10th try she will make her 3rd successful serve?

paste('probability of having 3rd successful serve on the 10th try =', (failRate^7) * (serveRate^3) * (choose(9,2))) # Negative Binomial distribution
## [1] "probability of having 3rd successful serve on the 10th try = 0.0389501162261719"
possible9Permutation <- factorial(9) / (factorial(2)*factorial(7))  #number of cases with 2 successes and 7 failures in 9 first attemps
paste('Calculating probability of having 3rd successful serve on the 10th try =',(failRate^7) * (serveRate^3) * (possible9Permutation))
## [1] "Calculating probability of having 3rd successful serve on the 10th try = 0.0389501162261719"

2.6.2 (b) Suppose she has made two successful serves in nine attempts. What is the probability that her 10th serve will be successful?

The probability that her 10th serve will be successful is 15% since all her serves are independent of each other.

2.6.3 (c) Even though parts (a) and (b) discuss the same scenario, the probabilities you calculated should be different. Can you explain the reason for this discrepancy?

For part a, it is calculated by negative binonmial distribution to count 3rd successful serve in 10th attempt (Only 2 of the former 9 attempts are success).

For part b, it is only calculated the probability that 10th attempt will be successful assuming that previous 9 attempts already had 2 successful serve.