DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Statistic.ih
1 
31 
32 #include <cstdlib>
33 #include <iostream>
34 #include <algorithm>
36 
38 // Implementation of inline methods //
42 template <typename RealNumberType>
43 inline
45 {}
46 
50 template <typename RealNumberType>
51 inline
53  : mySamples( 0 ), myExp( 0 ), myExp2( 0 ), myMax( 0 ),myMin( 0 ), myMedian(0), myStoreSamples (storeSample),
54  myIsTerminated(false)
55 {
56  myValues= std::vector<RealNumberType> ();
57 }
58 
63 template <typename RealNumberType>
64 inline
66 ( const Statistic<RealNumberType> & other )
67  : mySamples( other.mySamples ),
68  myExp( other.myExp ),
69  myExp2( other.myExp2 ),
70  myMax( other.myMax ),
71  myMin( other.myMin ),
72  myMedian( other.myMedian),
73  myStoreSamples (other.myStoreSamples),
74  myIsTerminated(other.myIsTerminated)
75 {
76  if(myStoreSamples){
77  myValues= std::vector<RealNumberType> ();
78  for(unsigned int i=0; i<other.myValues.size(); i++){
79  myValues.push_back(other.myValues.at(i));
80  }
81  }
82 
83 }
84 
90 template <typename RealNumberType>
91 inline
94 ( const Statistic<RealNumberType> & other )
95 {
96  if ( this != &other )
97  {
98  mySamples = other.mySamples;
99  myExp = other.myExp;
100  myExp2 = other.myExp2;
101  myMin = other.myMin;
102  myMax = other.myMax;
103  myMedian = other.myMedian;
104  myStoreSamples = other.myStoreSamples;
105  myIsTerminated=other.myIsTerminated;
106  if(myStoreSamples){
107  myValues= std::vector<RealNumberType> ();
108  for(unsigned int i=0; i<other.myValues.size(); i++){
109  myValues.push_back(other.myValues.at(i));
110  }
111  }
112 
113  }
114  return *this;
115 }
116 
117 
125 template <typename RealNumberType>
126 inline
128 DGtal::Statistic<RealNumberType>::operator+=
129 ( const Statistic<RealNumberType> & other )
130 {
131  if ( other.mySamples != 0 )
132  {
133  if ( ( mySamples == 0 ) || ( other.myMin < myMin ) )
134  myMin = other.myMin;
135  if ( ( mySamples == 0 ) || ( other.myMax > myMax ) )
136  myMax = other.myMax;
137  }
138  mySamples += other.mySamples;
139  myExp += other.myExp;
140  myExp2 += other.myExp2;
141  myIsTerminated=false;
142 
143  if(myStoreSamples && other.myStoreSamples){
144  for(unsigned int i=0; i<other.myValues.size(); i++){
145  myValues.push_back(other.myValues.at(i));
146  }
147  }else{
148  myStoreSamples=false;
149  }
150 
151 }
152 
153 
154 
155 
156 
163 template <typename RealNumberType>
164 inline
166 DGtal::Statistic<RealNumberType>::operator+
167 ( const Statistic<RealNumberType> & other ) const
168 {
169  Statistic<RealNumberType> stat( *this );
170  stat += other;
171  return stat;
172 }
173 
174 
175 
176 
177 
178 
180 // ----------------------- Accessors ------------------------------
181 
185 template <typename RealNumberType>
186 inline
187 unsigned int
189 {
190  return mySamples;
191 }
192 
196 template <typename RealNumberType>
197 inline
198 RealNumberType
200 {
201  return myExp / (RealNumberType) mySamples;
202 }
203 
207 template <typename RealNumberType>
208 inline
209 RealNumberType
211 {
212  return ( myExp2 / (RealNumberType) mySamples ) - mean() * mean();
213 }
214 
218 template <typename RealNumberType>
219 inline
220 RealNumberType
222 {
223  ASSERT( mySamples != 0 );
224  return ( (RealNumberType) mySamples ) * variance()
225  / ( (RealNumberType) mySamples );
226 }
227 
231 template <typename RealNumberType>
232 inline
233 RealNumberType
235 {
236  return myMax;
237 }
238 
242 template <typename RealNumberType>
243 inline
244 RealNumberType
246 {
247  return myMin;
248 }
249 
250 
251 
260 template <typename RealNumberType>
261 inline
262 RealNumberType
264 {
265  ASSERT( myStoreSamples || myIsTerminated );
266  if(myIsTerminated){
267  return myMedian;
268  }
269  else{
270  nth_element( myValues.begin(), myValues.begin()+(myValues.size()/2),
271  myValues.end());
272  return *(myValues.begin()+(myValues.size()/2));
273  }
274 }
275 
276 
283 template <typename RealNumberType>
284 inline
285 void
287 {
288  if ( mySamples == 0 )
289  {
290  myMin = v;
291  myMax = v;
292  }
293  else if ( v < myMin ) myMin = v;
294  else if ( v > myMax ) myMax = v;
295  myExp += v;
296  myExp2 += v * v;
297  ++mySamples;
298  if(myStoreSamples){
299  myValues.push_back(v);
300  }
301 }
302 
317 template <typename RealNumberType>
318 template <class Iter>
319 inline
320 void
322 {
323  for ( ; b != e; ++b )
324  addValue( *b );
325 }
326 
330 template <typename RealNumberType>
331 inline
332 void
334 {
335  mySamples = 0;
336  myExp = 0;
337  myExp2 = 0;
338  myMin = 0;
339  myMax = 0;
340  myMedian=0;
341  myIsTerminated=false;
342  if(myStoreSamples){
343  myValues.clear();
344  }
345 }
346 
347 
348 
349 
359 template< typename RealNumberType>
360 inline
361 void
363 {
364  if(myStoreSamples){
365  myMedian=median();
366  myValues.clear();
367  myStoreSamples=false;
368  myIsTerminated=true;
369  }
370 }
371 
372 
374 // Interface - public :
375 
380 template <typename RealNumberType>
381 inline
382 void
384 ( std::ostream& thatStream ) const
385 {
386  thatStream << "[Statistic "
387  << " nb=" << samples()
388  << " exp=" << mean()
389  << " var=" << variance()
390  << " uvar=" << unbiasedVariance()
391  << " min=" << min()
392  << " max=" << max()
393  << "]";
394 }
395 
400 template <typename RealNumberType>
401 inline
402 bool
404 {
405  return true;
406 }
407 
408 
410 // Implementation of inline functions and external operators //
411 
418 template <typename RealNumberType>
419 inline
420 std::ostream&
421 DGtal::operator<<( std::ostream & thatStream,
422  const Statistic<RealNumberType> & that_object_to_display )
423 {
424  that_object_to_display.selfDisplay( thatStream );
425  return thatStream;
426 }
427 
428 // //
430 
431