latest version v1.9 - last update 10 Apr 2010 |
00001 /* 00002 * Copyright (C) 2002, 2003, 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 .......: ltiBinomialDistribution.h 00027 * authors ....: Peter Doerfler 00028 * organization: LTI, RWTH Aachen 00029 * creation ...: 26.3.2002 00030 * revisions ..: $Id: ltiBinomialDistribution.h,v 1.9 2006/02/08 12:12:59 ltilib Exp $ 00031 */ 00032 00033 #ifndef _LTI_BINOMIAL_DISTRIBUTION_H_ 00034 #define _LTI_BINOMIAL_DISTRIBUTION_H_ 00035 00036 #include "ltiDiscreteRandDist.h" 00037 00038 00039 namespace lti { 00040 /** 00041 * This functor implements the binomial distribution. <p> 00042 * \f$B(n,k,p)=\binom{n}{k}p^k(1-p)^{(n-k)}\f$ with \f$n\f$ the sample size 00043 * \f$p\f$ the base probability and \f$k\f$ the number of events.<p> 00044 * It is also possible to calculate the cumulated probability<p> 00045 * \f$B(x\le k)=\sum_{x=0}^k\binom{n}{x}p^x(1-p)^{(n-x)}\f$<p> 00046 * Furthermore, the quantile \f$q\f$ for a given confidence probability 00047 * \f$P_c\f$ can be calcultated. With \f$B(n,q,p)\geq P_c \wedge 00048 * B(n,k,p)<P_c \forall k<q\f$. <p> 00049 * Finally, the upper bound for base probability can be estimated with 00050 * given \f$n\f$, \f$k\f$ and \f$P_c\f$, with the confidence probability 00051 * being one-sided. I.e. the probability \f$p\f$ returned is an approximate 00052 * solution for \f$B(n, x\le k, p) = P_c\f$. <p> 00053 * Of the parameters only sampleSize needs to be set for all applications of 00054 * the functor. The other parameters only need to be set if necessary for the 00055 * desired use of the functor. 00056 */ 00057 class binomialDistribution : public discreteRandomDistribution { 00058 public: 00059 /** 00060 * the parameters for the class binomialDistribution 00061 */ 00062 class parameters : public discreteRandomDistribution::parameters { 00063 public: 00064 /** 00065 * default constructor 00066 */ 00067 parameters(); 00068 00069 /** 00070 * copy constructor 00071 * @param other the parameters object to be copied 00072 */ 00073 parameters(const parameters& other); 00074 00075 /** 00076 * destructor 00077 */ 00078 ~parameters(); 00079 00080 /** 00081 * returns name of this type 00082 */ 00083 const char* getTypeName() const; 00084 00085 /** 00086 * copy the contents of a parameters object 00087 * @param other the parameters object to be copied 00088 * @return a reference to this parameters object 00089 */ 00090 parameters& copy(const parameters& other); 00091 00092 /** 00093 * copy the contents of a parameters object 00094 * @param other the parameters object to be copied 00095 * @return a reference to this parameters object 00096 */ 00097 parameters& operator=(const parameters& other); 00098 00099 00100 /** 00101 * returns a pointer to a clone of the parameters 00102 */ 00103 virtual functor::parameters* clone() const; 00104 00105 /** 00106 * write the parameters in the given ioHandler 00107 * @param handler the ioHandler to be used 00108 * @param complete if true (the default) the enclosing begin/end will 00109 * be also written, otherwise only the data block will be written. 00110 * @return true if write was successful 00111 */ 00112 virtual bool write(ioHandler& handler,const bool complete=true) const; 00113 00114 /** 00115 * read the parameters from the given ioHandler 00116 * @param handler the ioHandler to be used 00117 * @param complete if true (the default) the enclosing begin/end will 00118 * be also written, otherwise only the data block will be written. 00119 * @return true if write was successful 00120 */ 00121 virtual bool read(ioHandler& handler,const bool complete=true); 00122 00123 # ifdef _LTI_MSC_6 00124 /** 00125 * this function is required by MSVC only, as a workaround for a 00126 * very awful bug, which exists since MSVC V.4.0, and still by 00127 * V.6.0 with all bugfixes (so called "service packs") remains 00128 * there... This method is also public due to another bug, so please 00129 * NEVER EVER call this method directly: use read() instead 00130 */ 00131 bool readMS(ioHandler& handler,const bool complete=true); 00132 00133 /** 00134 * this function is required by MSVC only, as a workaround for a 00135 * very awful bug, which exists since MSVC V.4.0, and still by 00136 * V.6.0 with all bugfixes (so called "service packs") remains 00137 * there... This method is also public due to another bug, so please 00138 * NEVER EVER call this method directly: use write() instead 00139 */ 00140 bool writeMS(ioHandler& handler,const bool complete=true) const; 00141 # endif 00142 00143 // ------------------------------------------------ 00144 // the parameters 00145 // ------------------------------------------------ 00146 00147 //TODO: comment the parameters of your functor 00148 // If you add more parameters manually, do not forget to do following: 00149 // 1. indicate in the default constructor the default values 00150 // 2. make sure that the copy member also copy your new parameters 00151 // 3. make sure that the read and write members also read and 00152 // write your parameters 00153 00154 00155 /** 00156 * The size of the sample for the binomial distribution. 00157 * Default 1. This parameter needs to be set for all applications of 00158 * the functor. 00159 */ 00160 int sampleSize; 00161 00162 /** 00163 * The number of positive events. Default 0. It only needs to be set 00164 * for evaluating the upper bound for the base probability if the 00165 * method not having this argument is used. 00166 * @see upperBound() 00167 */ 00168 int events; 00169 00170 /** 00171 * Here called confidence probability. Default 0.95. It is needed for 00172 * the calculation of the quantile() and the upperBound(). 00173 */ 00174 double confidence; 00175 00176 /** 00177 * The accuracy in the iterative calculation of the upperBound(). The 00178 * default is 0.0001. 00179 */ 00180 double accuracy; 00181 00182 /** 00183 * The base probability of a event. Default 0.5. 00184 */ 00185 double baseProbability; 00186 00187 }; 00188 00189 /** 00190 * default constructor 00191 */ 00192 binomialDistribution(); 00193 00194 /** 00195 * constructor. Sets the number of samples 00196 * @param samples the number of samples 00197 */ 00198 binomialDistribution(const int& samples); 00199 00200 /** 00201 * constructor. Set the number of samples and the base probability. 00202 * Optionally, the confidence probabilty can be set for calculating the 00203 * quantile. 00204 * @param samples the number of samples 00205 * @param baseProb base probability 00206 * @param conf confidence probability 00207 */ 00208 binomialDistribution(const int& samples, const double& baseProb, 00209 const double& conf=0.95); 00210 00211 /** 00212 * constructor used for calculating the upperBound(). 00213 * @param samples the number of samples 00214 * @param posEvents number of positiv events 00215 * @param conf confidence probability 00216 */ 00217 binomialDistribution(const int& samples, const int& posEvents, 00218 const double& conf); 00219 00220 /** 00221 * copy constructor 00222 * @param other the object to be copied 00223 */ 00224 binomialDistribution(const binomialDistribution& other); 00225 00226 /** 00227 * destructor 00228 */ 00229 virtual ~binomialDistribution(); 00230 00231 /** 00232 * returns the name of this type ("binomialDistribution") 00233 */ 00234 virtual const char* getTypeName() const; 00235 00236 //TODO: comment your apply methods! 00237 00238 /** 00239 * Returns a binomial random number, i.e. zero or one. 1 is returned if 00240 * the event occurs, 0 if it doesn't. 00241 * @return binomial random number. 00242 */ 00243 virtual int draw() const; 00244 00245 /** 00246 * Returns the probability for exactly k events with the given parameters. 00247 * If k is out of bounds, zero is returned. 00248 * @param k number of positive events 00249 * @return probability of k events 00250 */ 00251 double pdf(const int& k) const; 00252 00253 /** 00254 * Returns the probability for base probability p with the given 00255 * parameters. 00256 * If p is out of bounds zero is returned. 00257 * @param p base probability of an event 00258 * @return probability of k events (paramters) with base probability p 00259 */ 00260 double pdf(const double& p) const; 00261 00262 /** 00263 * Returns the probability for k events and base probability p with 00264 * the given parameters. 00265 * If p is out of bounds zero is returned. 00266 * If k is out of bounds, zero is returned. 00267 * @param k number of positive events 00268 * @param p base probability of an event 00269 * @return probability of k events (paramters) with base probability p 00270 */ 00271 double pdf(const int& k, const double& p) const; 00272 00273 /** 00274 * Returns the probability for k events, base probability p and 00275 * number of samples n 00276 * If p is out of bounds zero is returned. 00277 * If k is out of bounds, zero is returned. 00278 * @param k number of positive events 00279 * @param p base probability of an event 00280 * @param n number of samples 00281 * @return probability of k events (paramters) with base probability p 00282 */ 00283 double pdf(const int& k, const double& p, const int& n) const; 00284 00285 /** 00286 * Returns the cumulated probability for greater equal k events 00287 * with the given parameters. 00288 * If k is out of bounds, zero is returned. 00289 * @param k max number of positive events 00290 * @return probability of up to k events 00291 */ 00292 double cdf(const int& k) const; 00293 00294 /** 00295 * Returns the cumulated probability for greater equal k events as set 00296 * in the parameters. The base probability in the argument is used. 00297 * If p is out of bounds it is truncated to 0 or 1. 00298 * @param p base probability of an event 00299 * @return probability of up to k events (parameters) with base prob p. 00300 */ 00301 double cdf(const double& p) const; 00302 00303 /** 00304 * Returns the cumulated probability for greater equal k events, 00305 * base probability p, and sample size n. 00306 * 00307 * If k is out of bounds, zero is returned. 00308 * If p is out of bounds it is truncated to 0 or 1. 00309 * @param k max number of positive events 00310 * @param p base probability of an event 00311 * @param n number of samples 00312 * @return probability of up to k events (parameters) with base prob p. */ 00313 double cdf(const int& k, const double& p, const int& n) const; 00314 00315 /** 00316 * Returns the quantile for a given confidence probability. For the 00317 * number of samples and the base probability, the parameters are used. 00318 * @param confProb confidence 00319 * @return the quantile 00320 */ 00321 int quantile(const double& confProb) const; 00322 00323 /** 00324 * Returns an approximated upper bound for the base probability of an 00325 * event given the number of events. The number of samples and the 00326 * confidence probability are taken from the parameters. If no solution 00327 * can be found, a value greater than one is returned. 00328 * @param k number of positive events 00329 * @return upper bound for the base probability 00330 */ 00331 double upperBound(const int& k) const; 00332 00333 /** 00334 * Returns an approximated upper bound for the base probability of an 00335 * event given the number of events and the number of samples. The 00336 * confidence probability is taken from the parameters. If no solution 00337 * can be found, a value greater than one is returned. 00338 * @param k number of positive events 00339 * @param n number of samples 00340 * @return upper bound for the base probability 00341 */ 00342 double upperBound(const int& k, const int& n) const; 00343 00344 /** 00345 * copy data of "other" functor. 00346 * @param other the functor to be copied 00347 * @return a reference to this functor object 00348 */ 00349 binomialDistribution& copy(const binomialDistribution& other); 00350 00351 /** 00352 * alias for copy member 00353 * @param other the functor to be copied 00354 * @return a reference to this functor object 00355 */ 00356 binomialDistribution& operator=(const binomialDistribution& other); 00357 00358 /** 00359 * returns a pointer to a clone of this functor. 00360 */ 00361 virtual functor* clone() const; 00362 00363 /** 00364 * returns used parameters 00365 */ 00366 const parameters& getParameters() const; 00367 00368 //TODO: comment the attributes of your functor 00369 // If you add more attributes manually, do not forget to do following: 00370 // 1. indicate in the default constructor the default values 00371 // 2. make sure that the copy member also copy your new attributes, or 00372 // to ensure there, that these attributes are properly initialized. 00373 00374 }; 00375 } 00376 00377 #endif