# Changeset 1145 for trunk/yat/statistics/PearsonCorrelation.h

Ignore:
Timestamp:
Feb 25, 2008, 9:23:47 PM (15 years ago)
Message:

fixes #292

File:
1 edited

### Legend:

Unmodified
 r1140 */ #include "AveragerPair.h" #include "AveragerPairWeighted.h" #include "yat/classifier/Target.h" #include "yat/utility/iterator_traits.h" namespace theplu { namespace yat { namespace utility { class VectorBase; } namespace statistics { /** \f$\frac{\vert \sum_i(x_i-\bar{x})(y_i-\bar{y})\vert }{\sqrt{\sum_i (x_i-\bar{x})^2\sum_i (x_i-\bar{x})^2}} \f$. Adding a data value to PearsonCorrelation. */ void add(double value, bool target, double weight=1.0); /** \brief correlation If ForwardIterator is weighted correlation is calculated as \f$\frac{\vert \sum_iw^2_i(x_i-\bar{x})(y_i-\bar{y})\vert } {\sqrt{\sum_iw^2_i(x_i-\bar{x})^2\sum_iw^2_i(y_i-\bar{y})^2}} \f$, where \f$m_x = \frac{\sum w_ix_i}{\sum w_i} \f$ and \f$m_x = \frac{\sum w_ix_i}{\sum w_i} \f$. This expression is chosen to get a correlation equal to unity when \a x and \a y are equal. Correlation is calculated as implemented in AveragerPairWeighted @return Pearson correlation, if absolute=true absolute value of Pearson is used. @return Pearson correlation. */ template double score(const classifier::Target& target, ForwardIterator first, ForwardIterator last); /** \f$\frac{\vert \sum_iw^2_i(x_i-\bar{x})(y_i-\bar{y})\vert } {\sqrt{\sum_iw^2_i(x_i-\bar{x})^2\sum_iw^2_i(y_i-\bar{y})^2}} \f$, where \f$m_x = \frac{\sum w_ix_i}{\sum w_i} \f$ and \f$m_x = \frac{\sum w_ix_i}{\sum w_i} \f$. This expression is chosen to get a correlation equal to unity when \a x and \a y are equal. \return absolute value of weighted version of Pearson correlation. \note ietartors must be non-weighted */ template double score(const classifier::Target& target, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2); double score(void) const; /** correlation is zero (and the data is Gaussian). @note This function can only be used together with the unweighted score. P-value is calculated using function pearson_p_value(double, u_int) where degrees of freedom is calculated using n(void) in AveragerPairWeighted. @return one-sided p-value */ private: double r_; int nof_samples_; template double score(const classifier::Target& target, ForwardIterator first, ForwardIterator last, utility::unweighted_iterator_tag); template double score(const classifier::Target& target, ForwardIterator first, ForwardIterator last, utility::weighted_iterator_tag); AveragerPairWeighted ap_; }; template double PearsonCorrelation::score(const classifier::Target& target, ForwardIterator first, ForwardIterator last) { nof_samples_ = target.size(); using utility::yat_assert; yat_assert("PearsonCorrelation: sizes mismatch"); r_ = score(target, first, last, utility::iterator_traits::type()); return r_; } template double PearsonCorrelation::score(const classifier::Target& target, ForwardIterator first, ForwardIterator last, utility::unweighted_iterator_tag tag) { AveragerPair ap; for (size_t i=0; first!=last; ++first, ++i) ap.add(target.binary(i), *first); nof_samples_ = ap.n(); return ap.correlation(); } template double PearsonCorrelation::score(const classifier::Target& target, ForwardIterator first, ForwardIterator last, utility::weighted_iterator_tag tag) { AveragerPairWeighted ap; for (size_t i=0; first!=last; ++first, ++i) ap.add(target.binary(i), first.data(), 1.0, first.weight()); nof_samples_ = ap.n(); return ap.correlation(); } template double PearsonCorrelation::score(const classifier::Target& target, ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2) { utility::check_iterator_is_unweighted(first1); utility::check_iterator_is_unweighted(first2); AveragerPairWeighted ap; for (size_t i=0; first1!=last1; ++first1, ++i, ++first2) ap.add(target.binary(i), *first1, 1.0, *first2); nof_samples_ = ap.n(); r_ = ap.correlation(); return r_; } }}} // of namespace statistics, yat, and theplu