Click
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
DirectEWMAX< P > Class Template Reference

An exponentially weighted moving average. More...

#include <click/ewma.hh>

Inherits P.

Public Types

typedef P::value_type value_type
 

Public Member Functions

 DirectEWMAX ()
 Construct a EWMA with initial average 0.
 
 DirectEWMAX (value_type scaled_value)
 Construct a EWMA with initial scaled average scaled_value.
 
value_type scaled_average () const
 Return the current scaled moving average.
 
value_type unscaled_average () const
 Return the current moving average, rounded up.
 
void clear ()
 Reset the EWMA to value 0.
 
void assign (value_type scaled_value)
 Assign the EWMA to scaled average scaled_value.
 
void update (value_type x)
 Update the moving average with a new observation.
 
void update_n (value_type x, unsigned n)
 Update the moving average with n identical observations.
 
String unparse () const
 Unparse the current average into a String.
 
void update_with (value_type x)
 Update the moving average with a new observation (deprecated).
 

Detailed Description

template<typename P>
class DirectEWMAX< P >

An exponentially weighted moving average.

The DirectEWMAX template class represents a simple exponentially weighted moving average. The average starts out with value 0. The update() function adds a new observation to the average.

The template parameter P defines three EWMA parameters: value type, stability shift, and scale factor.

The value type is simply the unsigned integral type used to store the average. It is also the type of each observation. unsigned and uint64_t are examples.

The stability shift specifies alpha, the stability parameter. Concretely, alpha = 1. / (2 ** stability_shift). Thus, a stability shift of 4 corresponds to an alpha of 1/16.

The scaling factor specifies how many bits of fraction are kept per observation. Fraction bits are necessary to account for small differences between observations. For example, consider a EWMA with value 0, alpha 1/16, and 0 bits of fraction. Assume the EWMA begins to observe a stream of observations equal to 1. Despite these observations, the EWMA's value will never change from 0, since the 1/16 alpha factor rounds the new observations down to 0. At least 4 bits of fraction are required to account for this difference. There is a tradeoff: the more bits of fraction, the more precise the EWMA, but the less bits available to account for large values.

These EWMA parameters are defined by five of P's members, two typedefs and three possibly static member functions.

P::value_type

The EWMA's value type. Example: unsigned.

P::signed_value_type

The signed version of P::value_type. Used internally. Example: int.

unsigned P::stability_shift()

This function should return this EWMA's stability shift (see above).

unsigned P::scale()

This function should return this EWMA's scaling factor (see above).

unsigned P::compensation()
This function should return this EWMA's stability compensation, which normally equals 1 << (stability_shift - 1).

Since DirectEWMAX inherits from an object of type P, these members are also directly available to callers.

The FixedEWMAXParameters and StabilityEWMAXParameters types are good template arguments for DirectEWMAX.

See also
RateEWMAX

Constructor & Destructor Documentation

◆ DirectEWMAX() [1/2]

template<typename P >
DirectEWMAX< P >::DirectEWMAX ( )
inline

Construct a EWMA with initial average 0.

◆ DirectEWMAX() [2/2]

template<typename P >
DirectEWMAX< P >::DirectEWMAX ( value_type  scaled_value)
inline

Construct a EWMA with initial scaled average scaled_value.

Member Function Documentation

◆ scaled_average()

template<typename P >
value_type DirectEWMAX< P >::scaled_average ( ) const
inline

Return the current scaled moving average.

Note
The returned value has scale() bits of fraction.

◆ unscaled_average()

template<typename P >
value_type DirectEWMAX< P >::unscaled_average ( ) const
inline

Return the current moving average, rounded up.

Note
The returned value is unscaled (has zero bits of fraction).

◆ clear()

template<typename P >
void DirectEWMAX< P >::clear ( )
inline

Reset the EWMA to value 0.

◆ assign()

template<typename P >
void DirectEWMAX< P >::assign ( value_type  scaled_value)
inline

Assign the EWMA to scaled average scaled_value.

◆ update()

template<typename P >
void DirectEWMAX< P >::update ( value_type  x)
inline

Update the moving average with a new observation.

Parameters
xthe observation (unscaled)

◆ update_n()

template<typename P >
void DirectEWMAX< P >::update_n ( value_type  x,
unsigned  n 
)

Update the moving average with n identical observations.

Parameters
xthe observation (unscaled)
nnumber of observations
Note
This may be faster than calling update(x) n times.

◆ unparse()

template<typename P >
String DirectEWMAX< P >::unparse ( ) const
inline

Unparse the current average into a String.

Note
The returned value is unscaled, but may contain a fractional part.

◆ update_with()

template<typename P >
void DirectEWMAX< P >::update_with ( value_type  x)
inline

Update the moving average with a new observation (deprecated).

Parameters
xthe observation (unscaled)
Deprecated:
Use update() instead.

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