BookmarkSubscribeRSS Feed
garfieldmadness
Calcite | Level 5

    proc iml;

    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.

proc iml;

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 );

finish tr;

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?

3 REPLIES 3
IanWakeling
Barite | Level 11

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.

Rick_SAS
SAS Super FREQ

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.

Good luck!

Rick_SAS
SAS Super FREQ

You should also look up the difference between the ** operator and the ## operator. Here is the doc:

SAS/IML(R) 13.1 User's Guide