function [wgt,alpha] = wgt_calc (m, boost_type)
%WGT_CALC  Recover boosting weights from margin info.
%
%   [wgt,alpha] = WGT_CALC(margin);
%
%   We also allow an extra parameter boost_type to specify
%   the way the weight is calculated.
%   1: AdaBoost, 2: LogiBoost, and 3: DPBoost.

if nargin < 2, boost_type = 1; end % adaboost

% m(:,1): iteration #
% m(:,2): sum of hypothesis weights
% m(:,3:end): un-normalized margins

alpha = diff([0; m(:,2)]);

% See lemga/cost.h, the weight is prop to -deriv1/y
wgt = exp(-m(:,3:end));
switch boost_type
  case 1 % adaboost
    % do nothing
  case 2 % logiboost
    wgt = wgt ./ (1+wgt);
  case 3 % dpboost
    t = max(2, wgt);
    wgt = wgt .* t ./ (t-1);
end

wgt = wgt ./ repmat(sum(wgt,2), 1, size(wgt,2));
