# 【模式识别】Learning To Rank之RankBoost

RankBoost的思想比较简单，是二元Learning to rank的常规思路：通过构造目标分类器，使得pair之间的对象存在相对大小关系。通俗点说，把对象组成一对对的pair，比如一组排序r1>r2>r3>r4，那可以构成pair：(r1,r2)(r1,r3),(r1,r4),(r2,r3)(r3,r4),这样的pair是正值，也就是label是1；而余下的pair如(r2,r1)的值应该是-1或0。这样一个排序问题就被巧妙的转换为了分类问题。近来CV界很多又用这种learning to rank的思想做识别问题（最早应该是这篇《Person Re-Identification by Support Vector Ranking》），也就是把识别转换为排序问题再转换为分类问题。

Pairwise的排序方法主要用RankSVM和RankBoost，这里主要说RankBoost，整体还是一个Boost的框架：

function [ rbf ] = RankBoost( X,Y,D,T )
%RankBoost implemetation of RankBoost algoritm
%   Input:
%       X - train set.
%       Y - train labels.
%       D - distribution function over X times X, it the form of 2D matrix.
%       T - number of iteration of the boosting.
%   Output:
%       rbf - Ranking Function.

rbf = RankBoostFunc(T);
% w - the current distribution in any iteration, initilize to D
w = D;
for t=1:T
tic;
fprintf('RankBoost: creating the function, iteration %d out of %d\n',t,T);
WL = getBestWeakLearner(X,Y,w);
alpha=WL.alpha;

%update the distribution
%eval the weak learnler on the set of X and Y
h=WL.eval(X);
[hlen, ~] = size(h);
tmph = (repmat(h,1,hlen) - repmat(h',hlen,1));
w=w.*exp(tmph.*alpha);
%normalize w
w = w./sum(w(:));
toc;
end
end

tmph = (repmat(h,1,hlen) - repmat(h',hlen,1));

   % tmph = (repmat(h,1,hlen) - repmat(h',hlen,1));
%w=w.*exp(tmph.*alpha);
[rows, cols] = size(w);
sumw = 0;
for r=1:rows
for c=1:cols
w(r,c) = w(r,c)*exp((h(r)-h(c))*alpha);
sumw = sumw + w(r,c);
end
end

%normalize w
%w = w./sum(w(:));
w = w./sumw;