libsequence  1.9.5
descriptiveStats.tcc
1 // -*- C++ -*-
2 /*
3 
4 Copyright (C) 2003-2009 Kevin Thornton, krthornt[]@[]uci.edu
5 
6 Remove the brackets to email me.
7 
8 This file is part of libsequence.
9 
10 libsequence is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14 
15 libsequence is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 long with libsequence. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 #ifndef __DESCRIPTIVE_STATS_TCC__
26 #define __DESCRIPTIVE_STATS_TCC__
27 #include <numeric>
28 #include <functional>
29 #include <iterator>
30 
31 /*! \file descriptiveStats.tcc
32  \short implementations for file descriptiveStats.hpp
33 */
34 
35 namespace Sequence
36 {
37  template<typename iterator>
38  double mean(iterator beg, iterator end)
39  {
40  double _result(0);
41  double _nsam(end-beg);
42  for( ; beg != end ; ++beg)
43  {
44  _result += *beg;
45  }
46  return double(_result)/double(_nsam);
47  }
48 
49  template<typename iterator>
50  double variance(iterator beg, iterator end)
51  {
52  double _mean(0);
53  double _xsq(0);
54  double _nsam(end-beg);
55  for( ; beg != end ; ++beg)
56  {
57  _mean += *beg;
58  _xsq += (*beg)*(*beg);
59  }
60  return double(_xsq)/double(_nsam-1) -
61  (double(_mean)/double(_nsam))*(double(_mean)/double(_nsam-1));
62  }
63 
64  template<typename ForwardIterator>
65  std::pair<double,double>
66  meanAndVar(ForwardIterator beg,
67  ForwardIterator end)
68  {
69  Sums<double> __s = std::accumulate(beg,end,Sums<double>());
70  return std::make_pair(__s.mean(),__s.variance());
71  }
72 
73  template<typename T>
74  Sums<T>::Sums() : __sum( T() ), __sumsq( T() ), __n(0u)
75  {
76  }
77 
78  template<typename T>
79  Sums<T> & Sums<T>::operator+=(const T & t)
80  {
81  __sum += t;
82  __sumsq += t*t;
83  __n++;
84  return *this;
85  }
86 
87  template<typename T>
88  Sums<T> & Sums<T>::operator+=(const Sums<T> & s)
89  {
90  __sum += s.__sum;
91  __sumsq += s.__sumsq;
92  __n += s.__n;
93  return *this;
94  }
95 
96  template<typename T>
97  const Sums<T> operator+(const Sums<T> & lhs,const Sums<T> & rhs)
98  {
99  return Sums<T>(lhs) += rhs;
100  }
101 
102  template<typename T>
103  const Sums<T> operator+(const Sums<T> & lhs,const T & rhs)
104  {
105  return Sums<T>(lhs) += rhs;
106  }
107 
108  template<typename T>
109  const T & Sums<T>::sum() const
110  {
111  return __sum;
112  }
113 
114  template<typename T>
115  const T & Sums<T>::sumSquares() const
116  {
117  return __sumsq;
118  }
119 
120  template<typename T>
121  double Sums<T>::mean() const
122  {
123  return double(__sum)/double(__n);
124  }
125 
126  template<typename T>
127  double Sums<T>::variance() const
128  {
129  return ( double(__sumsq)/double(__n-1) -
130  double(__sum*__sum)/double(__n*(__n-1)) );
131  }
132 }
133 #endif