Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
cras::RunningStats< T > Class Template Reference

Computation of running average and variance using Welford's algorithm. More...

#include <running_stats.hpp>

Public Member Functions

void addSample (T x)
 Add a new sample to the statistics. More...
 
size_t getCount () const
 Get the number of stored samples. More...
 
getMean () const
 Get the mean of stored samples. More...
 
getSampleVariance () const
 Get the sample variance of stored samples (like variance, but divided by count-1 instead of count, should be unbiased estimator). More...
 
getStandardDeviation () const
 Get standard deviation of stored samples. More...
 
getVariance () const
 Get the variance of stored samples. More...
 
RunningStats< T > operator+ (const RunningStats< T > &other) const
 Combine the two sequences represented by this and other and represent their joint stats. More...
 
RunningStats< T > operator+ (const T &sample) const
 Return a copy of this with the added sample. More...
 
RunningStats< T > & operator+= (const RunningStats< T > &other)
 Combine the two sequences represented by this and other and represent their joint stats. More...
 
RunningStats< T > & operator+= (const T &sample)
 Add the sample to the stats. More...
 
RunningStats< T > operator- (const RunningStats< T > &other) const
 Subtract the sequence represented by other from the sequence represented by this and return the updated stats. More...
 
RunningStats< T > operator- (const T &sample) const
 Remove a sample from the statistics. More...
 
RunningStats< T > & operator-= (const RunningStats< T > &other)
 Subtract the sequence represented by other from the sequence represented by this and update the stats. More...
 
RunningStats< T > & operator-= (const T &sample)
 Remove a sample from the statistics. More...
 
void removeSample (T x)
 Remove a sample from the statistics. More...
 
void reset ()
 Reset the statistics to represent an empty sequence. More...
 

Protected Member Functions

template<>
ros::Duration multiply (const ::ros::Duration &val1, const ::ros::Duration &val2)
 
template<>
ros::WallDuration multiply (const ::ros::WallDuration &val1, const ::ros::WallDuration &val2)
 
template<>
ros::Duration sqrt (const ::ros::Duration &val)
 
template<>
ros::WallDuration sqrt (const ::ros::WallDuration &val)
 
template<>
ros::Duration zero ()
 
template<>
ros::WallDuration zero ()
 

Static Protected Member Functions

static T multiply (const T &val1, const T &val2)
 val1 * val2 More...
 
static T multiplyScalar (const T &val, double scalar)
 val * scalar More...
 
static T sqrt (const T &val)
 Return the square root of the given value (whatever meaning that might have). More...
 
static T zero ()
 Return a T value representing zero. More...
 

Protected Attributes

size_t count {0u}
 Number of represented samples. More...
 
mean {RunningStats<T>::zero()}
 Mean of represented samples. More...
 
var {RunningStats<T>::zero()}
 Sk term of the computation such that var(X0...Xk) = this->var/this->count. More...
 

Detailed Description

template<typename T>
class cras::RunningStats< T >

Computation of running average and variance using Welford's algorithm.

Template Parameters
TType of the values. It has to support operator+ and operator- with T.

This class can keep track of mean and variance of a stream of data using an efficient online algorithm. The required memory is constant, and time to insert a new value and update the stats is also constant.

You can also add and subtract instances of this class which behaves as if you merged / diffed the streams that created the stats.

Definition at line 28 of file running_stats.hpp.

Member Function Documentation

◆ addSample()

template<typename T>
void cras::RunningStats< T >::addSample ( x)

Add a new sample to the statistics.

Parameters
[in]xThe sample to add.
Note
This function runs in O(1) time.

◆ getCount()

template<typename T>
size_t cras::RunningStats< T >::getCount ( ) const

Get the number of stored samples.

Returns
The number of samples.

◆ getMean()

template<typename T>
T cras::RunningStats< T >::getMean ( ) const

Get the mean of stored samples.

Returns
The mean.
Note
This function runs in O(1) time.

◆ getSampleVariance()

template<typename T>
T cras::RunningStats< T >::getSampleVariance ( ) const

Get the sample variance of stored samples (like variance, but divided by count-1 instead of count, should be unbiased estimator).

Returns
The sample variance.
Note
This function runs in O(1) time.

◆ getStandardDeviation()

template<typename T>
T cras::RunningStats< T >::getStandardDeviation ( ) const

Get standard deviation of stored samples.

Returns
The standard deviation.
Note
The standard deviation computation is based on the sample variance.
This function runs in O(1) time.

◆ getVariance()

template<typename T>
T cras::RunningStats< T >::getVariance ( ) const

Get the variance of stored samples.

Returns
The variance.
Note
This function runs in O(1) time.

◆ multiply() [1/3]

template<>
ros::Duration cras::RunningStats<::ros::Duration >::multiply ( const ::ros::Duration val1,
const ::ros::Duration val2 
)
protected

Definition at line 21 of file running_stats_duration.hpp.

◆ multiply() [2/3]

template<>
ros::WallDuration cras::RunningStats<::ros::WallDuration >::multiply ( const ::ros::WallDuration val1,
const ::ros::WallDuration val2 
)
protected

Definition at line 43 of file running_stats_duration.hpp.

◆ multiply() [3/3]

template<typename T>
static T cras::RunningStats< T >::multiply ( const T &  val1,
const T &  val2 
)
staticprotected

val1 * val2

Parameters
[in]val1Multiplicand.
[in]val2Multiplicand.
Returns
val1 * val2

◆ multiplyScalar()

template<typename T>
static T cras::RunningStats< T >::multiplyScalar ( const T &  val,
double  scalar 
)
staticprotected

val * scalar

Parameters
[in]valThe value to multiply.
[in]scalarThe scalar to multiply with.
Returns
val * scalar

◆ operator+() [1/2]

template<typename T>
RunningStats<T> cras::RunningStats< T >::operator+ ( const RunningStats< T > &  other) const

Combine the two sequences represented by this and other and represent their joint stats.

Parameters
[in]otherThe other stats to merge.
Returns
The combined stats.
Note
This function runs in O(1) time.

◆ operator+() [2/2]

template<typename T>
RunningStats<T> cras::RunningStats< T >::operator+ ( const T &  sample) const

Return a copy of this with the added sample.

Parameters
[in]sampleThe sample to add.
Returns
The new stats.
Note
This function runs in O(1) time.

◆ operator+=() [1/2]

template<typename T>
RunningStats<T>& cras::RunningStats< T >::operator+= ( const RunningStats< T > &  other)

Combine the two sequences represented by this and other and represent their joint stats.

Parameters
[in]otherThe other stats to merge.
Returns
This.
Note
This function runs in O(1) time.

◆ operator+=() [2/2]

template<typename T>
RunningStats<T>& cras::RunningStats< T >::operator+= ( const T &  sample)

Add the sample to the stats.

Parameters
[in]sampleThe sample to add.
Returns
This.
Note
This function runs in O(1) time.

◆ operator-() [1/2]

template<typename T>
RunningStats<T> cras::RunningStats< T >::operator- ( const RunningStats< T > &  other) const

Subtract the sequence represented by other from the sequence represented by this and return the updated stats.

Parameters
[in]otherThe stats to subtract.
Returns
The stats with other removed.
Note
If other is longer than this, empty stats will be returned.
This function runs in O(1) time.

◆ operator-() [2/2]

template<typename T>
RunningStats<T> cras::RunningStats< T >::operator- ( const T &  sample) const

Remove a sample from the statistics.

Parameters
[in]sampleThe sample to remove.
Returns
The stats with sample removed.
Note
This function runs in O(1) time.
It is the responsibility of the caller to make sure the sample being removed has actually been added before. If that is not true, the computed statistics will stop being valid.
Removing from an empty instance does nothing.

◆ operator-=() [1/2]

template<typename T>
RunningStats<T>& cras::RunningStats< T >::operator-= ( const RunningStats< T > &  other)

Subtract the sequence represented by other from the sequence represented by this and update the stats.

Parameters
[in]otherThe stats to subtract.
Returns
This.
Note
If other is longer than this, empty stats will be returned.
This function runs in O(1) time.

◆ operator-=() [2/2]

template<typename T>
RunningStats<T>& cras::RunningStats< T >::operator-= ( const T &  sample)

Remove a sample from the statistics.

Parameters
[in]sampleThe sample to remove.
Returns
This.
Note
This function runs in O(1) time.
It is the responsibility of the caller to make sure the sample being removed has actually been added before. If that is not true, the computed statistics will stop being valid.
Removing from an empty instance does nothing.

◆ removeSample()

template<typename T>
void cras::RunningStats< T >::removeSample ( x)

Remove a sample from the statistics.

Parameters
[in]xThe sample to remove.
Note
This function runs in O(1) time.
It is the responsibility of the caller to make sure the sample being removed has actually been added before. If that is not true, the computed statistics will stop being valid.
Removing from an empty instance does nothing.

◆ reset()

template<typename T>
void cras::RunningStats< T >::reset ( )

Reset the statistics to represent an empty sequence.

◆ sqrt() [1/3]

template<>
ros::Duration cras::RunningStats<::ros::Duration >::sqrt ( const ::ros::Duration val)
protected

Definition at line 31 of file running_stats_duration.hpp.

◆ sqrt() [2/3]

template<>
ros::WallDuration cras::RunningStats<::ros::WallDuration >::sqrt ( const ::ros::WallDuration val)
protected

Definition at line 54 of file running_stats_duration.hpp.

◆ sqrt() [3/3]

template<typename T>
static T cras::RunningStats< T >::sqrt ( const T &  val)
staticprotected

Return the square root of the given value (whatever meaning that might have).

Parameters
[in]valThe value to take root of.
Returns
The square root.

◆ zero() [1/3]

template<>
ros::Duration cras::RunningStats<::ros::Duration >::zero ( )
protected

Definition at line 37 of file running_stats_duration.hpp.

◆ zero() [2/3]

template<>
ros::WallDuration cras::RunningStats<::ros::WallDuration >::zero ( )
protected

Definition at line 60 of file running_stats_duration.hpp.

◆ zero() [3/3]

template<typename T>
static T cras::RunningStats< T >::zero ( )
staticprotected

Return a T value representing zero.

Returns
0.

Member Data Documentation

◆ count

template<typename T>
size_t cras::RunningStats< T >::count {0u}
protected

Number of represented samples.

Definition at line 193 of file running_stats.hpp.

◆ mean

template<typename T>
T cras::RunningStats< T >::mean {RunningStats<T>::zero()}
protected

Mean of represented samples.

Definition at line 196 of file running_stats.hpp.

◆ var

template<typename T>
T cras::RunningStats< T >::var {RunningStats<T>::zero()}
protected

Sk term of the computation such that var(X0...Xk) = this->var/this->count.

Definition at line 199 of file running_stats.hpp.


The documentation for this class was generated from the following file:


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Sat Jun 17 2023 02:32:53