library(grid)
library(futile.logger)
library(VennDiagram)
## Warning: package 'VennDiagram' was built under R version 3.4.4
library(knitr)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.4.4
library('DATA606')          # Load the package
## Loading required package: shiny
## Warning: package 'shiny' was built under R version 3.4.4
## Loading required package: openintro
## Please visit openintro.org for free statistics materials
## 
## Attaching package: 'openintro'
## The following object is masked from 'package:ggplot2':
## 
##     diamonds
## The following objects are masked from 'package:datasets':
## 
##     cars, trees
## Loading required package: OIdata
## Loading required package: RCurl
## Warning: package 'RCurl' was built under R version 3.4.4
## Loading required package: bitops
## Loading required package: maps
## Warning: package 'maps' was built under R version 3.4.4
## Loading required package: markdown
## 
## 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
library(knitr)

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.

(a) Z > -1.13

Z = − 1.13

x=seq(-3,3,length=500)
y=dnorm(x,mean=0,sd=1)
plot(x,y,type="l")
x=seq(-1.13,3,length=100)
y=dnorm(x,mean=0,sd=1)
polygon(c(-1.13,x,3),c(0,y,0),col="red")

1-pnorm(-1.13)
## [1] 0.8707619

(b) Z <0.18

x=seq(-3,3,length=500)
y=dnorm(x,mean=0,sd=1)
plot(x,y,type="l")
x=seq(-3,0.18,length=100)
y=dnorm(x,mean=0,sd=1)
polygon(c(-3,x,.18),c(0,y,0),col="red")

pnorm(0.18)
## [1] 0.5714237

(c) Z > 8

x=seq(-3,10,length=500)
y=dnorm(x,mean=0,sd=1)
plot(x,y,type="l")
x=seq(8,10,length=100)
y=dnorm(x,mean=0,sd=1)
polygon(c(8,x,10),c(0,y,0),col="red")

pnorm(8)
## [1] 1

(d) |Z|<0.5

x=seq(-3,3,length=500)
y=dnorm(x,mean=0,sd=1)
plot(x,y,type="l")
x=seq(-0.5, 0.5, length=100)
y=dnorm(x,mean=0,sd=1)
polygon(c(-.5,x,.5),c(0,y,0),col="red")

pnorm(.5)
## [1] 0.6914625

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.

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

Men’s group, age 30-34 μ=4313s, σ=583s
Women’s group, age 25-29 μ=5216s, σ=807s

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

ZscoreLeo <- (4948 - 4313) / 583
ZscoreLeo
## [1] 1.089194
ZscoreMary <- (5513 - 5261) / 807
ZscoreMary
## [1] 0.3122677

Z score Leo was 1.089 standard deviations away from the mean and Z score for Mary was .312 standard deviations away from the mean.

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

Mary was better than Leo as she was closer to mean than Leo in that group.

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

pnorm(ZscoreLeo)
## [1] 0.8619658

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

pnorm(ZscoreMary)
## [1] 0.6225814

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

The percentiles would change, but the Z scores will remain same.

3.18 Heights of female college students.

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.

femaleheight <- 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)
femaleheight
##  [1] 54 55 56 56 57 58 58 59 60 60 60 61 61 62 62 63 63 63 64 65 65 67 67
## [24] 69 73
fhgtmean <- mean(femaleheight)
fhgtsd <- sd(femaleheight)
hist(femaleheight)

qqnormsim(femaleheight)

pnorm(fhgtmean+fhgtsd, mean =  fhgtmean, sd = fhgtsd)
## [1] 0.8413447
pnorm(fhgtmean+2*fhgtsd, mean =  fhgtmean, sd = fhgtsd)
## [1] 0.9772499
pnorm(fhgtmean+3*fhgtsd, mean =  fhgtmean, sd = fhgtsd)
## [1] 0.9986501

Yes, it follow the 68-95-97% rule. 84.13% of the data are within 1 standard deviation of the mean.

97.72% of the data are within 2 standard deviation of the mean.

99.86% of the data are within 3 standard deviation of the mean.

  1. Do these data appear to follow a normal distribution? Explain your reasoning using the graphs provided below.
qqnorm(femaleheight)
qqline(femaleheight)

The distibution is normal with very few outliers, most data points are close to the line.

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?
pgeom(10-1,0.02)
## [1] 0.1829272
  1. What is the probability that the machine produces no defective transistors in a batch of 100?
1-pgeom(100,0.02)
## [1] 0.1299672
  1. On average, how many transistors would you expect to be produced before the first with a defect? What is the standard deviation?
first_defect_prob <- 1/0.02
first_defect_prob
## [1] 50
sd <- sqrt((1 - 0.02)/0.02^2)
sd
## [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?
first_defect_prob <- 1/0.05
first_defect_prob
## [1] 20
sd <- sqrt((1 - 0.05)/0.05^2)
sd
## [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?

Higher the probability less the wait time of the event.

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.

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

dbinom(2, 3, 0.51)
## [1] 0.382347

(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.

#addition rule for disjoint
#P=P({g,b,b})+P({b,g,b})+P({b,b,g})
prob <- ((.49*.51*.51)+(.51*.49*.51)+(.51*.51*.49))
prob
## [1] 0.382347

(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).

There are 8!/(8−3)!=336 permutations in this case.

dbinom(3,8,0.51)
## [1] 0.2098355

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.

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

n = 10
k = 3
p= 0.15
q = 0.85
prb = (factorial(n-1)/(factorial(k-1)*factorial(n-k))*p^k*q^(n-k))
prb
## [1] 0.03895012

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

Probability is 0.15 as each serve is independant.

(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?

Part (b) was only concerned with only one event where as part(a) was combination of events.

Appendix

library(grid)
library(futile.logger)
library(VennDiagram)
library(knitr)
library(ggplot2)
library('DATA606')          # Load the package
library(knitr)
x=seq(-3,3,length=500)
y=dnorm(x,mean=0,sd=1)
plot(x,y,type="l")
x=seq(-1.13,3,length=100)
y=dnorm(x,mean=0,sd=1)
polygon(c(-1.13,x,3),c(0,y,0),col="red")
1-pnorm(-1.13)
x=seq(-3,3,length=500)
y=dnorm(x,mean=0,sd=1)
plot(x,y,type="l")
x=seq(-3,0.18,length=100)
y=dnorm(x,mean=0,sd=1)
polygon(c(-3,x,.18),c(0,y,0),col="red")
pnorm(0.18)
x=seq(-3,10,length=500)
y=dnorm(x,mean=0,sd=1)
plot(x,y,type="l")
x=seq(8,10,length=100)
y=dnorm(x,mean=0,sd=1)
polygon(c(8,x,10),c(0,y,0),col="red")
pnorm(8)
x=seq(-3,3,length=500)
y=dnorm(x,mean=0,sd=1)
plot(x,y,type="l")
x=seq(-0.5, 0.5, length=100)
y=dnorm(x,mean=0,sd=1)
polygon(c(-.5,x,.5),c(0,y,0),col="red")
pnorm(.5)
ZscoreLeo <- (4948 - 4313) / 583
ZscoreLeo
ZscoreMary <- (5513 - 5261) / 807
ZscoreMary
pnorm(ZscoreLeo)
pnorm(ZscoreMary)
femaleheight <- 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)
femaleheight
fhgtmean <- mean(femaleheight)
fhgtsd <- sd(femaleheight)
hist(femaleheight)
qqnormsim(femaleheight)
pnorm(fhgtmean+fhgtsd, mean =  fhgtmean, sd = fhgtsd)
pnorm(fhgtmean+2*fhgtsd, mean =  fhgtmean, sd = fhgtsd)
pnorm(fhgtmean+3*fhgtsd, mean =  fhgtmean, sd = fhgtsd)
qqnorm(femaleheight)
qqline(femaleheight)
pgeom(10-1,0.02)
1-pgeom(100,0.02)
first_defect_prob <- 1/0.02
first_defect_prob
sd <- sqrt((1 - 0.02)/0.02^2)
sd
first_defect_prob <- 1/0.05
first_defect_prob
sd <- sqrt((1 - 0.05)/0.05^2)
sd
dbinom(2, 3, 0.51)
#addition rule for disjoint
#P=P({g,b,b})+P({b,g,b})+P({b,b,g})
prob <- ((.49*.51*.51)+(.51*.49*.51)+(.51*.51*.49))
prob
dbinom(3,8,0.51)
n = 10
k = 3
p= 0.15
q = 0.85
prb = (factorial(n-1)/(factorial(k-1)*factorial(n-k))*p^k*q^(n-k))
prb
``