<?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: question on maximun likelihood function in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/229108#M2376</link>
    <description>&lt;P&gt;I just realized what you are doing. I believe this is a probit regression model for the response variable D. The following call to PROC PROBIT gives the same answers if you&amp;nbsp;switch the negative sign on the loglikelihood function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc probit data=q1;
model d=z;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 08 Oct 2015 13:28:35 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2015-10-08T13:28:35Z</dc:date>
    <item>
      <title>question on maximun likelihood function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/228513#M2369</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;I am extremely new to sas, and I am working on a labor economics problem with sas.&lt;/P&gt;&lt;P&gt;the question is to generate a fake sample of observations and code the likelihood function assiciate with the problem.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;matlab code of the likelihood function is the following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;function [loglikelihood] = ps1q1_llk(beta, D, X)&lt;BR /&gt;pr = normcdf(X*beta);&lt;BR /&gt;loglikelihood = -sum(D.*log(pr) + (1-D).*log(1-pr));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;my sas cod is the following:&lt;/P&gt;&lt;P&gt;proc iml;&lt;BR /&gt;start loglike(param) global (x); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; (param is beta in the previous, and x includes D, X)&lt;BR /&gt;x1 = x[,{1 2}]; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; (x1 is X, and x3 is D)&lt;BR /&gt;x3 = x[,3];&lt;BR /&gt;pr = cdf("normal",x1*t(param));&lt;BR /&gt;pr2 = 1 - pr;&lt;BR /&gt;f = -(t(x3)*log(pr) + t(1-x3)*log(pr2));&lt;BR /&gt;return ( f );&lt;BR /&gt;finish;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I recevie error&amp;nbsp;&lt;SPAN&gt;Invocation of unresolved module LOGLIK, so i messed up in the likelihoood function. could someone plz help me with it? If you somehow need more info, I would like to provide in details.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Yours,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Frank&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Oct 2015 20:00:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/228513#M2369</guid>
      <dc:creator>frank021227</dc:creator>
      <dc:date>2015-10-05T20:00:45Z</dc:date>
    </item>
    <item>
      <title>Re: question on maximun likelihood function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/228518#M2370</link>
      <description>&lt;P&gt;Welcome to SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It doesn't look like you messed up the function, just the call. The function name is "loglike" but the error that you posted says "loglik" (without an 'e') was not found.&amp;nbsp;If I'm wrong, please also include the code that calls your function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are some other ideas.&lt;/P&gt;
&lt;P&gt;1) You can put multiple variables in the GLOBAL statement, so maybe rewrite your function like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;start loglike(beta) global (x, D);     
   pr = cdf("normal",x*t(beta));
   pr2 = 1 - pr;
   f = -(t(D)*log(pr) + t(1-D)*log(pr2));
   return ( f );
finish;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2) If you want the function to look more like your MATLAB prototype, you can use '#' to do elementwise multiplication.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;f = -sum(D#log(pr) + (1-D)#log(pr2));
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; Personally I like the way you wrote it, but the choice is yours.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3) &amp;nbsp;You say that you are eventually trying to simulate data.&amp;nbsp; You might search for topics that interest you on &lt;A href="http://blogs.sas.com/content/iml/" target="_self"&gt;The DO Loop blog&lt;/A&gt;, which has a lot of simulation topics.&amp;nbsp; Click the "Simulation" link in the word cloud in the right-hand sidebar.&lt;/P&gt;
&lt;P&gt;4) If you are going to need to do a lot of simulation, you might consider the book &lt;EM&gt;Simulating Data with SAS&lt;/EM&gt; (2013),&lt;/P&gt;
&lt;P&gt;5) There is a very short &lt;A href="http://blogs.sas.com/content/iml/2011/03/09/translating-a-matlab-program-into-the-sasiml-language-a-case-study.html" target="_self"&gt;MATLAB to SAS/IML Cheat Sheet&lt;/A&gt;. You might have already figured out a lot of what's there. See also these &lt;A href="http://blogs.sas.com/content/iml/2014/08/11/ten-tips-for-learning-sasiml.html" target="_self"&gt;ten tips for learning SAS/IML.&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Oct 2015 20:31:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/228518#M2370</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2015-10-05T20:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: question on maximun likelihood function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/228526#M2371</link>
      <description>&lt;P&gt;Hello Doc Wicklin,&lt;/P&gt;&lt;P&gt;I was just reading your artical&amp;nbsp;&lt;A href="http://blogs.sas.com/content/iml/2011/10/12/maximum-likelihood-estimation-in-sasiml.html," target="_blank"&gt;http://blogs.sas.com/content/iml/2011/10/12/maximum-likelihood-estimation-in-sasiml.html,&lt;/A&gt; and it has been extremely helpful. This is my data generating code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data q1;
call streaminit(1024);
do i = 1 to 1000;
z = rand("Normal", 0, 2);
v = rand("Normal", 0, 1);
if 2-z&amp;gt; v then d = 1;
else d = 0;
a = 1;
output;
end;
run;&lt;/PRE&gt;&lt;P&gt;I fixed the typo and the same error remains.&lt;/P&gt;&lt;PRE&gt;proc iml;
start loglik(param) global (x);
x1 = x[,{1 2}];
x3 = x[,3];
pr = cdf("normal",x1*t(param));
pr2 = 1 - pr;
f = -sum(t(x3)*log(pr)+t(1-x3)*log(pr2));
return ( f );
finish;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;the call code is the following:&lt;/P&gt;&lt;PRE&gt;proc iml;
use q1;
read all var {a z d} into x;
close q1;
con = { .   .,  .   .}; 
p = {-10 -10};
opt = {1,4};   
call nlpnra(rc, result, "loglik", p, opt, con);&lt;/PRE&gt;&lt;P&gt;Again, thank you very much for your help. Since I am extremely new to sas, i might make very stupid mistakes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yours,&lt;/P&gt;&lt;P&gt;Frank&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Oct 2015 20:46:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/228526#M2371</guid>
      <dc:creator>frank021227</dc:creator>
      <dc:date>2015-10-05T20:46:54Z</dc:date>
    </item>
    <item>
      <title>Re: question on maximun likelihood function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/228533#M2372</link>
      <description>&lt;P&gt;1) Delete the second PROC IML statement.&amp;nbsp; You are quitting one session (in which you defined the loglik function) and starting a second session (for which the function is not defined).&lt;/P&gt;
&lt;P&gt;2) If your linear predictor X*beta has an element that is very negative (less than about -5), the CDF function will return 0.&amp;nbsp; The LOG function is not going to like that. Similarly with large values (greater than 5).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would you like to share what you are trying to accomplish?&lt;/P&gt;</description>
      <pubDate>Mon, 05 Oct 2015 21:08:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/228533#M2372</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2015-10-05T21:08:06Z</dc:date>
    </item>
    <item>
      <title>Re: question on maximun likelihood function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/228537#M2373</link>
      <description>&lt;P&gt;Question 1. Consider the following discrete choice model:&lt;BR /&gt;D = 1[&amp;#11;a0 + &amp;#11;a1 Z &amp;gt; V ]:&lt;BR /&gt;Assume the following parameterization of the model: Z~&amp;#24; N(0; 2), V &amp;#24;~N(0; 1), &amp;#11;a0 = 2, and &amp;#11;a1 = -􀀀1.&lt;BR /&gt;(a) Using this parameterization, generate a fake sample of 1,000 observations.&lt;BR /&gt;(b) Code the likelihood function associated with this problem.&lt;/P&gt;&lt;P&gt;so use given a0 a1 to generate a fake sample of z v and d. then use that sample to estimate a0 and a1. I do notice that log(0) might be a problem. but i did a test that put in value for para and x, and i could calculate f with the likelihood function.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Oct 2015 21:51:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/228537#M2373</guid>
      <dc:creator>frank021227</dc:creator>
      <dc:date>2015-10-05T21:51:15Z</dc:date>
    </item>
    <item>
      <title>Re: question on maximun likelihood function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/228552#M2374</link>
      <description>&lt;P&gt;quick update.&amp;nbsp;&lt;/P&gt;&lt;DIV class="sasSource"&gt;call nlpnra(rc, result, "loglik", p, opt, con);&lt;/DIV&gt;&lt;DIV class="sasError"&gt;ERROR: (execution) Invalid argument to function.&lt;/DIV&gt;&lt;DIV class="sasError"&gt;It seems that sometimes 1-pr is too close to 0.&lt;/DIV&gt;</description>
      <pubDate>Mon, 05 Oct 2015 23:57:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/228552#M2374</guid>
      <dc:creator>frank021227</dc:creator>
      <dc:date>2015-10-05T23:57:43Z</dc:date>
    </item>
    <item>
      <title>Re: question on maximun likelihood function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/228669#M2375</link>
      <description>&lt;P&gt;I guess you'll have to truncate the smallest and largest values of the probabilities so that they are firmly within the interior of (0, 1);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   pr  = pr &amp;lt;&amp;gt; 1e-8;   /* bound probability away from 0 and 1 */
   pr2 = pr2 &amp;lt;&amp;gt; 1e-8; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The symbol "&amp;lt;&amp;gt;" is the &lt;A href="http://blogs.sas.com/content/iml/2014/12/15/elementwise-minmax-operators.html" target="_self"&gt;elementwise maximum operator.&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think another problem you are having is that you are maximizing the function in NLPNRA, but you've put a negative sign in the loglik function.&amp;nbsp; Thus your optimization will go off to infinity.&amp;nbsp; It also appears that the log-likelihood is pretty flat for those simulated data. You might try constraining the parameters to a large region&amp;nbsp; near the origin, like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;start loglik(param) global (x);
   x1 = x[,{1 2}];
   d = x[,3];
   y = X1*t(param);
   pr = cdf("normal", y);
   pr2 = 1 - pr;
   pr  = pr &amp;lt;&amp;gt; 1e-8;   /* bound probability away from 0 and 1 */
   pr2 = pr2 &amp;lt;&amp;gt; 1e-8; 
   f = -sum(d#log(pr)+(1-d)#log(pr2));
   return ( f );
finish;

con = { 0   -6 ,  
        10   1 }; 
p = {1 0.2};  
opt = {0,2};/* loglike is -LL? Minimize instead of maximize */   
call nlpnra(rc, result, "loglik", p, opt, con);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Oct 2015 16:43:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/228669#M2375</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2015-10-06T16:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: question on maximun likelihood function</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/229108#M2376</link>
      <description>&lt;P&gt;I just realized what you are doing. I believe this is a probit regression model for the response variable D. The following call to PROC PROBIT gives the same answers if you&amp;nbsp;switch the negative sign on the loglikelihood function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc probit data=q1;
model d=z;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Oct 2015 13:28:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/question-on-maximun-likelihood-function/m-p/229108#M2376</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2015-10-08T13:28:35Z</dc:date>
    </item>
  </channel>
</rss>

