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

- 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
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

08-24-2016 02:31 PM

Hello,

I'm trying to solve a nonlinear implicit equation with one varible by using the FROOT function. The flowing is my code:

proc iml;

start f1(x) global(y);

f=(2*y - 1 - x**0.5 + (1-x)**0.5);

return(f);

finish f1;

y = rand('Uniform');

interval = {0 1};

z = froot('f1',interval);

print z;

I got a nice result. For each y, I got z.

What I'm trying to do is to generate for example a random sample of 100 random varible z by using 100 y's of random numbers that are uniformly distributed. In other words, can I use the do loop to get 100 z's using 100 y's.

If the froot function gives just one solution, what do you recommend in order to solve this equation 100 times with differnt 100 inputs y.

THANK YOU!

Accepted Solutions

Solution

08-24-2016
05:51 PM

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

Posted in reply to Medo_Aldeni

08-24-2016 03:04 PM

You are asking to solve 100 different problems (rather than one problem 100 times) because for each parameter y you get a different function that you want the root for. Thus you need to update the y parameter before each call to FROOT:

```
proc iml;
start f1(x) global(y);
return 2*y - 1 - sqrt(x) + sqrt(1-x); /* use sqrt() instead of **0.5 */
finish f1;
u = j(100, 1); /* allocate vector */
call randgen(u, 'Uniform');
z = j(100, 1); /* store answers */
interval = {0 1};
do i = 1 to nrow(u);
y = u[i]; /* set global "target" variable */
z[i] = froot('f1', interval);
end;
call scatter(u, z);
```

Are you using the inverse CDF method to simulate data. If so, see this article on the inverse CDF method.

All Replies

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

Posted in reply to Medo_Aldeni

08-24-2016 03:00 PM

Here is a simple alteration to your code that gives 100 solutions:

proc iml; start f1(x) global(y); f=(2*y - 1 - x**0.5 + (1-x)**0.5); return(f); finish f1; num_trials = 100; z = j(num_trials,1,0.0);

interval = {0 1}; do ii = 1 to num_trials; y = rand('Uniform'); z[ii,1] = froot('f1',interval); end; print z; quit;

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

Posted in reply to dougc

08-24-2016 06:09 PM

Thank you dougc. your answer is great. I appreciate your help.

Solution

08-24-2016
05:51 PM

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

Posted in reply to Medo_Aldeni

08-24-2016 03:04 PM

You are asking to solve 100 different problems (rather than one problem 100 times) because for each parameter y you get a different function that you want the root for. Thus you need to update the y parameter before each call to FROOT:

```
proc iml;
start f1(x) global(y);
return 2*y - 1 - sqrt(x) + sqrt(1-x); /* use sqrt() instead of **0.5 */
finish f1;
u = j(100, 1); /* allocate vector */
call randgen(u, 'Uniform');
z = j(100, 1); /* store answers */
interval = {0 1};
do i = 1 to nrow(u);
y = u[i]; /* set global "target" variable */
z[i] = froot('f1', interval);
end;
call scatter(u, z);
```

Are you using the inverse CDF method to simulate data. If so, see this article on the inverse CDF method.

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

Posted in reply to Rick_SAS

08-24-2016 06:05 PM

Thank you professor. I really apreaciate your help. Yes, as you said I want to simulate data but unfortunately the inverse CDF is not in closed form.