1990issue C041-2
Integer construction of moving averages and exponential smoothing
Integer arithmetic can be much faster than floating-point arithmetic on many computer systems, but overflow and results smaller than one make moving-average construction harder. Editorial reading: a moving-average or exponential-smoothing forecast built from ordered prices is unfinished until overflow limits, multiply-before-divide order, and an integer-safe smoother algebra are stated explicitly.
- Integer arithmetic can be much faster than floating-point arithmetic on many computer systems, but integer overflow and results smaller than one make moving-average construction harder.
- Overflow is most likely in long-lookback linear moving averages, where summed weights can reach the tens of thousands. A 200-period linear moving average of integer prices between 200000 and 300000, with total weight 20100, can push an intermediate weighted sum above 4 billion and past the 32-bit signed ceiling of 2147483647.
- In integer arithmetic the order of multiply and divide changes the result: with factors 2, 1, and 2, multiplying first then dividing yields 1, while dividing first yields 0. Accurate construction uses deferred division, which simple and linear moving averages typically already do.
- The usual exponential-smoothing factor of 2 divided by lookback plus one cannot be stored in an integer variable. An equivalent update adds the previous smoothed value to twice the current residual and divides last by a precomputed lookback plus one.
Integer arithmetic and moving-average construction
Integer arithmetic can be much faster than floating-point arithmetic on many computer systems, but overflow and results smaller than one make moving-average construction harder.
A simple moving average equally weights a fixed lookback of ordered observations and is usually accumulated first, then divided once at the end. A linear moving average is a recency-weighted average whose weights rise in a straight line, so the sum of the weights grows quickly with lookback and can overflow an integer accumulator. Exponential smoothing is a recursive forecast that blends the latest observation with the previous smoothed value, conventionally through a factor of two divided by lookback plus one.
Overflow in long-lookback linear averages
Integer overflow is a running product or sum that exceeds the largest representable integer, including the 32-bit signed ceiling of 2147483647. Overflow is most likely in long-lookback linear moving averages because the summed weights can reach the tens of thousands.
A 200-period linear moving average of a price series stored as integers between 200000 and 300000, with total weight 20100, can produce an intermediate weighted sum above 4 billion, which exceeds that 32-bit signed maximum.
Construction can limit overflow by dividing a running weighted sum before it exceeds the integer range, by trapping overflow and switching to floating-point arithmetic, or by scaling stored prices so the product-sum cannot overflow.
When multiply and divide change places
In integer arithmetic the order of multiply and divide changes the result. With factors 2, 1, and 2, multiplying first then dividing yields 1, while dividing first yields 0.
That early divide is truncation: loss of a fractional result when integer division is performed before later multiplications, collapsing a nonzero quantity to zero. Accurate integer construction postpones division until the last step. Deferred division rearranges a moving-average or smoothing formula so every multiplication finishes before a single terminal division, which simple and linear moving averages typically already do.
An integer update for exponential smoothing
The usual exponential-smoothing factor of 2 divided by lookback plus one is a fraction between 0 and 1 and cannot be stored in an integer variable.
An equivalent integer exponential update adds the previous smoothed value to twice the current residual and divides last by lookback plus one, a denominator that can be precomputed. That keeps the smoother inside the same deferred-division discipline used for the averages.
All readings on this track · 41 readings
- 1984Matching average age across price filters
- 1984Residual-impact construction of exponential smoothers
- 1988Constructing simple and exponential moving averages as specified filters
- 1989Evaluating smoothed free reserves and funds-discount spreads
- 1990Integer construction of moving averages and exponential smoothing
- 1990Constructing incremental moving average filters
- 1991Constructing a double-smoothed stochastic oscillator
- 1991Constructing an exponential average from a smoothing weight
- 1991Constructing a double-smoothed momentum oscillator family
- 1991Competing certainty rules from exponential-average conditions
- 1991Hybrid construction of a mechanical timing system from smoothed signals
- 1991Constructing an exponential smoother from a moving-average length
- 1991Equivalent relative-strength-index constructions from a ratio and from smoothed momentum
- 1991Constructing the true strength index from double-smoothed momentum
- 1991True strength index from signed, double-smoothed momentum
- 1992Constructing exponential averages from simple lookbacks
- 1992Constructing exponential averages by the smoothing constant, startup length and lag
- 1992Nested range smoothing and windowed high-low damping
- 1992A weekly equity stance from the discount-bill spread
- 1993Double-smoothed stochastics momentum index construction
- 1993Constructing the true strength index with double exponential smoothing
- 1994Polarized fractal efficiency with a shared exponential smoother
- 1994Five-interval exponential smoothing of ten-interval signed path-efficiency
- 1994Hand-ranked price-volume series with stacked smoothing and stochastic
- 1995Range expansion filter on multi-horizon averages
- 1995The exponential smoothing constant from averaging lag
- 1997Construct a nested-average rainbow band and oscillator
- 1998Filter stacks that combine a moving average with exponential smoothing
- 2002Smoothed relative-strength construction from wilder-averaging and a price prefilter
- 2003Reverse-engineered RSI price targets
- 2004Piecewise trend quality from moving-average reversals
- 2004Nested smoothing recipes versus unaudited oscillator experiments
- 2008Weight construction beyond the N-day label
- 2010A consensus filter for objective RSI and smoothing construction
- 2012Sentiment-zone oscillator from signed closes
- 2012Classic RSI one-over-N smoothing versus the standard exponential average
- 2013Why exponential smoothing uses two over n plus one
- 2015Constructing ratio indicators with moving-average and exponential-smoothing weights
- 2015Slow relative-strength index from an exponentially smoothed price
- 2017Rebuilding the relative strength index as open-scale buy-sell pressure
- 2017Comparing Wilder and exponential smoothing inside a relative-strength index