DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Member Functions | Private Attributes
DGtal::Statistic< RealNumberType > Class Template Reference

#include <Statistic.h>

Collaboration diagram for DGtal::Statistic< RealNumberType >:
Collaboration graph
[legend]

Public Member Functions

 ~Statistic ()
 Statistic (bool storeSample=false)
 Statistic (const Statistic &other)
Statisticoperator= (const Statistic &other)
Statisticoperator+= (const Statistic &other)
Statistic operator+ (const Statistic &other) const
unsigned int samples () const
RealNumberType mean () const
RealNumberType variance () const
RealNumberType unbiasedVariance () const
RealNumberType max () const
RealNumberType min () const
RealNumberType median ()
void addValue (RealNumberType v)
template<class Iter >
void addValues (Iter b, Iter e)
void clear ()
void terminate ()
void selfDisplay (std::ostream &that_stream) const
bool OK () const

Private Attributes

unsigned int mySamples
RealNumberType myExp
RealNumberType myExp2
RealNumberType myMax
RealNumberType myMin
RealNumberType myMedian
std::vector< RealNumberType > myValues
bool myStoreSamples
bool myIsTerminated

Detailed Description

template<typename RealNumberType>
class DGtal::Statistic< RealNumberType >

Aim: This class processes a set of sample values for one variable and can then compute different statistics, like sample mean, sample variance, sample unbiased variance, etc. It is minimalistic for space efficiency. For multiple variables, sample storage and others, see Statistics class.

Description of class 'Statistic'

Backported from ImaGene. Lachaud03b

See also:
testStatistics.cpp

Definition at line 66 of file Statistic.h.


Constructor & Destructor Documentation

template<typename RealNumberType >
DGtal::Statistic< RealNumberType >::~Statistic ( )
inline

Destructor.

Definition at line 44 of file Statistic.ih.

{}
template<typename RealNumberType >
DGtal::Statistic< RealNumberType >::Statistic ( bool  storeSample = false)
inline

Constructor.

Definition at line 52 of file Statistic.ih.

References DGtal::false, and DGtal::Statistic< RealNumberType >::myValues.

: mySamples( 0 ), myExp( 0 ), myExp2( 0 ), myMax( 0 ),myMin( 0 ), myMedian(0), myStoreSamples (storeSample),
{
myValues= std::vector<RealNumberType> ();
}
template<typename RealNumberType >
DGtal::Statistic< RealNumberType >::Statistic ( const Statistic< RealNumberType > &  other)
inline

Copy constructor.

Parameters:
otherthe object to clone.

Definition at line 66 of file Statistic.ih.

References DGtal::Statistic< RealNumberType >::myValues.

: mySamples( other.mySamples ),
myExp( other.myExp ),
myExp2( other.myExp2 ),
myMax( other.myMax ),
myMin( other.myMin ),
myMedian( other.myMedian),
myStoreSamples (other.myStoreSamples),
myIsTerminated(other.myIsTerminated)
{
myValues= std::vector<RealNumberType> ();
for(unsigned int i=0; i<other.myValues.size(); i++){
myValues.push_back(other.myValues.at(i));
}
}
}

Member Function Documentation

template<typename RealNumberType >
void DGtal::Statistic< RealNumberType >::addValue ( RealNumberType  v)
inline

Adds a new sample value [v].

Parameters:
vthe new sample value.

Definition at line 286 of file Statistic.ih.

Referenced by DGtal::CompareLocalEstimators< TFirstEsimator, TSecondEstimator >::compare(), and DGtal::CompareLocalEstimators< TFirstEsimator, TSecondEstimator >::compareVectors().

{
if ( mySamples == 0 )
{
myMin = v;
myMax = v;
}
else if ( v < myMin ) myMin = v;
else if ( v > myMax ) myMax = v;
myExp += v;
myExp2 += v * v;
myValues.push_back(v);
}
}
template<typename RealNumberType >
template<class Iter >
void DGtal::Statistic< RealNumberType >::addValues ( Iter  b,
Iter  e 
)
inline

Adds a sequence of sample values, scanning a container from iterators [b] to [e].

Exemple:

vector<RealNumberType> x;
Statistic stats;
stats.addValue( x + 4, x + 10 );
Parameters:
ban iterator on the starting point.
ean iterator after the last point.

Adds a sequence of sample values, scanning a container from iterators [b] to [e].

Exemple: vector<RealNumberType> x; Statistic stats; stats.addValue( x + 4, x + 10 ); <endcode>

Parameters:
ban iterator on the starting point.
ean iterator after the last point.

Definition at line 321 of file Statistic.ih.

{
for ( ; b != e; ++b )
addValue( *b );
}
template<typename RealNumberType >
void DGtal::Statistic< RealNumberType >::clear ( )
inline

Clears the object. As if it has just been created.

Definition at line 333 of file Statistic.ih.

References DGtal::Statistic< RealNumberType >::clear().

Referenced by DGtal::Statistic< RealNumberType >::clear().

{
mySamples = 0;
myExp = 0;
myExp2 = 0;
myMin = 0;
myMax = 0;
myValues.clear();
}
}
template<typename RealNumberType >
RealNumberType DGtal::Statistic< RealNumberType >::max ( ) const
inline
Returns:
the maximal value.

Definition at line 234 of file Statistic.ih.

{
return myMax;
}
template<typename RealNumberType >
RealNumberType DGtal::Statistic< RealNumberType >::mean ( ) const
inline
Returns:
the sample mean.

Definition at line 199 of file Statistic.ih.

{
return myExp / (RealNumberType) mySamples;
}
template<typename RealNumberType >
RealNumberType DGtal::Statistic< RealNumberType >::median ( )
inline

Return the median value of the Statistic values. It can be given in two possible cases:

  • if the the values are stored in the 'Statistic' objects (not always a good solution). (complexity: linear on average)
  • if the values were first stored and computed by the function terminate().
    Returns:
    the median value.
    See also:
    terminate, Statistic
    Return the median value of the Statistic values. It can be given in two possible cases:

if the the values are stored in the 'Statistic' objects (not always a good solution). (complexity: linear on average)

  • if the values were first stored and computed by the function
    See also:
    terminate.
    Returns:
    the median value.
    See also:
    terminate, Statistic

Definition at line 263 of file Statistic.ih.

{
return myMedian;
}
else{
nth_element( myValues.begin(), myValues.begin()+(myValues.size()/2),
myValues.end());
return *(myValues.begin()+(myValues.size()/2));
}
}
template<typename RealNumberType >
RealNumberType DGtal::Statistic< RealNumberType >::min ( ) const
inline
Returns:
the minimal value.

Definition at line 245 of file Statistic.ih.

{
return myMin;
}
template<typename RealNumberType >
bool DGtal::Statistic< RealNumberType >::OK ( ) const
inline

Checks the validity/consistency of the object.

Returns:
'true' if the object is valid, 'false' otherwise.

Definition at line 403 of file Statistic.ih.

{
return true;
}
template<typename RealNumberType >
DGtal::Statistic< RealNumberType > DGtal::Statistic< RealNumberType >::operator+ ( const Statistic< RealNumberType > &  other) const
inline

Adds two set of statistics (should be of the same variable).

Parameters:
otherthe object to add.
Returns:
a new object that is the union of these statistics.

Definition at line 167 of file Statistic.ih.

{
Statistic<RealNumberType> stat( *this );
stat += other;
return stat;
}
template<typename RealNumberType >
DGtal::Statistic< RealNumberType > & DGtal::Statistic< RealNumberType >::operator+= ( const Statistic< RealNumberType > &  other)
inline

Adds to self another set of statistics (should be of the same variable).

Parameters:
otherthe object to add.
Returns:
a reference on 'this'.

Definition at line 129 of file Statistic.ih.

References DGtal::Statistic< RealNumberType >::myExp, DGtal::Statistic< RealNumberType >::myExp2, DGtal::Statistic< RealNumberType >::myMax, DGtal::Statistic< RealNumberType >::myMin, DGtal::Statistic< RealNumberType >::mySamples, DGtal::Statistic< RealNumberType >::myStoreSamples, and DGtal::Statistic< RealNumberType >::myValues.

{
if ( other.mySamples != 0 )
{
if ( ( mySamples == 0 ) || ( other.myMin < myMin ) )
myMin = other.myMin;
if ( ( mySamples == 0 ) || ( other.myMax > myMax ) )
myMax = other.myMax;
}
mySamples += other.mySamples;
myExp += other.myExp;
myExp2 += other.myExp2;
if(myStoreSamples && other.myStoreSamples){
for(unsigned int i=0; i<other.myValues.size(); i++){
myValues.push_back(other.myValues.at(i));
}
}else{
}
}
template<typename RealNumberType >
DGtal::Statistic< RealNumberType > & DGtal::Statistic< RealNumberType >::operator= ( const Statistic< RealNumberType > &  other)
inline

Assignment.

Parameters:
otherthe object to copy.
Returns:
a reference on 'this'.

Definition at line 94 of file Statistic.ih.

References DGtal::Statistic< RealNumberType >::myExp, DGtal::Statistic< RealNumberType >::myExp2, DGtal::Statistic< RealNumberType >::myIsTerminated, DGtal::Statistic< RealNumberType >::myMax, DGtal::Statistic< RealNumberType >::myMedian, DGtal::Statistic< RealNumberType >::myMin, DGtal::Statistic< RealNumberType >::mySamples, DGtal::Statistic< RealNumberType >::myStoreSamples, and DGtal::Statistic< RealNumberType >::myValues.

{
if ( this != &other )
{
mySamples = other.mySamples;
myExp = other.myExp;
myExp2 = other.myExp2;
myMin = other.myMin;
myMax = other.myMax;
myMedian = other.myMedian;
myStoreSamples = other.myStoreSamples;
myIsTerminated=other.myIsTerminated;
myValues= std::vector<RealNumberType> ();
for(unsigned int i=0; i<other.myValues.size(); i++){
myValues.push_back(other.myValues.at(i));
}
}
}
return *this;
}
template<typename RealNumberType >
unsigned int DGtal::Statistic< RealNumberType >::samples ( ) const
inline
Returns:
the number of samples.

Definition at line 188 of file Statistic.ih.

{
return mySamples;
}
template<typename RealNumberType >
void DGtal::Statistic< RealNumberType >::selfDisplay ( std::ostream &  thatStream) const
inline

Writes/Displays the object on an output stream.

Parameters:
that_streamthe output stream where the object is written.

Writes/Displays the object on an output stream.

Parameters:
thatStreamthe output stream where the object is written.

Definition at line 384 of file Statistic.ih.

Referenced by DGtal::operator<<().

{
thatStream << "[Statistic "
<< " nb=" << samples()
<< " exp=" << mean()
<< " var=" << variance()
<< " uvar=" << unbiasedVariance()
<< " min=" << min()
<< " max=" << max()
<< "]";
}
template<typename RealNumberType >
void DGtal::Statistic< RealNumberType >::terminate ( )
inline

Computes the median value of the statistics and switch to mode which does not save the statistics samples (myStore_samples = false). Usefull only if the values are stored (specified in the the constructor) else it doest nothing.

See also:
median, Statistic, myStore_samples

Computes the median value of the statistics and switch to mode which does not save the statistics samples (myStoreSamples = false). Usefull only if the values are stored (specified in the the constructor) else it doest nothing.

See also:
median, Statistic, myStoreSamples

Definition at line 362 of file Statistic.ih.

Referenced by DGtal::CompareLocalEstimators< TFirstEsimator, TSecondEstimator >::compare(), and DGtal::CompareLocalEstimators< TFirstEsimator, TSecondEstimator >::compareVectors().

template<typename RealNumberType >
RealNumberType DGtal::Statistic< RealNumberType >::unbiasedVariance ( ) const
inline
Returns:
the unbiased sample variance.

Definition at line 221 of file Statistic.ih.

{
ASSERT( mySamples != 0 );
return ( (RealNumberType) mySamples ) * variance()
/ ( (RealNumberType) mySamples );
}
template<typename RealNumberType >
RealNumberType DGtal::Statistic< RealNumberType >::variance ( ) const
inline
Returns:
the sample variance.

Definition at line 210 of file Statistic.ih.

{
return ( myExp2 / (RealNumberType) mySamples ) - mean() * mean();
}

Field Documentation

template<typename RealNumberType>
RealNumberType DGtal::Statistic< RealNumberType >::myExp
private

stores the sum of sample values for computing sample mean.

Definition at line 232 of file Statistic.h.

Referenced by DGtal::Statistic< RealNumberType >::operator+=(), and DGtal::Statistic< RealNumberType >::operator=().

template<typename RealNumberType>
RealNumberType DGtal::Statistic< RealNumberType >::myExp2
private

stores the sum of squared sample values for computing sample variance.

Definition at line 238 of file Statistic.h.

Referenced by DGtal::Statistic< RealNumberType >::operator+=(), and DGtal::Statistic< RealNumberType >::operator=().

template<typename RealNumberType>
bool DGtal::Statistic< RealNumberType >::myIsTerminated
private

Tells if terminate was called. Usefull to return median value even if the m_values are no more stored (possible after serialisation which doest store the values.).

Definition at line 276 of file Statistic.h.

Referenced by DGtal::Statistic< RealNumberType >::operator=().

template<typename RealNumberType>
RealNumberType DGtal::Statistic< RealNumberType >::myMax
private

stores the maximal sample value.

Definition at line 243 of file Statistic.h.

Referenced by DGtal::Statistic< RealNumberType >::operator+=(), and DGtal::Statistic< RealNumberType >::operator=().

template<typename RealNumberType>
RealNumberType DGtal::Statistic< RealNumberType >::myMedian
private

stores the median sample value.

Definition at line 254 of file Statistic.h.

Referenced by DGtal::Statistic< RealNumberType >::operator=().

template<typename RealNumberType>
RealNumberType DGtal::Statistic< RealNumberType >::myMin
private

stores the minimal sample value.

Definition at line 248 of file Statistic.h.

Referenced by DGtal::Statistic< RealNumberType >::operator+=(), and DGtal::Statistic< RealNumberType >::operator=().

template<typename RealNumberType>
unsigned int DGtal::Statistic< RealNumberType >::mySamples
private
template<typename RealNumberType>
bool DGtal::Statistic< RealNumberType >::myStoreSamples
private

Tells if values must be stored or not.

Definition at line 267 of file Statistic.h.

Referenced by DGtal::Statistic< RealNumberType >::operator+=(), and DGtal::Statistic< RealNumberType >::operator=().

template<typename RealNumberType>
std::vector< RealNumberType > DGtal::Statistic< RealNumberType >::myValues
private

The documentation for this class was generated from the following files: