# source:trunk/yat/statistics/PearsonDistance.h@1115

Last change on this file since 1115 was 1115, checked in by Markus Ringnér, 14 years ago

Fixes #254 and #295

• Property svn:eol-style set to native
• Property svn:keywords set to Id
File size: 2.9 KB
Line
1#ifndef theplu_yat_statistics_pearson_distance_h
2#define theplu_yat_statistics_pearson_distance_h
3
4// $Id: PearsonDistance.h 1115 2008-02-21 19:20:59Z markus$
5
6/*
7  Copyright (C) 2007 Peter Johansson, Markus Ringnér
8  Copyright (C) 2008 Peter Johansson
9
10  This file is part of the yat library, http://trac.thep.lu.se/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 2 of the
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25  02111-1307, USA.
26*/
27
28#include "AveragerPair.h"
29#include "AveragerPairWeighted.h"
30#include "yat/utility/iterator_traits.h"
31
32namespace theplu {
33namespace yat {
34namespace statistics {
35
36  ///
37  /// @brief Calculates the %Pearson correlation distance between two points given by elements of ranges.
38  ///
39  /// This class is modelling the concept \ref concept_distance.
40  ///
41  struct PearsonDistance
42  {
43    /**
44       \brief Calculates the %Pearson correlation distance between
45       elements of two ranges.
46
47       If elements of both ranges are unweighted the distance is
48       calculated as \f$1-\mbox{C}(x,y) \f$, where \f$x \f$ and \f$49 y \f$ are the two points and C is the %Pearson correlation.
50
51       If elements of one or both of ranges have weights the distance
52       is calculated as \f$1-[\sum w_{x,i}w_{y,i}(x_i-y_i)^2/(\sum 53 w_{x,i}w_{y,i}(x_i-m_x)^2\sum w_{x,i}w_{y,i}(y_i-m_y)^2)] \f$,
54       where and \f$w_x \f$ and \f$w_y \f$ are weights for the
55       elements of the first and the second range, respectively, and
56       \f$m_x=\sum w_{x,i}w_{y,i}x_i/\sum w_{x,i}w_{y,i} \f$ and
57       correspondingly for \f$m_y \f$.  If the elements of one of the
58       two ranges are unweighted, the weights for these elements are
59       set to unity.
60    */
61    template <typename Iter1, typename Iter2>
62    double operator()
63    (Iter1 beg1,Iter1 end1, Iter2 beg2) const
64    {
65      return this->distance(beg1, end1, beg2,
66                      typename utility::weighted_if_any2<Iter1,Iter2>::type());
67    }
68
69  private:
70    template <typename Iter1, typename Iter2>
71    double distance (Iter1 beg1,Iter1 end1, Iter2 beg2,
72                     utility::unweighted_iterator_tag) const
73    {
74      AveragerPairWeighted ap;
76      return 1-ap.correlation();
77    }
78
79    template <typename Iter1, typename Iter2>
80    double distance (Iter1 beg1,Iter1 end1, Iter2 beg2,
81                     utility::weighted_iterator_tag) const
82    {
83      AveragerPairWeighted ap;