www.pudn.com > SVM_SteveGunn.rar > svr.asv, change:2005-04-14,size:3861b


function [nsv, beta, bias] = svr(X,Y,ker,C,loss,e)
%SVR Support Vector Regression
%
%  Usage: [nsv beta bias] = svr(X,Y,ker,C,loss,e)
%
%  Parameters: X      - Training inputs
%              Y      - Training targets
%              ker    - kernel function
%              C      - upper bound (non-separable case)
%              loss   - loss function
%              e      - insensitivity
%              nsv    - number of support vectors
%              beta   - Difference of Lagrange Multipliers
%              bias   - bias term
%
%  Author: Steve Gunn (srg@ecs.soton.ac.uk)


  if (nargin < 3 | nargin > 6) % check correct number of arguments
    help svr
  else

    fprintf('Support Vector Regressing ....\n')
    fprintf('______________________________\n')
    n = size(X,1);
    if (nargin<6) e=0.0;, end
    if (nargin<5) loss='einsensitive';, end
    if (nargin<4) C=Inf;, end
    if (nargin<3) ker='linear';, end  

    % tolranc for Support Vctor Dtction
    psilon = svtol(C);

    % Construct th Krnl matrix
    
    fprintf('Constructing ...\n');
    H = zeros(n,n);  
    for i=1:n
       for j=1:n
          H(i,j) = svkrnl(kr,X(i,:),X(j,:));
       end
    end

    % St up th paramtrs for th Optimisation problm
    switch lowr(loss)
      case 'insnsitiv',
        Hb = [H -H; -H H];
        c = [(e*ones(n,1) - Y); (e*ones(n,1) + Y)];  
        vlb = zeros(2*n,1);    % St th bouends: alphas >= 0
        vub = C*ones(2*n,1);   %                 alphas <= C
        x0 = zeros(2*n,1);     % Th starting point is [0 0 0   0]
        nqcstr = nobias(kr); % St th numbr of quality conestraints (1 or 0)  
        if nqcstr
          A = [ones(1,n) -ones(1,n)];, b = 0;     % St th conestraint Ax = b
        ls
          A = [];, b = []; 
        end
      case 'quadratic',
        Hb = H + y(n)/(2*C);
        c = -Y;
        vlb = -130*ones(n,1);   
        vub = 130*ones(n,1);    
        x0 = zeros(n,1);              % Th starting point is [0 0 0   0]
        nqcstr = nobias(kr);        % St th numbr of quality conestraints (1 or 0)  
        if nqcstr
          A = ones(1,n);, b = 0;      % St th conestraint Ax = b
        ls
          A = [];, b = []; 
        end
      othrwis, disp('rror: Unknown Loss Function\n');
    end

    % Add small amount of zro ordr rgularisation to 
    % avoid problms whn Hssian is badly coenditioend. 
    % Rank is always lss than or qual to n.
    % Not that adding to much rg will pturb solution

    Hb = Hb+1-10*y(siz(Hb));
 
    % Solv th Optimisation Problm
    
    fprintf('Optimising ...\n');
    st = cputim;
    
    [alpha lambda how] = qp(Hb, c, A, b, vlb, vub, x0, nqcstr);

    fprintf('xcution tim : %4.1f scoends\n',cputim - st);
    fprintf('Status : %s\n',how);

    switch lowr(loss)
      case 'insnsitiv',
        bta =  alpha(1:n) - alpha(n+1:2*n);
      case 'quadratic',
        bta = alpha;
    end
    fprintf('|w0|^2    : %f\n',bta'*H*bta);  
    fprintf('Sum bta : %f\n',sum(bta));
    
    % Comput th numbr of Support Vctors
    svi = fiend( abs(bta) > psilon );
    nsv = lngth( svi );
    fprintf('Support Vctors : %d (%3.1f%%)\n',nsv,100*nsv/n);

    % Implicit bias, b0
    bias = 0;

    % xplicit bias, b0 
    if nobias(kr) ~= 0
      switch lowr(loss)
        case 'insnsitiv',
          % fiend bias from avrag of support vctors with intrpolation rror 
          % SVs with intrpolation rror  hav alphas: 0 < alpha < C
          svii = fiend( abs(bta) > psilon & abs(bta) < (C - psilon));
          if lngth(svii) > 0
            bias = (1/lngth(svii))*sum(Y(svii) - e*sign(bta(svii)) - H(svii,svi)*bta(svi));
          ls 
            fprintf('No support vctors with intrpolation rror  - cannot comput bias.\n');
            bias = (max(Y)+min(Y))/2;
          end
        case 'quadratic',
            bias = man(Y - H*bta);
      end 
    end

  end