error: variable not initialized

new, 10000;
screen on;
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;
if minc(minc(oshare))==0;
goto again;

draw = rndn(np,nd); @np x [email protected]
draw = draw.*.ones(1,nalt); @np x (Jxnd)@
draw = reshape(draw,np*nalt,nd); @(np x J) x [email protected]
draw = ones(times,1).*.draw; @(np x JT) x [email protected]

w2dr = rndn(nalt,nd2); @J x [email protected]
w2dr = ones(np*times,1).*.w2dr; @(np x JT) x [email protected]

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;
@[email protected]
@[email protected]
proc lik_fcn(prmtr);
local bb,sigb,dr,sim_prob,sim_prob2,xmat,gmat,z1,z2,k2,k,

bb = prmtr[1:4];
sigb = prmtr[5];
@bb = 1|1|1|1; sigb=1;@

dr = draw*sigb; @(np x JT) x [email protected]

sim_prob = zeros(np*times,nalt);
sim_prob2 = zeros(np*times,1); @nobs x [email protected]

/*initialize variables*/
ev = zeros(np*nalt*times,1); @(np x JT) x [email protected]
ev2 = zeros(np*times,nalt); @(np x T) x [email protected]
denom = zeros(np*times,1); @(np x T) x [email protected]
prob = zeros(np*times,nalt); @(np x T) x [email protected]
sim_prob0 = zeros(np*times,1); @(np x T) x [email protected]
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 [email protected]
lik = zeros(np*times,1); @(np x T) x [email protected]
sim_prob2 = zeros(np*times,1); @(np x times) x [email protected]

xmat = ones(np,1).*.xmat; @(np x JT) x [email protected]
gmat = ones(np,1).*.gmat; @(np x JT) x [email protected]
z1 = ones(np,1).*.zmat[.,1];
z2 = ones(np,1).*.zmat[.,2]; @(np x JT) x [email protected]

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 [email protected]
ev2 = reshape(ev,np*times,nalt);
denom = sumc(ev2');
prob = ev2./denom; @(np x T) x [email protected]
sim_prob = sim_prob + prob; /*not apply choice yet */
k = k+1;

sim_prob = sim_prob/nd; @(np x T) x [email protected]
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 [email protected]
lik = ln(const)+sumc((ln(sim_prob1.*choice).*n)'); @(np x T) x [email protected]
sim_prob2 = sim_prob2+lik; @(np x T) x [email protected]
sim_prob2 = sim_prob2/nd2;
k2 = k2+1;

@[email protected]
@[email protected]
local xmat,eta,share,t,prcoef,comat,c,xt,ejt,w1,w2,z1,z2,zmat,gmat,x1,x2,hhshare,

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 [email protected]
x1 = z1 + z2 + w1; @JT x [email protected]
x2 = rndu(nalt*times,1).>0.7; @JT x [email protected]
c = ones(times,1).*.(1~0|0~1);@JT x [email protected]
xmat = c ~ x1 ~ x2; @JT x [email protected]
eta = w1+w2; @JT x [email protected]
zmat = z1~z2; @JT x [email protected]
gmat = c[.,1]+c[.,2]+w1+x2; @JT x [email protected]

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))~
comat = ones(1,nalt).*.comat; @np x (4xJ)@
comat = reshape(comat,np*nalt,nreg); @(np x J) x [email protected]

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;

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;

hhshare[.,t] = ishare*seqa(1,1,nalt+1);
s = share[.,2:nalt+1]./np;
s = reshape(s,nalt*times,1);
@[email protected]

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


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 [email protected]

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 [email protected]

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.

You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.

Try GAUSS for 14 days for FREE

See what GAUSS can do for your data

© Aptech Systems, Inc. All rights reserved.

Privacy Policy