statutils.h

Go to the documentation of this file.
00001 #ifndef STATUTILS_H_
00002 #define STATUTILS_H_
00003 #include <numeric>
00004 #include <vector>
00005 #include <algorithm>
00006 #include <boost/bind.hpp>
00007 #include <boost/cast.hpp>
00008 #include "FatalException.h"
00009 
00010 namespace gplib
00011   {
00012 
00013     /** \addtogroup statistics Statistical methods */
00014     /* @{ */
00015 
00016     /*! /file This header contains some simple statistical routines. All all templates for widest
00017      * possible use */
00018 
00019     //! Calculate the mean for a given range
00020     template<typename InputIterator> inline typename std::iterator_traits<
00021         InputIterator>::value_type Mean(InputIterator begin, InputIterator end)
00022       {
00023         return std::accumulate(begin, end,
00024             typename std::iterator_traits<InputIterator>::value_type())
00025             / boost::numeric_cast<double>(distance(begin, end));
00026       }
00027 
00028     //! Calculate the Variance and give the mean as a third input parameter
00029     template<typename InputIterator> inline typename std::iterator_traits<
00030         InputIterator>::value_type Variance(InputIterator begin,
00031         InputIterator end,
00032         typename std::iterator_traits<InputIterator>::value_type mv)
00033       {
00034         typename std::iterator_traits<InputIterator>::value_type var = 0.0;
00035         for (InputIterator it = begin; it != end; ++it)
00036           var += (*it - mv) * (*it - mv);
00037         return var / boost::numeric_cast<double>(distance(begin, end) - 1);
00038       }
00039 
00040     //! Calculate the Variance for a given range when the mean is not known and has to be calculated as well
00041     template<typename InputIterator> inline typename std::iterator_traits<
00042         InputIterator>::value_type Variance(InputIterator begin,
00043         InputIterator end)
00044       {
00045         return Variance(begin, end, Mean(begin, end));
00046       }
00047 
00048     //! Calculate the Mean Error for a given input range
00049     template<typename InputIterator> inline typename std::iterator_traits<
00050         InputIterator>::value_type MeanErr(InputIterator begin,
00051         InputIterator end)
00052       {
00053         return MeanErr(begin, end, Mean(begin, end));
00054       }
00055 
00056     //! Calculate the Mean Error for a given input range when the mean is known and passed as a third parameter
00057     template<typename InputIterator> inline typename std::iterator_traits<
00058         InputIterator>::value_type MeanErr(InputIterator begin,
00059         InputIterator end,
00060         typename std::iterator_traits<InputIterator>::value_type mv)
00061       {
00062         return sqrt(Variance(begin, end, mv) / boost::numeric_cast<double>(
00063             distance(begin, end)));
00064       }
00065 
00066     //! Calculate the Standard deviation with a given mean
00067     template<typename InputIterator> inline typename std::iterator_traits<
00068         InputIterator>::value_type StdDev(InputIterator begin,
00069         InputIterator end,
00070         typename std::iterator_traits<InputIterator>::value_type mv)
00071       {
00072         return sqrt(Variance(begin, end, mv));
00073       }
00074 
00075     //! Calculate the Standard Deviation
00076     template<typename InputIterator> inline typename std::iterator_traits<
00077         InputIterator>::value_type StdDev(InputIterator begin,
00078         InputIterator end)
00079       {
00080         return StdDev(begin, end, Mean(begin, end));
00081       }
00082 
00083     //! Substract the mean from each element in the container, mean is passed as a parameter
00084     template<typename InputIterator> inline
00085     void SubMean(InputIterator begin, InputIterator end,
00086         typename std::iterator_traits<InputIterator>::value_type mean)
00087       {
00088         std::transform(begin, end, begin, boost::bind(std::minus<
00089             typename std::iterator_traits<InputIterator>::value_type>(), _1,
00090             mean));
00091       }
00092 
00093     //! Substract the mean from each element in the container, mean is calculated
00094     template<typename InputIterator> inline
00095     void SubMean(InputIterator begin, InputIterator end)
00096       {
00097         SubMean(begin, end, Mean(begin, end));
00098       }
00099 
00100     //! Calculate the median for a vector style container
00101     template<typename InputIterator> inline typename std::iterator_traits<
00102         InputIterator>::value_type Median(InputIterator begin,
00103         InputIterator end)
00104       {
00105         std::vector<typename std::iterator_traits<InputIterator>::value_type>
00106             localcopy(begin, end); // we have to create a local copy, because nth_element changes the order
00107         const unsigned int sortposition = localcopy.size() / 2 + 1;
00108         if (sortposition > localcopy.size() - 1)
00109           throw FatalException("Vector too small ! Median not defined !");
00110         nth_element(localcopy.begin(), localcopy.begin() + sortposition,
00111             localcopy.end());
00112         if ((localcopy.size() % 2) == 0)
00113           return ((localcopy.at(localcopy.size() / 2) + localcopy.at(
00114               sortposition)) / 2.);
00115         else
00116           return (localcopy.at(sortposition));
00117       }
00118   /* @} */
00119   }
00120 #endif /*STATUTILS_H_*/

Generated on Tue May 4 16:52:15 2010 for GPLIB++ by  doxygen 1.5.8