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

fixes #292

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/statistics/PearsonCorrelation.h

    r1140 r1145  
    2727*/
    2828
    29 #include "AveragerPair.h"
    3029#include "AveragerPairWeighted.h"
    31 #include "yat/classifier/Target.h"
    32 #include "yat/utility/iterator_traits.h"
    33 
    3430
    3531namespace theplu {
    3632namespace yat {
    37 namespace utility {
    38   class VectorBase;
    39 }
    4033namespace statistics {
    4134
     
    5851   
    5952    /**
    60        \f$ \frac{\vert \sum_i(x_i-\bar{x})(y_i-\bar{y})\vert
    61        }{\sqrt{\sum_i (x_i-\bar{x})^2\sum_i (x_i-\bar{x})^2}} \f$.
     53       Adding a data value to PearsonCorrelation.
     54    */
     55    void add(double value, bool target, double weight=1.0);
    6256
     57    /**
     58       \brief correlation
    6359
    64        If ForwardIterator is weighted correlation is calculated as
    65        \f$ \frac{\vert \sum_iw^2_i(x_i-\bar{x})(y_i-\bar{y})\vert }
    66        {\sqrt{\sum_iw^2_i(x_i-\bar{x})^2\sum_iw^2_i(y_i-\bar{y})^2}}
    67        \f$, where \f$ m_x = \frac{\sum w_ix_i}{\sum w_i} \f$ and \f$
    68        m_x = \frac{\sum w_ix_i}{\sum w_i} \f$. This expression is
    69        chosen to get a correlation equal to unity when \a x and \a y
    70        are equal.
     60       Correlation is calculated as implemented in AveragerPairWeighted
    7161
    72        @return Pearson correlation, if absolute=true absolute value
    73        of Pearson is used.
     62       @return Pearson correlation.
    7463    */
    75     template<typename ForwardIterator>
    76     double score(const classifier::Target& target,
    77                  ForwardIterator first, ForwardIterator last);
    78    
    79     /**
    80        \f$ \frac{\vert \sum_iw^2_i(x_i-\bar{x})(y_i-\bar{y})\vert }
    81        {\sqrt{\sum_iw^2_i(x_i-\bar{x})^2\sum_iw^2_i(y_i-\bar{y})^2}}
    82        \f$, where \f$ m_x = \frac{\sum w_ix_i}{\sum w_i} \f$ and \f$
    83        m_x = \frac{\sum w_ix_i}{\sum w_i} \f$. This expression is
    84        chosen to get a correlation equal to unity when \a x and \a y
    85        are equal.
    86 
    87        \return absolute value of weighted version of Pearson
    88        correlation.
    89 
    90        \note ietartors must be non-weighted
    91     */
    92     template<typename ForwardIterator1, typename ForwardIterator2>
    93     double score(const classifier::Target& target,
    94                  ForwardIterator1 first1, ForwardIterator1 last1,
    95                  ForwardIterator2 first2);
     64    double score(void) const;
    9665   
    9766    /**
     
    10069       correlation is zero (and the data is Gaussian).
    10170       
    102        @note This function can only be used together with the
    103        unweighted score.
    104        
     71       P-value is calculated using function pearson_p_value(double,
     72       u_int) where degrees of freedom is calculated using n(void) in
     73       AveragerPairWeighted.
     74
    10575       @return one-sided p-value
    10676    */
     
    10878   
    10979  private:
    110     double r_;
    111     int nof_samples_;
    112    
    113     template<typename ForwardIterator>
    114     double score(const classifier::Target& target,
    115                  ForwardIterator first, ForwardIterator last,
    116                  utility::unweighted_iterator_tag);
    117    
    118     template<typename ForwardIterator>
    119     double score(const classifier::Target& target,
    120                  ForwardIterator first, ForwardIterator last,
    121                  utility::weighted_iterator_tag);
     80    AveragerPairWeighted ap_;
    12281   
    12382  };
    124  
    125   template<typename ForwardIterator>
    126   double PearsonCorrelation::score(const classifier::Target& target,
    127                                    ForwardIterator first,
    128                                    ForwardIterator last)
    129   {
    130     nof_samples_ = target.size();
    131       using utility::yat_assert;
    132       yat_assert<std::runtime_error>("PearsonCorrelation: sizes mismatch");
    133       r_ = score(target, first, last,
    134                  utility::iterator_traits<ForwardIterator>::type());
    135       return r_;
    136   }
    137    
    138 
    139   template<typename ForwardIterator>
    140   double PearsonCorrelation::score(const classifier::Target& target,
    141                                    ForwardIterator first,
    142                                    ForwardIterator last,
    143                                    utility::unweighted_iterator_tag tag)
    144    
    145   {
    146     AveragerPair ap;
    147     for (size_t i=0; first!=last; ++first, ++i)
    148       ap.add(target.binary(i), *first);
    149     nof_samples_ = ap.n();
    150     return ap.correlation();
    151   }
    152    
    153 
    154   template<typename ForwardIterator>
    155   double PearsonCorrelation::score(const classifier::Target& target,
    156                                    ForwardIterator first,
    157                                    ForwardIterator last,
    158                                    utility::weighted_iterator_tag tag)
    159    
    160   {
    161     AveragerPairWeighted ap;
    162     for (size_t i=0; first!=last; ++first, ++i)
    163       ap.add(target.binary(i), first.data(), 1.0, first.weight());
    164     nof_samples_ = ap.n();
    165     return ap.correlation();
    166   }
    167    
    168   template<typename ForwardIterator1, typename ForwardIterator2>
    169   double PearsonCorrelation::score(const classifier::Target& target,
    170                                    ForwardIterator1 first1,
    171                                    ForwardIterator1 last1,
    172                                    ForwardIterator2 first2)
    173   {
    174     utility::check_iterator_is_unweighted(first1);
    175     utility::check_iterator_is_unweighted(first2);
    176     AveragerPairWeighted ap;
    177     for (size_t i=0; first1!=last1; ++first1, ++i, ++first2)
    178       ap.add(target.binary(i), *first1, 1.0, *first2);
    179     nof_samples_ = ap.n();
    180     r_ = ap.correlation();
    181     return r_;
    182   }
    18383 
    18484}}} // of namespace statistics, yat, and theplu
Note: See TracChangeset for help on using the changeset viewer.