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/>.
18 * @file MultiStatistics.ih
19 * @author Bertrand Kerautret (\c kerautre@loria.fr )
20 * LORIA (CNRS, UMR 7503), University of Nancy, France
22 * @author Jacques-Olivier Lachaud
25 * Implementation of inline methods defined in MultiStatistics.h
27 * This file is part of the DGtal library.
30///////////////////////////////////////////////////////////////////////////////
31// IMPLEMENTATION of inline methods.
32///////////////////////////////////////////////////////////////////////////////
34//////////////////////////////////////////////////////////////////////////////
37//////////////////////////////////////////////////////////////////////////////
42///////////////////////////////////////////////////////////////////////////////
43// Implementation of inline
47DGtal::MultiStatistics::MultiStatistics( const unsigned int size,
48 const bool storeSamples )
60 myIsTerminate = false;
61 init( size, storeSamples );
71DGtal::MultiStatistics::~MultiStatistics()
80DGtal::MultiStatistics::read( std::istream & in, MultiStatistics & samples,
81 const std::vector<unsigned int> & indices )
88 && ( str[ 0 ] != '#' ) )
90 std::istringstream in_str( str );
93 while ( in_str.good() )
96 for ( unsigned int j = 0; j < indices.size(); ++j )
97 if ( indices[ j ] == idx )
99 samples.addValue( j, val );
100 // cout << "Adding " << val << " to " << j << endl;
113DGtal::MultiStatistics::nb() const
121DGtal::MultiStatistics::samples( const unsigned int k ) const
123 return mySamples[ k ];
129DGtal::MultiStatistics::mean( const unsigned int k ) const
131 ASSERT(myIsTerminate);
139DGtal::MultiStatistics::variance( const unsigned int k ) const
141 ASSERT(myIsTerminate);
149DGtal::MultiStatistics::unbiasedVariance( const unsigned int k ) const
151 ASSERT(myIsTerminate);
152 return myUnbiasedVar[ k ];
158DGtal::MultiStatistics::max( const unsigned int k ) const
166DGtal::MultiStatistics::maxIndice( const unsigned int k ) const
168 return myIndiceMax[ k ];
175DGtal::MultiStatistics::min( const unsigned int k ) const
182DGtal::MultiStatistics::minIndice( const unsigned int k ) const
184 return myIndiceMin[ k ];
191DGtal::MultiStatistics::value( const unsigned int k, const unsigned int i ) const
193 if ( myStoreSamples ) {
196 && ( i < mySamples[ k ] ) );
197 return myValues[ k ][ i ];
205DGtal::MultiStatistics::addValue( unsigned int k, double v )
211 if ( mySamples[ k ] == 1 )
219 else if ( v > myMax[ k ] ){
221 myIndiceMax[k]=mySamples[k]-1;
223 else if ( v < myMin[ k ] ){
225 myIndiceMin[k]=mySamples[k]-1;
227 if ( myStoreSamples )
228 myValues[ k ].push_back( v );
229 myIsTerminate = false;
236DGtal::MultiStatistics::addValues( const unsigned int k, Iter b, Iter e )
238 for ( ; b != e; ++b )
240 myIsTerminate = false;
246DGtal::MultiStatistics::terminate()
248 for ( unsigned int k = 0; k < myNb; ++k )
250 myExp[ k ] /= mySamples[ k ];
251 myExp2[ k ] /= mySamples[ k ];
252 myVar[ k ] = myExp2[ k ] - myExp[ k ] * myExp[ k ];
253 if ( mySamples[ k ] > 1 )
254 myUnbiasedVar[ k ] = mySamples[ k ] * myVar[ k ] / ( mySamples[ k ] - 1 );
256 myUnbiasedVar[ k ] = 0;
259 myIsTerminate = true;
264DGtal::MultiStatistics::init( unsigned int size, bool storeSamples )
267 if ( size == 0 ) return;
269 mySamples = new unsigned int[ size ];
270 myExp = new double[ size ];
271 myExp2 = new double[ size ];
272 myVar = new double[ size ];
273 myUnbiasedVar = new double[ size ];
274 myMax = new double[ size ];
275 myMin = new double[ size ];
276 myIndiceMax = new unsigned int[ size ];
277 myIndiceMin = new unsigned int[ size ];
278 myStoreSamples = storeSamples;
279 if ( myStoreSamples )
280 myValues = new std::vector<double>[ size ];
282 myIsTerminate = false;
289DGtal::MultiStatistics::clear()
291 if ( myNb == 0 ) return;
292 for ( unsigned int i = 0; i < myNb; ++ i )
298 myUnbiasedVar[ i ] = 0.0;
301 myIndiceMin[ i ] = 0;
302 myIndiceMax[ i ] = 0;
304 if ( myStoreSamples ) {
305 myValues[ i ].clear();
306 myValues[ i ].reserve( 128 );
309 myIsTerminate = false;
315DGtal::MultiStatistics::erase()
317 if ( mySamples != 0 ) delete[] mySamples;
318 if ( myExp != 0 ) delete[] myExp;
319 if ( myExp2 != 0 ) delete[] myExp2;
320 if ( myVar != 0 ) delete[] myVar;
321 if ( myUnbiasedVar != 0 ) delete[] myUnbiasedVar;
322 if ( myMax != 0 ) delete[] myMax;
323 if ( myMin != 0 ) delete[] myMin;
324 if ( myIndiceMax != 0 ) delete[] myIndiceMax;
325 if ( myIndiceMin != 0 ) delete[] myIndiceMin;
326 if ( myValues != 0 ) delete[] myValues;
340 myStoreSamples = false;
346DGtal::MultiStatistics::covariance( const unsigned int x, const unsigned int y,
347 const unsigned int s, unsigned int e ) const
349 ASSERT( ( x < myNb ) && ( y < myNb ) && ( x != y )
351 && ( samples( x ) == samples( y ) ) );
352 unsigned int size = samples( x );
353 double coVariance = 0.0;
357 if ( e == 0 ) e = size;
359 for( unsigned int k = s; k != e; ++k )
361 coVariance += value( x, k ) * value( y, k );
367 coVariance = coVariance / ( e - s );
368 coVariance = coVariance - mx * my;
374std::pair<double,double>
375DGtal::MultiStatistics::linearRegression( const unsigned int x, const unsigned int y ) const
377 ASSERT( ( x < myNb ) && ( y < myNb ) && ( x != y )
379 && ( samples( x ) == samples( y ) ) );
381 double cov = covariance( x, y );
382 double slope = cov / unbiasedVariance( x );
383 double b = mean( y ) - slope * mean( x );
384 return std::make_pair( slope, b );
390DGtal::MultiStatistics::median( unsigned int k )
392 ASSERT( myStoreSamples );
394 nth_element( myValues[ k ].begin(), myValues[ k ].begin()+(myValues[k].size()/2),
395 myValues[ k ].end());
396 return *(myValues[ k ].begin()+(myValues[k].size()/2));
402///////////////////////////////////////////////////////////////////////////////
403// Interface - public :
408DGtal::MultiStatistics::selfDisplay( std::ostream& out ) const
410 out << "[Statistics] nb=" << nb() << std::endl;
415DGtal::MultiStatistics::isValid() const
422///////////////////////////////////////////////////////////////////////////////
423// Implementation of inline functions and external operators //
432 * Overloads 'operator<<' for displaying objects of class 'MultiStatistics'.
433 * @param out the output stream where the object is written.
434 * @param object the object of class 'MultiStatistics' to write.
435 * @return the output stream after the writing.
439DGtal::operator<< ( std::ostream & out,
440 const MultiStatistics & object )
442 object.selfDisplay ( out );
447///////////////////////////////////////////////////////////////////////////////