In: Statistics and Probability
Make sure all figures are clearly labeled (e.g., Figure 1, Figure 2, etc.), and include all R code used
to answer questions.
Simulating from distributions
For each of the following subproblems:
i. Plot the distribution you’re sampling from (e.g., if Xi∼Exp(λ), plot the exponential distribution with rate λ
ii. Generate a sample of size n= 10 from the distribution, and find the sample mean.
iii. Repeat step ii. 999 times (i.e.,k= 1000), and then plot a histogram of the samples.
iv. Add a plot of the approximate normal density on top of the histogram.
v. Repeat steps ii. – iv. with sample size n= 100.
R code:
n=10
x=matrix(0,nrow=1000,ncol=n)
m=1:1000*0
for(i in 1:1000)
{
x[i,]=runif(n, min = -2, max = 2)
m[i]=mean(x[i,])
}
hist(m, density=20, breaks=20, prob=TRUE,
xlab="x-variable", ylim=c(0, 1.4),
main="normal curve over histogram when n=10")
curve(dnorm(x,mean(m),sd(m)),col="blue", lwd=2, add=TRUE,
yaxt="n")
R code:
n=100
x=matrix(0,nrow=1000,ncol=n)
m=1:1000*0
for(i in 1:1000)
{
x[i,]=runif(n, min = -2, max = 2)
m[i]=mean(x[i,])
}
hist(m, density=20, breaks=20, prob=TRUE,
xlab="x-variable", ylim=c(0, 4),
main="normal curve over histogram when n=100")
curve(dnorm(x,mean(m),sd(m)),col="blue", lwd=2, add=TRUE,
R code:
n=10
x=matrix(0,nrow=1000,ncol=n)
m=1:1000*0
for(i in 1:1000)
{
x[i,]=rgamma(n, 2,0.5)
m[i]=mean(x[i,])
}
hist(m, density=20, breaks=20, prob=TRUE,
xlab="x-variable", ylim=c(0, 0.6),
main="normal curve over histogram when n=10")
curve(dnorm(x,mean(m),sd(m)),col="blue", lwd=2, add=TRUE
R code:
n=100
x=matrix(0,nrow=1000,ncol=n)
m=1:1000*0
for(i in 1:1000)
{
x[i,]=rgamma(n, 2,0.5)
m[i]=mean(x[i,])
}
hist(m, density=20, breaks=20, prob=TRUE,
xlab="x-variable", ylim=c(0, 2),
main="normal curve over histogram when n=100")
curve(dnorm(x,mean(m),sd(m)),col="blue", lwd=2, add=TRUE,
yaxt="n")
R code:
n=10
x=matrix(0,nrow=1000,ncol=n)
m=1:1000*0
for(i in 1:1000)
{
x[i,]=rpois(n, 1)
m[i]=mean(x[i,])
}
hist(m, density=20, breaks=20, prob=TRUE,
xlab="x-variable", ylim=c(0, 2),
main="normal curve over histogram when n=10")
curve(dnorm(x,mean(m),sd(m)),col="blue", lwd=2, add=TRUE,
yaxt="n")
R code:
n=100
x=matrix(0,nrow=1000,ncol=n)
m=1:1000*0
for(i in 1:1000)
{
x[i,]=rpois(n, 1)
m[i]=mean(x[i,])
}
hist(m, density=20, breaks=20, prob=TRUE,
xlab="x-variable", ylim=c(0, 4),
main="normal curve over histogram when n=100")
curve(dnorm(x,mean(m),sd(m)),col="blue", lwd=2, add=TRUE,
yaxt="n")
R code:
n=10
x=matrix(0,nrow=1000,ncol=n)
m=1:1000*0
for(i in 1:1000)
{
x[i,]=rgeom(n, 0.2)
m[i]=mean(x[i,])
}
hist(m, density=20, breaks=20, prob=TRUE,
xlab="x-variable", ylim=c(0, 0.5),
main="normal curve over histogram when n=10")
curve(dnorm(x,mean(m),sd(m)),col="blue", lwd=2, add=TRUE,
yaxt="n")
R code:
n=100
x=matrix(0,nrow=1000,ncol=n)
m=1:1000*0
for(i in 1:1000)
{
x[i,]=rgeom(n, 0.2)
m[i]=mean(x[i,])
}
hist(m, density=20, breaks=20, prob=TRUE,
xlab="x-variable", ylim=c(0, 2),
main="normal curve over histogram when n=100")
curve(dnorm(x,mean(m),sd(m)),col="blue", lwd=2, add=TRUE,
yaxt="n")