GPLIB++
statutils.h
Go to the documentation of this file.
1 #ifndef STATUTILS_H_
2 #define STATUTILS_H_
3 #include <numeric>
4 #include <vector>
5 #include <algorithm>
6 #include <boost/bind.hpp>
7 #include <boost/cast.hpp>
8 #include "FatalException.h"
9 
10 namespace gplib
11  {
12 
13  /** \addtogroup statistics Statistical methods */
14  /* @{ */
15 
16  /*! /file This header contains some simple statistical routines. All all templates for widest
17  * possible use */
18 
19  //! Calculate the mean for a given range
20  template<typename InputIterator> inline typename std::iterator_traits<
21  InputIterator>::value_type Mean(InputIterator begin, InputIterator end)
22  {
23  return std::accumulate(begin, end,
24  typename std::iterator_traits<InputIterator>::value_type())
25  / boost::numeric_cast<double>(distance(begin, end));
26  }
27 
28  //! Calculate the Variance and give the mean as a third input parameter
29  template<typename InputIterator> inline typename std::iterator_traits<
30  InputIterator>::value_type Variance(InputIterator begin,
31  InputIterator end,
32  typename std::iterator_traits<InputIterator>::value_type mv)
33  {
34  typename std::iterator_traits<InputIterator>::value_type var = 0.0;
35  for (InputIterator it = begin; it != end; ++it)
36  var += (*it - mv) * (*it - mv);
37  return var / boost::numeric_cast<double>(distance(begin, end) - 1);
38  }
39 
40  //! Calculate the Variance for a given range when the mean is not known and has to be calculated as well
41  template<typename InputIterator> inline typename std::iterator_traits<
42  InputIterator>::value_type Variance(InputIterator begin,
43  InputIterator end)
44  {
45  return Variance(begin, end, Mean(begin, end));
46  }
47 
48  //! Calculate the Mean Error for a given input range
49  template<typename InputIterator> inline typename std::iterator_traits<
50  InputIterator>::value_type MeanErr(InputIterator begin,
51  InputIterator end)
52  {
53  return MeanErr(begin, end, Mean(begin, end));
54  }
55 
56  //! Calculate the Mean Error for a given input range when the mean is known and passed as a third parameter
57  template<typename InputIterator> inline typename std::iterator_traits<
58  InputIterator>::value_type MeanErr(InputIterator begin,
59  InputIterator end,
60  typename std::iterator_traits<InputIterator>::value_type mv)
61  {
62  return sqrt(Variance(begin, end, mv) / boost::numeric_cast<double>(
63  distance(begin, end)));
64  }
65 
66  //! Calculate the Standard deviation with a given mean
67  template<typename InputIterator> inline typename std::iterator_traits<
68  InputIterator>::value_type StdDev(InputIterator begin,
69  InputIterator end,
70  typename std::iterator_traits<InputIterator>::value_type mv)
71  {
72  return sqrt(Variance(begin, end, mv));
73  }
74 
75  //! Calculate the Standard Deviation
76  template<typename InputIterator> inline typename std::iterator_traits<
77  InputIterator>::value_type StdDev(InputIterator begin,
78  InputIterator end)
79  {
80  return StdDev(begin, end, Mean(begin, end));
81  }
82 
83  //! Substract the mean from each element in the container, mean is passed as a parameter
84  template<typename InputIterator> inline
85  void SubMean(InputIterator begin, InputIterator end,
86  typename std::iterator_traits<InputIterator>::value_type mean)
87  {
88  std::transform(begin, end, begin, boost::bind(std::minus<
89  typename std::iterator_traits<InputIterator>::value_type>(), _1,
90  mean));
91  }
92 
93  //! Substract the mean from each element in the container, mean is calculated
94  template<typename InputIterator> inline
95  void SubMean(InputIterator begin, InputIterator end)
96  {
97  SubMean(begin, end, Mean(begin, end));
98  }
99 
100  //! Calculate the median for a vector style container
101  template<typename InputIterator> inline typename std::iterator_traits<
102  InputIterator>::value_type Median(InputIterator begin,
103  InputIterator end)
104  {
105  std::vector<typename std::iterator_traits<InputIterator>::value_type>
106  localcopy(begin, end); // we have to create a local copy, because nth_element changes the order
107  const unsigned int sortposition = localcopy.size() / 2 + 1;
108  if (sortposition > localcopy.size() - 1)
109  throw FatalException("Vector too small ! Median not defined !");
110  nth_element(localcopy.begin(), localcopy.begin() + sortposition,
111  localcopy.end());
112  if ((localcopy.size() % 2) == 0)
113  return ((localcopy.at(localcopy.size() / 2) + localcopy.at(
114  sortposition)) / 2.);
115  else
116  return (localcopy.at(sortposition));
117  }
118  /* @} */
119  }
120 #endif /*STATUTILS_H_*/
std::iterator_traits< InputIterator >::value_type Median(InputIterator begin, InputIterator end)
Calculate the median for a vector style container.
Definition: statutils.h:102
std::iterator_traits< InputIterator >::value_type Mean(InputIterator begin, InputIterator end)
Calculate the mean for a given range.
Definition: statutils.h:21
void SubMean(InputIterator begin, InputIterator end, typename std::iterator_traits< InputIterator >::value_type mean)
Substract the mean from each element in the container, mean is passed as a parameter.
Definition: statutils.h:85
std::iterator_traits< InputIterator >::value_type MeanErr(InputIterator begin, InputIterator end)
Calculate the Mean Error for a given input range.
Definition: statutils.h:50
std::iterator_traits< InputIterator >::value_type StdDev(InputIterator begin, InputIterator end, typename std::iterator_traits< InputIterator >::value_type mv)
Calculate the Standard deviation with a given mean.
Definition: statutils.h:68
std::iterator_traits< InputIterator >::value_type Variance(InputIterator begin, InputIterator end, typename std::iterator_traits< InputIterator >::value_type mv)
Calculate the Variance and give the mean as a third input parameter.
Definition: statutils.h:30
The basic exception class for all errors that arise in gplib.