2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20 * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
24 * Implementation of inline methods defined in Statistics.h
26 * Backport from ImaGene
28 * This file is part of the DGtal library.
31//////////////////////////////////////////////////////////////////////////////
35//////////////////////////////////////////////////////////////////////////////
37///////////////////////////////////////////////////////////////////////////////
38// Implementation of inline methods //
42template <typename TQuantity>
44DGtal::Statistic<TQuantity>::~Statistic()
48template <typename TQuantity>
50DGtal::Statistic<TQuantity>::Statistic(bool storeSample)
51 : mySamples( 0 ), myExp( NumberTraits<Quantity>::ZERO ), myExp2( NumberTraits<Quantity>::ZERO ), myMax( NumberTraits<Quantity>::ZERO ),myMin( NumberTraits<Quantity>::ZERO ), myMedian(NumberTraits<Quantity>::ZERO), myStoreSamples (storeSample),
54 myValues= std::vector<Quantity> ();
59template <typename TQuantity>
61DGtal::Statistic<TQuantity>::Statistic
62( const Statistic<TQuantity> & other )
63 : mySamples( other.mySamples ),
65 myExp2( other.myExp2 ),
68 myMedian( other.myMedian),
69 myStoreSamples (other.myStoreSamples),
70 myIsTerminated(other.myIsTerminated)
73 myValues= std::vector<TQuantity> ();
74 for(unsigned int i=0; i<other.myValues.size(); i++){
75 myValues.push_back(other.myValues.at(i));
83template <typename TQuantity>
85DGtal::Statistic<TQuantity> &
86DGtal::Statistic<TQuantity>::operator=
87( const Statistic<TQuantity> & other )
91 mySamples = other.mySamples;
93 myExp2 = other.myExp2;
96 myMedian = other.myMedian;
97 myStoreSamples = other.myStoreSamples;
98 myIsTerminated=other.myIsTerminated;
100 myValues= std::vector<Quantity> ();
101 for(unsigned int i=0; i<other.myValues.size(); i++){
102 myValues.push_back(other.myValues.at(i));
112template <typename TQuantity>
114DGtal::Statistic<TQuantity> &
115DGtal::Statistic<TQuantity>::operator+=
116( const Statistic<TQuantity> & other )
118 if ( other.mySamples != 0 )
120 if ( ( mySamples == 0 ) || ( other.myMin < myMin ) )
122 if ( ( mySamples == 0 ) || ( other.myMax > myMax ) )
125 mySamples += other.mySamples;
126 myExp += other.myExp;
127 myExp2 += other.myExp2;
128 myIsTerminated=false;
130 if(myStoreSamples && other.myStoreSamples){
131 for(unsigned int i=0; i<other.myValues.size(); i++){
132 myValues.push_back(other.myValues.at(i));
135 myStoreSamples=false;
143template <typename TQuantity>
145DGtal::Statistic<TQuantity>
146DGtal::Statistic<TQuantity>::operator+
147( const Statistic<TQuantity> & other ) const
149 Statistic<TQuantity> stat( *this );
155//-----------------------------------------------------------------------------
156template <typename TQuantity>
158typename DGtal::Statistic<TQuantity>::ConstIterator
159DGtal::Statistic<TQuantity>::begin() const
161 return myValues.begin();
163//-----------------------------------------------------------------------------
164template <typename TQuantity>
166typename DGtal::Statistic<TQuantity>::ConstIterator
167DGtal::Statistic<TQuantity>::end() const
169 return myValues.end();
174///////////////////////////////////////////////////////////////////////////////
175// ----------------------- Accessors ------------------------------
178template <typename TQuantity>
181DGtal::Statistic<TQuantity>::samples() const
186template <typename TQuantity>
189DGtal::Statistic<TQuantity>::size() const
191 return myValues.size();
193template <typename TQuantity>
196DGtal::Statistic<TQuantity>::operator[]( unsigned int i ) const
198 return i < size() ? myValues[ i ] : Quantity();
201template <typename TQuantity>
204DGtal::Statistic<TQuantity>::mean() const
206 return NumberTraits<Quantity>::castToDouble(myExp) /
207 static_cast<double>(mySamples );
211template <typename TQuantity>
214DGtal::Statistic<TQuantity>::variance() const
216 return ( NumberTraits<Quantity>::castToDouble(myExp2) / (double) mySamples ) - mean() * mean();
220template <typename TQuantity>
223DGtal::Statistic<TQuantity>::unbiasedVariance() const
225 ASSERT( mySamples != 0 );
226 return ( (double) mySamples ) * variance()
227 / ( (double) mySamples );
231template <typename TQuantity>
234DGtal::Statistic<TQuantity>::max() const
240template <typename TQuantity>
243DGtal::Statistic<TQuantity>::min() const
249template <typename TQuantity>
252DGtal::Statistic<TQuantity>::median()
254 ASSERT( myStoreSamples || myIsTerminated );
259 ASSERT(myValues.size()>0);
260 nth_element( myValues.begin(), myValues.begin()+(myValues.size()/2),
262 return *(myValues.begin()+(myValues.size()/2));
268template <typename TQuantity>
271DGtal::Statistic<TQuantity>::addValue( TQuantity v )
273 if ( mySamples == 0 )
278 else if ( v < myMin ) myMin = v;
279 else if ( v > myMax ) myMax = v;
284 myValues.push_back(v);
290template <typename TQuantity>
294DGtal::Statistic<TQuantity>::addValues( Iter b, Iter e )
296 for ( ; b != e; ++b )
302template <typename TQuantity>
305DGtal::Statistic<TQuantity>::clear()
308 myExp = NumberTraits<Quantity>::ZERO;
309 myExp2 = NumberTraits<Quantity>::ZERO;
310 myMin = NumberTraits<Quantity>::ZERO;
311 myMax = NumberTraits<Quantity>::ZERO;
312 myMedian=NumberTraits<Quantity>::ZERO;
313 myIsTerminated=false;
322template< typename TQuantity>
325DGtal::Statistic<TQuantity>::terminate()
329 // JOL: Perhaps a cleanUp() or dispose() method is
330 // preferable. Sometimes, it is useful to access also the values.
332 myStoreSamples=false;
338///////////////////////////////////////////////////////////////////////////////
339// Interface - public :
342template <typename TQuantity>
345DGtal::Statistic<TQuantity>::selfDisplay
346( std::ostream& thatStream ) const
348 thatStream << "[Statistic "
349 << " nb=" << samples()
351 << " var=" << variance()
352 << " uvar=" << unbiasedVariance()
356 thatStream << " median =" << myMedian;
362template <typename TQuantity>
365DGtal::Statistic<TQuantity>::OK() const
371///////////////////////////////////////////////////////////////////////////////
372// Implementation of inline functions and external operators //
375template <typename TQuantity>
378DGtal::operator<<( std::ostream & thatStream,
379 const Statistic<TQuantity> & that_object_to_display )
381 that_object_to_display.selfDisplay( thatStream );
386///////////////////////////////////////////////////////////////////////////////