new, 10000;
screen on;
cls;
library optmum, pgraph, maxlik;
nalt = 2;
np = 100;
times = 100;
nreg = 4;
nd = 10;
nd2= 10;
niter = 1;
tru_b1 = 0.2;
tru_b2 = 0.5;
tru_b3 = -1;
tru_b4 = 1;
tru_sig1 = 1;
/* for EX2 */
tru_sig1 = 1;
tru_sig2 = 1;
tru_Sig3 = 1;
out = {};
iter = 1;
do until iter > niter;
again:
{oshare,xmat,eta,zmat,gmat,s,hhshare,choice}=datagen;
if minc(minc(oshare))==0;
goto again;
endif;
draw = rndn(np,nd); @np x nd@
draw = draw.*.ones(1,nalt); @np x (Jxnd)@
draw = reshape(draw,np*nalt,nd); @(np x J) x nd@
draw = ones(times,1).*.draw; @(np x JT) x nd@
w2dr = rndn(nalt,nd2); @J x nd2@
w2dr = ones(np*times,1).*.w2dr; @(np x JT) x nd2@
prmtr_in = (0.2|0.5|-1|1|1);
{xout,fout,gout,cout} = optmum(&lik_fcn,prmtr_in);
hessian = hessp(&lik_fcn,xout);
cov = inv(hessian);
se = sqrt(diag(cov));
out = xout~se;
iter = iter+1;
iter;
endo;
end;
@==============================================================@
@==============================================================@
proc lik_fcn(prmtr);
local bb,sigb,dr,sim_prob,sim_prob2,xmat,gmat,z1,z2,k2,k,
ev,ev2,denom,prob,sim_prob0,sim_prob1,n,const,lik,choice;
bb = prmtr[1:4];
sigb = prmtr[5];
@bb = 1|1|1|1; sigb=1;@
dr = draw*sigb; @(np x JT) x nd@
sim_prob = zeros(np*times,nalt);
sim_prob2 = zeros(np*times,1); @nobs x 1@
/*initialize variables*/
ev = zeros(np*nalt*times,1); @(np x JT) x 1@
ev2 = zeros(np*times,nalt); @(np x T) x J@
denom = zeros(np*times,1); @(np x T) x 1@
prob = zeros(np*times,nalt); @(np x T) x J@
sim_prob0 = zeros(np*times,1); @(np x T) x 1@
sim_prob1 = zeros(np*times,nalt+1); @(np x T) x (J+1)@
n = zeros(np*times,nalt+1); @(np x T) x (J+1)@
const = 0; @1 x 1@
lik = zeros(np*times,1); @(np x T) x 1@
sim_prob2 = zeros(np*times,1); @(np x times) x 1@
xmat = ones(np,1).*.xmat; @(np x JT) x 4@
gmat = ones(np,1).*.gmat; @(np x JT) x 1@
z1 = ones(np,1).*.zmat[.,1];
z2 = ones(np,1).*.zmat[.,2]; @(np x JT) x 1@
k2 = 1;
do while k2 <= nd2;
k = 1;
do while k <= nd;
ev = exp(xmat*bb+gmat+w2dr[.,k2]+(xmat[.,3].*dr[.,k])); @b22j = 1, (np x JT) x 1@
ev2 = reshape(ev,np*times,nalt);
denom = sumc(ev2');
prob = ev2./denom; @(np x T) x J@
sim_prob = sim_prob + prob; /*not apply choice yet */
k = k+1;
endo;
sim_prob = sim_prob/nd; @(np x T) x J@
sim_prob0 = ones(np*times,1) -sim_prob[.,1] - sim_prob[.,2];
sim_prob1 = sim_prob0~sim_prob; @(np x T) x (J+1)@
/*find njt's */
n = oshare.*.ones(np,1); @(np x T) x (J+1)@
/* Eqn(12) */
const = np!;
const = const/n[.,1]!.*n[.,2]!.*n[.,3]!; @(np x T) x 1@
lik = ln(const)+sumc((ln(sim_prob1.*choice).*n)'); @(np x T) x 1@
sim_prob2 = sim_prob2+lik; @(np x T) x 1@
sim_prob2 = sim_prob2/nd2;
k2 = k2+1;
endo;
retp(-sumc(sumc(sim_prob2)'));
endp;
@==============================================================@
@==============================================================@
proc(8)=datagen;
local xmat,eta,share,t,prcoef,comat,c,xt,ejt,w1,w2,z1,z2,zmat,gmat,x1,x2,hhshare,
emat,oumat,umat,fumat,k,ishare,op,maxu,j;
w1 = 0.5*rndn(nalt*times,1);
w2 = 0.5*rndn(nalt*times,1);
z1 = 0.5*rndn(nalt*times,1);
z2 = 0.5*rndn(nalt*times,1); @JT x 1@
x1 = z1 + z2 + w1; @JT x 1@
x2 = rndu(nalt*times,1).>0.7; @JT x 1@
c = ones(times,1).*.(1~0|0~1);@JT x 2@
xmat = c ~ x1 ~ x2; @JT x 4@
eta = w1+w2; @JT x 1@
zmat = z1~z2; @JT x 2@
gmat = c[.,1]+c[.,2]+w1+x2; @JT x 1@
share = zeros(times,nalt+1);
hhshare = zeros(np,times);
choice = zeros(np*times,nalt+1);
/*intialize variables*/
xt = zeros(nalt,1);
ejt = zeros(nalt,1);
oumat = zeros(np,1);
umat = zeros(nalt*np,1);
fumat = zeros(np,nalt+1);
op = zeros(1,nalt+1);
maxu = 0;
s = zeros(times,nalt);
/* Coefficients */
comat = (tru_b1*ones(np,1))~
(tru_b2*ones(np,1))~
(tru_b3*ones(np,1))+(tru_sig1*rndn(np,1))~
(tru_b4*ones(np,1));
comat = ones(1,nalt).*.comat; @np x (4xJ)@
comat = reshape(comat,np*nalt,nreg); @(np x J) x 4@
t = 1;
do while t <=times;
xt = xmat[1+nalt*(t-1):nalt*t,.];
xt = ones(np,1).*.xt;
ejt = eta[1+nalt*(t-1):nalt*t,.];
ejt = ones(np,1).*.ejt;
emat = rndu(np,nalt+1);
emat = -ln(-ln(emat));
oumat = emat[.,1];
emat = reshape(emat[.,2:nalt+1],nalt*np,1);
umat = sumc((xt.*comat)')+ejt+emat;
umat = reshape(umat,np,nalt);
fumat = oumat~umat;
k=1;
ishare = zeros(np,nalt+1);
do while k<=np;
op = fumat[k,.];
op = op';
maxu = maxc(op);
j = 1;
do while j<=nalt+1;
if fumat[k,j] == maxu; ishare[k,j]=1; else; ishare[k,j]=0; endif;
j = j+1;
endo;
k=k+1;
endo;
share[t,.]=(sumc(ishare))';
hhshare[.,t] = ishare*seqa(1,1,nalt+1);
choice[(t-1)*np+1:t*np,nalt+1];
t=t+1;
endo;
s = share[.,2:nalt+1]./np;
s = reshape(s,nalt*times,1);
retp(share,xmat,eta,zmat,gmat,s,hhshare,choice);
endp;
@==============================================================@
2 Answers
0
This code generates error messages such that
"
C:\program files\gauss13\src\optutil.src(153) : error G0152 : Variable not initialized
Currently active call: lik_fcn [153] C:\program files\gauss13\src\optutil.src
Stack trace:
lik_fcn called from C:\program files\gauss13\src\optutil.src, line 153
_optmum called from C:\program files\gauss13\src\optmum.src, line 264
optmum
"
I tried to initialize every variable that I found by pre-defining certain values before they are used indeed, but it still does not work. Could you give any advice on this problem? Any help would be greatly appreciated!
0
When I run the code that you posted, I get a "variable not initialized error from this line inside of your lik_fcn procedure:
xmat = ones(np,1).*.xmat; @(np x JT) x 4@
xmat is declared as a local variable at the top of the lik_fcn function and then it is not referenced again until the line that causes the error. So xmat is not given a value before this line:
xmat = ones(np,1).*.xmat; @(np x JT) x 4@
You need to define xmat before this line. Or if you want to use the lik_fcn to reference the global variable xmat that is returned by the datagen procedure, you need to remove xmat from the list of declared local variables at the top of the lik_fcn procedure. Then, since there is no local variable named xmat inside of lik_fcn, GAUSS will search to see if there is a global variable named xmat and will find the one that was created by the return from the call to the datagen procedure.
(Note that the same issue is present for gmat inside of lik_fcn.
Your Answer
2 Answers
This code generates error messages such that
"
C:\program files\gauss13\src\optutil.src(153) : error G0152 : Variable not initialized
Currently active call: lik_fcn [153] C:\program files\gauss13\src\optutil.src
Stack trace:
lik_fcn called from C:\program files\gauss13\src\optutil.src, line 153
_optmum called from C:\program files\gauss13\src\optmum.src, line 264
optmum
"
I tried to initialize every variable that I found by pre-defining certain values before they are used indeed, but it still does not work. Could you give any advice on this problem? Any help would be greatly appreciated!
When I run the code that you posted, I get a "variable not initialized error from this line inside of your lik_fcn procedure:
xmat = ones(np,1).*.xmat; @(np x JT) x 4@
xmat is declared as a local variable at the top of the lik_fcn function and then it is not referenced again until the line that causes the error. So xmat is not given a value before this line:
xmat = ones(np,1).*.xmat; @(np x JT) x 4@
You need to define xmat before this line. Or if you want to use the lik_fcn to reference the global variable xmat that is returned by the datagen procedure, you need to remove xmat from the list of declared local variables at the top of the lik_fcn procedure. Then, since there is no local variable named xmat inside of lik_fcn, GAUSS will search to see if there is a global variable named xmat and will find the one that was created by the return from the call to the datagen procedure.
(Note that the same issue is present for gmat inside of lik_fcn.