DGtal  1.5.beta
testHistogram.cpp File Reference
#include <iostream>
#include <vector>
#include "DGtal/math/Statistic.h"
#include "DGtal/math/Histogram.h"
Include dependency graph for testHistogram.cpp:

Go to the source code of this file.

Functions

double getRandomNumber (double first, double last)
 
bool testHistogramUniform ()
 
bool testHistogramGaussian ()
 
bool testHistogramGaussian2 ()
 
int main (int, char **)
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5127), University of Savoie, France
Date
2013/10/25

Functions for testing class Histogram.

This file is part of the DGtal library.

Definition in file testHistogram.cpp.

Function Documentation

◆ getRandomNumber()

double getRandomNumber ( double  first,
double  last 
)

Example of a test. To be completed.

Definition at line 48 of file testHistogram.cpp.

49 {
50  double v = ((double)rand()) / (double) RAND_MAX;
51  return v * ( last - first ) + first;
52 }

Referenced by testHistogramGaussian(), testHistogramGaussian2(), and testHistogramUniform().

◆ main()

int main ( int  ,
char **   
)

Definition at line 116 of file testHistogram.cpp.

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 }
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
double endBlock()
Trace trace
Definition: Common.h:153
bool testHistogramUniform()
bool testHistogramGaussian()
bool testHistogramGaussian2()

References DGtal::Trace::beginBlock(), DGtal::Trace::emphase(), DGtal::Trace::endBlock(), testHistogramGaussian(), testHistogramGaussian2(), testHistogramUniform(), and DGtal::trace.

◆ testHistogramGaussian()

bool testHistogramGaussian ( )

Definition at line 73 of file testHistogram.cpp.

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 }
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
double getRandomNumber(double first, double last)

References DGtal::Statistic< TQuantity >::addValue(), DGtal::Histogram< TQuantity, TBinner >::addValues(), DGtal::Statistic< TQuantity >::begin(), DGtal::Histogram< TQuantity, TBinner >::cdf(), DGtal::Statistic< TQuantity >::end(), getRandomNumber(), DGtal::Histogram< TQuantity, TBinner >::init(), DGtal::Histogram< TQuantity, TBinner >::pdf(), DGtal::Histogram< TQuantity, TBinner >::size(), DGtal::Histogram< TQuantity, TBinner >::terminate(), and DGtal::Statistic< TQuantity >::terminate().

Referenced by main().

◆ testHistogramGaussian2()

bool testHistogramGaussian2 ( )

Definition at line 92 of file testHistogram.cpp.

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 }

References DGtal::Statistic< TQuantity >::addValue(), DGtal::Histogram< TQuantity, TBinner >::addValues(), DGtal::Statistic< TQuantity >::begin(), DGtal::Histogram< TQuantity, TBinner >::cdf(), DGtal::Statistic< TQuantity >::end(), getRandomNumber(), DGtal::Histogram< TQuantity, TBinner >::init(), DGtal::Histogram< TQuantity, TBinner >::pdf(), DGtal::Histogram< TQuantity, TBinner >::size(), DGtal::Histogram< TQuantity, TBinner >::terminate(), and DGtal::Statistic< TQuantity >::terminate().

Referenced by main().

◆ testHistogramUniform()

bool testHistogramUniform ( )

Definition at line 54 of file testHistogram.cpp.

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 }

References DGtal::Statistic< TQuantity >::addValue(), DGtal::Histogram< TQuantity, TBinner >::addValues(), DGtal::Statistic< TQuantity >::begin(), DGtal::Histogram< TQuantity, TBinner >::cdf(), DGtal::Statistic< TQuantity >::end(), getRandomNumber(), DGtal::Histogram< TQuantity, TBinner >::init(), DGtal::Histogram< TQuantity, TBinner >::pdf(), DGtal::Histogram< TQuantity, TBinner >::size(), DGtal::Histogram< TQuantity, TBinner >::terminate(), and DGtal::Statistic< TQuantity >::terminate().

Referenced by main().