Ignore:
Timestamp:
Aug 8, 2008, 12:47:18 AM (13 years ago)
Author:
Peter
Message:

refs #366 - weighted percentile
Added a class Percentile that can calculate the percentile for both
unweighted and weighted ranges. In order to make sense of the weighted
case, I had to modify the definition of percentile (slightly). The old
function percentile is using the old definition, but a new function,
percentile2, is using the new function and is simply calling the
Percentile class. The median is the same for the two definitions and
therefore it makes no difference which function to call, but to enable
calculation of weighted median percentile2 is called.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/statistics_test.cc

    r1317 r1404  
    3030#include "yat/statistics/Average.h"
    3131#include "yat/statistics/utility.h"
     32#include "yat/utility/MatrixWeighted.h"
    3233#include "yat/utility/Vector.h"
    3334
     
    5657  data.resize(1);
    5758  statistics::median(data.begin(), data.end());
     59  // testing percentile2
     60  data.resize(5);
     61  std::copy(gsl_vec.begin(), gsl_vec.begin()+5, data.begin());
     62  std::map<double, double> correct;
     63  correct[0]=0;
     64  correct[10]=0;
     65  correct[30]=1;
     66  correct[40]=1.5;
     67  correct[50]=2;
     68  correct[70]=3;
     69  correct[90]=4;
     70  correct[100]=4;
     71  for (std::map<double, double>::const_iterator i=correct.begin();
     72       i!=correct.end(); ++i) {
     73    if (!suite.add(suite.equal(statistics::percentile2(data.begin(), data.end(),
     74                                                       i->first),
     75                               i->second))) {
     76      suite.err() << "error: unweighted percentile2 " << i->first
     77                  << "th percentile = "
     78                  << statistics::percentile2(data.begin(),data.end(),i->first)
     79                  << " expected to be " << i->second
     80                  << std::endl;
     81    }
     82  }
     83
     84  utility::MatrixWeighted mw(1,data.size(),1);
     85  for (size_t i=0; i<mw.columns(); ++i)
     86    mw(0,i).data()=data[i];
     87  if (!suite.equal(statistics::median(mw.begin_row(0), mw.end_row(0), true),2))
     88    suite.err() << "error: weighted median sorted failed" << std::endl;
     89  if (!suite.equal(statistics::median(mw.begin_row(0), mw.end_row(0), false),2))
     90    suite.err() << "error: weighted median failed" << std::endl;
     91  for (std::map<double, double>::const_iterator i=correct.begin();
     92       i!=correct.end(); ++i) {
     93    if (!suite.add(suite.equal(statistics::percentile2(mw.begin_row(0),
     94                                                       mw.end_row(0),
     95                                                       i->first),
     96                               i->second))) {
     97      suite.err() << "error: weighted percentile2 " << i->first
     98                  << "th percentile = "
     99                  << statistics::percentile2(mw.begin_row(0),
     100                                             mw.end_row(0), i->first)
     101                  << " expected to be " << i->second
     102                  << std::endl;
     103    }
     104  }
    58105
    59106  double skewness_gsl=statistics::skewness(gsl_vec);
Note: See TracChangeset for help on using the changeset viewer.