4 Copyright (C) 2003-2009 Kevin Thornton, krthornt[]@[]uci.edu
6 Remove the brackets to email me.
8 This file is part of libsequence.
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.
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.
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/>.
25 #ifndef __DESCRIPTIVE_STATS_TCC__
26 #define __DESCRIPTIVE_STATS_TCC__
31 /*! \file descriptiveStats.tcc
32 \short implementations for file descriptiveStats.hpp
37 template<typename iterator>
38 double mean(iterator beg, iterator end)
41 double _nsam(end-beg);
42 for( ; beg != end ; ++beg)
46 return double(_result)/double(_nsam);
49 template<typename iterator>
50 double variance(iterator beg, iterator end)
54 double _nsam(end-beg);
55 for( ; beg != end ; ++beg)
58 _xsq += (*beg)*(*beg);
60 return double(_xsq)/double(_nsam-1) -
61 (double(_mean)/double(_nsam))*(double(_mean)/double(_nsam-1));
64 template<typename ForwardIterator>
65 std::pair<double,double>
66 meanAndVar(ForwardIterator beg,
69 Sums<double> __s = std::accumulate(beg,end,Sums<double>());
70 return std::make_pair(__s.mean(),__s.variance());
74 Sums<T>::Sums() : __sum( T() ), __sumsq( T() ), __n(0u)
79 Sums<T> & Sums<T>::operator+=(const T & t)
88 Sums<T> & Sums<T>::operator+=(const Sums<T> & s)
97 const Sums<T> operator+(const Sums<T> & lhs,const Sums<T> & rhs)
99 return Sums<T>(lhs) += rhs;
103 const Sums<T> operator+(const Sums<T> & lhs,const T & rhs)
105 return Sums<T>(lhs) += rhs;
109 const T & Sums<T>::sum() const
115 const T & Sums<T>::sumSquares() const
121 double Sums<T>::mean() const
123 return double(__sum)/double(__n);
127 double Sums<T>::variance() const
129 return ( double(__sumsq)/double(__n-1) -
130 double(__sum*__sum)/double(__n*(__n-1)) );