<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic SAS-IML complex optimization problem/ Huber regression:  Returning from module FF without return. in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Re-SAS-IML-complex-optimization-problem-Huber-regression/m-p/484329#M4297</link>
    <description>&lt;P&gt;Recently I see that sicikitlearn has a Huberregressor and I want to implement it using SAS. The loss function is in the picture:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2018-08-06_20-05-33.jpg" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22242iC931D1F6CD8F1D3E/image-size/large?v=v2&amp;amp;px=999" role="button" title="2018-08-06_20-05-33.jpg" alt="2018-08-06_20-05-33.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;My code is:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
	use mach.leaf_cla;
	read all var _all_ into data[colname=varNames]; 
	
	A=data[,1:ncol(data)-1];
	y=data[,ncol(data)];
	cv=j(nrow(A), 1, 1);
	A=insert(A,cv,0,ncol(A)+1);&lt;BR /&gt;
	*Using OLS estimator as the start value;
	b=solve(A`*A,A`*y);
	*Hm(x);
	start huberf(x) global(epsilon);
		if abs(x)&amp;lt;epsilon then f=x**2;
		else f=2*epsilon*abs(x)-epsilon**2;

		return(f);
	finish huberf;&lt;BR /&gt;
	*Objective function;
	start ff(x) global(A,y,lambda,l);
		xx=x[1:l-1];
		sigma=x[l];
		AX=(A*xx-y)/sigma;
		AXt=apply("huberf",AX);
		
		f=sum(sigma+AXt*sigma)+lambda*norm(x,"L2");
		return(f);
	finish ff;

	epsilon=1.35;
	lambda=1;
	l=ncol(A)+1;
	opt={0 2};
	sigma=50;
	b=insert(b,sigma,nrow(b)+1,0);
	temp=ff(b);
	print temp;
	call nlpnrr(rc, xres, "ff", b, opt);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My dataset is in the attachments.&lt;/P&gt;&lt;P&gt;But nlprr gives an error:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2018-08-06_20-09-51.jpg" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22243iB3281F310DDA55B8/image-size/large?v=v2&amp;amp;px=999" role="button" title="2018-08-06_20-09-51.jpg" alt="2018-08-06_20-09-51.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I have tried nlpcg, nlpdd and nothing works. It's strange that I have tried &lt;STRONG&gt;ff(b)&lt;/STRONG&gt; using start value and it give correct result.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2018-08-06_20-14-06.jpg" style="width: 596px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22244i43A117CA5CBE4C49/image-size/large?v=v2&amp;amp;px=999" role="button" title="2018-08-06_20-14-06.jpg" alt="2018-08-06_20-14-06.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;More details:&lt;/P&gt;&lt;P&gt;&amp;nbsp;The dataset has 64 explanatory variables and 1 target variable. The model has 64+1 constant +1 sigma =66 parameters.&lt;/P&gt;</description>
    <pubDate>Mon, 06 Aug 2018 12:41:56 GMT</pubDate>
    <dc:creator>kilasue</dc:creator>
    <dc:date>2018-08-06T12:41:56Z</dc:date>
    <item>
      <title>Re: SAS-IML complex optimization problem/ Huber regression:  Returning from module FF without return</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Re-SAS-IML-complex-optimization-problem-Huber-regression/m-p/484348#M4295</link>
      <description>&lt;P&gt;Better post it at IML forum.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;is there.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope I have time to review your question .&lt;/P&gt;
&lt;P&gt;Good Luck.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 13:15:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Re-SAS-IML-complex-optimization-problem-Huber-regression/m-p/484348#M4295</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-08-06T13:15:58Z</dc:date>
    </item>
    <item>
      <title>SAS-IML complex optimization problem/ Huber regression:  Returning from module FF without return.</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Re-SAS-IML-complex-optimization-problem-Huber-regression/m-p/484329#M4297</link>
      <description>&lt;P&gt;Recently I see that sicikitlearn has a Huberregressor and I want to implement it using SAS. The loss function is in the picture:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2018-08-06_20-05-33.jpg" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22242iC931D1F6CD8F1D3E/image-size/large?v=v2&amp;amp;px=999" role="button" title="2018-08-06_20-05-33.jpg" alt="2018-08-06_20-05-33.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;My code is:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
	use mach.leaf_cla;
	read all var _all_ into data[colname=varNames]; 
	
	A=data[,1:ncol(data)-1];
	y=data[,ncol(data)];
	cv=j(nrow(A), 1, 1);
	A=insert(A,cv,0,ncol(A)+1);&lt;BR /&gt;
	*Using OLS estimator as the start value;
	b=solve(A`*A,A`*y);
	*Hm(x);
	start huberf(x) global(epsilon);
		if abs(x)&amp;lt;epsilon then f=x**2;
		else f=2*epsilon*abs(x)-epsilon**2;

		return(f);
	finish huberf;&lt;BR /&gt;
	*Objective function;
	start ff(x) global(A,y,lambda,l);
		xx=x[1:l-1];
		sigma=x[l];
		AX=(A*xx-y)/sigma;
		AXt=apply("huberf",AX);
		
		f=sum(sigma+AXt*sigma)+lambda*norm(x,"L2");
		return(f);
	finish ff;

	epsilon=1.35;
	lambda=1;
	l=ncol(A)+1;
	opt={0 2};
	sigma=50;
	b=insert(b,sigma,nrow(b)+1,0);
	temp=ff(b);
	print temp;
	call nlpnrr(rc, xres, "ff", b, opt);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My dataset is in the attachments.&lt;/P&gt;&lt;P&gt;But nlprr gives an error:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2018-08-06_20-09-51.jpg" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22243iB3281F310DDA55B8/image-size/large?v=v2&amp;amp;px=999" role="button" title="2018-08-06_20-09-51.jpg" alt="2018-08-06_20-09-51.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I have tried nlpcg, nlpdd and nothing works. It's strange that I have tried &lt;STRONG&gt;ff(b)&lt;/STRONG&gt; using start value and it give correct result.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2018-08-06_20-14-06.jpg" style="width: 596px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22244i43A117CA5CBE4C49/image-size/large?v=v2&amp;amp;px=999" role="button" title="2018-08-06_20-14-06.jpg" alt="2018-08-06_20-14-06.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;More details:&lt;/P&gt;&lt;P&gt;&amp;nbsp;The dataset has 64 explanatory variables and 1 target variable. The model has 64+1 constant +1 sigma =66 parameters.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 12:41:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Re-SAS-IML-complex-optimization-problem-Huber-regression/m-p/484329#M4297</guid>
      <dc:creator>kilasue</dc:creator>
      <dc:date>2018-08-06T12:41:56Z</dc:date>
    </item>
    <item>
      <title>Re: SAS-IML complex optimization problem/ Huber regression:  Returning from module FF without return</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Re-SAS-IML-complex-optimization-problem-Huber-regression/m-p/484366#M4296</link>
      <description>&lt;P&gt;I rewrite the objective function using&amp;nbsp;&lt;STRONG&gt;do:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;nr=nrow(A);	&lt;BR /&gt;start ff(x) global(A,y,lambda,l,nr);
	xx=x[1:l-1];
	sigma=x[l];
	f=0;
	do i=1 to nr;
	  f=f+sigma+huberf((A[i,]*xx-y[i])/sigma)*sigma;
	end;
	f=f+lambda*norm(x,"L2");
	return(f);
finish ff;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But I still can understand why my previous function caused an error. Maybe we cannot using &lt;STRONG&gt;apply()&lt;/STRONG&gt; in objective function?&lt;/P&gt;</description>
      <pubDate>Mon, 06 Aug 2018 14:05:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Re-SAS-IML-complex-optimization-problem-Huber-regression/m-p/484366#M4296</guid>
      <dc:creator>kilasue</dc:creator>
      <dc:date>2018-08-06T14:05:50Z</dc:date>
    </item>
    <item>
      <title>Re: SAS-IML complex optimization problem/ Huber regression:  Returning from module FF without return</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Re-SAS-IML-complex-optimization-problem-Huber-regression/m-p/485232#M4301</link>
      <description>&lt;P&gt;I suspect you are correct that the APPLY function is causing&amp;nbsp;the&amp;nbsp;problem, although I don't know why. I will enter a bug report with SAS R&amp;amp;D so that someone can investigate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An efficient alternative is to&amp;nbsp;&lt;A href="https://blogs.sas.com/content/iml/2011/08/15/complex-assignment-statements-choose-wisely.html" target="_self"&gt;use the CHOOSE function&lt;/A&gt;. The APPLY function is not recommended because &lt;A href="https://blogs.sas.com/content/iml/2015/03/02/avoid-apply-vectorize.html" target="_self"&gt;the APPLY function tends to be less efficient than vectorized computations.&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your case, you can avoid the loop by using the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;start huberf(x) global(epsilon);
   f = choose( abs(x)&amp;lt;epsilon, x##2, 2*epsilon*abs(x)-epsilon##2 );
   return(f);
finish huberf;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS. In your objective function, I think you meant to write NORM(xx) instead of NORM(x)?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2018 19:48:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Re-SAS-IML-complex-optimization-problem-Huber-regression/m-p/485232#M4301</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2018-08-08T19:48:52Z</dc:date>
    </item>
  </channel>
</rss>

