DGtal  0.9.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 double
189 DGtal::Statistic<TQuantity>::mean() const
190 {
191  return NumberTraits<Quantity>::castToDouble(myExp) /
192  static_cast<double>(mySamples );
193 }
194 
195 
196 template <typename TQuantity>
197 inline
198 double
199 DGtal::Statistic<TQuantity>::variance() const
200 {
201  return ( NumberTraits<Quantity>::castToDouble(myExp2) / (double) mySamples ) - mean() * mean();
202 }
203 
204 
205 template <typename TQuantity>
206 inline
207 double
208 DGtal::Statistic<TQuantity>::unbiasedVariance() const
209 {
210  ASSERT( mySamples != 0 );
211  return ( (double) mySamples ) * variance()
212  / ( (double) mySamples );
213 }
214 
215 
216 template <typename TQuantity>
217 inline
218 TQuantity
219 DGtal::Statistic<TQuantity>::max() const
220 {
221  return myMax;
222 }
223 
224 
225 template <typename TQuantity>
226 inline
227 TQuantity
228 DGtal::Statistic<TQuantity>::min() const
229 {
230  return myMin;
231 }
232 
233 
234 template <typename TQuantity>
235 inline
236 TQuantity
237 DGtal::Statistic<TQuantity>::median()
238 {
239  ASSERT( myStoreSamples || myIsTerminated );
240  if(myIsTerminated){
241  return myMedian;
242  }
243  else{
244  ASSERT(myValues.size()>0);
245  nth_element( myValues.begin(), myValues.begin()+(myValues.size()/2),
246  myValues.end());
247  return *(myValues.begin()+(myValues.size()/2));
248  }
249 }
250 
251 
252 
253 template <typename TQuantity>
254 inline
255 void
256 DGtal::Statistic<TQuantity>::addValue( TQuantity v )
257 {
258  if ( mySamples == 0 )
259  {
260  myMin = v;
261  myMax = v;
262  }
263  else if ( v < myMin ) myMin = v;
264  else if ( v > myMax ) myMax = v;
265  myExp += v;
266  myExp2 += v * v;
267  ++mySamples;
268  if(myStoreSamples){
269  myValues.push_back(v);
270  }
271 }
272 
273 
274 
275 template <typename TQuantity>
276 template <class Iter>
277 inline
278 void
279 DGtal::Statistic<TQuantity>::addValues( Iter b, Iter e )
280 {
281  for ( ; b != e; ++b )
282  addValue( *b );
283 }
284 
285 
286 
287 template <typename TQuantity>
288 inline
289 void
290 DGtal::Statistic<TQuantity>::clear()
291 {
292  mySamples = 0;
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;
299  if(myStoreSamples){
300  myValues.clear();
301  }
302 }
303 
304 
305 
306 
307 template< typename TQuantity>
308 inline
309 void
310 DGtal::Statistic<TQuantity>::terminate()
311 {
312  if(myStoreSamples){
313  myMedian=median();
314  // JOL: Perhaps a cleanUp() or dispose() method is
315  // preferable. Sometimes, it is useful to access also the values.
316  // myValues.clear();
317  myStoreSamples=false;
318  myIsTerminated=true;
319  }
320 }
321 
322 
323 ///////////////////////////////////////////////////////////////////////////////
324 // Interface - public :
325 
326 
327 template <typename TQuantity>
328 inline
329 void
330 DGtal::Statistic<TQuantity>::selfDisplay
331 ( std::ostream& thatStream ) const
332 {
333  thatStream << "[Statistic "
334  << " nb=" << samples()
335  << " exp=" << mean()
336  << " var=" << variance()
337  << " uvar=" << unbiasedVariance()
338  << " min=" << min()
339  << " max=" << max();
340  if(myIsTerminated){
341  thatStream << " median =" << myMedian;
342  }
343  thatStream << "]";
344 }
345 
346 
347 template <typename TQuantity>
348 inline
349 bool
350 DGtal::Statistic<TQuantity>::OK() const
351 {
352  return true;
353 }
354 
355 
356 ///////////////////////////////////////////////////////////////////////////////
357 // Implementation of inline functions and external operators //
358 
359 
360 template <typename TQuantity>
361 inline
362 std::ostream&
363 DGtal::operator<<( std::ostream & thatStream,
364  const Statistic<TQuantity> & that_object_to_display )
365 {
366  that_object_to_display.selfDisplay( thatStream );
367  return thatStream;
368 }
369 
370 // //
371 ///////////////////////////////////////////////////////////////////////////////
372 
373