exponential_moving_average

Function exponential_moving_average 

Source
pub fn exponential_moving_average(
    slice: &[f64],
    old: Option<f64>,
) -> Result<f64>
Expand description

Exponential moving average (EMA) is a filter that applies weighting factors which decrease exponentially. The weighting for each older datum decreases exponentially, never reaching zero.

The number of periods depends on the analytical objectives. Typical number of periods for short, medium and long term trends are 5-20, 20-60 and 100-200 respectively.

Function calculates the weighting factor from the slice length and calculates the exponential moving average using that factor. If EMA is calculated for the first time previous argument needs to be 0. When previous is 0 function returns simple moving average of the datum points.

§Arguments

  • slice - array of values
  • old - previous EMA if any

§Example

use stat::analysis::trend;

let array = [3.5, 3.4, 3.3, 3.6, 3.7];
let value = trend::exponential_moving_average(&array, Some(3.4));
assert_eq!(value.ok(), Some(3.5));