hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_best_subset_given_size (h05ab)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

Purpose

Given a set of m features and a scoring mechanism for any subset of those features, nag_best_subset_given_size (h05ab) selects the best n subsets of size p using a direct communication branch and bound algorithm.

Syntax

[la, bscore, bz, user, ifail] = h05ab(mincr, m, ip, nbest, f, mincnt, gamma, acc, 'user', user)
[la, bscore, bz, user, ifail] = nag_best_subset_given_size(mincr, m, ip, nbest, f, mincnt, gamma, acc, 'user', user)

Description

Given Ω=xi:i,1im, a set of m unique features and a scoring mechanism f S  defined for all S Ω  then nag_best_subset_given_size (h05ab) is designed to find So1 Ω , So1 = p , an optimal subset of size p. Here So1 denotes the cardinality of So1, the number of elements in the set.
The definition of the optimal subset depends on the properties of the scoring mechanism, if
fSi fSj , for all ​ Sj Ω ​ and ​ Si Sj (1)
then the optimal subset is defined as one of the solutions to
maximize SΩ fS   subject to   S = p  
else if
f Si fSj , for all ​ Sj Ω ​ and ​ Si Sj (2)
then the optimal subset is defined as one of the solutions to
minimize S Ω fS   subject to   S = p .  
If neither of these properties hold then nag_best_subset_given_size (h05ab) cannot be used.
As well as returning the optimal subset, So1, nag_best_subset_given_size (h05ab) can return the best n solutions of size p. If Soi denotes the ith best subset, for i=1,2,,n-1, then the i+1th best subset is defined as the solution to either
maximize S Ω - Soj : j , 1ji fS   subject to   S = p  
or
minimize S Ω - Soj : j,1ji fS   subject to   S = p  
depending on the properties of f.
The solutions are found using a branch and bound method, where each node of the tree is a subset of Ω. Assuming that (1) holds then a particular node, defined by subset Si, can be trimmed from the tree if fSi < f^Son  where f^Son  is the nth highest score we have observed so far for a subset of size p, i.e., our current best guess of the score for the nth best subset. In addition, because of (1) we can also drop all nodes defined by any subset Sj where SjSi, thus avoiding the need to enumerate the whole tree. Similar short cuts can be taken if (2) holds. A full description of this branch and bound algorithm can be found in Ridout (1988).
Rather than calculate the score at a given node of the tree nag_best_subset_given_size (h05ab) utilizes the fast branch and bound algorithm of Somol et al. (2004), and attempts to estimate the score where possible. For each feature, xi, two values are stored, a count ci and μ^i, an estimate of the contribution of that feature. An initial value of zero is used for both ci and μ^i. At any stage of the algorithm where both f S  and f S - xi  have been calculated (as opposed to estimated), the estimated contribution of the feature xi is updated to
ciμ^i + f S - f S - xj ci+1  
and ci is incremented by 1, therefore at each stage μ^i is the mean contribution of xi observed so far and ci is the number of observations used to calculate that mean.
As long as cik, for the user-supplied constant k, then rather than calculating f S - xi  this function estimates it using f^ S - xi = f S - γ μ^i  or f^ S - γ μ^i  if f S  has been estimated, where γ is a user-supplied scaling factor. An estimated score is never used to trim a node or returned as the optimal score.
Setting k=0 in this function will cause the algorithm to always calculate the scores, returning to the branch and bound algorithm of Ridout (1988). In most cases it is preferable to use the fast branch and bound algorithm, by setting k>0, unless the score function is iterative in nature, i.e., f S  must have been calculated before f S - xi  can be calculated.
nag_best_subset_given_size (h05ab) is a direct communication version of nag_best_subset_given_size_revcomm (h05aa).

References

Narendra P M and Fukunaga K (1977) A branch and bound algorithm for feature subset selection IEEE Transactions on Computers 9 917–922
Ridout M S (1988) Algorithm AS 233: An improved branch and bound algorithm for feature subset selection Journal of the Royal Statistics Society, Series C (Applied Statistics) (Volume 37) 1 139–147
Somol P, Pudil P and Kittler J (2004) Fast branch and bound algorithms for optimal feature selection IEEE Transactions on Pattern Analysis and Machine Intelligence (Volume 26) 7 900–912

Parameters

Compulsory Input Parameters

1:     mincr int64int32nag_int scalar
Flag indicating whether the scoring function f is increasing or decreasing.
mincr=1
fSi fSj , i.e., the subsets with the largest score will be selected.
mincr=0
fSi fSj , i.e., the subsets with the smallest score will be selected.
For all SjΩ and SiSj
Constraint: mincr=0 or 1.
2:     m int64int32nag_int scalar
m, the number of features in the full feature set.
Constraint: m2.
3:     ip int64int32nag_int scalar
p, the number of features in the subset of interest.
Constraint: 1ipm.
4:     nbest int64int32nag_int scalar
n, the maximum number of best subsets required. The actual number of subsets returned is given by la on final exit. If on final exit lanbest then ifail=42 is returned.
Constraint: nbest1.
5:     f – function handle or string containing name of m-file
f must evaluate the scoring function f.
[score, user, info] = f(m, drop, lz, z, la, a, user, info)

Input Parameters

1:     m int64int32nag_int scalar
m=Ω, the number of features in the full feature set.
2:     drop int64int32nag_int scalar
Flag indicating whether the intermediate subsets should be constructed by dropping features from the full set (drop=1) or adding features to the empty set (drop=0). See score for additional details.
3:     lz int64int32nag_int scalar
The number of features stored in z.
4:     zlz int64int32nag_int array
zi, for i=1,2,,lz, contains the list of features which, along with those specified in a, define the subsets whose score is required. See score for additional details.
5:     la int64int32nag_int scalar
If la>0, the number of subsets for which a score must be returned.
If la=0, the score for a single subset should be returned. See score for additional details.
6:     ala int64int32nag_int array
aj, for j=1,2,,la, contains the list of features which, along with those specified in z, define the subsets whose score is required. See score for additional details.
7:     user – Any MATLAB object
f is called from nag_best_subset_given_size (h05ab) with the object supplied to nag_best_subset_given_size (h05ab).
8:     info int64int32nag_int scalar
info=0.

Output Parameters

1:     scoremaxla,1 – double array
The value fSj , for j=1,2,,la, the score associated with the jth subset. Sj is constructed as follows:
drop=1
Sj is constructed by dropping the features specified in the first lz elements of z and the single feature given in aj from the full set of features, Ω. The subset will therefore contain m-lz-1 features.
drop=0
Sj is constructed by adding the features specified in the first lz elements of z and the single feature specified in aj to the empty set, . The subset will therefore contain lz+1 features.
In both cases the individual features are referenced by the integers 1 to m with 1 indicating the first feature, 2 the second, etc., for some arbitrary ordering of the features, chosen by you prior to calling nag_best_subset_given_size (h05ab). For example, 1 might refer to the first variable in a particular set of data, 2 the second, etc..
If la=0, the score for a single subset should be returned. This subset is constructed by adding or removing only those features specified in the first lz elements of z. If lz=0, this subset will either be Ω or .
2:     user – Any MATLAB object
3:     info int64int32nag_int scalar
Set info to a nonzero value if you wish nag_best_subset_given_size (h05ab) to terminate with ifail=82.
6:     mincnt int64int32nag_int scalar
k, the minimum number of times the effect of each feature, xi, must have been observed before f S - xi  is estimated from f S  as opposed to being calculated directly.
If k=0 then f S - xi  is never estimated. If mincnt<0 then k is set to 1.
7:     gamma – double scalar
γ, the scaling factor used when estimating scores. If gamma<0 then γ=1 is used.
8:     acc2 – double array
A measure of the accuracy of the scoring function, f.
Letting ai = ε1 fSi + ε2 , then when confirming whether the scoring function is strictly increasing or decreasing (as described in mincr), or when assessing whether a node defined by subset Si can be trimmed, then any values in the range fSi ± ai  are treated as being numerically equivalent.
If 0acc11 then ε1=acc1, otherwise ε1=0.
If acc20 then ε2=acc2, otherwise ε2=0.
In most situations setting both ε1 and ε2 to zero should be sufficient. Using a nonzero value, when one is not required, can significantly increase the number of subsets that need to be evaluated.

Optional Input Parameters

1:     user – Any MATLAB object
user is not used by nag_best_subset_given_size (h05ab), but is passed to f. Note that for large objects it may be more efficient to use a global variable which is accessible from the m-files than to use user.

Output Parameters

1:     la int64int32nag_int scalar
The number of best subsets returned.
2:     bscorenbest – double array
Holds the score for the la best subsets returned in bz.
3:     bzm-ipnbest int64int32nag_int array
The jth best subset is constructed by dropping the features specified in bzij, for i=1,2,,m-ip and j=1,2,,la, from the set of all features, Ω. The score for the jth best subset is given in bscorej.
4:     user – Any MATLAB object
5:     ifail int64int32nag_int scalar
ifail=0 unless the function detects an error (see Error Indicators and Warnings).

Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

   ifail=11
Constraint: mincr=0 or 1.
   ifail=21
Constraint: m2.
   ifail=31
Constraint: 1ipm.
   ifail=41
Constraint: nbest1.
W  ifail=42
On entry, nbest=_.
But only _ best subsets could be calculated.
   ifail=81
On exit from f, score_=_, which is inconsistent with the score for the parent node.
   ifail=82
A nonzero value for info has been returned:
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.

Accuracy

The subsets returned by nag_best_subset_given_size (h05ab) are guaranteed to be optimal up to the accuracy of the calculated scores.

Further Comments

The maximum number of unique subsets of size p from a set of m features is N= m! m-p!p! . The efficiency of the branch and bound algorithm implemented in nag_best_subset_given_size (h05ab) comes from evaluating subsets at internal nodes of the tree, that is subsets with more than p features, and where possible trimming branches of the tree based on the scores at these internal nodes as described in Narendra and Fukunaga (1977). Because of this it is possible, in some circumstances, for more than N subsets to be evaluated. This will tend to happen when most of the features have a similar effect on the subset score.
If multiple optimal subsets exist with the same score, and nbest is too small to return them all, then the choice of which of these optimal subsets is returned is arbitrary.

Example

This example finds the three linear regression models, with five variables, that have the smallest residual sums of squares when fitted to a supplied dataset. The data used in this example was simulated.
function h05ab_example


fprintf('h05ab example results\n\n');

m      = int64(14);
ip     = int64(5);
nbest  = int64(3);
mincr  = int64(0);
mincnt = int64(-1);
gamma  = -1;
acc    = [-1, -1];
mip    = m - ip;
z      = zeros(mip, 1, 'int64');
a      = zeros(max(nbest, m), 1, 'int64');
bz     = zeros(mip, nbest, 'int64');

% Data required by the scoring function
n = int64(40);
[x,y] = gen_data(n,m);

% Put x and y in user to be passed to f, along with a placeholder for the
% number of subsets evaluated
user = {x; y; 0};

% Call the forward communication best subset routine
[la, bscore, bz, user, ifail] = ...
    h05ab(...
          mincr, m, ip, nbest, @f, mincnt, gamma, acc, 'user', user);

fprintf('\n    Score        Feature Subset\n');
fprintf('    -----        --------------\n');
% Display the best subsets and corresponding scores. h05aa returns
% a list of features excluded from the best subsets, so this is inverted
% to give the set of features included in each subset
ibz = 1:m;
for i = 1:la
  mask = ones(1, m, 'int64');
  for j = 1:mip
    mask(bz(j, i)) = 0;
  end
  fprintf('%12.5e %5d %5d %5d %5d %5d\n', bscore(i), ibz(logical(mask)));
end
fprintf('\n%d subsets evaluated in total\n', user{3});



function [score, user, info] = f(m, drop, lz, z, la, a, user, info)
  % Set up the initial feature set.
  % If drop = 0, this is the Null set (i.e. no features).
  % If drop = 1 then this is the full set (i.e. all features)
  if drop == 0
    isx = zeros(m, 1, 'int64');
  else
    isx = ones(m, 1, 'int64');
  end

  % Add (if drop = 0) or remove (if drop = 1) all the features specified in z
  inv_drop = not(drop);
  for i = 1:lz
    isx(z(i)) = inv_drop;
  end

  score = zeros(max(la, 1), 1);
  for i=1:max(la, 1)
    if (la > 0)
      if (i > 1)
        % Reset the feature altered at the last iteration
        isx(a(i-1)) = drop;
      end

      %  Add or drop the i'th feature in a
      isx(a(i)) = inv_drop;
    end

    ip = int64(sum(isx));

    % Fit the regression model
    rss = g02da('z', user{1}, isx, ip, user{2});

    % Return the score (the residual sums of squares)
    score(i) = rss;
  end

  % Keep track of the number of subsets evaluated
  user{3} = user{3} + max(1, la);

function [x, y] = gen_data(n,m)
  x = zeros(n,m);
  genid = int64(3);
  subid = int64(1);
  seed(1) = int64(23124124);
  [state, ifail] = g05kf(genid, subid, seed);
  for i = 1:m
    [state, x(1:n,i), ifail] = g05sk( ...
                                      n, 0, sqrt(3), state);
  end
  [state, b, ifail] = g05sk(m, 1.5, 3, state);
  [state, y, ifail] = g05sk(n, 0, 1, state);
  y = x*b + y;
h05ab example results


    Score        Feature Subset
    -----        --------------
 1.21567e+03     3     4     6     7    14
 1.32495e+03     3     5     6     7    14
 1.37244e+03     3     5     6    12    14

337 subsets evaluated in total

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015