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>::mean() const
191 return NumberTraits<Quantity>::castToDouble(myExp) /
192 static_cast<double>(mySamples );
196 template <typename TQuantity>
199 DGtal::Statistic<TQuantity>::variance() const
201 return ( NumberTraits<Quantity>::castToDouble(myExp2) / (double) mySamples ) - mean() * mean();
205 template <typename TQuantity>
208 DGtal::Statistic<TQuantity>::unbiasedVariance() const
210 ASSERT( mySamples != 0 );
211 return ( (double) mySamples ) * variance()
212 / ( (double) mySamples );
216 template <typename TQuantity>
219 DGtal::Statistic<TQuantity>::max() const
225 template <typename TQuantity>
228 DGtal::Statistic<TQuantity>::min() const
234 template <typename TQuantity>
237 DGtal::Statistic<TQuantity>::median()
239 ASSERT( myStoreSamples || myIsTerminated );
244 ASSERT(myValues.size()>0);
245 nth_element( myValues.begin(), myValues.begin()+(myValues.size()/2),
247 return *(myValues.begin()+(myValues.size()/2));
253 template <typename TQuantity>
256 DGtal::Statistic<TQuantity>::addValue( TQuantity v )
258 if ( mySamples == 0 )
263 else if ( v < myMin ) myMin = v;
264 else if ( v > myMax ) myMax = v;
269 myValues.push_back(v);
275 template <typename TQuantity>
276 template <class Iter>
279 DGtal::Statistic<TQuantity>::addValues( Iter b, Iter e )
281 for ( ; b != e; ++b )
287 template <typename TQuantity>
290 DGtal::Statistic<TQuantity>::clear()
293 myExp = NumberTraits<Quantity>::ZERO;
294 myExp2 = NumberTraits<Quantity>::ZERO;
295 myMin = NumberTraits<Quantity>::ZERO;
296 myMax = NumberTraits<Quantity>::ZERO;
297 myMedian=NumberTraits<Quantity>::ZERO;
298 myIsTerminated=false;
307 template< typename TQuantity>
310 DGtal::Statistic<TQuantity>::terminate()
314 // JOL: Perhaps a cleanUp() or dispose() method is
315 // preferable. Sometimes, it is useful to access also the values.
317 myStoreSamples=false;
323 ///////////////////////////////////////////////////////////////////////////////
324 // Interface - public :
327 template <typename TQuantity>
330 DGtal::Statistic<TQuantity>::selfDisplay
331 ( std::ostream& thatStream ) const
333 thatStream << "[Statistic "
334 << " nb=" << samples()
336 << " var=" << variance()
337 << " uvar=" << unbiasedVariance()
341 thatStream << " median =" << myMedian;
347 template <typename TQuantity>
350 DGtal::Statistic<TQuantity>::OK() const
356 ///////////////////////////////////////////////////////////////////////////////
357 // Implementation of inline functions and external operators //
360 template <typename TQuantity>
363 DGtal::operator<<( std::ostream & thatStream,
364 const Statistic<TQuantity> & that_object_to_display )
366 that_object_to_display.selfDisplay( thatStream );
371 ///////////////////////////////////////////////////////////////////////////////