Ignore:
Timestamp:
Mar 5, 2007, 7:58:30 PM (15 years ago)
Author:
Peter
Message:

Refs #101

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/yat/statistics/tTest.cc

    r776 r779  
    2222*/
    2323
     24#include "tTest.h"
    2425#include "tScore.h"
    25 #include "Averager.h"
    26 #include "AveragerWeighted.h"
    27 #include "yat/classifier/DataLookupWeighted1D.h"
    28 #include "yat/classifier/Target.h"
    29 #include "yat/utility/vector.h"
     26//#include "Averager.h"
     27//#include "AveragerWeighted.h"
     28//#include "yat/classifier/DataLookupWeighted1D.h"
     29//#include "yat/classifier/Target.h"
     30//#include "yat/utility/vector.h"
    3031
    3132#include <cassert>
     
    3637namespace statistics { 
    3738
    38   tScore::tScore(bool b)
    39     : Score(b),  t_(0)
     39  tTest::tTest(bool b)
     40    : t_(0)
    4041  {
    4142  }
    4243
    43   double tScore::score(const classifier::Target& target,
    44                        const utility::vector& value)
     44  double tTest::score(const classifier::Target& target,
     45                      const utility::vector& value)
    4546  {
    46     weighted_=false;
    47     statistics::Averager positive;
    48     statistics::Averager negative;
    49     for(size_t i=0; i<target.size(); i++){
    50       if (target.binary(i))
    51         positive.add(value(i));
    52       else
    53         negative.add(value(i));
    54     }
    55     double diff = positive.mean() - negative.mean();
    56     dof_=positive.n()+negative.n()-2;
    57     double s2=(positive.sum_xx_centered()+negative.sum_xx_centered())/dof_;
    58 
    59     t_=diff/sqrt(s2/positive.n()+s2/negative.n());
    60     if (t_<0 && absolute_)
    61       t_=-t_;
    62      
     47    tScore score;
     48    t_ = score.score(target, value, &dof_);
    6349    return t_;
    6450  }
    6551
    6652
    67   double tScore::score(const classifier::Target& target,
    68                        const classifier::DataLookupWeighted1D& value)
     53  double tTest::score(const classifier::Target& target,
     54                      const classifier::DataLookupWeighted1D& value)
    6955  {
    70     weighted_=true;
    71 
    72     statistics::AveragerWeighted positive;
    73     statistics::AveragerWeighted negative;
    74     for(size_t i=0; i<target.size(); i++){
    75       if (target.binary(i))
    76         positive.add(value.data(i),value.weight(i));
    77       else
    78         negative.add(value.data(i),value.weight(i));
    79     }
    80     double diff = positive.mean() - negative.mean();
    81     dof_=positive.n()+negative.n()-2;
    82     double s2=(positive.sum_xx_centered()+negative.sum_xx_centered())/dof_;
    83     t_=diff/sqrt(s2/positive.n()+s2/(negative.n()));
    84     if (t_<0 && absolute_)
    85       t_=-t_;
    86 
    87     if(positive.sum_w()==0 || negative.sum_w()==0)
    88       t_=0;
     56    tScore score;
     57    t_ = score.score(target, value, &dof_);
    8958    return t_;
    9059  }
    9160
    9261
    93   double tScore::score(const classifier::Target& target,
    94                        const utility::vector& value,
    95                        const utility::vector& weight)
     62  double tTest::score(const classifier::Target& target,
     63                      const utility::vector& value,
     64                      const utility::vector& weight)
    9665  {
    97     weighted_=true;
    98 
    99     statistics::AveragerWeighted positive;
    100     statistics::AveragerWeighted negative;
    101     for(size_t i=0; i<target.size(); i++){
    102       if (target.binary(i))
    103         positive.add(value(i),weight(i));
    104       else
    105         negative.add(value(i),weight(i));
    106     }
    107     double diff = positive.mean() - negative.mean();
    108     dof_=positive.n()+negative.n()-2;
    109     double s2=(positive.sum_xx_centered()+negative.sum_xx_centered())/dof_;
    110     t_=diff/sqrt(s2/positive.n()+s2/(negative.n()));
    111     if (t_<0 && absolute_)
    112       t_=-t_;
    113 
    114     if(positive.sum_w()==0 || negative.sum_w()==0)
    115       t_=0;
     66    tScore score;
     67    t_ = score.score(target, value, weight, &dof_);
    11668    return t_;
    11769  }
    11870
    119   double tScore::p_value(void) const
     71  double tTest::p_value(void) const
    12072  {
    121     double p = gsl_cdf_tdist_Q(t_, dof_);
    122     return (dof_ > 0 && !weighted_) ? p : 1;
     73    if (!dof_)
     74      return 1.0;
     75    return gsl_cdf_tdist_Q(t_, dof_);
    12376  }
    12477
Note: See TracChangeset for help on using the changeset viewer.