source: trunk/yat/statistics/AveragerPair.h

Last change on this file was 3550, checked in by Peter, 5 years ago

Update copyright years. Happy New Year

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