% Calculation of gradient and objective for Least Squares Regression
%
% function [obj,grad] = ls(type,w,x,y,lambda)
% w - parameter vector [d,1]
% x - examples [n,d]
% y - target values [n,1]
% lambda - regularization parameter [scalar]
% obj - objective value at w [scalar]
% grad - gradient at w [d,1]
% 
% Written by Jason Rennie, February 2005
% Last modified: Wed May 25 16:05:44 2005

function [obj,grad,hessdiag] = ls(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 = x*w-y; % [n,1]
  obj = sum(h(z)) + lambda.*w'*w./2;
  grad = x'*(hprime(z)) + lambda.*w; % [d,1]
  hessdiag = (x.^2)'*(hprimeprime(z)) + lambda; % [d,1]
  
function [ret] = h(z)
  ret = z.^2./2;

function [ret] = hprime(z)
  ret = z;

function [ret] = hprimeprime(z)
  ret = ones(size(z));

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