yat/statistics/PearsonDistance.h

Code
Comments
Other
Rev Date Author Line
1031 04 Feb 08 markus 1 #ifndef theplu_yat_statistics_pearson_distance_h
1031 04 Feb 08 markus 2 #define theplu_yat_statistics_pearson_distance_h
889 25 Sep 07 markus 3
889 25 Sep 07 markus 4 // $Id$
889 25 Sep 07 markus 5
999 23 Dec 07 jari 6 /*
2119 12 Dec 09 peter 7   Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson, Markus Ringnér
4359 23 Aug 23 peter 8   Copyright (C) 2010 Peter Johansson
999 23 Dec 07 jari 9
1437 25 Aug 08 peter 10   This file is part of the yat library, http://dev.thep.lu.se/yat
999 23 Dec 07 jari 11
999 23 Dec 07 jari 12   The yat library is free software; you can redistribute it and/or
999 23 Dec 07 jari 13   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 14   published by the Free Software Foundation; either version 3 of the
999 23 Dec 07 jari 15   License, or (at your option) any later version.
999 23 Dec 07 jari 16
999 23 Dec 07 jari 17   The yat library is distributed in the hope that it will be useful,
999 23 Dec 07 jari 18   but WITHOUT ANY WARRANTY; without even the implied warranty of
999 23 Dec 07 jari 19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
999 23 Dec 07 jari 20   General Public License for more details.
999 23 Dec 07 jari 21
999 23 Dec 07 jari 22   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 23   along with yat. If not, see <http://www.gnu.org/licenses/>.
999 23 Dec 07 jari 24 */
999 23 Dec 07 jari 25
1306 14 May 08 peter 26 #include "averager_traits.h"
937 05 Oct 07 peter 27 #include "yat/utility/iterator_traits.h"
889 25 Sep 07 markus 28
889 25 Sep 07 markus 29 namespace theplu {
889 25 Sep 07 markus 30 namespace yat {
889 25 Sep 07 markus 31 namespace statistics {
889 25 Sep 07 markus 32
1069 11 Feb 08 markus 33   ///
3531 11 Oct 16 peter 34   /// @brief Calculates the %Pearson correlation distance between two points given by elements of ranges.
1069 11 Feb 08 markus 35   ///
1115 21 Feb 08 markus 36   /// This class is modelling the concept \ref concept_distance.
1069 11 Feb 08 markus 37   ///
1050 07 Feb 08 peter 38   struct PearsonDistance
889 25 Sep 07 markus 39   {
1115 21 Feb 08 markus 40     /**
1115 21 Feb 08 markus 41        \brief Calculates the %Pearson correlation distance between
1115 21 Feb 08 markus 42        elements of two ranges.
3531 11 Oct 16 peter 43
1115 21 Feb 08 markus 44        If elements of both ranges are unweighted the distance is
1115 21 Feb 08 markus 45        calculated as \f$ 1-\mbox{C}(x,y) \f$, where \f$ x \f$ and \f$
1115 21 Feb 08 markus 46        y \f$ are the two points and C is the %Pearson correlation.
1115 21 Feb 08 markus 47
1115 21 Feb 08 markus 48        If elements of one or both of ranges have weights the distance
1115 21 Feb 08 markus 49        is calculated as \f$ 1-[\sum w_{x,i}w_{y,i}(x_i-y_i)^2/(\sum
1115 21 Feb 08 markus 50        w_{x,i}w_{y,i}(x_i-m_x)^2\sum w_{x,i}w_{y,i}(y_i-m_y)^2)] \f$,
1115 21 Feb 08 markus 51        where and \f$ w_x \f$ and \f$ w_y \f$ are weights for the
1115 21 Feb 08 markus 52        elements of the first and the second range, respectively, and
1115 21 Feb 08 markus 53        \f$ m_x=\sum w_{x,i}w_{y,i}x_i/\sum w_{x,i}w_{y,i} \f$ and
1115 21 Feb 08 markus 54        correspondingly for \f$ m_y \f$.  If the elements of one of the
1115 21 Feb 08 markus 55        two ranges are unweighted, the weights for these elements are
1115 21 Feb 08 markus 56        set to unity.
3531 11 Oct 16 peter 57     */
2202 21 Feb 10 peter 58     template <typename ForwardIterator1, typename ForwardIterator2>
3531 11 Oct 16 peter 59     double operator()(ForwardIterator1 beg1, ForwardIterator1 end1,
2202 21 Feb 10 peter 60                       ForwardIterator2 beg2) const
1050 07 Feb 08 peter 61     {
2202 21 Feb 10 peter 62       typename averager_pair<ForwardIterator1, ForwardIterator2>::type ap;
1050 07 Feb 08 peter 63       add(ap,beg1,end1,beg2);
1050 07 Feb 08 peter 64       return 1-ap.correlation();
1050 07 Feb 08 peter 65     }
1050 07 Feb 08 peter 66   };
890 25 Sep 07 markus 67
889 25 Sep 07 markus 68 }}} // of namespace statistics, yat, and theplu
3531 11 Oct 16 peter 69
889 25 Sep 07 markus 70 #endif