source: trunk/test/statistics_test.cc @ 1619

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

Added structure to deprecate functions. I chose to use a gcc style,
and it is tested in configure whether the compiler supports it. If not
the deprecation has no effect. Using a deprecated function will cause
a compiler warning. In gcc the warning can be turned off with
'-Wno-deprecated'. Possibly we should turn off the warning by default,
so a user has to turn it on by defining
-DYAT_DISABLE_DEPRECATED...

fixes #367

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