DGtal
2.2.0
Toggle main menu visibility
Loading...
Searching...
No Matches
testHistogram.cpp
Go to the documentation of this file.
1
16
29
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
54
bool
testHistogramUniform
()
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
73
bool
testHistogramGaussian
()
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
92
bool
testHistogramGaussian2
()
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
()
121
&&
testHistogramGaussian
()
122
&&
testHistogramGaussian2
();
123
trace
.
emphase
() << ( res ?
"Passed."
:
"Error."
) << endl;
124
125
trace
.
endBlock
();
126
return
res ? 0 : 1;
127
}
128
// //
DGtal::Histogram
Aim: Represents a typical histogram in statistics, which is a discrete estimate of the probability di...
Definition
Histogram.h:146
DGtal::Histogram::addValues
void addValues(TInputIterator it, TInputIterator itE)
DGtal::Histogram::pdf
double pdf(Bin b) const
DGtal::Histogram::Scott
@ Scott
Definition
Histogram.h:169
DGtal::Histogram::size
Bin size() const
DGtal::Histogram::cdf
double cdf(Bin b) const
DGtal::Histogram::init
void init(Clone< Binner > binner)
DGtal::Histogram::terminate
void terminate()
DGtal::Statistic
Aim: This class processes a set of sample values for one variable and can then compute different stat...
Definition
Statistic.h:70
DGtal::Statistic::end
ConstIterator end() const
DGtal::Statistic::terminate
void terminate()
DGtal::Statistic::begin
ConstIterator begin() const
DGtal::Statistic::addValue
void addValue(Quantity v)
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::Trace::emphase
std::ostream & emphase()
DGtal::Trace::endBlock
double endBlock()
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition
ClosedIntegerHalfPlane.h:49
DGtal::trace
Trace trace
std
STL namespace.
testHistogramUniform
bool testHistogramUniform()
Definition
testHistogram.cpp:54
testHistogramGaussian
bool testHistogramGaussian()
Definition
testHistogram.cpp:73
testHistogramGaussian2
bool testHistogramGaussian2()
Definition
testHistogram.cpp:92
getRandomNumber
double getRandomNumber(double first, double last)
Definition
testHistogram.cpp:48
main
int main(int, char **)
Definition
testIntegerComputer.cpp:331
tests
math
testHistogram.cpp
Generated on
for DGtal by
1.18.0