BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Steve1964
Obsidian | Level 7

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

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);

Run the code, error message pops up in Log window as following.

ERROR: Too many arguments for function LOGLIK.
ERROR: NLPDD call: Error in argument module FUN.
ERROR: (execution) Unknown or error operation executed.
 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

Pleas tell me how to correct the error.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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:

start loglik(parms) global(dp,n);
mp=parms[1]; sp=parms[2]; beta=parms[3]; sq=parms[4];
...
finish;

I have two additional suggestions:

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

blc = {. 0 . 0,  /* lower bounds */
       . . . .}; /* upper bounds */
call nlpdd(xr,xc,"loglik",x0) blc=blc;

2. Before you try to optimize, you should look at the "Ten tips before you run an optimization." 

View solution in original post

2 REPLIES 2
Ksharp
Super User

Better pose it at IML forum and calling @Rick_SAS

Rick_SAS
SAS Super FREQ

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:

start loglik(parms) global(dp,n);
mp=parms[1]; sp=parms[2]; beta=parms[3]; sq=parms[4];
...
finish;

I have two additional suggestions:

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

blc = {. 0 . 0,  /* lower bounds */
       . . . .}; /* upper bounds */
call nlpdd(xr,xc,"loglik",x0) blc=blc;

2. Before you try to optimize, you should look at the "Ten tips before you run an optimization." 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 771 views
  • 0 likes
  • 3 in conversation