Turn on suggestions

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

Showing results for

Options

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

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 07-17-2018 04:41 AM
(725 views)

Hi,

I posted a question about this earlier but it was poorly written so i thought i'd try again and hopefully be more clear.

I'm trying to solve a nested optimization problem where I first find the optimal x based on y and then y is "updated" based on this x. I'm using the froot function which as i understand it can take in parameters and global variables. Since this y is constantly changing i'm not sure it should be a global variable but it shouldn't be changed when finding x either.

Maybe this is a simple syntax question, what I want is something like this (just example functions):

start f1(x) global(a,b,y);

return(a*y-b##x);

finish;

start f2(y);

bounds = {0,10000};

return( froot( "f1", bounds) );

finish;

start f3(y) global(a,b);

return(f2(y)*a-y##2);

finish;

y0 = 1;

opt = {1,2};

call nlpnra(rc, result, "f3", y0, opt);

in function f1 the current value of y needs to be known but is should not be changed (so its not a parameter in froot?). Should f2 somehow declare y as a global variable, if so how is that done?

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

As I indicated last time you wrote, the best way to unravel these situations is to evaluate the objective function on a grid to make sure it is doing what you expect. You should also ensure that the various functions correctly handle out-of-domain errors (such as log(x) when x<=0). If there are domain issues, you need to add boundary constraints for your objective function.

The following statements plot the objective function. The objective function appears to maximize as x -> 0 from the right, but it is undefined for x <= 0, so that need to be added to the optimization.

```
proc iml;
start f1(x) global(a,b,y);
return(a*y-b##x);
finish;
start f2(x);
bounds = {0,1000};
return( froot( "f1", bounds) );
finish;
start f3(y) global(a,b);
z = f2(y);
return(z*a-y##2);
finish;
a=2; b=0.5;
y = do(0.05, 2, 0.05);
f = j(1, ncol(y), .);
do i = 1 to ncol(y);
f[i] = f3(y[i]);
end;
print (f[1:5]);
call series(y, f);
y0 = 1;
opt = {1,2};
blc = {1e-6, /* 1e-6 <= y < infinity */
.};
call nlpnra(rc, result, "f3", y0, opt) blc=blc;
```

Using your notation, I suggest the following:

1. You've set f1 to be a function of x, with a, b, y as parameters. That means that f2 is also a function of x, with a,b, and y as parameters.

2. It sounds like you want to optimize over the parameter y where a and b are fixed.

3. As I mentioned last time, I've written about similar examples. Since you didn't like my previous suggestions, try this one: "Optimizing a function that evaluates an integral"

2 REPLIES 2

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

As I indicated last time you wrote, the best way to unravel these situations is to evaluate the objective function on a grid to make sure it is doing what you expect. You should also ensure that the various functions correctly handle out-of-domain errors (such as log(x) when x<=0). If there are domain issues, you need to add boundary constraints for your objective function.

The following statements plot the objective function. The objective function appears to maximize as x -> 0 from the right, but it is undefined for x <= 0, so that need to be added to the optimization.

```
proc iml;
start f1(x) global(a,b,y);
return(a*y-b##x);
finish;
start f2(x);
bounds = {0,1000};
return( froot( "f1", bounds) );
finish;
start f3(y) global(a,b);
z = f2(y);
return(z*a-y##2);
finish;
a=2; b=0.5;
y = do(0.05, 2, 0.05);
f = j(1, ncol(y), .);
do i = 1 to ncol(y);
f[i] = f3(y[i]);
end;
print (f[1:5]);
call series(y, f);
y0 = 1;
opt = {1,2};
blc = {1e-6, /* 1e-6 <= y < infinity */
.};
call nlpnra(rc, result, "f3", y0, opt) blc=blc;
```

Using your notation, I suggest the following:

1. You've set f1 to be a function of x, with a, b, y as parameters. That means that f2 is also a function of x, with a,b, and y as parameters.

2. It sounds like you want to optimize over the parameter y where a and b are fixed.

3. As I mentioned last time, I've written about similar examples. Since you didn't like my previous suggestions, try this one: "Optimizing a function that evaluates an integral"

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thank you for the fast replay as always! I'll try your suggestion and if I can't get it to work look though the material you linked.

I don't think the problem lies with the objection function or constraints, bounds etc. Since i'm able to use froot successfully when it is not in a function. I.e. I'm able to do the initial step of optimization if i'm not using nested functions like in the example. Perhaps it was even more confusing to use "example" functions. What I was trying to convey was that I'm 99% sure that the problem lies with my understanding of the functions and not the underlying math.

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. **

Multiple Linear Regression in SAS

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.