3.5.2.40 Tmovavg

Description

This function is for calculating triangular moving averages. In fact, this function equals to using function Movavg twice with n_1=\left \lfloor n/2 \right \rfloor backward and 0 forward. Thus, at point = i, the triangular moving average value is:

T_i = \frac{1}{n_1}\sum_{j=1}^{n_1}S_{j-n_1+i-1},\; \textup{if} \; i >2n_1,

where S_j = \frac{1}{n_1}\sum_{k=1}^{n_1}x_{k-n_1+j-1},\; \textup{if} \; j>n1 and n_1=\left \lfloor n/2 \right \rfloor.

Syntax

vector tmovavg(vector vd, int n[, int missing])

Parameters

vd

The data vector is used to calculate triangular moving average.

n

is the time period.

missing

Optional. Determine how to deal with the missing value in the moving range.
  • missing = 0 omits missing values from the calculation. This is the default value.
  • missing = 1 includes missing values in the calculation, which means, if an adjacent range includes missing its output will be missing;
  • missing = 2 omits missing values from the calculation but keeps missing in the ouput, which means, if a row is missing, its output will be missing.
Please refer to example 2 to see the difference.

Return

Return the triangular moving average vector.

Example

Example 1

// Col(2) will be filled with triangular moving average value at each point, 
//with stating point = 9. 

for(ii=1;ii<=30;ii++) col(1)[ii] = ii;
col(2)=tmovavg(col(1),9);

Example 2

newbook;
col(A) = {4,8,6,-1,NAN,-3,-1, 3, 4, 5};
col(B) = tmovavg(col(A),3,0); //returns [-- -- 6.5 4.75 0.75 -2 -2.5 -0.5 2.25 4]
col(C) = tmovavg(col(A),3,1); //returns [-- -- 6.5 4.75 -- -- -- -0.5 2.25 4]
col(D) = tmovavg(col(A),3,2); //returns [-- -- 6.5 4.75 -- -3 -2.5 -0.5 2.25 4]