source: trunk/test/statistics_test.cc @ 1404

Last change on this file since 1404 was 1404, checked in by Peter, 13 years ago

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.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 KB
Line 
1// $Id: statistics_test.cc 1404 2008-08-07 22:47:18Z peter $
2
3/*
4  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2005 Peter Johansson
6  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
7  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2008 Peter Johansson
9
10  This file is part of the yat library, http://trac.thep.lu.se/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 2 of the
15  License, or (at your option) any later version.
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25  02111-1307, USA.
26*/
27
28#include "Suite.h"
29
30#include "yat/statistics/Average.h"
31#include "yat/statistics/utility.h"
32#include "yat/utility/MatrixWeighted.h"
33#include "yat/utility/Vector.h"
34
35#include <vector>
36#include <cstdlib>
37#include <iostream>
38#include <cmath>
39
40int main(int argc, char* argv[])
41{ 
42  using namespace theplu::yat;
43  test::Suite suite(argc, argv);
44
45  utility::Vector gsl_vec(10);
46  std::vector<double> data;
47  for (unsigned int i=0; i<10; i++){
48    data.push_back(static_cast<double>(i));
49    gsl_vec(i)=i;
50  }
51
52  double m=statistics::median(data.begin(), data.end());
53  double m_gsl=statistics::median(gsl_vec.begin(), gsl_vec.end());
54  if (m!=4.5 || m!=m_gsl)
55    suite.add(false);
56  statistics::percentile(data.begin(), data.end(), 100);
57  data.resize(1);
58  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  }
105
106  double skewness_gsl=statistics::skewness(gsl_vec);
107  if (!suite.equal(1-skewness_gsl, 1.0) )
108    suite.add(false);
109  double kurtosis_gsl=statistics::kurtosis(gsl_vec);
110  if (!(std::abs(kurtosis_gsl+1.5616363636363637113)<1e-10) ) 
111    suite.add(false); 
112  statistics::Average func;
113  suite.add(suite.equal(func(gsl_vec.begin(), gsl_vec.end()),4.5));
114  // easiest way to get a weighted iterator
115  classifier::MatrixLookupWeighted mlw(10,20,2.0, 1.0);
116  suite.add(suite.equal(func(mlw.begin(), mlw.end()),2.0));
117 
118
119  return suite.return_value();
120}
Note: See TracBrowser for help on using the repository browser.