C:/programs/SCPPNT/src/include/statfunc.h

Go to the documentation of this file.
00001 /*! \file statfunc.h
00002  \brief Function objects implementing statistical functions.
00003  
00004  Definitions of function objects Standardize, Sum, Mean,
00005  and Moment.
00006  These function objects be applied to rows or columns of matrix with functions
00007  defined in rowcolfunc.h.
00008 
00009  */
00010 
00011 /*
00012 
00013  Function objects implementing statistical functions.
00014  
00015  Simple C++ Numerical Toolkit (SCPPNT)
00016  http://www.smallwaters.com/software/cpp/scppnt.html
00017  This release updates original work contributed by 
00018  Brad Hanson (http://www.b-a-h.com/) in 2001.
00019 
00020  */
00021 
00022 #ifndef SCPPNT_STATFUNC_H
00023 #define SCPPNT_STATFUNC_H
00024 
00025 #ifdef SCPPNT_NO_DIR_PREFIX
00026 #include "scppnt.h"
00027 #else
00028 #include "scppnt/scppnt.h"
00029 #endif
00030 
00031 namespace SCPPNT
00032 {
00033 
00034   /*!
00035    \brief Standardize a set of values so they sum to a particular value.
00036    
00037    \param T A floating point type.
00038    */
00039   template<class T> class Standardize
00040   {
00041 
00042 public:
00043 
00044     /*! \brief Constructor
00045      \param s The value the sequence should sum to.
00046      */
00047     Standardize(T s = 1)
00048     {
00049       sum = s;
00050     }
00051 
00052     /*! \brief Compute standardized values for data pointed to by begin.
00053      
00054      \param I An iterator type pointing to values
00055      which are standardized.
00056      
00057      \param nelem Number of values to be standardized.
00058      \param begin Iterator to values to be standardized.
00059      */
00060     template<class I> void operator()(Subscript nelem, I begin)
00061     {
00062       int i;
00063       T oldsum = 0;
00064 
00065       I ib = begin;
00066       for (i = nelem; i--; ++ib)
00067       {
00068         oldsum += *ib;
00069       }
00070 
00071       oldsum *= sum;
00072       for (i = nelem; i--; ++begin)
00073       {
00074         *begin /= oldsum;
00075       }
00076     }
00077 
00078 private:
00079 
00080     T sum; //!< The value the sequence is standardized to.
00081   };
00082 
00083   /*!
00084    \brief Compute the sum of a sequence of values.
00085    
00086    \param T A floating point type.
00087    */
00088   template<class T> class Sum
00089   {
00090 
00091 public:
00092 
00093     /*! \brief Compute sum from data pointed to by begin.
00094      
00095      \param I An iterator type pointing to values
00096      for which the sum is computed.
00097      
00098      \param nelem Number of values used to compute sum.
00099      \param begin Iterator to values used to compute sum.
00100      */
00101     template<class I> T operator()(Subscript nelem, I begin)
00102     {
00103       T sum = 0;
00104 
00105       while (nelem--)
00106       {
00107         sum += *begin;
00108         ++begin;
00109       }
00110 
00111       return sum;
00112     }
00113 
00114   };
00115 
00116   /*!
00117    \brief Compute the mean of a sequence of values.
00118    
00119    \param T A floating point type.
00120    */
00121   template<class T> class Mean
00122   {
00123 
00124 public:
00125 
00126     /*! \brief Compute sample mean from data pointed to by begin.
00127      
00128      \param I An iterator type pointing to values
00129      for which the mean is computed.
00130      
00131      \param nelem Number of values used to compute mean.
00132      \param begin Iterator to values used to compute mean.
00133      */
00134     template<class I> T operator()(Subscript nelem, I begin)
00135     {
00136       T n = nelem;
00137       T sum = 0;
00138 
00139       while (nelem--)
00140       {
00141         sum += *begin;
00142         ++begin;
00143       }
00144 
00145       return sum / n;
00146     }
00147 
00148   };
00149 
00150   /*
00151    \brief Compute the central moment of a sequence of values.
00152    
00153    compute sum_{i=1}^N (x_i - m)^o, where there are N value of x_i,
00154    m is the mean of the x_i, and o is the order of the
00155    moment computed.
00156    
00157    \param T A floating point type.
00158    */
00159   template<class T> class Moment
00160   {
00161 
00162 public:
00163 
00164     /*! \brief Constructor
00165      \param o Order of moment (e.g., 2 is variance).
00166      \param m Mean of values previously computed.
00167      \param m1  If true divide by N-1 rather than N.
00168      */
00169     Moment(int o, T m = 0, bool m1 = false)
00170     {
00171       order = o;
00172       mean = m;
00173       nm1 = m1;
00174     }
00175 
00176     /*! \brief Compute sample moment from data pointed to by begin.
00177      
00178      \param I An iterator type pointing to values
00179      for which the central moment is computed.
00180      
00181      \param nelem Number of values used to compute moment.
00182      \param begin Iterator to values used to compute moment.
00183      */
00184     template<class I> T operator()(Subscript nelem, I begin)
00185     {
00186       int n = (nm1) ? (nelem-1) : nelem;
00187       T sum = 0;
00188 
00189       while (nelem--)
00190       {
00191         T dev = *begin - mean;
00192         T dev2 = dev;
00193         for (int i = order-1; i--;)
00194           dev *= dev2;
00195         sum += dev;
00196         ++begin;
00197       }
00198 
00199       return sum / n;
00200     }
00201 
00202 private:
00203 
00204     int order; //!< Order of moment (e.g., 2 is variance)
00205     T mean; //! Mean of values.
00206     bool nm1; //!< if true divide by N-1, otherwise divide by N
00207 
00208   };
00209 
00210 } // namespace SCPPNT
00211 
00212 #endif // SCPPNT_STATFUNC_H

Generated on Tue Dec 18 23:34:06 2007 for SCPPNT by  doxygen 1.5.4