nAverager Class Reference

class responsible for calculating averages More...

#include <nNetwork.h>

List of all members.

Public Member Functions

 nAverager ()
 constructor
 ~nAverager ()
 destructor
REAL GetAverage () const
 returns the average value
REAL GetDataVariance () const
 returns the variance of the data ( average of (value-average)^2 )
REAL GetAverageVariance () const
 returns the expected variance of the returned average
void Timestep (REAL decay)
 lets all values decay, so they can be replaced by new ones
void Add (REAL value, REAL weight=1)
 adds a value to the average
void Reset ()
 resets average to zero
std::istream & operator<< (std::istream &stream)
 read operator
std::ostream & operator>> (std::ostream &stream) const
 write operator

Private Attributes

REAL weight_
 the total statistical weight
REAL sum_
 the total sum of value*weight
REAL sumSquared_
 the total sum of value*value*weight
REAL weightSquared_
 the sum of all weights, squared


Detailed Description

class responsible for calculating averages

Definition at line 241 of file nNetwork.h.


Constructor & Destructor Documentation

nAverager::nAverager ( void   ) 

constructor

Definition at line 3596 of file nNetwork.cpp.

03600         : weight_(0), sum_(0), sumSquared_(0), weightSquared_(0)

nAverager::~nAverager ( void   ) 

destructor

Definition at line 3610 of file nNetwork.cpp.


Member Function Documentation

REAL nAverager::GetAverage ( void   )  const

returns the average value

Returns:
the average value over the last time

Definition at line 3679 of file nNetwork.cpp.

Referenced by eTimer::AverageFPS(), eTimer::AverageFrameTime(), eTimer::IsSynced(), eTimer::Reset(), and eTimer::SyncTime().

03683 {
03684     if ( weight_ > 0 )
03685         return sum_ / weight_;

Here is the caller graph for this function:

REAL nAverager::GetDataVariance ( void   )  const

returns the variance of the data ( average of (value-average)^2 )

Returns:
the average recent variance in the incoming data

Definition at line 3697 of file nNetwork.cpp.

Referenced by GameLoop(), and gCycleMovement::TimestepCore().

03701 {
03702     if ( weight_ > 0 )
03703     {
03704         REAL average       = sum_ / weight_;
03705         REAL averageSquare = sumSquared_ / weight_;
03706         REAL ret = averageSquare - average * average;
03707         if ( ret < 0 )
03708             ret = 0;
03709         return ret;
03710     }

Here is the caller graph for this function:

REAL nAverager::GetAverageVariance ( void   )  const

returns the expected variance of the returned average

Returns:
the expected variance of the return value of GetAverage()

Definition at line 3722 of file nNetwork.cpp.

Referenced by eTimer::IsSynced(), eTimer::ReadSync(), sn_GetTimeout(), and eTimer::SyncTime().

03726 {
03727     if ( weight_ > 0 )
03728     {
03729         REAL square = weight_ * weight_;
03730 
03731         REAL denominator = square - weightSquared_;
03732         REAL numerator = GetDataVariance() * weightSquared_;
03733         if ( denominator > numerator * 1E-30 )
03734         {
03735             return numerator/denominator;
03736         }
03737         else
03738             return 1E+30;
03739     }

Here is the caller graph for this function:

void nAverager::Timestep ( REAL  decay  ) 

lets all values decay, so they can be replaced by new ones

Parameters:
decay decay factor 0 .. infinity; larger values lead to more decay.

Definition at line 3624 of file nNetwork.cpp.

Referenced by eTimer::ReadSync(), and eTimer::SyncTime().

03628 {
03629     REAL factor = 1/(1+decay);
03630 
03631     // pretend all data so far was collected with a weight of the original weight multiplied by factor
03632     weight_        *= factor;
03633     sum_           *= factor;

Here is the caller graph for this function:

void nAverager::Add ( REAL  value,
REAL  weight = 1 
)

adds a value to the average

Parameters:
value the value to add
weight its statistical weight (importance compared to other values)

Definition at line 3646 of file nNetwork.cpp.

Referenced by eTimer::eTimer(), eTimer::ReadSync(), eTimer::Reset(), and eTimer::SyncTime().

03650 {
03651     tASSERT( weight >= 0 );
03652     weight_        += weight;
03653     sum_           += weight * value;

Here is the caller graph for this function:

void nAverager::Reset ( void   ) 

resets average to zero

Definition at line 3664 of file nNetwork.cpp.

Referenced by eTimer::eTimer(), eTimer::ReadSync(), and eTimer::Reset().

03668 {

Here is the caller graph for this function:

std::istream & nAverager::operator<< ( std::istream &  stream  ) 

read operator

Parameters:
stream stream to read from
Returns:
stream for chaining

Definition at line 3752 of file nNetwork.cpp.

03756 {
03757     char c;
03758     stream >> c;
03759     tASSERT( c == '(' );
03760 
03761     stream >> weight_ >> sum_ >> sumSquared_ >> weightSquared_;
03762 
03763     stream >> c;
03764     tASSERT( c == ')' );

std::ostream & nAverager::operator>> ( std::ostream &  stream  )  const

write operator

Parameters:
stream stream to write to
Returns:
stream for chaining

Definition at line 3777 of file nNetwork.cpp.

03781 {
03782     stream << '(' << weight_ << ' ' << sum_  << ' ' << sumSquared_  << ' ' << weightSquared_  << ')';


Member Data Documentation

REAL nAverager::weight_ [private]

the total statistical weight

Definition at line 256 of file nNetwork.h.

REAL nAverager::sum_ [private]

the total sum of value*weight

Definition at line 257 of file nNetwork.h.

REAL nAverager::sumSquared_ [private]

the total sum of value*value*weight

Definition at line 258 of file nNetwork.h.

REAL nAverager::weightSquared_ [private]

the sum of all weights, squared

Definition at line 259 of file nNetwork.h.


The documentation for this class was generated from the following files:
Generated on Sat Mar 15 23:46:45 2008 for Armagetron Advanced by  doxygen 1.5.4