function [theta, ll] = logRegressionGradientAscent(X, Y)

% rows of X are training samples
% rows of Y are corresponding 0/1 values

stepsize = .0001;
m = size(X, 1);
n = size(X, 2);

% Add in the intercept term. Matlab expands the matrix.
X(:,n + 1) = ones(m, 1);
n = n + 1;

theta = zeros(n, 1);

max_iters = 500;

for i = 1:max_iters
    grad = zeros(n, 1);
    ll(i) = 0;

%    for j = 1:m
%        hxj = sigmoid(X(j,:) * theta);
%        ll(i) = ll(i) + Y(j) * log(hxj) + (1 - Y(j)) * log(1 - hxj);
%        grad = grad + X(j, :)' * (Y(j) - hxj);
%    end

    hx = sigmoid(X * theta);
    ll(i) = sum( Y .* log(hx) + (1 - Y) .* log(1 - hx) );
    grad = X' * (Y - hx);

    theta = theta + stepsize * grad;
end

ll = ll';

return;
