Question: Use R code!!!!!!! We wish to generate realizations from the following pdf using the Accept - Reject method: ( | ) = K ( -

Use R code!!!!!!!
We wish to generate realizations from the following pdf using the Accept-Reject method:
(|)=K(-x2);>0,-2x2
K is the normalizing constant so that it is a proper pdf. The value of K is not difficult to find and is
K=3432.(Also note that (|)=0 for x-2 or x>2.)
The mean of the above distribution is clearly equal to 0 as it is symmetric about 0. So the first step is
to find the variance of the above distribution, 2=V[x]=E[x2].
Recall that in the A-R method, a candidate generating distribution g(x) is needed along with a
constant M such that f(x)g(x)M for all x.
The candidate generating distribution that I want you to use is the Normal (0,2) distribution where
2 is the variance calculated above. The value of M will not be provided by the user, so you have
two choices to determine its value automatically:
You can determine its value by maximizing fxg(x) mathematically (i.e. taking derivative,
setting equal to 0, etc.) to find xm=argmaxxfxg(x) and letting M=fxmg(xm).
You can determine its value by maximizing fxg(x) empirically by searching for the value of
xm numerically. I would suggest a simple grid search, but if your background includes
knowledge of numerical optimization, then feel free to utilize it.
With the preliminaries out of the way, on to the task at hand. Write a function called "myrdist" to
generate n realizations from (|) above using the Accept-Reject method.
The function's name must be "myrdist".
It must take the following arguments (named as such):
n- The sample size; i.e. the number of realizations to generate.
Include error checking to be sure that n is a numeric scalar and greater than or
equal to 1.
If it is not an integer, then coerce into the nearest integer less than the value
given.
No default value.
beta - This is the value of .
Include error checking to be sure that beta is a numeric scalar and greater than
0.
The default value is 1.
It must return a list with the following components:
The vector of n realizations, named "realizations".
The value of M used, named "M".
The average number of iterations required per realization (i.e. the number of candidate
x' s generated divided by n), named "avg,iterations".
We wish to generate realizations from the following pdf using the Accept-Reject method:
(|)=K(-x2);>0,-2x2
K is the normalizing constant so that it is a proper pdf. The value of K is not difficult to find and is
K=3432.(Also note that (|)=0 for x-2 or x>2.)
The mean of the above distribution is clearly equal to 0 as it is symmetric about 0. So the first step is
to find the variance of the above distribution, 2=V[x]=E[x2].
Recall that in the A-R method, a candidate generating distribution g(x) is needed along with a
constant M such that f(x)g(x)M for all x.
The candidate generating distribution that I want you to use is the Normal (0,2) distribution where
2 is the variance calculated above. The value of M will not be provided by the user, so you have
two choices to determine its value automatically:
You can determine its value by maximizing fxg(x) mathematically (i.e. taking derivative,
setting equal to 0, etc.) to find xm=argmaxxfxg(x) and letting
Instructions - i should be able to copy and paste the R code and execute without having to track down aby bugs . Dont use global variables
Use R code!!!!!!! We wish to generate

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!