#Plots a QQ-Norm plot
library(StMoSim)
## Warning: package 'StMoSim' was built under R version 3.4.2
## Loading required package: RcppParallel
## Warning: package 'RcppParallel' was built under R version 3.4.2
## Loading required package: Rcpp
## 
## Attaching package: 'Rcpp'
## The following object is masked from 'package:RcppParallel':
## 
##     LdFlags
library(DATA606)
## 
## Welcome to CUNY DATA606 Statistics and Probability for Data Analytics 
## This package is designed to support this course. The text book used 
## is OpenIntro Statistics, 3rd Edition. You can read this by typing 
## vignette('os3') or visit www.OpenIntro.org. 
##  
## The getLabs() function will return a list of the labs available. 
##  
## The demo(package='DATA606') will list the demos that are available.
## 
## Attaching package: 'DATA606'
## The following object is masked from 'package:utils':
## 
##     demo

Homework # 3

3.6.1 Normal distribution

3.2 Area under the curve II.

 What percent of a standard normal distribution N(??=0,sd=1) is found in each region?

  *a) Z > -1.13*    *b) Z < 0.18*       *c) Z > 8*    *d)Z > 0.5*
pnorm(q=-1.13, mean=0, sd=1, , lower.tail=FALSE)
## [1] 0.8707619
#Plots a nomral distribution with the area within the given bounds shaded
#Inf and -Inf are positive and negative infinity
normalPlot(mean = 0, sd = 1, bounds= c(-1.13,Inf), tail=FALSE)

pnorm(q=0.18, mean=0, sd=1)
## [1] 0.5714237
normalPlot(mean = 0, sd = 1, bounds= c(-Inf, 0.18), tail=FALSE)

pnorm(q=8, mean=0, sd=1, lower.tail=FALSE)
## [1] 6.220961e-16
normalPlot(mean = 0, sd = 1, bounds= c(8, Inf), tail=FALSE)

pnorm(q=0.5, mean=0, sd=1, lower.tail=FALSE)
## [1] 0.3085375
normalPlot(mean = 0, sd = 1, bounds= c(0.5, Inf), tail=FALSE)

3.4 Triathlon times, part I.

In triathlons, it is common for reacers 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 how they did within their respective groups. Here is the information on the performance of their groups:

*The finishing times of the Men Ages 30-34 group has a mean = 4313 sec, sd = 583 sec*

*The finishing times of the Women Ages 25-29 group has a mean = 5261 sec, sd = 807 sec*

*The Distributions of finishing times for both groups are approximately Normal.*
  1. Write down the shorthand for these two normal distributions.

    Men Ages 30-34 group : N(?? = 4313,sd = 583)

    Women Ages 25-29 group : N(?? = 5261,sd = 807)

  2. What are the Z scores for Leo’s and Mary’s finishing times

    Formula: Z Score = (X - ??)/sd

    Leo’s Z score:

LeoZ <- (4948 - 4313)/583 
Mary's Z score :
MaryZ <- (5513 - 5261)/807
  1. Did Leo or Mary rank better in their respective groups?

Yes, Leo perfomed better than Mary because Mary’s percentil is 0.6217 lower than Leo’s percentil 0.8599

  1. What percent of the triathletes did Leo finish faster than in his group?
100 - (pnorm(q=LeoZ)*100)
## [1] 13.80342
  1. What percent of the triathletes did Mary finish faster than in her group?
100 - (pnorm(q=MaryZ)*100)
## [1] 37.74186
  1. If the distributions of finishing times are not nearly normal, would your answers to parts (b) - (e) change? Explain your reasoning.

The answer will be the same, the number of standard deviations falls above or below the mean.

3.18 Heights of female college students.

Below are heights of 25 female college students.

f.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)
hist(f.heights)

qqnormSim(f.heights, nSim=100)

a) The mean height is 61.52 inches with a standard deviation of 4.58 inches. Use this info to determine whether the heights approximately follow the 68-95-99.7% rule.

*R:/ The observation sample of 25 female hights doesn’t follow the 68-95-99.7% rule, However, the sample data follow the Z = 1 & Z = -1 about 68% and Z = -2 and Z = 2 about 95% but the 99.7 reach the maximum value of the data set 73 with a value of 74.56367.

summary(f.heights)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   54.00   58.00   61.00   61.52   64.00   73.00
#probabilities for falling within 1,2 or 3 standard deviations of the mean in a normal distribution
# 68% of 25 is 17 
#P = vector of probabilities
p.68 = f.heights[17]
p.68
## [1] 63
qnorm(p=0.68, mean=61.52, sd=4.58)
## [1] 63.66206
qnorm(p=0.34, mean=61.52, sd=4.58)
## [1] 59.63092
qnorm(p=0.95, mean=61.52, sd=4.58)
## [1] 69.05343
qnorm(p=0.05, mean=61.52, sd=4.58)
## [1] 53.98657
qnorm(p=0.9978, mean=61.52, sd=4.58)
## [1] 74.56367
qnorm(p=0.0121, mean=61.52, sd=4.58)
## [1] 51.19696
  1. Do these data appear to follow a normal distribution? Explain your reasoning using the graphs provided below. R:/ The data set is too small to see clear structure in the histogram. The normal probability plot also reflects this, where there are some deviations from the line. More deviations from the line means a small data set.
hist(f.heights, probability = TRUE, main = "Histogram of Women Height",  col = "red")
x <- 50:75
y <- dnorm(x = x, mean = 61.52, sd = 4.58)
lines(x = x, y = y , col = "blue")

qqnorm(f.heights)
qqline(f.heights)

# 3.6.3 Geometric distribution

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. What is the probability that the 10th transistor produced is the first with a defect?

Bernoulli distribution p = # of success /# of trials

Number of failures before success

#dgeom(x, prob, log = FALSE)
dgeom(10-1,0.02)
## [1] 0.01667496
  1. What is the probability that the machine produces no defective transistors in a batch of 100?
#pgeom(q, prob, lower.tail = TRUE, log.p = FALSE)
pgeom(100,0.02)
## [1] 0.8700328
  1. On average, how many transistors would you expect to be produced before the first with a defect? What is the standard deviation?
#The probability of failure 
q = 1 - 0.02
q
## [1] 0.98
# in AVG the probability of failure 
mean = 1/0.02
mean
## [1] 50
sqrt((1-0.02)/0.02^2)
## [1] 49.49747
  1. 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?
mean2 = 1/0.05
mean2
## [1] 20
sqrt((1-0.05)/0.05^2)
## [1] 19.49359
  1. 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 failure increases the probability of success decreases and of course the mean and the standard deviation also decreases.

3.6.4 Binomial Distribution

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.

  1. Use the binomial model to calculate the probability that two of them will be boys.
#This is conventionally interpreted as the number of 'successes' in size trials.
#dbinom(x, size, prob, log = FALSE)
dbinom(x=2,size=3,prob=0.51)
## [1] 0.382347
  1. Write out all possible orderings of 3 children, 2 of whom are boys. Use these scenarios to calculate the same probability from part B-B-G B-G-B G-B-B

    1. but using the addition rule for disjoint outcomes. Confirm that your answers from parts a) and d) match
(0.51^2*0.49) + (0.51*0.49*0.51) + (0.49*0.51^2)
## [1] 0.382347
  1. 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).

The approach from part (b) would be more tedious because manually I have to write all possible scenarios of 3 boys from 8 kids and then use the addition rule to find the answer while in option a) the function dbinom() will do the calculation given the x, size and prob parameters.

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.

  1. What is the probability that on the 10th try she will make her 3rd successful serve? p(x) = choose(n, x) p^k (1-p)^(n-k) x = 2 p = 0.15 k = 3 n - k = 7
choose(9,2)*0.15^3*(.85)^(7)
## [1] 0.03895012
  1. Suppose she has made two successful serves in nine attempts. What is the probability that her 10th serve will be successful?

The probabilty of having k successful serves in n independent attempts is the same so in this sample the probability that n serve will be 15%

  1. 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?

Because in part (b) no matter what is the probabilities of the first 9 attempts, we are looking the probability of success on the 10 attempt. while in part (a) we are looking the probability of that the 3rd serve will be success in 10th attempts.