Simulating from a Rayleigh Distribution. The Rayleigh distribution is the distri
ID: 3201494 • Letter: S
Question
Simulating from a Rayleigh Distribution. The Rayleigh distribution is the distribution of the magnitude of a vector (X, Y) which is itself distributed as a multivariate Normal with independent components. That is, if X ~ N(0, sigma^2) and Y ~ N(0, sigma^2) are independent random variables, then Z = Squareroot X^2 + Y^2 comes from a Rayleigh distribution. The pdf of Z is as follows: f_Z (z) = z/sigma^2 e^-z^2/2sigma^2, x greaterthanorequalto 0 (a) Simulate 1000 iid random variables from a. Rayleigh distribution with sigma^2 = 2 using the inverse-CDF method. Include a written, human-readable algorithm that clearly shows how you are doing the sampling. Plot a histogram of the resulting realizations together with a curve of the density function f_Z(z). (b) Simulate 1000 iid random draws from the same distribution by using tin; "rnorm" command in R and calculating Z_i from two independent normal random variables X_i and Y_i. Plot a histogram of the resulting realizations together with a curve of the density function f_Z(z). (c) Use Monte Carlo integration to estimate the mean of a Rayleigh distribution with sigma^2 = 2. Use enough samples so that you are confident of your estimate out to at least 2 decimal places. Run the simulation and estimation procedure at least twice to verify that your estimates from the two runs are the same out to at least two decimal places.Explanation / Answer
the r code is as follows:
#a
z=array(dim=1)
for(i in 1:1000){
u=runif(1,0,1)
z[i]=sqrt(-log(1-u)*2*2*2)
}
fz<-(z*exp(-(z*z)/(2*2*2))/(2*2))
plot(z,fz)
#algorithm
#define array z
#run a loop which runs 1000 times
#draw a random sample from uniform u(0,1)
#using the inverse cdf mapping tchnique we can draw a sample by the formula
#x=sqrt(ln(1-u)*2*sigma^2)
#cdf of the distribution is= 1-e^(-x^2/2sigma^2)
#b
z=array(dim=1)
for(i in 1:1000){
x=rnorm(1,0,2)
y=rnorm(1,0,2)
z[i]=sqrt(x*x+y*y)}
fz<-(z*exp(-(z*z)/(2*2*2))/(2*2))
plot(z,fz)
#c
#run 1
sum=0
for(i in 1:10000)
{
x=rexp(1,1)
#drawing sample from exponential with mean 1
t=(x*x*exp(-x*x/(2*2*2)))/(2*2*exp(-x))
sum=sum+t
}
expectation=sum/10000
expectation
#run 2
sum2=0
for(i in 1:10000)
{
x=rexp(1,1)
#drawing sample from exponential with mean 1
t=(x*x*exp(-x*x/(2*2*2)))/(2*2*exp(-x))
sum2=sum2+t
}
expectation2=sum2/10000
expectation2