<?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: non-linear optimisation BFGS (proc nlpqn) in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/non-linear-optimisation-BFGS-proc-nlpqn/m-p/878013#M5992</link>
    <description>&lt;PRE&gt;1)
I think the problem is you are using EXP() funtion for call nlpqn() .
Variable "timelag" has value like &amp;gt;60 is too way big for EXP().
If you make it smaller, you would get convergency result, Like:

timelag=rkk;
--&amp;gt;
timelag=rkk*0.01;

2)
And "loglik=-2#log(sum(contrib#myds[3]));" should be "myds[ ,3]"  ??
&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1685360545872.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/84419iC81C53F820536A08/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1685360545872.png" alt="Ksharp_0-1685360545872.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 29 May 2023 11:42:24 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2023-05-29T11:42:24Z</dc:date>
    <item>
      <title>non-linear optimisation BFGS (proc nlpqn)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/non-linear-optimisation-BFGS-proc-nlpqn/m-p/877976#M5990</link>
      <description>&lt;P&gt;I am trying to implement a non linear optimisation for a non-hidden multi-state markov model. A similar methodology, if you are interested, can be found in the msm R package (methodology explained in &lt;A href="https://cran.r-project.org/web/packages/msm/vignettes/msm-manual.pdf)" target="_blank"&gt;https://cran.r-project.org/web/packages/msm/vignettes/msm-manual.pdf)&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the input data are composed by:&lt;/P&gt;
&lt;P&gt;TIMELAG NOCC TR1 TR2 TR3 TR4&lt;/P&gt;
&lt;P&gt;timelag is a float value indicating the timelag of the transition&lt;/P&gt;
&lt;P&gt;nocc is an integer indicating how many times the transition is happening&lt;/P&gt;
&lt;P&gt;tr1 is 0/1 depending on whether this transition is 0-0 or not&lt;/P&gt;
&lt;P&gt;tr2 is 0/1 depending on whether this transition is 0-1 or not&lt;/P&gt;
&lt;P&gt;tr3 is 0/1 depending on whether this transition is 1-0 or not&lt;/P&gt;
&lt;P&gt;tr4 is 0/1 depending on whether this transition is 1-1 or not&lt;/P&gt;
&lt;P&gt;(I need the 4 transition variables to understand with IML which record to select of the Pmatrix below)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;what I want to optimise is basically a piecewise likelihood function, for every record I will have a contribution, then I'll sum all contributions and that will be my likelihood function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I start with&amp;nbsp;take 2 values as input, here e.g.:&lt;/P&gt;
&lt;P&gt;0.3, 0.7&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then compose the Qmatrix as:&lt;/P&gt;
&lt;P&gt;-0.3 0.3 ,&lt;/P&gt;
&lt;P&gt;0.7 -0.7&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the create the Pmatrix for each record as:&lt;/P&gt;
&lt;P&gt;exp(timelag*qmat11)&amp;nbsp; &amp;nbsp; &amp;nbsp; exp(timelag*qmat11)*qmat12&lt;/P&gt;
&lt;P&gt;exp(timelag*qmat22)*qmat21&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; exp(timelag*qmat22)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then depending on which type of transition it is, I use as contribution to the likelihood the respective record in the pmatrix, e.g. if 0-0 I select Pmat11.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;since this computation has to happen for every record, I thought it might have been much simpler to do that rowwise, so instead of using 2x2 matrixes, I will have a vector with 4 elements as qmatrix for each record, a vector with 4 elements as pmatrix for each record, then sum the product between pmatrix columns and tr1 tr2 tr3 tr4 columns, in order to save only the correct contribution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
	reset noname;

	use mod10;
		read all var {WHICHA TIMELAG NOCC} into myds;
		read all var {TR1 TR2 TR3 TR4} into trds;
	close mod10;

	start mylik(x) global(myds,trds);
		qmat=shape(-x[1],nrow(myds),1)||shape(x[1],nrow(myds),1)||shape(x[2],nrow(myds),1)||shape(-x[2],nrow(myds),1);
		pmat=exp(myds[,2]#qmat[,1])||exp(myds[,2]#qmat[,1])#qmat[,2]||exp(myds[,2]#qmat[,4])#qmat[,3]||exp(myds[,2]#qmat[,4]);
		contrib=pmat[,1]#trds[,1]+pmat[,2]#trds[,2]+pmat[,3]#trds[,3]+pmat[,4]#trds[,4];
		loglik=-2#log(sum(contrib#myds[3]));
		return(loglik);
	finish mylik;

	x={0.1,0.8};

	opt={0 5 . 3};
	call nlpqn(rc,xr,"mylik",x,opt);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am here not to ask you about the contruction of pmatrix, qmatrix and selection of contrib, this part might be better of course, but it works and I am happy with it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I really need help with is the optimisation part.&lt;BR /&gt;&lt;BR /&gt;In order to mimic the methodology of the R paper I am trying to implement BFGS, with CALL NLPQN opt[4]=3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically I can't manage to get SAS to start the optimisation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help on this part? what am I missing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;below some code to mimic the input dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;
data mod10;
	call streaminit(123);
	whicha=0;
	do rky=1 to 4;
		tr1=0;
		tr2=0;
		tr3=0;
		tr4=0;
		if rky=1 then tr1=1;
		if rky=2 then tr2=1;
		if rky=3 then tr3=1;
		if rky=4 then tr4=1;
		do rkk=1 to 100;
			if rand("Uniform")&amp;gt;.5 then do;
				whicha=whicha+1;
				timelag=rkk;
				nocc=1+round(rand("Uniform")*2,1);
				output;
			end;
		end;
	end;
run;


proc iml;
	reset noname;

	use mod10;
		read all var {WHICHA TIMELAG NOCC} into myds;
		read all var {TR1 TR2 TR3 TR4} into trds;
	close mod10;


	print myds trds;

	start mylik(x) global(myds,trds);
		qmat=shape(-x[1],nrow(myds),1)||shape(x[1],nrow(myds),1)||shape(x[2],nrow(myds),1)||shape(-x[2],nrow(myds),1);
		pmat=exp(myds[,2]#qmat[,1])||exp(myds[,2]#qmat[,1])#qmat[,2]||exp(myds[,2]#qmat[,4])#qmat[,3]||exp(myds[,2]#qmat[,4]);
		contrib=pmat[,1]#trds[,1]+pmat[,2]#trds[,2]+pmat[,3]#trds[,3]+pmat[,4]#trds[,4];
		loglik=-2#log(sum(contrib#myds[3]));
		return(loglik);
	finish mylik;

	x={0.1,0.8};

	opt={0 5 . 3};
	call nlpqn(rc,xr,"mylik",x,opt);

quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 May 2023 22:02:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/non-linear-optimisation-BFGS-proc-nlpqn/m-p/877976#M5990</guid>
      <dc:creator>scan</dc:creator>
      <dc:date>2023-05-28T22:02:55Z</dc:date>
    </item>
    <item>
      <title>Re: non-linear optimisation BFGS (proc nlpqn)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/non-linear-optimisation-BFGS-proc-nlpqn/m-p/877979#M5991</link>
      <description>&lt;P&gt;I'm on vacation until Tuesday, but here are three hints:&lt;/P&gt;
&lt;P&gt;1. Double check your objective function. Usually, we compute SUM(LOG(...)) , not LOG(SUM(...)).&lt;/P&gt;
&lt;P&gt;2. Look at this blog post:&amp;nbsp;&lt;A href="https://blogs.sas.com/content/iml/2017/06/14/maximum-likelihood-estimates-in-sas.html" target="_blank"&gt;Two ways to compute maximum likelihood estimates in SAS - The DO Loop&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;3. Read&amp;nbsp;&lt;A href="https://blogs.sas.com/content/iml/2016/05/11/tips-before-optimization.html" target="_blank"&gt;Ten tips before you run an optimization - The DO Loop (sas.com)&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2023 00:18:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/non-linear-optimisation-BFGS-proc-nlpqn/m-p/877979#M5991</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2023-05-29T00:18:44Z</dc:date>
    </item>
    <item>
      <title>Re: non-linear optimisation BFGS (proc nlpqn)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/non-linear-optimisation-BFGS-proc-nlpqn/m-p/878013#M5992</link>
      <description>&lt;PRE&gt;1)
I think the problem is you are using EXP() funtion for call nlpqn() .
Variable "timelag" has value like &amp;gt;60 is too way big for EXP().
If you make it smaller, you would get convergency result, Like:

timelag=rkk;
--&amp;gt;
timelag=rkk*0.01;

2)
And "loglik=-2#log(sum(contrib#myds[3]));" should be "myds[ ,3]"  ??
&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1685360545872.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/84419iC81C53F820536A08/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1685360545872.png" alt="Ksharp_0-1685360545872.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 May 2023 11:42:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/non-linear-optimisation-BFGS-proc-nlpqn/m-p/878013#M5992</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-05-29T11:42:24Z</dc:date>
    </item>
    <item>
      <title>Re: non-linear optimisation BFGS (proc nlpqn)</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/non-linear-optimisation-BFGS-proc-nlpqn/m-p/878015#M5993</link>
      <description>Thanks, I fixed this part, rick was right, it was a mistake in the conceptual order of sum and log.&lt;BR /&gt;&lt;BR /&gt;Thanks for your support, iml is not my usual thing, but I'm becoming more and more interested in it with this exercise.&lt;BR /&gt;&lt;BR /&gt;I'm now working on the gradient function, will get back if I still run into issues.</description>
      <pubDate>Mon, 29 May 2023 11:46:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/non-linear-optimisation-BFGS-proc-nlpqn/m-p/878015#M5993</guid>
      <dc:creator>scan</dc:creator>
      <dc:date>2023-05-29T11:46:39Z</dc:date>
    </item>
  </channel>
</rss>

