source: trunk/test/statistics_test.cc @ 1418

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

refs #366 - need to polish on some special cases

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1// $Id: statistics_test.cc 1418 2008-08-19 23:15:32Z 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));
100  }
101  test_percentiler(suite, x.begin(), x.end(), 50, 2);
102  x.push_back(5);
103  test_percentiler(suite, x.begin(), x.end(), 50, 2.5);
104  test_percentiler(suite, x.begin(), x.end(), 25, 1);
105  test_percentiler(suite, x.begin(), x.end(), 0, 0);
106  test_percentiler(suite, x.begin(), x.end(), 10, 0);
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 with unity weights" << std::endl;
125  cmp_percentiler(suite, x.begin(), x.end(), xw.begin(), xw.end());
126
127  suite.err() << "testing that w=0 equals removed data point\n";
128  xw=xw_orig;
129  std::vector<utility::DataWeight> xw2(xw_orig);
130  xw[3].weight() = 0.0;
131  xw2.erase(xw2.begin()+3);
132  cmp_percentiler(suite, xw.begin(), xw.end(), xw2.begin(), xw2.end());
133
134  suite.err() << "testing rescaling of weights\n";
135  xw2 = xw;
136  for (size_t i=0; i<xw2.size(); ++i)
137    xw2[i].weight()*=2;
138  cmp_percentiler(suite, xw.begin(), xw.end(), xw2.begin(), xw2.end());
139
140}
141
142template<typename RandomAccessIterator>
143void test_percentiler(test::Suite& suite, 
144                      RandomAccessIterator first, 
145                      RandomAccessIterator last,
146                      double p, double correct)
147{
148  using statistics::percentile2;
149  double x = percentile2(first, last, p);
150  if (!suite.add(suite.equal(x, correct, 10))) {
151    suite.err() << "Error in percentile2\n";
152    suite.err() << "  calculated value: " << x << "\n";
153    suite.err() << "  expected value: " << correct << "\n";
154  }
155}
156
157template<typename RandomAccessIterator1, typename RandomAccessIterator2>
158void cmp_percentiler(test::Suite& suite, 
159                     RandomAccessIterator1 first1, 
160                     RandomAccessIterator1 last1,
161                     RandomAccessIterator2 first2,
162                     RandomAccessIterator2 last2)
163{
164  for (double p=0; p<100; p+=10) {
165    double correct=statistics::percentile2(first1, last1, p);
166    test_percentiler(suite, first2, last2, p, correct);
167  }
168
169}
Note: See TracBrowser for help on using the repository browser.