yat/statistics/AveragerPair.h

Code
Comments
Other
Rev Date Author Line
680 11 Oct 06 jari 1 #ifndef _theplu_yat_statistics_averagerpair_
680 11 Oct 06 jari 2 #define _theplu_yat_statistics_averagerpair_
138 20 Aug 04 peter 3
675 10 Oct 06 jari 4 // $Id$
295 29 Apr 05 peter 5
675 10 Oct 06 jari 6 /*
2119 12 Dec 09 peter 7   Copyright (C) 2004, 2005 Jari Häkkinen, Peter Johansson
2119 12 Dec 09 peter 8   Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
4359 23 Aug 23 peter 9   Copyright (C) 2007 Peter Johansson
4359 23 Aug 23 peter 10   Copyright (C) 2008 Jari Häkkinen, Peter Johansson
3550 03 Jan 17 peter 11   Copyright (C) 2009, 2010, 2011, 2016 Peter Johansson
675 10 Oct 06 jari 12
1437 25 Aug 08 peter 13   This file is part of the yat library, http://dev.thep.lu.se/yat
675 10 Oct 06 jari 14
675 10 Oct 06 jari 15   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 16   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 17   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 18   License, or (at your option) any later version.
675 10 Oct 06 jari 19
675 10 Oct 06 jari 20   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 21   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 22   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
675 10 Oct 06 jari 23   General Public License for more details.
675 10 Oct 06 jari 24
675 10 Oct 06 jari 25   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 26   along with yat. If not, see <http://www.gnu.org/licenses/>.
675 10 Oct 06 jari 27 */
675 10 Oct 06 jari 28
680 11 Oct 06 jari 29 #include "Averager.h"
675 10 Oct 06 jari 30
937 05 Oct 07 peter 31 #include "yat/utility/iterator_traits.h"
912 29 Sep 07 peter 32
2202 21 Feb 10 peter 33 #include <boost/concept_check.hpp>
3529 11 Oct 16 peter 34 #include <boost/iterator/iterator_concepts.hpp>
2202 21 Feb 10 peter 35
138 20 Aug 04 peter 36 #include <cmath>
180 04 Oct 04 peter 37 #include <utility>
138 20 Aug 04 peter 38
138 20 Aug 04 peter 39 namespace theplu{
680 11 Oct 06 jari 40 namespace yat{
197 27 Oct 04 jari 41 namespace statistics{
487 04 Jan 06 peter 42
138 20 Aug 04 peter 43   ///
767 22 Feb 07 peter 44   /// @brief Class for taking care of mean and covariance of two variables.
138 20 Aug 04 peter 45   ///
490 04 Jan 06 peter 46   /// @see Averager AveragerWeighted AveragerPairWeighted
489 04 Jan 06 peter 47   ///
138 20 Aug 04 peter 48   class AveragerPair
138 20 Aug 04 peter 49   {
138 20 Aug 04 peter 50   public:
138 20 Aug 04 peter 51
138 20 Aug 04 peter 52     ///
703 18 Dec 06 jari 53     /// @brief The default constructor
138 20 Aug 04 peter 54     ///
703 18 Dec 06 jari 55     AveragerPair(void);
703 18 Dec 06 jari 56
138 20 Aug 04 peter 57     ///
703 18 Dec 06 jari 58     /// The copy constructor
138 20 Aug 04 peter 59     ///
703 18 Dec 06 jari 60     AveragerPair(const AveragerPair&);
703 18 Dec 06 jari 61
138 20 Aug 04 peter 62     ///
197 27 Oct 04 jari 63     /// Adding \a n pairs of data points with value \a x and \a y.
138 20 Aug 04 peter 64     ///
1295 12 May 08 jari 65     void add(const double x, const double y, const long n=1);
138 20 Aug 04 peter 66
1043 06 Feb 08 peter 67     /**
1187 28 Feb 08 peter 68        \brief Concordence correlation coefficient.
1187 28 Feb 08 peter 69
1043 06 Feb 08 peter 70        \f$ \frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sum_i
3529 11 Oct 16 peter 71        (x_i-m_x)^2+\sum_i (y_i-m_y)^2 + n(m_x-m_y)^2} \f$
3529 11 Oct 16 peter 72
1043 06 Feb 08 peter 73     */
718 26 Dec 06 jari 74     double ccc(void) const;
718 26 Dec 06 jari 75
1187 28 Feb 08 peter 76     /**
1187 28 Feb 08 peter 77        \f$ \frac{\sum_i (x_i-m_x)(y_i-m_y)}{\sqrt{\sum_i
3529 11 Oct 16 peter 78        (x_i-m_x)^2\sum_i (y_i-m_y)^2}} \f$
3529 11 Oct 16 peter 79
1187 28 Feb 08 peter 80        \return %Pearson correlation coefficient.
1187 28 Feb 08 peter 81     */
718 26 Dec 06 jari 82     double correlation(void) const;
3529 11 Oct 16 peter 83
138 20 Aug 04 peter 84     ///
487 04 Jan 06 peter 85     /// Calculating covariance using
597 28 Aug 06 markus 86     /// \f$ \frac{1}{N}\sum_i (x_i-m_x)(y_i-m_y) \f$,
597 28 Aug 06 markus 87     /// where \f$ m \f$ is the mean.
138 20 Aug 04 peter 88     ///
197 27 Oct 04 jari 89     /// @return The covariance.
197 27 Oct 04 jari 90     ///
718 26 Dec 06 jari 91     double covariance(void) const;
3529 11 Oct 16 peter 92
138 20 Aug 04 peter 93     ///
197 27 Oct 04 jari 94     /// @return The mean of xy.
138 20 Aug 04 peter 95     ///
718 26 Dec 06 jari 96     double mean_xy(void) const;
138 20 Aug 04 peter 97
1187 28 Feb 08 peter 98     /**
1187 28 Feb 08 peter 99        \return Average squared deviation between x and y \f$
1187 28 Feb 08 peter 100        \frac{1}{N} \sum (x-y)^2 \f$
1187 28 Feb 08 peter 101     */
718 26 Dec 06 jari 102     double msd(void) const;
138 20 Aug 04 peter 103
138 20 Aug 04 peter 104     ///
219 30 Dec 04 peter 105     /// @return The number of pair of data points.
219 30 Dec 04 peter 106     ///
1295 12 May 08 jari 107     long n(void) const;
219 30 Dec 04 peter 108
219 30 Dec 04 peter 109     ///
703 18 Dec 06 jari 110     /// @brief Reset everything to zero
138 20 Aug 04 peter 111     ///
703 18 Dec 06 jari 112     void reset(void);
138 20 Aug 04 peter 113
138 20 Aug 04 peter 114     ///
197 27 Oct 04 jari 115     /// @return The sum of xy.
138 20 Aug 04 peter 116     ///
718 26 Dec 06 jari 117     double sum_xy(void) const;
138 20 Aug 04 peter 118
1187 28 Feb 08 peter 119     /**
1187 28 Feb 08 peter 120        \return Sum of squared deviation between x and y \f$
1187 28 Feb 08 peter 121        \sum (x-y)^2 \f$
1187 28 Feb 08 peter 122     */
772 25 Feb 07 peter 123     double sum_squared_deviation(void) const;
772 25 Feb 07 peter 124
772 25 Feb 07 peter 125     ///
597 28 Aug 06 markus 126     /// @return \f$ \sum_i (x_i-m_x)(y_i-m_y) \f$
219 30 Dec 04 peter 127     ///
718 26 Dec 06 jari 128     double sum_xy_centered(void) const;
219 30 Dec 04 peter 129
219 30 Dec 04 peter 130     ///
197 27 Oct 04 jari 131     /// @return A const refencer to the averager object for x.
138 20 Aug 04 peter 132     ///
718 26 Dec 06 jari 133     const Averager& x_averager(void) const;
138 20 Aug 04 peter 134
138 20 Aug 04 peter 135     ///
197 27 Oct 04 jari 136     /// @return A const reference to the averager object for y
138 20 Aug 04 peter 137     ///
718 26 Dec 06 jari 138     const Averager& y_averager(void) const;
138 20 Aug 04 peter 139
138 20 Aug 04 peter 140     ///
703 18 Dec 06 jari 141     /// @brief The assigment operator
138 20 Aug 04 peter 142     ///
703 18 Dec 06 jari 143     const AveragerPair& operator=(const AveragerPair& a);
138 20 Aug 04 peter 144
138 20 Aug 04 peter 145     ///
197 27 Oct 04 jari 146     /// Operator to add another Averager
138 20 Aug 04 peter 147     ///
138 20 Aug 04 peter 148     const AveragerPair& operator+=(const AveragerPair&);
138 20 Aug 04 peter 149
138 20 Aug 04 peter 150   private:
138 20 Aug 04 peter 151     Averager x_;
138 20 Aug 04 peter 152     Averager y_;
2565 26 Sep 11 peter 153     double  xy_centered_;
138 20 Aug 04 peter 154
2565 26 Sep 11 peter 155     void xy_add(double mx, double my, double xy_centered, long n);
138 20 Aug 04 peter 156   };
138 20 Aug 04 peter 157
886 24 Sep 07 peter 158   /**
1043 06 Feb 08 peter 159      \brief adding data from two ranges to AveragerPair \a ap
1886 31 Mar 09 peter 160
3529 11 Oct 16 peter 161      Type Requirements:
3529 11 Oct 16 peter 162      - \c InputIterator1 is \readable_iterator
3529 11 Oct 16 peter 163      - \c InputIterator1 is unweighted
3529 11 Oct 16 peter 164      - \c InputIterator1 is \single_pass_iterator
3529 11 Oct 16 peter 165      - \c InputIterator2 is \readable_iterator
3529 11 Oct 16 peter 166      - \c InputIterator2 is unweighted
3529 11 Oct 16 peter 167      - \c InputIterator2 is \single_pass_iterator
3529 11 Oct 16 peter 168
1886 31 Mar 09 peter 169      \relates AveragerPair
886 24 Sep 07 peter 170    */
2202 21 Feb 10 peter 171   template <class InputIterator1, class InputIterator2>
3529 11 Oct 16 peter 172   void add(AveragerPair& ap, InputIterator1 first1, InputIterator1 last1,
2202 21 Feb 10 peter 173            InputIterator2 first2)
886 24 Sep 07 peter 174   {
3529 11 Oct 16 peter 175     BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<InputIterator1>));
3529 11 Oct 16 peter 176     BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator1>));
3529 11 Oct 16 peter 177     BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<InputIterator2>));
3529 11 Oct 16 peter 178     BOOST_CONCEPT_ASSERT((boost_concepts::SinglePassIterator<InputIterator2>));
3529 11 Oct 16 peter 179
914 29 Sep 07 peter 180     utility::check_iterator_is_unweighted(first1);
914 29 Sep 07 peter 181     utility::check_iterator_is_unweighted(first2);
886 24 Sep 07 peter 182     for ( ; first1 != last1; ++first1, ++first2)
886 24 Sep 07 peter 183       ap.add(*first1, *first2);
886 24 Sep 07 peter 184   }
886 24 Sep 07 peter 185
886 24 Sep 07 peter 186
680 11 Oct 06 jari 187 }}} // of namespace statistics, yat, and theplu
138 20 Aug 04 peter 188
138 20 Aug 04 peter 189 #endif