DGtal  1.5.beta
testHistogram.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include <vector>
33 #include "DGtal/math/Statistic.h"
34 #include "DGtal/math/Histogram.h"
35 
37 
38 using namespace std;
39 using namespace DGtal;
40 
42 // Functions for testing class Histogram.
44 
48 double getRandomNumber( double first, double last )
49 {
50  double v = ((double)rand()) / (double) RAND_MAX;
51  return v * ( last - first ) + first;
52 }
53 
55 {
56  unsigned int nbok = 0;
57  unsigned int nb = 0;
58 
59  Statistic<double> stat( true );
60  for(unsigned int k=0; k < 1000; k++)
61  stat.addValue( getRandomNumber( -1.0, 1.0 ) );
62  stat.terminate();
63  Histogram<double> hist;
64  hist.init( Histogram<double>::Scott, stat );
65  hist.addValues( stat.begin(), stat.end() );
66  hist.terminate();
67  for ( unsigned int i = 0; i < hist.size(); ++i )
68  std::cout << i << " " << hist.pdf( i ) << " " << hist.cdf( i ) << std::endl;
69  ++nb; nbok += hist.cdf( hist.size()-1 ) > 0.9999 ? 1 : 0;
70  return nbok == nb;
71 }
72 
74 {
75  unsigned int nbok = 0;
76  unsigned int nb = 0;
77 
78  Statistic<double> stat( true );
79  for(unsigned int k=0; k < 1000; k++)
80  stat.addValue( getRandomNumber( -1.0, 1.0 ) + getRandomNumber( -1.0, 1.0 ) );
81  stat.terminate();
82  Histogram<double> hist;
83  hist.init( Histogram<double>::Scott, stat );
84  hist.addValues( stat.begin(), stat.end() );
85  hist.terminate();
86  for ( unsigned int i = 0; i < hist.size(); ++i )
87  std::cout << i << " " << hist.pdf( i ) << " " << hist.cdf( i ) << std::endl;
88  ++nb; nbok += hist.cdf( hist.size()-1 ) > 0.9999 ? 1 : 0;
89  return nbok == nb;
90 }
91 
93 {
94  unsigned int nbok = 0;
95  unsigned int nb = 0;
96 
97  Statistic<double> stat( true );
98  for(unsigned int k=0; k < 1000; k++)
99  stat.addValue( getRandomNumber( -1.0, 1.0 ) + getRandomNumber( -1.0, 1.0 )
100  + getRandomNumber( -1.0, 1.0 ) );
101  stat.terminate();
102  Histogram<double> hist;
103  hist.init( Histogram<double>::Scott, stat );
104  hist.addValues( stat.begin(), stat.end() );
105  hist.terminate();
106  for ( unsigned int i = 0; i < hist.size(); ++i )
107  std::cout << i << " " << hist.pdf( i ) << " " << hist.cdf( i ) << std::endl;
108  ++nb; nbok += hist.cdf( hist.size()-1 ) > 0.9999 ? 1 : 0;
109  return nbok == nb;
110 }
111 
112 
114 // Standard services - public :
115 
116 int main( int, char** )
117 {
118  trace.beginBlock ( "Testing class Histogram" );
119 
120  bool res = testHistogramUniform()
123  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
124 
125  trace.endBlock();
126  return res ? 0 : 1;
127 }
128 // //
Aim: Represents a typical histogram in statistics, which is a discrete estimate of the probability di...
Definition: Histogram.h:146
void addValues(TInputIterator it, TInputIterator itE)
double pdf(Bin b) const
Bin size() const
double cdf(Bin b) const
void init(Clone< Binner > binner)
Aim: This class processes a set of sample values for one variable and can then compute different stat...
Definition: Statistic.h:70
ConstIterator end() const
ConstIterator begin() const
void addValue(Quantity v)
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
bool testHistogramUniform()
bool testHistogramGaussian()
int main(int, char **)
bool testHistogramGaussian2()
double getRandomNumber(double first, double last)