%function admit ( )

%*****************************************************************************80
%
%% ADMIT does an exercise in logistic regression on college admission data.
%
%  Modified:
%
%    03 Ocober 2018
%
  fprintf ( '\n' );
  fprintf ( 'ADMIT:' );
  fprintf ( '  Seek parameters for a logistic regression example.\n' );
  fprintf ( '  We have M examples of N-dimensional data X.\n' );
  fprintf ( '  Consider the model\n' );
  fprintf ( '    y(x) = 1 / ( 1 + exp ( - w(1) * x(1) - w(2) * x(2) - b )\n' );
  fprintf ( '  or\n' );
  fprintf ( '    y(x) = 1 / ( 1 + exp ( - w(1) * x(1) - w(2) * x(2) - w(3) * x(3) )\n' );
  fprintf ( '  where x(3) is a vector of 1''s, and w(3) is usually called b.\n' );
  fprintf ( '  Train the model using first 80 data sets.\n' );
  fprintf ( '  Test the model using last 20 data sets.\n' );
%
%  Read the data: (english, math, admit(0/1))
%
  data = load ( 'admit.txt' );
  [ m, n ] = size ( data );
  fprintf ( '\n' );
  fprintf ( '  Input data has dimensions M = %d, N = %d\n', m, n );
%
%  Normalize the data.
%
  data_min = min ( data );
  data_max = max ( data );
  for j = 1 : n
    data(:,j)= ( data(:,j) - data_min(j) ) / ( data_max(j) - data_min(j) );
  end
%
%  Extract test and training sets.
%
  xtrain(1:80,1:2) = data(1:80,1:2);
  xtrain(1:80,3) = ones(80,1);
  ytrain(1:80,1) = data(1:80,3);

  xtest(1:20,1:2) = data(81:100,1:2);
  xtest(1:20,3) = ones(20,1);
  ytest(1:20,1) = data(81:100,3);
%
%  Select alpha, and initial w's.
%
  alpha = 1.0;
  w = zeros ( 1, n );
  kmax = 1000;

  for k = 1 : kmax

    y = 1.0 ./ ( 1 + exp ( - xtrain * w' ) );

    for j = 1 : n
      w(j) = w(j) - ( alpha / m ) .* ( y - ytrain )' * xtrain(:,j);
    end

    J(k) = ( 1 / m ) * sum ( - ytrain' * log ( y ) - ( 1 - ytrain )' * log ( 1 - y ) );

  end
  fprintf ( '\n' );
  fprintf ( '  Using ALPHA = %g\n', alpha );
  fprintf ( '  Final value of J = %g\n', J(kmax) );
  fprintf ( '  Final W = (%g,%g,%g)\n', w(1:3) );
%
%  Display the data.
%
  i0 = find ( ytrain(:) == 0 );
  i1 = find ( ytrain(:) == 1 );
%
%  Display training data.
%
  clf
  hold on
  plot ( xtrain(i0,1), xtrain(i0,2), 'r.', 'markersize', 30 );
  plot ( xtrain(i1,1), xtrain(i1,2), 'b.', 'markersize', 30 );
  xlabel ( '<-- English Exam -->' );
  ylabel ( '<-- Math Exam -->' );
  title ( 'Blue = Admitted, Red = Not admitted' );
  grid ( 'on' );
  plot ( [-w(3)/w(1), (-w(3)-w(2))/w(1)], [0,1], 'k-', 'linewidth', 3 );
  grid ( 'on' )
  pause
%
%  Compare ytest to y(xtest).
%
  y = 1.0 ./ ( 1 + exp ( - xtest * w' ) );
  error_percent = 100 * sum ( abs ( y - ytest ) ) / 20;
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Average error percentage = %g\n', error_percent );

  fprintf ( 1, '\n' );
  fprintf ( 1, '   I    X1    X2    Y   Ypredict\n' );
  fprintf ( 1, '\n' );
  for i = 1 : 20
    fprintf ( 1, '  %2d  %4.2f  %4.2f  %4.2f  %4.2f\n', i, xtest(i,1), xtest(i,2), ytest(i), y(i) );
  end

  i0 = find ( y(:) == 0 );
  i1 = find ( y(:) == 1 );
  plot ( xtest(i0,1), xtest(i0,2), 'm.', 'markersize', 30 );
  plot ( xtest(i1,1), xtest(i1,2), 'c.', 'markersize', 30 );
  hold off

% return
%end
