                 Stochastic Discrimination Utilities
                           David C. Lambert
                     Version 0.91, September 2002

================================================================================
                                INTRO
================================================================================

From [1]:
"Stochastic discrimination is a general methodology for constructing classifiers
appropriate for pattern recognition. It is based on combining arbitrary numbers
of very weak components, which are usually generated by some pseudorandom process,
and it has the property that the very complex and accurate classifiers produced
in this way retain the ability, characteristic of their weak component pieces,
to generalize to new data. In fact, it is often observed, in practice, that
classifier performance on test sets continues to rise as more weak components
are added, even after performance on training sets seems to have reached a maximum.
This is predicted by the underlying theory, for even though the formal error rate
on the training set may have reached a minimum, more sophisticated measures intrinsic
to this method indicate that classifier performance on both training and test
sets continues to improve as complexity increases."

This package is a *beta* version of Dr. Eugene Kleinberg's "Stochastic
Discrimination" algorithm.  This implementation is based on the description in [1].

[It's beta because the dump file format will probably change, and there are various
ease-of-use mods that I plan to make eventually.  It should be quite useful in
its present state though, and I haven't ever seen it crash.]

This document does not contain a description of the algorithm. Dr. Kleinberg's SD
site provides references describing Stochastic Discrimination, and has pointers to
the papers used to create this implementation:

    http://kappa.math.buffalo.edu/sd/

The makefile is very vanilla and the apps have been tested on Solaris and Linux using
g++ only.

There are some example datasets in the 'data' subdirectory; check the README in that
directory for example command lines and descriptions.

If you use these utilities in any publication, presentation, or other derivative
work, please include a citation such as the following (or some reasonable facsimile
if you use a different citation style):

    Lambert, David C. (2002). Stochastic discrimination utilities
        (Version 0.91) [Computer Program]. Chicago, IL.

and include a link to the main site for obtaining this code:

    <http://www.panix.com/~dcl>

The software is being released under the GNU Public License; see the file COPYING
included with the distribution.

Enjoy.

- David C. Lambert
  dcl _AT_ panix _DOT_ com

================================================================================
                         NOTES AND TO DO LIST
================================================================================

* I am not an academic, and I do *not* have very much time to work on this
  project.  That said, there are a couple of things that I still want to do,
  and will probably get to if I get some time:

  - improve the documentation
  - allow sets to be added to an existing model
  - include cross-validation and bias-search tools
  - include more standard data sets (but see last note below)
  - improve the output readability, and allow verbosity to be specified
  - better command line support for underlying models' parameters
  - use quasi-random sequences to generate the candidate sets in the hopes
    of accelerating convergence

* The "beta" parameter for the enrichment test is currently set to be the
  raw difference between the positive and negative frequency scores for a
  candidate weak model set.  I prefer to specify the maximum ratio of negative
  to positive coverage frequencies as the criterion. If you want to enable this,
  uncomment the line that defines "DCL_BETA" in the top of the file "SD.C" - it
  seems to have little impact on performance.

  HOWEVER, if you do this, you will reverse the relationship of beta value to
  the 'difficulty' of the enrichment test.  In the default configuration,
  increasing beta makes the enrichment test *harder* for a candidate set to
  pass; if DCL_BETA is defined, a larger beta makes the enrichment test *easier*
  for a candidate set to pass.

* Increasing the strenuousness of the enrichment test by increasing beta (or
  reducing it, if DCL_BETA is defined) makes the algorithm run more slowly, since
  more candidate sets are rejected for failing the enrichment test.

* If you want to add a different type of weak model, you will need to derive it
  from the class "WeakModel" [see RectWM.[CH], HammWM.[CH], PlaneWM.[CH] for details].
  You will also need to implement global Loader, Copier and Factory functions,
  and add them to the WMFuncs array at the top of the file "WMSet.C".

* Accuracy stats are only computed with every display; if you set the display
  interval higher than 1 (which shows accuracy after every set addition), it's
  entirely possible that you will achieve the highest test set accuracy for a
  number of sets that you didn't display.  Since accuracy testing is expensive,
  there's no handy way around this.  You could run with high display intervals,
  storing the random number seed used, and then go back to good looking regions in
  subsequent runs, or I might make accuracy testing for every set count a future
  feature of SDTest.

* I have not (yet) attempted to duplicate the results described in [1].  The
  data sets that are included with the distribution are just ones that I had on
  hand.  I've made no effort to cross-validate or optimize biases on any of
  the datasets.  
              
================================================================================
                                USAGE
================================================================================

Usage: SDTrain -F trainFile -N nTrain  -i nInputs [-f testFile][-n nTest]
               [-C nClasses][-M maxSets][-I dispInterval][-b beta]
               [-R rhoMax][-r rhoMin][-D rhoDecay][-t tau][-k kappa]
               [-s seed][-o dumpFileName][-T subsetThresh]
Defaults:
  modelType = 0         // 0=Rect (a la Kleinberg), 1=Hamming, 2=Hyperplane
  maxSets = 100         // maximum number of weak classifiers
  dispInterval = 10     // display after 'dispInterval' classifier allocations
  beta = 0.1            // enrichment test: ((fracPos - fracNeg) > beta)
  kappa = 1             // make no more than kappa weak models/classifier
  subsetThresh = 1.0    // proportion of input features used to create weak models
  seed = time(0)        // random number seed

  rhoMax = 0.125        // [depends on weak model - see "WEAK MODEL TYPES" below]
  rhoMin = 0.005        // [depends on weak model - see "WEAK MODEL TYPES" below]
  rhoDecay = 0.990      // [depends on weak model - see "WEAK MODEL TYPES" below]
  tau = 1.1             // [depends on weak model - see "WEAK MODEL TYPES" below]


Usage: SDTest -i nInputs -C nClasses -n nTest -f testFileName -o dumpFileName [-t thresh]
Defaults:
  thresh = 0.500        // threshold for determination of positive class (for
                           binary classifiers only)

[The dumpFileName is the file to which a classifier trained by SDTrain is dumped.
 You can then use the dumpFileName with SDTest to look at other data sets.  The format
 of this file is something that is likely to change subsequent to this release.]

================================================================================
                            SDTrain OUTPUT
================================================================================

                       -- Binary Classifiers --

When learning binary classifiers, SDTrain prints an output line of the following form
after every 'dispInterval' classifier additions:

  nSets trainAcc testAcc minPos maxPos posAvg posSD minNeg maxNeg negAvg negSD

  where:
    nSets       => current number of classifiers
    trainAcc    => percent of training examples correctly classified
    testAcc     => percent of testing examples correctly classified
    minPos      => minimum frequency score on positive training examples
    maxPos      => maximum frequency score on positive training examples
    posAvg      => avg frequency score on positive training examples
    posSD       => std dev of frequency scores on positive training examples
    minNeg      => minimum frequency score on negative training examples
    maxNeg      => maximum frequency score on negative training examples
    negAvg      => avg frequency score on negative training examples
    negSD       => std dev of frequency scores on negative training examples


                      -- Multiple Classifiers --

When learning multiple classifiers, SDTrain prints output of the following form after
every 'dispInterval' classifier additions:

    classIndex0 trainAcc testAcc minPos maxPos posAvg posSD minNeg maxNeg negAvg negSD
    classIndex1 trainAcc testAcc minPos maxPos posAvg posSD minNeg maxNeg negAvg negSD
       ...
    classIndexN trainAcc testAcc minPos maxPos posAvg posSD minNeg maxNeg negAvg negSD
== nSets multiTrainAcc multiTestAcc

where all definitions are as above, and:

    classIndex      => the class used for positive examples in a given 1 vs N classifier
    multiTrainAcc   => the accuracy over the training set
    multiTestAcc    => the accuracy over the test set

SDTrain also prints a dot '.' after the successful addition of a single set to every
classifier.

                        -- Accuracy Report --

When the -d ("dumpAccuracy") flag is supplied on the command line, an accuracy report
will be generated at the end of training.  It has the following format:

    index       score  target output
      .           .      .      .
      .           .      .      .
      .           .      .      .

                   0     1  <- classified as
    targets: 0    n00   n01 
             1    n11   n10 


Where the score columns and (mis-)classification matrix are extended as necessary
for the multi-class case.

================================================================================
                           WMSets and WEAK MODEL TYPES
================================================================================

All weak models use a random subset of input features determined by the
'subsetThresh' parameter.  The default setting for this parameter is 1.0 (ie, use all
inputs).  Each classifier is actually a union of of a random number of weak models (a
weak model set or WMSet), where the number of weak models in a given set (classifier)
is given by taking the integral value of a uniform random number between 1 and the
value of the command line option 'kappa' (inclusive).


                -- Type 0: RectWM (see RectWM.[CH]) --

    These weak models are hyperrectangles formed as follows:

    (Roughly following [2] and [3]):

    Let R be the smallest rectangular region containing the training set.
    Then let R(tau) be the rectangular region in Rd such that R(tau)
    and R have the same center, R(tau) is similar to R and the width
    of R(tau) along each input axis is tau times the corresponding
    width of R.

    A rectangular region is then generated by taking a random input vector
    as the center, and assigning the width of the rectangle in each dimension
    to a random value between "rhoMin" and "rhoMax" times the width of the input
    data in that dimension.  Finally, the rectangle is clipped to reside entirely
    within R(tau).

    "rhoMax" is multiplied by "rhoDecay" after each set is added, until
    "rhoMax" equals "rhoMin".


                -- Type 1: HammWM (see HammWM.[CH]) --

    These weak models are based on the Hamming distance from a prototype,
    (the data are assumed to be binary) and are formed as follows:

    A random in-class vector and out-of-class vector are taken from the
    training data, and the Hamming distance between them is determined.
    The in-class vector and Hamming distance are stored.

    A sample input vector is classified as positive if its Hamming distance
    from the stored in-class vector is less than the stored Hamming distance.

    The parameters "rhoMin", "rhoMax", "rhoDecay" and "tau" have no effect.


               -- Type 2: PlaneWM (see PlaneWM.[CH]) --
    
    These weak models divide the space with a hyperplane and examples are
    classified depending on which section of the divided space they belong
    to.  The hyperplane is formed as follows:

    A random in-class point and out-of-class point are taken from the
    training data, and the difference vector [which is the normal vector of
    a hyperplane that separates the in-class and out-of-class points] is
    computed and stored.  An internally cached value 'k' is used to represent
    where (along the normal vector) the discriminating hyperplane is located
    with respect to the points used to compute the normal.  A value of -1
    for 'k' means that the hyperplane is placed in such a way as to
    intersect the out-of-class point; +1 intersects the positive point, and
    a value of 0 passes through the midpoint.

    When the weak models are generated, a value for 'k' is determined by
    taking a uniform random value between the 'rhoMin' and 'rhoMax' command
    line parameters.  The 'rhoDecay' and 'tau' parameters have no effect.

    [An alternative method of computing the separating hyperplane is to use
    a random vector for the projections of the sample points, rather than
    the difference vector; this removes the constraint that the hyperplane
    be perpendicular to the difference vector.  This feature can be enabled
    by uncommenting the line that defines "RANDOM_VECT" at the top of the
    file "PlaneWM.C".]

    I hope to have a diagram of this process in a future copy of the docs.

================================================================================
                              REFERENCES
================================================================================

[1] Kleinberg, E.: "On the Algorithmic Implementation of Stochastic Discrimination",
     IEEE Trans. on Pattern Analysis and Machine Intelligence, vol. 2, no. 5, May 2000

[2] Chen, D., Huang, P., and Cheng, X.: "A Concrete Statistical Realization of
     Kleinberg's Stochastic Discrimination for Pattern Recognition", Part I. Two-Class
     Classification", to appear in Annals of Statistics

[3] Chen, D. and Cheng, X. : "A Simple Implementation of the Stochastic Discrimination
     for Pattern Recognition", Advances in Pattern Recognition, Proc. Joint IAPR
     Int. Workshops SSPR 2000 and SPR 2000 (Ferri, Inesta, et al., eds.), Alicante,
     Spain, Springer LNCS-1876, pp. 882-887, 2000
