# constructing variables and objectives within loops

115 views

### rmi...@umich.edu

May 13, 2009, 2:20:57 PM5/13/09
to AMPL Modeling Language
I'm trying to estimate a simple binary choice (probit) model by
maximum likelihood. The outcome variable, y, equals 1 if the latent
variable, z, is less than X*b and equals 0 otherwise. (X is a matrix
of observable variables and b is a parameter vector to be estimated.)
I assume z is a standard normal random variable. Thus, y=1 with
probability Prob(z< X*b). I have a panel of data on N individuals; I
observe each individual for T periods.

Suppose b is just a 2x1 equal to [alpha beta]'; these are the
variables I seek to estimate. But in the process I need to define a
number of auxiliary variables. In this case, that is relatively easy
to do. I define:

var Bnd {i in N, t in T} = alpha + beta*x[i,t]; # this is X*b (the
upper limit of integration)

# Now, I need to evaluate the standard normal CDF at X*b. Ideally, I
would wrap in a function, but it appears the NEOS server does not
accept pipe-in functions. So here, I just calculate it directly. The
parameters N_a1, etc. would be defined earlier in the mod file. (The
code is borrowed from John Burkardt, http://people.sc.fsu.edu/~burkardt.)

var NCDF {i in N, t in T} = 1 - (
if Bnd[i,t] > N_max then 1 else (
if Bnd[i,t]<= N_con then 0.5-Bnd[i,t]*(N_p - N_q*0.5*Bnd[i,t]^2
/(0.5*Bnd[i,t]^2 +N_a1+N_b1
/(0.5*Bnd[i,t]^2 +N_a2+N_b2
/(0.5*Bnd[i,t]^2 +N_a3)))) else
if Bnd[i,t] > N_con then N_r*exp(-0.5*Bnd[i,t]^2)
/(Bnd[i,t] + N_c1+N_d1
/(Bnd[i,t] + N_c2+N_d2
/(Bnd[i,t] + N_c3+N_d3
/(Bnd[i,t] + N_c4+N_d4
/(Bnd[i,t] + N_c5+N_d5
/(Bnd[i,t] + N_c6))))))
)
);

# Lastly, I calculate the likelihood contribution of each
observation.
var Like {i in N, t in T} = (
if y[i,t]=1 then NCDF[i,t] else
if y[i,t]=0 then 1-NCDF[i,t]
);

maximize Log_Likelihood: sum {i in N, t in T} Like[i,t];

As far as I can tell, this code works well.

Here, I do not need for loops. But in more complicated problems, it's
a lot easier for me to think in terms of loops. So I wanted to try to
re-estimate this model by building up the likelihood within a for
loop. But thus far, this has been a failure. My code is provided
below. I'm receiving the message, "Unbounded objective". In addition,
even after substitution, my output reads, "50 variables, all linear"
-- but substitution should eliminate all variables except alpha and
beta. Any help would be greatly appreciated. (sorry for such a long
email.)

var alpha := 0.25;
var beta := 0.1;

var Bnd {i in N, t in T};
var NCDF {i in N, t in T};
var Like {i in N, t in T};
var Log_Like {i in N};

for {i in N} {

for {t in T} {
let Bnd[i,t] := alpha + beta*x[i,t];

let NCDF[i,t] := 1 - (
if Bnd[i,t] > N_max then 1 else (
if Bnd[i,t]<= N_con then 0.5-Bnd[i,t]*(N_p - N_q*0.5*Bnd[i,t]^2
/(0.5*Bnd[i,t]^2 +N_a1+N_b1
/(0.5*Bnd[i,t]^2 +N_a2+N_b2
/(0.5*Bnd[i,t]^2 +N_a3)))) else
if Bnd[i,t] > N_con then N_r*exp(-0.5*Bnd[i,t]^2)
/(Bnd[i,t] + N_c1+N_d1
/(Bnd[i,t] + N_c2+N_d2
/(Bnd[i,t] + N_c3+N_d3
/(Bnd[i,t] + N_c4+N_d4
/(Bnd[i,t] + N_c5+N_d5
/(Bnd[i,t] + N_c6))))))
)
);

let Like[i,t] := (
if y[i,t]=1 then NCDF[i,t] else
if y[i,t]=0 then 1-NCDF[i,t]
);
}
}

maximize Log_Likelihood: sum {i in N, t in T} Like[i,t];

### Robert Fourer

May 15, 2009, 10:49:56 AM5/15/09

Writing the loop

for {i in N} {
for {t in T} {

...

let Like[i,t] := (
if y[i,t]=1 then NCDF[i,t] else
if y[i,t]=0 then 1-NCDF[i,t]
);
}
}

does not have the same effect as

var Like {i in N, t in T} = (
if y[i,t]=1 then NCDF[i,t] else
if y[i,t]=0 then 1-NCDF[i,t]
);

The "let" statement only assigns values to the Like variables, which are passed
to the solver as initial values but which will in general be changed as the
optimum is computed. Only the "var" statement does what you want, which is to
define the Like variables to represent expressions in the NCDF variables and to
pass those definitions to the solver.

Bob Fourer
4...@ampl.com