library(ggplot2)
  1. Five coins are tossed and the number of heads X is counted. Estimate via simulation the pmf of X.
fliptable <- (table(replicate(10000, {coinflip <- sum(sample(1:2, 5, TRUE) == TRUE)}))/10000)

This line flips 5 coins and counts the number of heads 10000 times in order to compute the pmf.

  1. Let X and Y be uniform random variables on the interval [0,1]. Estimate the pdf of X+Y.
X <- runif(10000, 0, 1)
Y <- runif(10000, 0, 1)
data <-  X+Y
plotData <-  data.frame(x = data)
ggplot(plotData, aes(x=x))+geom_density()

X and Y are random uniform variables on the specified interval and the sum of them is plotted in order to show the pdf.

  1. Let X and Y be uniform random variables on the interval [0,1]. Let Z be the maximum of X and Y.
  1. Estimate the pdf of Z.
X <- runif(10000, 0, 1)
Y <- runif(10000, 0, 1)
Z <- pmax(X, Y)
plotData <- data.frame(x = Z)
ggplot(plotData, aes(x=x))+geom_density()

X and Y are random uniform variables on the specified interval and the max of them is plotted in order to show the pdf.

  1. From your answer to (a), decide whether P(0???Z???1/3) or P(1/3???Z???2/3) is larger.
sum(Z>1/3 & Z<2/3)
## [1] 3316
sum(Z>0 & Z<1/3)
## [1] 1130

The sum of the first P is found to be 3338 and the second P is 1119, showing the first is larger.

  1. Let X1,.,Xk be uniform rv’s on the interval (0,1). How large does k need to be before the pdf of (X?????)/(??/sqrt(n)) is approximately that of a standard normal rv? (Note: there is no “right” answer, as it depends on what is meant by “approximately.” You should try various values of k until you find one where the estimate of the pdf is close to that of a standard normal.)
N <- 10
unifData <- replicate(10000, (mean(runif(N,0,1)) - 0.5)/(sqrt(1/12)/sqrt(N)))
plotData <- data.frame(x = unifData) 
xvals <- seq(-3, 3,.1)
plotData2 <- data.frame(x = xvals, y = dnorm(xvals, 0, 1))
ggplot(plotData, aes(x)) + geom_density() + geom_line(data = plotData2, mapping = aes(x = x, y = y), color = "red")

Above the variable k is shown as N, and a value of 10 is used to show that the pdf is nearly the same as a standard normal rv.

  1. Repeat the above for a chi-squared rv with 1 degree of freedom.
xvals <- seq(-3, 3,.1)
chisqData <- data.frame(x = rchisq(10000,2)+rchisq(10000,3))
chisqDensity <- data.frame(x = xvals, y = dchisq(xvals, df  =5))
plotData2 <- data.frame(x = xvals, y = dnorm(xvals, 0, 1))
ggplot(plotData, aes(x)) + geom_density() + geom_line(data = plotData2, mapping = aes(x = x, y = y), color = "red")

The same process as problem 5 is repeated with a chi squared rv with 1 degree of freedom.

  1. The sum of two independent chi-squared rv’s with 1 degree of freedom is either exponential or chi-squared. Which one is it? What is the parameter associated with your answer?
xvals <- seq(-3, 3,.1)
chisqData1 <- data.frame(x = rchisq(10000,2)+rchisq(10000,3))
chisqData2 <- data.frame(x = rchisq(10000,2)+rchisq(10000,3))
chisqData <-  chisqData1 + chisqData2
chisqDensity <- data.frame(x = xvals, y = dchisq(xvals, df  =5))
plotData2 <- data.frame(x = xvals, y = dnorm(xvals, 0, 1))
ggplot(plotData, aes(x)) + geom_density() + geom_line(data = plotData2, mapping = aes(x = x, y = y), color = "red")

The sum of two independent chi squared rvs is found to be chi squared as well.

  1. The minimum of two exponential rv’s with mean 2 is an exponential rv. Use simulation to determine what the rate is.
xData1 <- rexp(10000,2)
xData2 <- rexp(10000,2)
plotData <- data.frame(x = xData1 + xData2)
ggplot(plotData, aes(x)) + geom_density()

  1. Estimate the value of a such that P(a???Y???a+1) is maximized when Y is the maximum value of two exponential random variables with mean 2.
xData1 <- rexp(10000,2)
xData2 <- rexp(10000,2)
xData <- max(xData1, xData2)
plotData <- data.frame(x = xData)
ggplot(plotData, aes(x)) + geom_density()