Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize halflife-based EMA's in csp.stats #288

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions cpp/csp/cppnodes/statsimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,7 @@ class AlphaDebiasEMA
w0 = 1.0;
else
w0 = 1 - m_decay;
m_sqsum += pow( w0, 2 );
m_sqsum += w0 * w0;
m_wsum += w0;
}
else
Expand All @@ -1746,7 +1746,7 @@ class AlphaDebiasEMA
double wh = pow( m_decay, lookback );
if( !m_adjust )
wh *= ( 1- m_decay );
m_sqsum -= pow( wh, 2 );
m_sqsum -= wh * wh;
m_wsum -= wh;
if( m_wsum < EPSILON || m_sqsum < EPSILON )
{
Expand All @@ -1767,7 +1767,7 @@ class AlphaDebiasEMA

double compute() const
{
double wsum_sq = pow( m_wsum, 2 );
double wsum_sq = m_wsum * m_wsum;
if( abs( wsum_sq - m_sqsum ) > EPSILON )
return wsum_sq / ( wsum_sq - m_sqsum );
else
Expand All @@ -1794,7 +1794,7 @@ class HalflifeEMA

HalflifeEMA( TimeDelta halflife, DateTime start )
{
m_halflife = halflife;
m_decay_factor = log( 0.5 ) / halflife.asNanoseconds();
m_last_tick = start;
reset();
}
Expand All @@ -1808,12 +1808,9 @@ class HalflifeEMA
if( likely( !isnan( x ) ) )
{
TimeDelta delta_t = now - m_last_tick;
double decay_duration = ( double )( delta_t.asNanoseconds() ) / ( m_halflife.asNanoseconds() );
double decay = exp( -( decay_duration ) * log( 2 ) );
m_ema *= decay;
m_norm *= decay;
m_ema += x;
m_norm++;
double decay = exp( m_decay_factor * delta_t.asNanoseconds() );
m_ema = decay * m_ema + x;
m_norm = decay * m_norm + 1.0;
m_last_tick = now;
}
}
Expand All @@ -1832,7 +1829,7 @@ class HalflifeEMA

double m_ema;
double m_norm;
TimeDelta m_halflife;
double m_decay_factor;
DateTime m_last_tick;

};
Expand All @@ -1844,7 +1841,7 @@ class HalflifeDebiasEMA

HalflifeDebiasEMA( TimeDelta halflife, DateTime start )
{
m_halflife = halflife;
m_decay_factor = log( 0.5 ) / halflife.asNanoseconds();
m_last_tick = start;
reset();
}
Expand All @@ -1858,11 +1855,9 @@ class HalflifeDebiasEMA
if( likely( !isnan( x ) ) )
{
TimeDelta delta_t = now - m_last_tick;
double decay = exp( -( delta_t/m_halflife ) * log( 2 ) );
m_sqsum *= pow( decay, 2 );
m_wsum *= decay;
m_sqsum++;
m_wsum++;
double decay = exp( m_decay_factor * delta_t.asNanoseconds() );
m_sqsum = decay * decay * m_sqsum + 1.0;
m_wsum = decay * m_wsum + 1.0;
m_last_tick = now;
}
}
Expand All @@ -1874,7 +1869,7 @@ class HalflifeDebiasEMA

double compute() const
{
double wsum_sq = pow( m_wsum, 2 );
double wsum_sq = m_wsum * m_wsum;
if( wsum_sq != m_sqsum )
return wsum_sq / ( wsum_sq - m_sqsum );
else
Expand All @@ -1885,7 +1880,7 @@ class HalflifeDebiasEMA

double m_wsum;
double m_sqsum;
TimeDelta m_halflife;
double m_decay_factor;
DateTime m_last_tick;
};

Expand Down
Loading