DGtal 1.3.0
Loading...
Searching...
No Matches
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 */
42template <typename TQuantity>
43inline
44DGtal::Statistic<TQuantity>::~Statistic()
45{}
46
47
48template <typename TQuantity>
49inline
50DGtal::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
59template <typename TQuantity>
60inline
61DGtal::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
83template <typename TQuantity>
84inline
85DGtal::Statistic<TQuantity> &
86DGtal::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
112template <typename TQuantity>
113inline
114DGtal::Statistic<TQuantity> &
115DGtal::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
143template <typename TQuantity>
144inline
145DGtal::Statistic<TQuantity>
146DGtal::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//-----------------------------------------------------------------------------
156template <typename TQuantity>
157inline
158typename DGtal::Statistic<TQuantity>::ConstIterator
159DGtal::Statistic<TQuantity>::begin() const
160{
161 return myValues.begin();
162}
163//-----------------------------------------------------------------------------
164template <typename TQuantity>
165inline
166typename DGtal::Statistic<TQuantity>::ConstIterator
167DGtal::Statistic<TQuantity>::end() const
168{
169 return myValues.end();
170}
171
172
173
174///////////////////////////////////////////////////////////////////////////////
175// ----------------------- Accessors ------------------------------
176
177
178template <typename TQuantity>
179inline
180unsigned int
181DGtal::Statistic<TQuantity>::samples() const
182{
183 return mySamples;
184}
185
186template <typename TQuantity>
187inline
188unsigned int
189DGtal::Statistic<TQuantity>::size() const
190{
191 return myValues.size();
192}
193template <typename TQuantity>
194inline
195TQuantity
196DGtal::Statistic<TQuantity>::operator[]( unsigned int i ) const
197{
198 return i < size() ? myValues[ i ] : Quantity();
199}
200
201template <typename TQuantity>
202inline
203double
204DGtal::Statistic<TQuantity>::mean() const
205{
206 return NumberTraits<Quantity>::castToDouble(myExp) /
207 static_cast<double>(mySamples );
208}
209
210
211template <typename TQuantity>
212inline
213double
214DGtal::Statistic<TQuantity>::variance() const
215{
216 return ( NumberTraits<Quantity>::castToDouble(myExp2) / (double) mySamples ) - mean() * mean();
217}
218
219
220template <typename TQuantity>
221inline
222double
223DGtal::Statistic<TQuantity>::unbiasedVariance() const
224{
225 ASSERT( mySamples != 0 );
226 return ( (double) mySamples ) * variance()
227 / ( (double) mySamples );
228}
229
230
231template <typename TQuantity>
232inline
233TQuantity
234DGtal::Statistic<TQuantity>::max() const
235{
236 return myMax;
237}
238
239
240template <typename TQuantity>
241inline
242TQuantity
243DGtal::Statistic<TQuantity>::min() const
244{
245 return myMin;
246}
247
248
249template <typename TQuantity>
250inline
251TQuantity
252DGtal::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
268template <typename TQuantity>
269inline
270void
271DGtal::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
290template <typename TQuantity>
291template <class Iter>
292inline
293void
294DGtal::Statistic<TQuantity>::addValues( Iter b, Iter e )
295{
296 for ( ; b != e; ++b )
297 addValue( *b );
298}
299
300
301
302template <typename TQuantity>
303inline
304void
305DGtal::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
322template< typename TQuantity>
323inline
324void
325DGtal::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
342template <typename TQuantity>
343inline
344void
345DGtal::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
362template <typename TQuantity>
363inline
364bool
365DGtal::Statistic<TQuantity>::OK() const
366{
367 return true;
368}
369
370
371///////////////////////////////////////////////////////////////////////////////
372// Implementation of inline functions and external operators //
373
374
375template <typename TQuantity>
376inline
377std::ostream&
378DGtal::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