Statistical programming, matrix languages, and more

How to Wrap optimization call inside a sas iml module?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

How to Wrap optimization call inside a sas iml module?

proc iml;
	x = {1,1,1,2,2,2,3,3,3};
	y = {0.5,0.6,0.5,1,1.2,1,1.5,1.5,1.6};
Start reg(x,y);
	Start SSE(alpha) global(x,y);
		m = nrow(y);
		res = j(1,m,0);
		res = ((y - (alpha[1] + x*alpha[2:ncol(alpha)]))##2)`;
		return(res);
	finish SSE;
	alpha = {0.5 30};
	m1 = nrow(y);
	optn = m1||2;
	con = {0 0,
		   . 1};
	call NLPLM(rc,xres,"SSE",alpha,optn,con);
	print rc;
	print xres;
return(xres);
finish reg;
	xres = reg(x,y);
	m1 = nrow(y);	
	xd = (j(m1,1,1) || x);
	sol = solve(xd`*xd,xd`*y); /*checking*/
	print sol;
	
quit;


		

Hi,

 

I wanted to create a Non negative least squares inside proc iml as above. I want to wrap the nlplm call inside a modue becuase the values of x and y changes in my program (ie, i do reg on diferent partitions of orginal data). I get error for this program because x and y are not defined in the module SSE needed for nlplm. How do i go about this?

 

Thank you.


Accepted Solutions
Highlighted
Solution
Monday
SAS Super FREQ
Posts: 3,304

Re: How to Wrap optimization call inside a sas iml module?

First, you need to realize that there is NO SUCH THING as a "local" or "nested" module. All modules are global in scope.

 

Second, you might want to review the rules for local and global symbols in a module.

 

Together, these rules imply that you can run the program like this:

proc iml;
Start SSE(alpha) global(g_x,g_y);
	m = nrow(g_y);
	res = j(1,m,0);
	res = ((g_y - (alpha[1] + g_x*alpha[2:ncol(alpha)]))##2)`;
	return(res);
finish SSE;

Start reg(x,y) global(g_x, g_y);
   g_x = x; g_y = y;  /* copy into global symbols */
	alpha = {0.5 30};
	m1 = nrow(y);
	optn = m1||2;
	con = {0 0,
		   . 1};
	call NLPLM(rc,xres,"SSE",alpha,optn,con);
	print rc;
	print xres;
return(xres);
finish reg;

x = {1,1,1,2,2,2,3,3,3};
y = {0.5,0.6,0.5,1,1.2,1,1.5,1.5,1.6};
xres = reg(x,y);
m1 = nrow(y);	
xd = (j(m1,1,1) || x);
sol = solve(xd`*xd,xd`*y); /*checking*/
print sol;
	

View solution in original post


All Replies
Highlighted
Solution
Monday
SAS Super FREQ
Posts: 3,304

Re: How to Wrap optimization call inside a sas iml module?

First, you need to realize that there is NO SUCH THING as a "local" or "nested" module. All modules are global in scope.

 

Second, you might want to review the rules for local and global symbols in a module.

 

Together, these rules imply that you can run the program like this:

proc iml;
Start SSE(alpha) global(g_x,g_y);
	m = nrow(g_y);
	res = j(1,m,0);
	res = ((g_y - (alpha[1] + g_x*alpha[2:ncol(alpha)]))##2)`;
	return(res);
finish SSE;

Start reg(x,y) global(g_x, g_y);
   g_x = x; g_y = y;  /* copy into global symbols */
	alpha = {0.5 30};
	m1 = nrow(y);
	optn = m1||2;
	con = {0 0,
		   . 1};
	call NLPLM(rc,xres,"SSE",alpha,optn,con);
	print rc;
	print xres;
return(xres);
finish reg;

x = {1,1,1,2,2,2,3,3,3};
y = {0.5,0.6,0.5,1,1.2,1,1.5,1.5,1.6};
xres = reg(x,y);
m1 = nrow(y);	
xd = (j(m1,1,1) || x);
sol = solve(xd`*xd,xd`*y); /*checking*/
print sol;
	
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 37 views
  • 0 likes
  • 2 in conversation