#ifndef theplu_yat_statistics_pearson_distance_h
#define theplu_yat_statistics_pearson_distance_h
// $Id: PearsonDistance.h 1115 2008-02-21 19:20:59Z markus $
/*
Copyright (C) 2007 Peter Johansson, Markus Ringnér
Copyright (C) 2008 Peter Johansson
This file is part of the yat library, http://trac.thep.lu.se/yat
The yat library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The yat library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/
#include "AveragerPair.h"
#include "AveragerPairWeighted.h"
#include "yat/utility/iterator_traits.h"
namespace theplu {
namespace yat {
namespace statistics {
///
/// @brief Calculates the %Pearson correlation distance between two points given by elements of ranges.
///
/// This class is modelling the concept \ref concept_distance.
///
struct PearsonDistance
{
/**
\brief Calculates the %Pearson correlation distance between
elements of two ranges.
If elements of both ranges are unweighted the distance is
calculated as \f$ 1-\mbox{C}(x,y) \f$, where \f$ x \f$ and \f$
y \f$ are the two points and C is the %Pearson correlation.
If elements of one or both of ranges have weights the distance
is calculated as \f$ 1-[\sum w_{x,i}w_{y,i}(x_i-y_i)^2/(\sum
w_{x,i}w_{y,i}(x_i-m_x)^2\sum w_{x,i}w_{y,i}(y_i-m_y)^2)] \f$,
where and \f$ w_x \f$ and \f$ w_y \f$ are weights for the
elements of the first and the second range, respectively, and
\f$ m_x=\sum w_{x,i}w_{y,i}x_i/\sum w_{x,i}w_{y,i} \f$ and
correspondingly for \f$ m_y \f$. If the elements of one of the
two ranges are unweighted, the weights for these elements are
set to unity.
*/
template
double operator()
(Iter1 beg1,Iter1 end1, Iter2 beg2) const
{
return this->distance(beg1, end1, beg2,
typename utility::weighted_if_any2::type());
}
private:
template
double distance (Iter1 beg1,Iter1 end1, Iter2 beg2,
utility::unweighted_iterator_tag) const
{
AveragerPairWeighted ap;
add(ap,beg1,end1,beg2);
return 1-ap.correlation();
}
template
double distance (Iter1 beg1,Iter1 end1, Iter2 beg2,
utility::weighted_iterator_tag) const
{
AveragerPairWeighted ap;
add(ap,beg1,end1,beg2);
return 1-ap.correlation();
}
};
}}} // of namespace statistics, yat, and theplu
#endif