Lindo Systems

! LOGIT model for deriving a score to predict
  a 0/1 outcome, e.g. default on a loan. 
  Dependent variable should be 0/1.
  LOGIT assumes score() has a standard Logistics
  distribution, thus probability that
  dependent variable i is in fact 1
  = cdflogistic( score(i));
! Keywords: LOGIT, Discriminant analysis, Scoring, Binary choice
      Credit scoring, Maximum likelihood estimation, MLE;

sets:
 obs: score;
 var: w;
 oxv(obs,var): x;
endsets
data:
! Reference: Greene, W.(1997), Econometric Analysis,
 Prentice-Hall, NJ,  ISBN 0-02-346602-2.
Soln: w = 2.826, 0.095, 2.379, -13.021;
 cbnd = 14; !Assume coefs satisfy: -cbnd < w < cbnd;
 ndep = 4; ! Which column is the dependent variable.
  w(ndep) = constant term of regression;
 obs = 1..32;
 var = gpa tuce  psi grade;
 x =   2.66 20    0    0
       2.89 22    0    0
       3.28 24    0    0
       2.92 12    0    0
       4.00 21    0    1
       2.86 17    0    0
       2.76 17    0    0
       2.87 21    0    0
       3.03 25    0    0
       3.92 29    0    1
       2.63 20    0    0
       3.32 23    0    0
       3.57 23    0    0
       3.26 25    0    1
       3.53 26    0    0
       2.74 19    0    0
       2.75 25    0    0
       2.83 19    0    0
       3.12 23    1    0
       3.16 25    1    1
       2.06 22    1    0
       3.62 28    1    1
       2.89 14    1    0
       3.51 26    1    0
       3.54 24    1    1
       2.83 27    1    1
       3.39 17    1    1
       2.67 24    1    0
       3.65 21    1    1
       4.00 23    1    1
       3.10 21    1    0
       2.39 19    1    1;

! Sequence number of observation to be held out, if any.
  Use 0 if none;
  holdout = 0;
enddata
! Put bounds on coefficients. 
  These may be restrictive;
@for(var(k): @bnd(-cbnd,w(k),cbnd ););

@for(obs(i):
  @free( score(i));
  score(i) = w(ndep) 
            + @sum(var(k)|k#ne# ndep: w(k)*x(i,k));
     );

! Maximize the log likelihood function;
! Note, @log(a + b*@exp(c*z)) is
     convex in z if a*b >= 0,
     concave if a*b <= 0;
max = @sum( obs(i)| i#ne#holdout:
      -( 1 - x(i,ndep))* score(i) 
      - @log(1 + @exp(-score(i))));