<?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 Re: Problem IML and loop for estimation in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Problem-IML-and-loop-for-estimation/m-p/418801#M3855</link>
    <description>&lt;P&gt;The error seems to indicate that you are trying to evaluate the LOG function for an invalid parameter. Note that if P=0 then LOG(P) is undefined and if P=1 then LOG(1-P) is undefined. In your case, I think you are running into the P=1 problem&amp;nbsp;for certain values of j.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't completely understand what you are doing, but it looks like you are evaluating the logistic log-likelihood function for linear combinations&amp;nbsp;of the&amp;nbsp;variables for a grid of (X2, X3) coffficients.&amp;nbsp;For some values of j, the value of XB is so large that logistic(XB) is numerically equal to 1.&amp;nbsp; Try using a more restricted range of coefficients, such as&lt;/P&gt;
&lt;P&gt;X2=do(-1,1,0.5)`; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 06 Dec 2017 14:31:37 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2017-12-06T14:31:37Z</dc:date>
    <item>
      <title>Problem IML and loop for estimation</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Problem-IML-and-loop-for-estimation/m-p/418028#M3851</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm a student and for a school project, i need to compute many loglikelihood of a logit model based on Spector and Mazzeo's article, and this in order to produce a 3D plot.&lt;/P&gt;&lt;P&gt;So for this i created a sequence of parameters and i try to compute log likelihood for each couple of parameters and used a loop for this.&lt;/P&gt;&lt;P&gt;But programe refuses to execute at 28th iteration and i don't understand why.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data spector;
	input grade const tuce gpa ; 
cards;
0 1 20 2.66 
0 1 22 2.89 
0 1 24 3.28 
0 1 12 2.92 
0 1 17 2.86 
0 1 17 2.76 
0 1 21 2.87 
0 1 25 3.03 
0 1 20 2.63 
0 1 23 3.32 
0 1 23 3.57 
0 1 26 3.53 
0 1 19 2.74 
0 1 25 2.75 
0 1 19 2.83 
0 1 23 3.12 
0 1 22 2.06 
0 1 14 2.89 
0 1 26 3.51 
0 1 24 2.67 
0 1 21 3.10 
1 1 21 4.00 
1 1 29 3.92 
1 1 25 3.26 
1 1 25 3.16 
1 1 28 3.62 
1 1 24 3.54 
1 1 27 2.83 
1 1 17 3.39 
1 1 21 3.65 
1 1 23 4.00 
1 1 19 2.39 
;
run;


PROC IML ; 
use spector;
read all var{const tuce gpa} into X;
read all var{grade} into Y ; 
close spector;
print X Y ;

N = nrow(X);

*step 1  : beta sequence ; 
X2=do(-1.5,1.5,0.5)`;		
X3=do(1,4,0.5)`;		
nX2=nrow(X2);
nX3=nrow(X3);
print nX2, nX3;

*step 2 : beta occurence;
rX2=repeat(X2,nX3,1);
rX3=repeat(X3,nX2,1);
rcst=repeat(-10.656002,nrow(rX2),1);

print rX2, rX3;

*step 3 sort;
call sort(rX3);
print rX3;

*step 4 concat;
theta=rcst||rX2||rX3;
ntheta = nrow(theta);
print theta ntheta;

*step 5 compute loglikelihood for each sequence of beta;
*Compute log likelihood of the sample;

	do j=1 to 49;
	          XB = X*(theta`)[,j]; 		
	          expXB = exp(XB);  			
	          P = expXB/(1+expXB);		
	          logli1 = (Y#log(P))[+] ;
	          b=1-P;
	          a = 1-Y;
	          logli2 = (log(b##a))[+];
	          logli = logli1 + logli2 ; 
	          logln = logln//logli ;
	end;	

print logln;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I know that " b##a " is not a good way but it's the only one i've found.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Someone can help me please ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance for helping.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 03 Dec 2017 17:35:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Problem-IML-and-loop-for-estimation/m-p/418028#M3851</guid>
      <dc:creator>willi_m</dc:creator>
      <dc:date>2017-12-03T17:35:00Z</dc:date>
    </item>
    <item>
      <title>Re: Problem IML and loop for estimation</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Problem-IML-and-loop-for-estimation/m-p/418045#M3852</link>
      <description>&lt;P&gt;log(a * b) = log(a)&amp;nbsp;+ log(b).&lt;/P&gt;
&lt;P&gt;log(a ## b) = b * log(a)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Avoid the multiplication or exponentiation followed by log to keep the numbers smaller and more manageable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Log(a ## b) might overflow whereas b * log(a) will be better behaved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks, Rick.&amp;nbsp; I corrected my typos.&lt;/P&gt;</description>
      <pubDate>Sun, 03 Dec 2017 23:47:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Problem-IML-and-loop-for-estimation/m-p/418045#M3852</guid>
      <dc:creator>WarrenKuhfeld</dc:creator>
      <dc:date>2017-12-03T23:47:16Z</dc:date>
    </item>
    <item>
      <title>Re: Problem IML and loop for estimation</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Problem-IML-and-loop-for-estimation/m-p/418049#M3853</link>
      <description>&lt;P&gt;Warren's advice is good, although he meant to write that&lt;/P&gt;
&lt;P&gt;log(a&amp;nbsp;# b) = log(a)&amp;nbsp;+ log(b)&lt;/P&gt;
&lt;P&gt;log(a ## b) = b # log(a)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are some articles for you to read:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;A href="https://blogs.sas.com/content/iml/2017/06/14/maximum-likelihood-estimates-in-sas.html" target="_self"&gt;"Two simple ways to construct a log-likelihood function in SAS"&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="https://blogs.sas.com/content/iml/2017/06/14/maximum-likelihood-estimates-in-sas.html" target="_self"&gt;"Two ways to compute maximum likelihood estimates in SAS"&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;To evaluate the log-likelihood at on a uniform grid of points, use the EXPANDGRID function as shown in "&lt;A href="https://blogs.sas.com/content/iml/2014/06/11/initial-guess-for-optimization.html" target="_self"&gt;How to find an initial guess for an optimization"&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;To graph a surface plot, see &lt;A href="https://blogs.sas.com/content/iml/2015/10/12/create-surface-plot-sas.html" target="_self"&gt;"Create a surface plot in SAS"&lt;/A&gt;&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Mon, 04 Dec 2017 10:52:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Problem-IML-and-loop-for-estimation/m-p/418049#M3853</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-12-04T10:52:34Z</dc:date>
    </item>
    <item>
      <title>Re: Problem IML and loop for estimation</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Problem-IML-and-loop-for-estimation/m-p/418070#M3854</link>
      <description>&lt;P&gt;Yes i read this somewhere in the forum.&lt;BR /&gt;But in writting my log as&amp;nbsp;&amp;nbsp;&amp;nbsp; " b*log(a) " there is an error of compute.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;332      logli2 = (a*log(b))[+];
ERROR: (execution) Matrices do not conform to the operation.

 operation : * at line 332 column 16
 operands  : a, _TEM1001
a     32 rows      1 col     (numeric)
_TEM1001     32 rows      1 col     (numeric)

 statement : ASSIGN at line 332 column 5&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;There are no error when i write&amp;nbsp; " a#log(b) "&amp;nbsp;&amp;nbsp; and out of the loop&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;	XB = X*(theta`)[,1]; 		
	expXB = exp(XB);  			
	P = expXB/(1+expXB);		
	logli1 = (Y#log(P))[+] ;
	b=1-P;
	a = 1-Y;
	logli2 = (a#log(b))[+];   /* HERE */
	logli = logli1 + logli2 ; 
	logln = logln//logli ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But when i want to implemant this in a loop there is a problem&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;345      do j=1 to 49;
346      XB = X*(theta`)[,j];
346!                                
347      expXB = exp(XB);
347!                                
348      P = expXB/(1+expXB);
348!                               
349      logli1 = (Y#log(P))[+] ;
350      b=1-P;
351      a = 1-Y;
352      logli2 = (a#log(b))[+];
353    
354      logli = logli1 + logli2 ;
355      logln = logln//logli ;
356      end;
ERROR: (execution) Invalid argument to function.

 count     : number of occurrences is 2
 operation : LOG at line 352 column 20
 operands  : b
b     32 rows      1 col     (numeric)

 statement : ASSIGN at line 352 column 5&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Moreover, my loop is ok for a less number of iteration (13)&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;429      do j=1 to 13;             /*13 iterations    OK*/
430      XB = X*(theta`)[,j];
430!                                 
431      expXB = exp(XB);
431!                                 
432      P = expXB/(1+expXB);
432!                               
433      logli1 = (Y#log(P))[+] ;
434      b=1-P;
435      a = 1-Y;
436      logli2 = (a#log(b))[+];
437      
438      logli = logli1 + logli2 ;
439      logln = logln//logli ;
440      end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And with 14 iterations : problems&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;441      do j=1 to 14;                 /* 14 iterations PROBLEMS */
442      XB = X*(theta`)[,j];
442!                               
443      expXB = exp(XB);
443!                                
444      P = expXB/(1+expXB);
444!                                 
445      logli1 = (Y#log(P))[+] ;
446      b=1-P;
447      a = 1-Y;
448      logli2 = (a#log(b))[+];
449
450      logli = logli1 + logli2 ;
451      logln = logln//logli ;
452      end;
ERROR: (execution) Invalid argument to function.

 count     : number of occurrences is 2
 operation : LOG at line 448 column 20
 operands  : b
b     32 rows      1 col     (numeric)

 statement : ASSIGN at line 448 column 5&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So i don't understand where is the problem, and this is problematic because after i would like to compute more of 160 000 loglikelihood for my 3D plot in order to obtain a smoother graph.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2017 06:58:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Problem-IML-and-loop-for-estimation/m-p/418070#M3854</guid>
      <dc:creator>willi_m</dc:creator>
      <dc:date>2017-12-04T06:58:49Z</dc:date>
    </item>
    <item>
      <title>Re: Problem IML and loop for estimation</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Problem-IML-and-loop-for-estimation/m-p/418801#M3855</link>
      <description>&lt;P&gt;The error seems to indicate that you are trying to evaluate the LOG function for an invalid parameter. Note that if P=0 then LOG(P) is undefined and if P=1 then LOG(1-P) is undefined. In your case, I think you are running into the P=1 problem&amp;nbsp;for certain values of j.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't completely understand what you are doing, but it looks like you are evaluating the logistic log-likelihood function for linear combinations&amp;nbsp;of the&amp;nbsp;variables for a grid of (X2, X3) coffficients.&amp;nbsp;For some values of j, the value of XB is so large that logistic(XB) is numerically equal to 1.&amp;nbsp; Try using a more restricted range of coefficients, such as&lt;/P&gt;
&lt;P&gt;X2=do(-1,1,0.5)`; &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2017 14:31:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Problem-IML-and-loop-for-estimation/m-p/418801#M3855</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-12-06T14:31:37Z</dc:date>
    </item>
    <item>
      <title>Re: Problem IML and loop for estimation</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Problem-IML-and-loop-for-estimation/m-p/495729#M4366</link>
      <description>&lt;P&gt;Thanks for your response, the problems was this. The X*B&amp;nbsp; hit 1 so the log equal 0 and compute was impossible.&lt;BR /&gt;Thank you for your help too., we won the first prize at school with this.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Sep 2018 15:20:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/Problem-IML-and-loop-for-estimation/m-p/495729#M4366</guid>
      <dc:creator>willi_m</dc:creator>
      <dc:date>2018-09-14T15:20:37Z</dc:date>
    </item>
  </channel>
</rss>

