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 //
42 template <typename TQuantity>
44 DGtal::Statistic<TQuantity>::~Statistic()
48 template <typename TQuantity>
50 DGtal::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> ();
59 template <typename TQuantity>
61 DGtal::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));
83 template <typename TQuantity>
85 DGtal::Statistic<TQuantity> &
86 DGtal::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));
112 template <typename TQuantity>
114 DGtal::Statistic<TQuantity> &
115 DGtal::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;
143 template <typename TQuantity>
145 DGtal::Statistic<TQuantity>
146 DGtal::Statistic<TQuantity>::operator+
147 ( const Statistic<TQuantity> & other ) const
149 Statistic<TQuantity> stat( *this );
155 //-----------------------------------------------------------------------------
156 template <typename TQuantity>
158 typename DGtal::Statistic<TQuantity>::ConstIterator
159 DGtal::Statistic<TQuantity>::begin() const
161 return myValues.begin();
163 //-----------------------------------------------------------------------------
164 template <typename TQuantity>
166 typename DGtal::Statistic<TQuantity>::ConstIterator
167 DGtal::Statistic<TQuantity>::end() const
169 return myValues.end();
174 ///////////////////////////////////////////////////////////////////////////////
175 // ----------------------- Accessors ------------------------------
178 template <typename TQuantity>
181 DGtal::Statistic<TQuantity>::samples() const
186 template <typename TQuantity>
189 DGtal::Statistic<TQuantity>::size() const
191 return myValues.size();
193 template <typename TQuantity>
196 DGtal::Statistic<TQuantity>::operator[]( unsigned int i ) const
198 return i < size() ? myValues[ i ] : Quantity();
201 template <typename TQuantity>
204 DGtal::Statistic<TQuantity>::mean() const
206 return NumberTraits<Quantity>::castToDouble(myExp) /
207 static_cast<double>(mySamples );
211 template <typename TQuantity>
214 DGtal::Statistic<TQuantity>::variance() const
216 return ( NumberTraits<Quantity>::castToDouble(myExp2) / (double) mySamples ) - mean() * mean();
220 template <typename TQuantity>
223 DGtal::Statistic<TQuantity>::unbiasedVariance() const
225 ASSERT( mySamples != 0 );
226 return ( (double) mySamples ) * variance()
227 / ( (double) mySamples );
231 template <typename TQuantity>
234 DGtal::Statistic<TQuantity>::max() const
240 template <typename TQuantity>
243 DGtal::Statistic<TQuantity>::min() const
249 template <typename TQuantity>
252 DGtal::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));
268 template <typename TQuantity>
271 DGtal::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);
290 template <typename TQuantity>
291 template <class Iter>
294 DGtal::Statistic<TQuantity>::addValues( Iter b, Iter e )
296 for ( ; b != e; ++b )
302 template <typename TQuantity>
305 DGtal::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;
322 template< typename TQuantity>
325 DGtal::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 :
342 template <typename TQuantity>
345 DGtal::Statistic<TQuantity>::selfDisplay
346 ( std::ostream& thatStream ) const
348 thatStream << "[Statistic "
349 << " nb=" << samples()
351 << " var=" << variance()
352 << " uvar=" << unbiasedVariance()
356 thatStream << " median =" << myMedian;
362 template <typename TQuantity>
365 DGtal::Statistic<TQuantity>::OK() const
371 ///////////////////////////////////////////////////////////////////////////////
372 // Implementation of inline functions and external operators //
375 template <typename TQuantity>
378 DGtal::operator<<( std::ostream & thatStream,
379 const Statistic<TQuantity> & that_object_to_display )
381 that_object_to_display.selfDisplay( thatStream );
386 ///////////////////////////////////////////////////////////////////////////////