6 #include <boost/bind.hpp>
7 #include <boost/cast.hpp>
20 template<
typename InputIterator>
inline typename std::iterator_traits<
21 InputIterator>::value_type
Mean(InputIterator begin, InputIterator end)
23 return std::accumulate(begin, end,
24 typename std::iterator_traits<InputIterator>::value_type())
25 / boost::numeric_cast<
double>(distance(begin, end));
29 template<
typename InputIterator>
inline typename std::iterator_traits<
30 InputIterator>::value_type
Variance(InputIterator begin,
32 typename std::iterator_traits<InputIterator>::value_type mv)
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);
41 template<
typename InputIterator>
inline typename std::iterator_traits<
42 InputIterator>::value_type
Variance(InputIterator begin,
49 template<
typename InputIterator>
inline typename std::iterator_traits<
50 InputIterator>::value_type
MeanErr(InputIterator begin,
57 template<
typename InputIterator>
inline typename std::iterator_traits<
58 InputIterator>::value_type
MeanErr(InputIterator begin,
60 typename std::iterator_traits<InputIterator>::value_type mv)
62 return sqrt(
Variance(begin, end, mv) / boost::numeric_cast<double>(
63 distance(begin, end)));
67 template<
typename InputIterator>
inline typename std::iterator_traits<
68 InputIterator>::value_type
StdDev(InputIterator begin,
70 typename std::iterator_traits<InputIterator>::value_type mv)
72 return sqrt(
Variance(begin, end, mv));
76 template<
typename InputIterator>
inline typename std::iterator_traits<
77 InputIterator>::value_type
StdDev(InputIterator begin,
84 template<
typename InputIterator>
inline
85 void SubMean(InputIterator begin, InputIterator end,
86 typename std::iterator_traits<InputIterator>::value_type mean)
88 std::transform(begin, end, begin, boost::bind(std::minus<
89 typename std::iterator_traits<InputIterator>::value_type>(), _1,
94 template<
typename InputIterator>
inline
95 void SubMean(InputIterator begin, InputIterator end)
101 template<
typename InputIterator>
inline typename std::iterator_traits<
102 InputIterator>::value_type
Median(InputIterator begin,
105 std::vector<typename std::iterator_traits<InputIterator>::value_type>
106 localcopy(begin, end);
107 const unsigned int sortposition = localcopy.size() / 2 + 1;
108 if (sortposition > localcopy.size() - 1)
110 nth_element(localcopy.begin(), localcopy.begin() + sortposition,
112 if ((localcopy.size() % 2) == 0)
113 return ((localcopy.at(localcopy.size() / 2) + localcopy.at(
114 sortposition)) / 2.);
116 return (localcopy.at(sortposition));
std::iterator_traits< InputIterator >::value_type Median(InputIterator begin, InputIterator end)
Calculate the median for a vector style container.
std::iterator_traits< InputIterator >::value_type Mean(InputIterator begin, InputIterator end)
Calculate the mean for a given range.
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.
std::iterator_traits< InputIterator >::value_type MeanErr(InputIterator begin, InputIterator end)
Calculate the Mean Error for a given input range.
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.
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.
The basic exception class for all errors that arise in gplib.