LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiStudentDistribution.h

00001 /*
00002  * Copyright (C) 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*--------------------------------------------------------------------
00025  * project ....: LTI-Lib: Image Processing and Computer Vision Library
00026  * file .......: ltiStudentDistribution.h
00027  * authors ....: Claudia Goenner
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 20.9.2004
00030  * revisions ..: $Id: ltiStudentDistribution.h,v 1.7 2007/01/10 02:26:23 alvarado Exp $
00031  */
00032 
00033 #ifndef _LTI_STUDENT_DISTRIBUTION_H_
00034 #define _LTI_STUDENT_DISTRIBUTION_H_
00035 
00036 #include "ltiMathFunction.h"
00037 #include <vector>
00038 #include <cassert>
00039 
00040 #undef _LTI_DEBUG
00041 //#define _LTI_DEBUG 1
00042 #include "ltiDebug.h"
00043 
00044 #ifdef _LTI_DEBUG
00045 using std::cout;
00046 using std::endl;
00047 using std::flush;
00048 #endif
00049 
00050 namespace lti {
00051 
00052   /**
00053    * The Student t-test compares the means of two normally distributed
00054    * samples of sizes n1 and n2, assuming that their unknown variances are
00055    * equal. If the test value is bigger than T(k,alpha), k=n1+n2-2,
00056    * then the equality hypothesis is rejected at significance level alpha.
00057    *
00058    * This class only computes T(k,alpha). Computing the test value is left
00059    * to the application. For efficiency, e.g. if testing many times in a row
00060    * with varying degrees of freedom the values T(k,alpha) may be precomputed
00061    * for a constant significance level alpha and stored in a LUT.
00062    */
00063   class studentDistribution : public mathFunction {
00064   public:
00065     /**
00066      * The parameters for the class studentDistribution
00067      */
00068     class parameters : public mathFunction::parameters {
00069     public:
00070       /**
00071        * Default constructor
00072        */
00073       parameters();
00074 
00075       /**
00076        * Copy constructor
00077        * @param other the parameters object to be copied
00078        */
00079       parameters(const parameters& other);
00080 
00081       /**
00082        * Destructor
00083        */
00084       ~parameters();
00085 
00086       /**
00087        * Returns name of this type
00088        */
00089       const char* getTypeName() const;
00090 
00091       /**
00092        * Copy the contents of a parameters object
00093        * @param other the parameters object to be copied
00094        * @return a reference to this parameters object
00095        */
00096       parameters& copy(const parameters& other);
00097 
00098       /**
00099        * Copy the contents of a parameters object
00100        * @param other the parameters object to be copied
00101        * @return a reference to this parameters object
00102        */
00103       parameters& operator=(const parameters& other);
00104 
00105 
00106       /**
00107        * Returns a pointer to a clone of the parameters
00108        */
00109       virtual functor::parameters* clone() const;
00110 
00111       /**
00112        * Write the parameters in the given ioHandler
00113        * @param handler the ioHandler to be used
00114        * @param complete if true (the default) the enclosing begin/end will
00115        *        be also written, otherwise only the data block will be written.
00116        * @return true if write was successful
00117        */
00118       virtual bool write(ioHandler& handler,const bool complete=true) const;
00119 
00120       /**
00121        * Read the parameters from the given ioHandler
00122        * @param handler the ioHandler to be used
00123        * @param complete if true (the default) the enclosing begin/end will
00124        *        be also written, otherwise only the data block will be written.
00125        * @return true if write was successful
00126        */
00127       virtual bool read(ioHandler& handler,const bool complete=true);
00128 
00129 #     ifdef _LTI_MSC_6
00130       /**
00131        * This function is required by MSVC only, as a workaround for a
00132        * very awful bug, which exists since MSVC V.4.0, and still by
00133        * V.6.0 with all bugfixes (so called "service packs") remains
00134        * there...  This method is also public due to another bug, so please
00135        * NEVER EVER call this method directly: use read() instead
00136        */
00137       bool readMS(ioHandler& handler,const bool complete=true);
00138 
00139       /**
00140        * This function is required by MSVC only, as a workaround for a
00141        * very awful bug, which exists since MSVC V.4.0, and still by
00142        * V.6.0 with all bugfixes (so called "service packs") remains
00143        * there...  This method is also public due to another bug, so please
00144        * NEVER EVER call this method directly: use write() instead
00145        */
00146       bool writeMS(ioHandler& handler,const bool complete=true) const;
00147 #     endif
00148 
00149       // ------------------------------------------------
00150       // the parameters
00151       // ------------------------------------------------
00152 
00153       /**
00154        * If this is true a LUT is computed on the first call of accept and
00155        * used from there on. Please note that the LUT is only used by accept.
00156        * The apply method computes the confidence on every call.
00157        *
00158        * Default: false
00159        */
00160       bool useLUT;
00161 
00162       /**
00163        * The maximum degrees of freedom considered in the LUT. For some
00164        * common alpha, the an additional entry storing the limit is added.
00165        * Please note that computing the LUT is expensive.
00166        *
00167        * Default: 30.
00168        */
00169       int maxDOF;
00170 
00171       /**
00172        * If this is true, the significance level is interpreted to belong to a
00173        * one-sided test. Otherwise a two-sided test is performed.
00174        *
00175        * Default: false
00176        */
00177       bool oneSided;
00178 
00179       /**
00180        * The significance level.
00181        *
00182        * Default: 0.05
00183        */
00184       float alpha;
00185     };
00186 
00187     /**
00188      * Default constructor
00189      */
00190     studentDistribution();
00191 
00192     /**
00193      * Construct a functor using the given parameters
00194      */
00195     studentDistribution(const parameters& par);
00196 
00197     /**
00198      * Copy constructor
00199      * @param other the object to be copied
00200      */
00201     studentDistribution(const studentDistribution& other);
00202 
00203     /**
00204      * Destructor
00205      */
00206     virtual ~studentDistribution();
00207 
00208     /**
00209      * Returns the name of this type ("studentDistribution")
00210      */
00211     virtual const char* getTypeName() const;
00212 
00213     /**
00214      * Computes Student's distribution probability function for a given
00215      * degree of freedom and t-value, which code the similarity of the
00216      * two sample means.
00217      *
00218      * @param n      degrees of freedom of the student distribution
00219      * @param tValue t-value computed by your aplication
00220      * @param confidence the result, i.e. the probability that the two
00221      *                   sample means are similar
00222      * @return true on success and false otherwise
00223      */
00224     bool apply (const int n, const float& tValue, float& confidence) const;
00225 
00226     /**
00227      * Compute the lowest t-value, for which the hypothesis is rejected.
00228      * @param n      degrees of freedom of the student distribution
00229      * @return the lowest t-value for which the hypothesis is rejected.
00230      */
00231     float bound (const int n) const;
00232 
00233     /**
00234      * Tests if the hypothesis of equal means is accepted at the
00235      * significance level given in the parameters. Depending on the
00236      * parameter settings this method uses the LUT or computes the
00237      * result. If the LUT is requested but not initialized properly,
00238      * it is done here.
00239      *
00240      * @param n      degrees of freedom of the student distribution
00241      * @param tValue t-value computed by your application
00242      * @return true if the hypothesis is accepted and false otherwise
00243      */
00244     bool accept (const int n, const float& tValue);
00245 
00246     /**
00247      * Tests if the hypothesis of equal means is accepted at the
00248      * significance level given in the parameters. The appropriate
00249      * bound of Student's t-distribution is taken from a pre-computed
00250      * LUT.
00251      *
00252      * CAUTION: This method does not check if the LUT is initialized,
00253      * nor does it check that the LUT has been computed for the current
00254      * significance level. To initialize it please call generateLUT().
00255      *
00256      * @param n      degrees of freedom of the student distribution
00257      * @param tValue t-value computed by your application
00258      * @return true if the hypothesis is accepted and false otherwise
00259      */
00260     inline bool acceptLUT (const int n, const float& tValue);
00261 
00262     /**
00263      * Tests if the hypothesis of equal means is accepted at the
00264      * significance level given in the parameters. The significance
00265      * for your t-value is computed and compared with the significance
00266      * level supplied in the parameters.
00267      *
00268      * @param n      degrees of freedom of the student distribution
00269      * @param tValue t-value computed by your application
00270      * @return true if the hypothesis is accepted and false otherwise
00271      */
00272     bool acceptCompute (const int n, const float& tValue) const;
00273   
00274     /**
00275      * Copy data of "other" functor.
00276      * @param other the functor to be copied
00277      * @return a reference to this functor object
00278      */
00279     studentDistribution& copy(const studentDistribution& other);
00280 
00281     /**
00282      * Alias for copy member
00283      * @param other the functor to be copied
00284      * @return a reference to this functor object
00285      */
00286     studentDistribution& operator=(const studentDistribution& other);
00287 
00288     /**
00289      * Returns a pointer to a clone of this functor.
00290      */
00291     virtual functor* clone() const;
00292 
00293     /**
00294      * Returns used parameters
00295      */
00296     const parameters& getParameters() const;
00297 
00298     /**
00299      * Generates the LUT.
00300      */
00301     bool generateLUT();
00302     
00303   protected:
00304 
00305     /**
00306      * Remember the significance level for which the LUT has been computed.
00307      * Normed to one-sided.
00308      */
00309     float alpha_;
00310 
00311     /**
00312      * The LUT. Due to computational expensive of the used algorithm
00313      * double does not make any sense here.
00314      */
00315     std::vector<float> lut_;
00316 
00317     /**
00318      * Compute the lowest t-value, for which the hypothesis is rejected.
00319      * @param n      degrees of freedom of the student distribution
00320      * @param alpha  confidence value
00321      * @return the lowest t-value for which the hypothesis is rejected.
00322      */
00323     float computeBound(const int n, const float& alpha) const;
00324 
00325     /**
00326      * Compute the Student distribution probability function
00327      * for a given n (degrees of freedom) and t-value.
00328      * @param n      degrees of freedom of the student distribution
00329      * @param tValue t-value computed by your application
00330      * @return the two sided confidence
00331      */
00332     float distribution(const int n,
00333                        const float& tValue) const;
00334   };
00335 
00336   inline bool studentDistribution::acceptLUT (const int n,
00337                                               const float& tValue) {
00338     
00339     //make sure that lut in initialized in debug mode
00340     assert(lut_.size()>0);
00341     if ( n <= 0 ) {
00342       setStatusString("\napplyLUT: n (degrees of freedom) must be > 0\n");
00343       return false;
00344       
00345     } else if ( n >= static_cast<int>(lut_.size()) ) {
00346       //test with the value at infinity
00347       _lti_debug(" _bound "<< lut_.back());
00348       const float tmp ( tValue >= 0 ? tValue : -tValue );
00349       return tmp < lut_.back();
00350 
00351     } else {
00352       _lti_debug(" _bound " << lut_[n-1]);
00353       const float tmp ( tValue >= 0 ? tValue : -tValue );
00354       return tmp < lut_[n-1];
00355     }
00356   };
00357 
00358 }
00359 #include "ltiUndebug.h"
00360 
00361 #endif

Generated on Sat Apr 10 15:26:15 2010 for LTI-Lib by Doxygen 1.6.1