<?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 error in calling optimization subroutine nlpdd() in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/error-in-calling-optimization-subroutine-nlpdd/m-p/673978#M5224</link>
    <description>&lt;P&gt;I defined my own function named "loglik" to compute loglikelihood of State Space Model, and then call optimization subroutine "nlpdd()" to maximize my target function "loglik". The IML codes are following&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
m_p=0.002;sp=0.02;/*pars in the measurement eq.1*/
m_v1=0.05;m_v2=0.028;sv=0.04;/*pars in the measurement eq.2*/
beta=0.95;sq=0.03;a=0.85;/*pars in Transition*/
n=150;/*length of time series */
/*simulating state model*/
l=j(n+50,1,0);r=l;
do i=2 to nrow(l);
l[i]=0.85*l[i-1]+rannor(3432);
r[i]=0.95*r[i-1]+0.03*rannor(45345);
end;
l=l[51:n+50];r=r[51:n+50];I=exp(r);
/*generate observation Y=(Dp,V)*/
Dp=0.002*I+0.02*sqrt(I)#randfun(n,"norm");
v=0.05*I+0.028*L+0.04*sqrt(I)#randfun(n,"norm");

/*estimate the model using EKF */
  /*build the loglik function for linearized DP SSM */
start loglik(mp,sp,beta,sq) global(dp,n);/*'m' stand for 'mean' and 's' for 'sigma'*/
alp_f=j(n,1,0);sig_f=j(n,1,0);
v=j(n,1,0);F=j(n,1,0);
alp_f[1]=0;sig_f[1]=sq/(1-beta);
v[1]=dp[1]-mp*exp(alp_f[1]);
do i=2 to n;
F[i-1]=exp(2*alp_f[i-1])*sig_f[i-1]+sp##2*exp(alp_f[i-1]);
K=beta*sig_f[i-1]*exp(alp_f[i-1])/F[i-1];
L=beta-K*exp(alp_f[i-1]);
sig_f[i]=beta*sig_f[i-1]*L+sq**2;
alp_f[i]=beta*alp_f[i-1]+K*v[i-1];
v[i]=dp[i]-mp*exp(alp_f[i]);
end;
F[n]=exp(2*alp_f[n])*sig_f[n]+sp##2*exp(alp_f[n]);
loglik=-0.5*sum(log(F)+v##2/F);
return(loglik);
finish;
x0={0.1 0.1 0.1 0.1};
call nlpdd(xr,xc,"loglik",x0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Run the code, error message pops up in Log window as following.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;FONT color="#0000FF"&gt;ERROR: Too many arguments for function LOGLIK.
ERROR: NLPDD call: Error in argument module FUN.
ERROR: (execution) Unknown or error operation executed.&lt;/FONT&gt;
 operation : NLPDD at line 1009 column 1
 operands  : *LIT1067, x0
*LIT1067      1 row       1 col     (character, size 6)
 loglik
x0      1 row       4 cols    (numeric)

       0.1       0.1       0.1       0.1

 statement : CALL at line 1009 column 1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Pleas tell me how to correct the error. &lt;BR /&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 02 Aug 2020 09:08:54 GMT</pubDate>
    <dc:creator>Steve1964</dc:creator>
    <dc:date>2020-08-02T09:08:54Z</dc:date>
    <item>
      <title>error in calling optimization subroutine nlpdd()</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/error-in-calling-optimization-subroutine-nlpdd/m-p/673978#M5224</link>
      <description>&lt;P&gt;I defined my own function named "loglik" to compute loglikelihood of State Space Model, and then call optimization subroutine "nlpdd()" to maximize my target function "loglik". The IML codes are following&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
m_p=0.002;sp=0.02;/*pars in the measurement eq.1*/
m_v1=0.05;m_v2=0.028;sv=0.04;/*pars in the measurement eq.2*/
beta=0.95;sq=0.03;a=0.85;/*pars in Transition*/
n=150;/*length of time series */
/*simulating state model*/
l=j(n+50,1,0);r=l;
do i=2 to nrow(l);
l[i]=0.85*l[i-1]+rannor(3432);
r[i]=0.95*r[i-1]+0.03*rannor(45345);
end;
l=l[51:n+50];r=r[51:n+50];I=exp(r);
/*generate observation Y=(Dp,V)*/
Dp=0.002*I+0.02*sqrt(I)#randfun(n,"norm");
v=0.05*I+0.028*L+0.04*sqrt(I)#randfun(n,"norm");

/*estimate the model using EKF */
  /*build the loglik function for linearized DP SSM */
start loglik(mp,sp,beta,sq) global(dp,n);/*'m' stand for 'mean' and 's' for 'sigma'*/
alp_f=j(n,1,0);sig_f=j(n,1,0);
v=j(n,1,0);F=j(n,1,0);
alp_f[1]=0;sig_f[1]=sq/(1-beta);
v[1]=dp[1]-mp*exp(alp_f[1]);
do i=2 to n;
F[i-1]=exp(2*alp_f[i-1])*sig_f[i-1]+sp##2*exp(alp_f[i-1]);
K=beta*sig_f[i-1]*exp(alp_f[i-1])/F[i-1];
L=beta-K*exp(alp_f[i-1]);
sig_f[i]=beta*sig_f[i-1]*L+sq**2;
alp_f[i]=beta*alp_f[i-1]+K*v[i-1];
v[i]=dp[i]-mp*exp(alp_f[i]);
end;
F[n]=exp(2*alp_f[n])*sig_f[n]+sp##2*exp(alp_f[n]);
loglik=-0.5*sum(log(F)+v##2/F);
return(loglik);
finish;
x0={0.1 0.1 0.1 0.1};
call nlpdd(xr,xc,"loglik",x0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Run the code, error message pops up in Log window as following.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;FONT color="#0000FF"&gt;ERROR: Too many arguments for function LOGLIK.
ERROR: NLPDD call: Error in argument module FUN.
ERROR: (execution) Unknown or error operation executed.&lt;/FONT&gt;
 operation : NLPDD at line 1009 column 1
 operands  : *LIT1067, x0
*LIT1067      1 row       1 col     (character, size 6)
 loglik
x0      1 row       4 cols    (numeric)

       0.1       0.1       0.1       0.1

 statement : CALL at line 1009 column 1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Pleas tell me how to correct the error. &lt;BR /&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Aug 2020 09:08:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/error-in-calling-optimization-subroutine-nlpdd/m-p/673978#M5224</guid>
      <dc:creator>Steve1964</dc:creator>
      <dc:date>2020-08-02T09:08:54Z</dc:date>
    </item>
    <item>
      <title>Re: error in calling optimization subroutine nlpdd()</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/error-in-calling-optimization-subroutine-nlpdd/m-p/673981#M5225</link>
      <description>&lt;P&gt;Better pose it at IML forum and calling&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Aug 2020 11:35:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/error-in-calling-optimization-subroutine-nlpdd/m-p/673981#M5225</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-08-02T11:35:43Z</dc:date>
    </item>
    <item>
      <title>Re: error in calling optimization subroutine nlpdd()</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/error-in-calling-optimization-subroutine-nlpdd/m-p/673987#M5226</link>
      <description>&lt;P&gt;The objective function in an optimization must take ONE parameter, although the parameter can be a vector. I see that you defined x0 to be a four-element vector, so I assume you want pass in a four-element vector, like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;start loglik(parms) global(dp,n);
mp=parms[1]; sp=parms[2]; beta=parms[3]; sq=parms[4];
...
finish;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have two additional suggestions:&lt;/P&gt;
&lt;P&gt;1. If any of these parameters represent variance or standard deviation, be sure to define bounds so that the parameters are positive. For example, if the 2nd and 4th parameters must be positive, you could define&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;blc = {. 0 . 0,  /* lower bounds */
       . . . .}; /* upper bounds */
call nlpdd(xr,xc,"loglik",x0) blc=blc;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;2. Before you try to optimize, you should look at the &lt;A href="https://blogs.sas.com/content/iml/2016/05/11/tips-before-optimization.html" target="_self"&gt;"Ten tips before you run an optimization."&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Aug 2020 12:20:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/error-in-calling-optimization-subroutine-nlpdd/m-p/673987#M5226</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2020-08-02T12:20:36Z</dc:date>
    </item>
  </channel>
</rss>

