library(ggplot2)
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.
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.
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.
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.
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.
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.
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.
xData1 <- rexp(10000,2)
xData2 <- rexp(10000,2)
plotData <- data.frame(x = xData1 + xData2)
ggplot(plotData, aes(x)) + geom_density()
xData1 <- rexp(10000,2)
xData2 <- rexp(10000,2)
xData <- max(xData1, xData2)
plotData <- data.frame(x = xData)
ggplot(plotData, aes(x)) + geom_density()