DGtal  1.4.2
Statistic.ih
1 /**
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.
6  *
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.
11  *
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/>.
14  *
15  **/
16 
17 /**
18  * @file
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
21  *
22  * @date 2011/06/24
23  *
24  * Implementation of inline methods defined in Statistics.h
25  *
26  * Backport from ImaGene
27  *
28  * This file is part of the DGtal library.
29  */
30 
31 //////////////////////////////////////////////////////////////////////////////
32 #include <cstdlib>
33 #include <iostream>
34 #include <algorithm>
35 //////////////////////////////////////////////////////////////////////////////
36 
37 ///////////////////////////////////////////////////////////////////////////////
38 // Implementation of inline methods //
39 /**
40  * Destructor.
41  */
42 template <typename TQuantity>
43 inline
44 DGtal::Statistic<TQuantity>::~Statistic()
45 {}
46 
47 
48 template <typename TQuantity>
49 inline
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),
52  myIsTerminated(false)
53 {
54  myValues= std::vector<Quantity> ();
55 }
56 
57 
58 
59 template <typename TQuantity>
60 inline
61 DGtal::Statistic<TQuantity>::Statistic
62 ( const Statistic<TQuantity> & other )
63  : mySamples( other.mySamples ),
64  myExp( other.myExp ),
65  myExp2( other.myExp2 ),
66  myMax( other.myMax ),
67  myMin( other.myMin ),
68  myMedian( other.myMedian),
69  myStoreSamples (other.myStoreSamples),
70  myIsTerminated(other.myIsTerminated)
71 {
72  if(myStoreSamples){
73  myValues= std::vector<TQuantity> ();
74  for(unsigned int i=0; i<other.myValues.size(); i++){
75  myValues.push_back(other.myValues.at(i));
76  }
77  }
78 
79 }
80 
81 
82 
83 template <typename TQuantity>
84 inline
85 DGtal::Statistic<TQuantity> &
86 DGtal::Statistic<TQuantity>::operator=
87 ( const Statistic<TQuantity> & other )
88 {
89  if ( this != &other )
90  {
91  mySamples = other.mySamples;
92  myExp = other.myExp;
93  myExp2 = other.myExp2;
94  myMin = other.myMin;
95  myMax = other.myMax;
96  myMedian = other.myMedian;
97  myStoreSamples = other.myStoreSamples;
98  myIsTerminated=other.myIsTerminated;
99  if(myStoreSamples){
100  myValues= std::vector<Quantity> ();
101  for(unsigned int i=0; i<other.myValues.size(); i++){
102  myValues.push_back(other.myValues.at(i));
103  }
104  }
105  }
106  return *this;
107 }
108 
109 
110 
111 
112 template <typename TQuantity>
113 inline
114 DGtal::Statistic<TQuantity> &
115 DGtal::Statistic<TQuantity>::operator+=
116 ( const Statistic<TQuantity> & other )
117 {
118  if ( other.mySamples != 0 )
119  {
120  if ( ( mySamples == 0 ) || ( other.myMin < myMin ) )
121  myMin = other.myMin;
122  if ( ( mySamples == 0 ) || ( other.myMax > myMax ) )
123  myMax = other.myMax;
124  }
125  mySamples += other.mySamples;
126  myExp += other.myExp;
127  myExp2 += other.myExp2;
128  myIsTerminated=false;
129 
130  if(myStoreSamples && other.myStoreSamples){
131  for(unsigned int i=0; i<other.myValues.size(); i++){
132  myValues.push_back(other.myValues.at(i));
133  }
134  }else{
135  myStoreSamples=false;
136  }
137  return *this;
138 }
139 
140 
141 
142 
143 template <typename TQuantity>
144 inline
145 DGtal::Statistic<TQuantity>
146 DGtal::Statistic<TQuantity>::operator+
147 ( const Statistic<TQuantity> & other ) const
148 {
149  Statistic<TQuantity> stat( *this );
150  stat += other;
151  return stat;
152 }
153 
154 
155 //-----------------------------------------------------------------------------
156 template <typename TQuantity>
157 inline
158 typename DGtal::Statistic<TQuantity>::ConstIterator
159 DGtal::Statistic<TQuantity>::begin() const
160 {
161  return myValues.begin();
162 }
163 //-----------------------------------------------------------------------------
164 template <typename TQuantity>
165 inline
166 typename DGtal::Statistic<TQuantity>::ConstIterator
167 DGtal::Statistic<TQuantity>::end() const
168 {
169  return myValues.end();
170 }
171 
172 
173 
174 ///////////////////////////////////////////////////////////////////////////////
175 // ----------------------- Accessors ------------------------------
176 
177 
178 template <typename TQuantity>
179 inline
180 unsigned int
181 DGtal::Statistic<TQuantity>::samples() const
182 {
183  return mySamples;
184 }
185 
186 template <typename TQuantity>
187 inline
188 unsigned int
189 DGtal::Statistic<TQuantity>::size() const
190 {
191  return myValues.size();
192 }
193 template <typename TQuantity>
194 inline
195 TQuantity
196 DGtal::Statistic<TQuantity>::operator[]( unsigned int i ) const
197 {
198  return i < size() ? myValues[ i ] : Quantity();
199 }
200 
201 template <typename TQuantity>
202 inline
203 double
204 DGtal::Statistic<TQuantity>::mean() const
205 {
206  return NumberTraits<Quantity>::castToDouble(myExp) /
207  static_cast<double>(mySamples );
208 }
209 
210 
211 template <typename TQuantity>
212 inline
213 double
214 DGtal::Statistic<TQuantity>::variance() const
215 {
216  return ( NumberTraits<Quantity>::castToDouble(myExp2) / (double) mySamples ) - mean() * mean();
217 }
218 
219 
220 template <typename TQuantity>
221 inline
222 double
223 DGtal::Statistic<TQuantity>::unbiasedVariance() const
224 {
225  ASSERT( mySamples != 0 );
226  return ( (double) mySamples ) * variance()
227  / ( (double) mySamples );
228 }
229 
230 
231 template <typename TQuantity>
232 inline
233 TQuantity
234 DGtal::Statistic<TQuantity>::max() const
235 {
236  return myMax;
237 }
238 
239 
240 template <typename TQuantity>
241 inline
242 TQuantity
243 DGtal::Statistic<TQuantity>::min() const
244 {
245  return myMin;
246 }
247 
248 
249 template <typename TQuantity>
250 inline
251 TQuantity
252 DGtal::Statistic<TQuantity>::median()
253 {
254  ASSERT( myStoreSamples || myIsTerminated );
255  if(myIsTerminated){
256  return myMedian;
257  }
258  else{
259  ASSERT(myValues.size()>0);
260  nth_element( myValues.begin(), myValues.begin()+(myValues.size()/2),
261  myValues.end());
262  return *(myValues.begin()+(myValues.size()/2));
263  }
264 }
265 
266 
267 
268 template <typename TQuantity>
269 inline
270 void
271 DGtal::Statistic<TQuantity>::addValue( TQuantity v )
272 {
273  if ( mySamples == 0 )
274  {
275  myMin = v;
276  myMax = v;
277  }
278  else if ( v < myMin ) myMin = v;
279  else if ( v > myMax ) myMax = v;
280  myExp += v;
281  myExp2 += v * v;
282  ++mySamples;
283  if(myStoreSamples){
284  myValues.push_back(v);
285  }
286 }
287 
288 
289 
290 template <typename TQuantity>
291 template <class Iter>
292 inline
293 void
294 DGtal::Statistic<TQuantity>::addValues( Iter b, Iter e )
295 {
296  for ( ; b != e; ++b )
297  addValue( *b );
298 }
299 
300 
301 
302 template <typename TQuantity>
303 inline
304 void
305 DGtal::Statistic<TQuantity>::clear()
306 {
307  mySamples = 0;
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;
314  if(myStoreSamples){
315  myValues.clear();
316  }
317 }
318 
319 
320 
321 
322 template< typename TQuantity>
323 inline
324 void
325 DGtal::Statistic<TQuantity>::terminate()
326 {
327  if(myStoreSamples){
328  myMedian=median();
329  // JOL: Perhaps a cleanUp() or dispose() method is
330  // preferable. Sometimes, it is useful to access also the values.
331  // myValues.clear();
332  myStoreSamples=false;
333  myIsTerminated=true;
334  }
335 }
336 
337 
338 ///////////////////////////////////////////////////////////////////////////////
339 // Interface - public :
340 
341 
342 template <typename TQuantity>
343 inline
344 void
345 DGtal::Statistic<TQuantity>::selfDisplay
346 ( std::ostream& thatStream ) const
347 {
348  thatStream << "[Statistic "
349  << " nb=" << samples()
350  << " exp=" << mean()
351  << " var=" << variance()
352  << " uvar=" << unbiasedVariance()
353  << " min=" << min()
354  << " max=" << max();
355  if(myIsTerminated){
356  thatStream << " median =" << myMedian;
357  }
358  thatStream << "]";
359 }
360 
361 
362 template <typename TQuantity>
363 inline
364 bool
365 DGtal::Statistic<TQuantity>::OK() const
366 {
367  return true;
368 }
369 
370 
371 ///////////////////////////////////////////////////////////////////////////////
372 // Implementation of inline functions and external operators //
373 
374 
375 template <typename TQuantity>
376 inline
377 std::ostream&
378 DGtal::operator<<( std::ostream & thatStream,
379  const Statistic<TQuantity> & that_object_to_display )
380 {
381  that_object_to_display.selfDisplay( thatStream );
382  return thatStream;
383 }
384 
385 // //
386 ///////////////////////////////////////////////////////////////////////////////
387 
388