turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-09-2014 01:05 AM

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

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.

Good luck!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-09-2014 08:48 AM

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