source: trunk/test/statistics_test.cc @ 1420

Last change on this file since 1420 was 1420, checked in by Peter, 15 years ago

fixing the weighted implementation of Percentiler - refs #366. Some documentation is needed before closing the ticket.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1// $Id: statistics_test.cc 1420 2008-08-20 17:33:39Z 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 <cmath>
36#include <cstdlib>
37#include <iostream>
38#include <map>
39#include <vector>
40
41using namespace theplu::yat;
42void test_percentiler(test::Suite&);
43
44template<typename RandomAccessIterator>
45void test_percentiler(test::Suite&, RandomAccessIterator, 
46                      RandomAccessIterator,
47                      double p, double correct);
48
49template<typename RandomAccessIterator1, typename RandomAccessIterator2>
50void cmp_percentiler(test::Suite&, 
51                     RandomAccessIterator1, 
52                     RandomAccessIterator1,
53                     RandomAccessIterator2,
54                     RandomAccessIterator2);
55
56int main(int argc, char* argv[])
57{ 
58  test::Suite suite(argc, argv);
59
60  utility::Vector gsl_vec(10);
61  std::vector<double> data;
62  for (unsigned int i=0; i<10; i++){
63    data.push_back(static_cast<double>(i));
64    gsl_vec(i)=i;
65  }
66
67  double m=statistics::median(data.begin(), data.end());
68  double m_gsl=statistics::median(gsl_vec.begin(), gsl_vec.end());
69  if (m!=4.5 || m!=m_gsl)
70    suite.add(false);
71  statistics::percentile(data.begin(), data.end(), 100);
72  data.resize(1);
73  statistics::median(data.begin(), data.end());
74  // testing percentile2
75  test_percentiler(suite);
76
77  double skewness_gsl=statistics::skewness(gsl_vec);
78  if (!suite.equal(1-skewness_gsl, 1.0) )
79    suite.add(false);
80  double kurtosis_gsl=statistics::kurtosis(gsl_vec);
81  if (!(std::abs(kurtosis_gsl+1.5616363636363637113)<1e-10) ) 
82    suite.add(false); 
83  statistics::Average func;
84  suite.add(suite.equal(func(gsl_vec.begin(), gsl_vec.end()),4.5));
85  // easiest way to get a weighted iterator
86  classifier::MatrixLookupWeighted mlw(10,20,2.0, 1.0);
87  suite.add(suite.equal(func(mlw.begin(), mlw.end()),2.0));
88 
89
90  return suite.return_value();
91}
92
93void test_percentiler(test::Suite& suite)
94{
95  suite.err() << "testing unweighted percentile2" << std::endl;
96  std::vector<double> x;
97  x.reserve(6);
98  for (unsigned int i=0; i<5; i++){
99    x.push_back(static_cast<double>(i+1));
100  }
101  test_percentiler(suite, x.begin(), x.end(), 50, 3);
102  x.push_back(6);
103  test_percentiler(suite, x.begin(), x.end(), 50, 3.5);
104  test_percentiler(suite, x.begin(), x.end(), 25, 2);
105  test_percentiler(suite, x.begin(), x.end(), 0, 1);
106  test_percentiler(suite, x.begin(), x.end(), 10, 1);
107
108  suite.err() << "testing duplication of data\n";
109  std::vector<double> x2(x);
110  for (size_t i=0; i<x.size(); ++i)
111    x2.push_back(x[i]);
112  cmp_percentiler(suite, x.begin(), x.end(), x2.begin(), x2.end());
113
114
115  // testing weighted
116
117  suite.err() << "testing weighted percentile2" << std::endl;
118  std::vector<utility::DataWeight> xw(x.size());
119  for (size_t i=0; i<xw.size(); ++i) {
120    xw[i].data() = x[i];
121    xw[i].weight() = 1.0;
122  }
123  const std::vector<utility::DataWeight> xw_orig(xw);
124  suite.err() << "testing weighted" << std::endl;
125  test_percentiler(suite, xw.begin(), xw.end(), 0, 1);
126  test_percentiler(suite, xw.begin(), xw.end(), 100, 6);
127  test_percentiler(suite, xw.begin(), xw.end(), 49, 3);
128  test_percentiler(suite, xw.begin(), xw.end(), 51, 4);
129  test_percentiler(suite, xw.begin(), xw.end(), 50, 3.5);
130  test_percentiler(suite, x.begin(), x.end(), 10, 1);
131
132  suite.err() << "testing weighted with unity weights" << std::endl;
133  cmp_percentiler(suite, x.begin(), x.end(), xw.begin(), xw.end());
134
135  suite.err() << "testing that w=0 equals removed data point\n";
136  xw=xw_orig;
137  std::vector<utility::DataWeight> xw2(xw_orig);
138  xw[3].weight() = 0.0;
139  xw2.erase(xw2.begin()+3);
140  cmp_percentiler(suite, xw.begin(), xw.end(), xw2.begin(), xw2.end());
141
142  suite.err() << "testing rescaling of weights\n";
143  xw2 = xw;
144  for (size_t i=0; i<xw2.size(); ++i)
145    xw2[i].weight()*=2;
146  cmp_percentiler(suite, xw.begin(), xw.end(), xw2.begin(), xw2.end());
147
148}
149
150template<typename RandomAccessIterator>
151void test_percentiler(test::Suite& suite, 
152                      RandomAccessIterator first, 
153                      RandomAccessIterator last,
154                      double p, double correct)
155{
156  using statistics::percentile2;
157  double x = percentile2(first, last, p);
158  if (!suite.add(suite.equal(x, correct, 10))) {
159    suite.err() << "Error in percentile2 for " << p << "th percentile \n";
160    suite.err() << "  calculated value: " << x << "\n";
161    suite.err() << "  expected value: " << correct << "\n";
162  }
163}
164
165template<typename RandomAccessIterator1, typename RandomAccessIterator2>
166void cmp_percentiler(test::Suite& suite, 
167                     RandomAccessIterator1 first1, 
168                     RandomAccessIterator1 last1,
169                     RandomAccessIterator2 first2,
170                     RandomAccessIterator2 last2)
171{
172  for (double p=0; p<100; p+=10) {
173    double correct=statistics::percentile2(first1, last1, p);
174    test_percentiler(suite, first2, last2, p, correct);
175  }
176
177}
Note: See TracBrowser for help on using the repository browser.