% Calculation of gradient and objective for Modified Least Squares (binary
% classification)
%
% function [obj,grad,lossobj,regobj] = bcmls(w,x,y,lambda)
% w - parameter vector [d,1]
% x - examples [n,d]
% y - binary (0/1 or -1/+1) labels [n,1]
% lambda - regularization parameter [scalar]
% obj - objective value at w [scalar]
% gradient - gradient at w [d,1]
% lossobj - loss portion of obj [scalar]
% regobj - regularization penalty portion of obj [scalar]
% 
% Written by Jason Rennie, February 2005
% Last modified: Tue May 24 17:51:11 2005

function [obj,grad,lossobj,regobj] = bcmls(w,x,y,lambda)
  fn = mfilename;
  if nargin < 4
  end
  [n,d] = size(x);
  if (n ~= length(y))
    error('x and y dimensions don''t match');
  end
  if (d ~= length(w))
    error('x and w dimensions don''t match');
  end
  z = y.*(x*w); % [n,1]
  lossobj = sum(h(z));
  regobj = lambda.*w'*w/2.0;
  obj = lossobj + regobj;
  grad = x'*(y.*hprime(z)) + lambda.*w; % [d,1]
  
function [ret] = h(z)
  ret = ((z-1).^2).*(z<1);

function [ret] = hprime(z)
  ret = (2.*(z-1)).*(z<1);

% ChangeLog:
% 2/27/05 - Added objective calculation
