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?
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.
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!
You should also look up the difference between the ** operator and the ## operator. Here is the doc:
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.