05-09-2014 01:05 AM
call randseed(4545); * initialize the stream (like streaminit);
x = J(5000,1,.); * pre-allocate space for random numbers;
call randgen(x,'normal',0,1); * fill x with N(0,1) deviates;
y = y + (x**2 - 3*x**3 + 5x < 1);
p = y / 5000; * MEAN acts on each matrix column;
se = sqrt(y*(1-y)/5000); * VAR, but not STD or STDERR, also acts on columns;
print "IML STEP: estimated mean of sqrt(abs(X)) is" p "with standard error" se; * use PRINT, not PUT;
I'm trying to use monte carlo integration with proc iml to estimate the probability that x**2 - 3*x**3 + 5x is less than 1. What am I doing wrong? Do loops are not allowed by the way.
start tr(x,y); * create function called tr;
/* handle bad case */
/*if x<0 then do; return (.); end; * send back missing;*/
N = nrow(x);
dx = x[2:N] - x[1:N-1];
ymean = (y[2:N] + y[1:N-1]) / 2;
return(dx` * ymean );
x = do(-2,5,0.01);
print "Integral of y over x is" ( tr(x,sin(x##2)) );
I keep receiving the (execution) invalid subscript or subscript out of range. How do I solve this problem?
05-09-2014 03:15 AM
I will take the second problem. Insert a "print N;" statement into the module tr, I suspect the value of N is not what you think it is.
05-09-2014 06:06 AM
As Ian notes, when dealing with a matrix language it is essential to always know the dimensions of your vectors and matrices.
This is a good exercise. Your professor has constructed two classic problems. For the Monte Carlo problem, look carefully at the statement
y = y + (f(x)<1));
Has y been defined? If not, you cannot use it on the right hand side of an expression.
05-09-2014 08:48 AM
You should also look up the difference between the ** operator and the ## operator. Here is the doc: