Problem 1

Simulation of a binomial random variable - n=60, p=0.10

  1. Total # of students out of 60 who did not turn in their homework:
## [1] 3
  1. Histogram of 1000000 values for “number of successes”:

This distridution is not symetric because p < 0.5 - we can see that the distribution is slightly skewed to the right.

  1. The average of the number of successes in 60 trials:
## [1] 5.999515

SD of the number of successes in 60 trials:

## [1] 2.322118
  1. The probability that all students turned in their homework
## [1] 0.00175
  1. The probability that at least four students did not turn in their homework
## [1] 0.728704
  1. The median number of students who will forget their homeworks
## [1] 6

Problem 2

Simulation a Negative Binomial random variable.

  1. Selecting random people until 5 wear glasses, p=0.40.
## [1] 17
  1. Histogarm of 20000 values of how many people were selected until 5 wore glasses

  2. Median of the vector:

## [1] 12
  1. The mean of the vector from (b):
## [1] 12.5146

SD of the vector from (b)

## [1] 4.298245
  1. The probability we had to sample more than 10 people before we found 5 that needed glasses.
## [1] 0.6376
  1. The probability that if it takes more than 10 people, it will take an additional 5 to find 5 that wore glasses
## [1] 0.341123

Problem 3

Using function rnorm, which simulates a normal random variable. The following code will generate 2000 normal random variables with mean 100, standard deviation 15: X = rnorm(2000, mean = 100, sd = 15)

  1. 10000 values of a normal random variable with mean 2, standard deviation 1 - vector X. 10000 values of a normal random variable with mean -2, standard deviation 5 - vector Y. Estimating mean and sd of both vectors.

Mean of X:

## [1] 1.996753

SD of X:

## [1] 1.005326

Mean of Y:

## [1] -2.002978

SD of Y:

## [1] 5.031236
  1. Creating vector W by adding the two vectors fromn (a) together. Estimating mean and SD of W

mean of W:

## [1] -0.006224924

SD of W:

## [1] 5.126173
  1. The probability that W is larger than 3:
## [1] 0.2812
  1. Calculating the error for all the values

mean error for the vector X:

## [1] -0.003247279

SD error for the vector X:

## [1] 0.005325917

mean error for the vector Y:

## [1] -0.002977645

SD error for the vector Y:

## [1] 0.03123577

mean error for the vector W:

## [1] -0.006224924

SD error for the vector W:

## [1] -0.8738268

W prob error:

## [1] 0.006946882

Appendix Code

#1.a
sim <- function(p) {
  success = sample(c("1","0"), 60, prob=c(0.90,0.10), replace = TRUE)
  result = sum(success == "0")
  return (result)
}
sim(0.90)

#1.b

all = sapply(1:1000000, function(p){
  p = 0.10
  sim(p)
})

hist(all, main = "Distribution of 1000000 means ",xlab="Number of students", border="red", col="green")

#1.c
mean(all)
#1.c 
sd(all)
#1.d
prob = sum(all == "0")/length(all)
prob
#1.e
prob2=sum(all>4)/length(all)
prob2

#1.f
median(all)
#2.a
num.glasses = 0;
People = 0;
while(num.glasses<5){
  People = People+1
  glasses = sample(c("Glass","NotGlass"),1, prob = c(0.4,0.6),replace = FALSE)
  if (glasses == "Glass"){
    num.glasses = num.glasses+1
  }
}
People

#2.b
many.people = sapply(1:20000,function(i){
num.glasses = 0;
People = 0;
while(num.glasses<5){
  People = People+1
  glasses= sample(c("Glass","NotGlass"),1,prob = c(0.4,0.6),replace = FALSE)
  if (glasses == "Glass"){
    num.glasses = num.glasses+1
  }
}
  return(People)
})
hist(many.people, main = "Distribution of 20000 means",xlab="People", border="red", col="green")

#2.c
median(many.people)
#2.d 
mean(many.people)
#2.d 
sd(many.people)
#2.e
p=sum(many.people>10)/length(many.people)
p
#2.f
p2 = (sum(many.people>10 & many.people>15)/length(many.people))/(sum(many.people>10)/length(many.people))
p2
#3.a
X = rnorm(10000, mean = 2, sd = 1)
Y = rnorm(10000, mean = -2, sd = 5)
#3.a Mean of vector X
meanX = mean(X)
meanX
#3.a Standard Deviation of vector X
sdX = sd(X)
sdX
#3.a Mean of vector Y
meanY = mean(Y)
meanY
#3.a Standard Deviation of vector Y
sdY = sd(Y)
sdY
#3.b
W=X+Y
MeanW = mean(W)
MeanW
#3.b
sdW = sd(W)
sdW
#3.c
ProbW = sum(W>3)/length(W)
ProbW
#3.d
# mean error for the vector X
MEX = (mean(X)-2)
MEX
#3.d SD error for the vector X
SDEX = (sd(X)-1) 
SDEX
# 3.d mean error for the vector Y
MEY = (mean(Y)+2) 
MEY
# 3.d SD error for the vector Y
SDEY = (sd(Y)-5) 
SDEY
#3.d mean error for the vectpr W
MEW = mean(W)-(2-2)
MEW
#3.d SD error for the vector W
SDEW = sd(W)-(sqrt(1*1)+sqrt(5*5))
SDEW
#3d W prob error
WPE=ProbW - (1- pnorm(3/sqrt(25)))
WPE