Question: 1 . The double exponential function has density fL ( x ) = theta 2 exp ( theta | x | ) for
The double exponential function has density
fLx
theta
exptheta x
for theta
a Derive an algorithm to generate variates from a doubleexponential distribution using
the inverse CDF method.
b Derive an acceptreject algorithm for obtaining samples from the N distribution
using the double exponential with theta as a proposal. Make sure that you use the
optimal enveloping constant.
c Implement a function in R that generates variates from the N distribution using
your acceptreject algorithm. Use it for generating variates. Plot some interesting
plots histogram density, etc. Test your samples for normality using a Kolmogorov
Smirnov test kstest see R help Comment your results.
Implement your acceptreject standard normal sampler in C You will have to implement
a function rnormal with prototype,
double rnormal;
that generates a single draw from a standard normal distribution using your rejection algorithm.
For this you will need to generate uniform variates. Dont use the default prng from the C
libraryrand; it is a lowquality algorithm, unsuitable for statistical applications. Instead
you will use the Mersenne twister algorithm. You can found a C implementation in the
"MersenneTwister.h file, uploaded together with this document to Canvas. To use it download
the file and save it in the same directory of your code. Include the following lines
after your "include" statements in your source code file:
#include"MersenneTwister.h
#include
MTRand rng;
These lines declare and initialize a global object called rng of class MTRand, that can be
used to generate pseudorandom numbers. To get a pseudorandom number on the interval
you can use the class method "rand within any of your functions. For example: double u;
u rngrand;
If your object rng is global, a call to rngrand will generate a different pseudorandom
number each time.
Test your code with the following main function you need to include the header file stdio.h
for this to work
int main
FILE f fopennumbersdat", w;
for int i ; i ; i
fprintffe
rnormal;
fclosef;
This program will generate normal variates using your rnorm function assuming your
function is correct! and will write them into a text file called "numbers.dat".
Load these numbers into an object in R by using the scan function read about this function
in the R help files
x scannumbersdat"
Again generate some plots and perform a test of normality. Comment your results.
Now you will write an R interface to your C code. Write a function mynorm, callable from
R using the C mechanism return type void, and pointer arguments This function should
generate an arbitrary number specified by the user of random variates using your rnormal
function, and return them to R Dont forget to include the externC declaration when
compiling with g
Write an R wrapper function to call your C function from R Use it to generate
samples and compare speed with your R implementation from the previous question using
the system.time function read the corresponding help file to learn about this function
Comment your results.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
