pg.362 9.3.5

5. Write a program to choose independently 25 numbers at random from [0, 20], compute their sum \(S_{25}\), and repeat this experiment 1000 times. Make a bar graph for the density of \(S_{25}\) and compare it with the normal approximation of Exercise 4. How good is the fit? Now do the same for the standardized sum \(S_{25}^*\) and the average \(A_{25}\).

Sum of 25 values from uniform distribution

# Empty vector
Vector = vector()

# 1000 trials
for(i in 1:1000){
  # choosing 25 random numbers from uniform distribution
  choices = runif(25, min = 0, max = 20)
  # sum of numbers
  S25 = sum(choices)
  # filing in the empty vector
  Vector[[i]] = S25
}

# S25
summary(Vector)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   159.4   231.8   252.0   251.1   271.0   331.6
par(mfrow = c(1,3))
# barplot of S25
barplot(Vector, main = "Barplot of S25")
# histogram of S25
hist(Vector, main = "Histogram of S25")
# density plot of S25
plot(density(Vector), main = "Density plot of S25")

Standardized sum of 25 values from uniform distribution

Define \(S_n^{*}\) as \(S_n^{*}=\frac{S_n-n\mu}{\sqrt{n}\sigma}\)

# Standardized sum of 25
Vector2 = vector()

# 1000 trials
for(i in 1:1000){
  # choosing 25 random numbers from uniform distribution
  choices = runif(25, min = 0, max = 20)
  # standardized sum of numbers
  S.star.25 = (sum(choices)-(25*mean(choices)))/(sqrt(25)*sd(choices))
  # filing in the empty vector
  Vector2[[i]] = S.star.25
}

summary(Vector2)
##       Min.    1st Qu.     Median       Mean    3rd Qu.       Max. 
## -1.435e-15  0.000e+00  0.000e+00 -4.914e-18  0.000e+00  1.254e-15
par(mfrow = c(1,3))
# barplot of S25
barplot(Vector2, main = "Barplot of Standardized S25")
# histogram of S25
hist(Vector2, main = "Histogram of Standardized S25")
# density plot of S25
plot(density(Vector2), main = "Density plot of Standardized S25")

Average of 25 values from uniform distribution

# Average of 25
Vector3 = vector()

# 1000 trials
for(i in 1:1000){
  # choosing 25 random numbers from uniform distribution
  choices = runif(25, min = 0, max = 20)
  # mean of numbers
  A.25 = mean(choices)
  # filing in the empty vector
  Vector3[[i]] = A.25
}

summary(Vector2)
##       Min.    1st Qu.     Median       Mean    3rd Qu.       Max. 
## -1.435e-15  0.000e+00  0.000e+00 -4.914e-18  0.000e+00  1.254e-15
par(mfrow = c(1,3))
# barplot of S25
barplot(Vector3, main = "Barplot of A25")
# histogram of S25
hist(Vector3, main = "Histogram of A25")
# density plot of S25
plot(density(Vector3), main = "Density plot of A25")