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

Generated on Fri Jul 4 15:30:21 2008 for GPLIB++ by  doxygen 1.5.5