3.5.2.9 Emovavg

Description

This function is for calculating exponential moving averages, where the weight factors decrease exponentially. The degree of weighting decrease is defined as \alpha = 2/(n + 1), where n = timeperiod. Two methods involved to start the calculation.

  • Method I: starts from point = n

E_i=\left\{\begin{matrix}
\frac{(1-\alpha)}{n}\sum_{j=1}^{n}x_j + \alpha x_n, & \textup{if} \; i = n;\\ 
(1-\alpha)E_{i-1} + \alpha x_i, & \textup{if} \; i > n. 
\end{matrix}\right.

  • Method II: starts from point = 1

E_i=\left\{\begin{matrix}
x_1, & \textup{if} \; i = 1;\\ 
(1-\alpha)E_{i-1} + \alpha x_i, & \textup{if} \; i > 1. 
\end{matrix}\right.

Syntax

vector emovavg(vector vd, int n[, int method=0])

Parameters

vd

The data vector used to calculate exponential moving average.

n

is the timeperiod.

method

is the method controller.If we set method = 1, then we use Method II described above. And by default method = 0, which means using Method I.

Return

Return the exponential moving average vector.

Example

// Col(2) will be filled with exponential moving average value at each point, 
//with stating point = 10. 
// Col(3) will be filled with exponential moving average value at each point, 
//with stating point = 1.

for(ii=1;ii<=30;ii++) col(1)[ii] = ii;
col(2)=emovavg(col(1),10); //method I
col(3)=emovavg(col(1),10, 1); //method II