# 3.5.2.25 Movavg

## Description

This function returns the moving average of adjacent ranges [i-nb, i+nf], for a point i. The part of range beyond the input vector is dropped.

The adjacent average at point i with iback backward offset and iforw forward offset is:

$adjavg =\frac{\sum_{i-nb}^{i+nf} x_i}{nb+nf+1}$

## Syntax

vector movavg(vector vx, int back, int forward[,int missing])

## Parameters

vx

The data vector used to calculate adjacent average.

back

is the offset backward with respect to current row number.

forward

is the offset forward with respect to current row number.

missing

Optional. Determine how to deal with the missing value in the adjacent 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 3 to see the difference.

## Example

Example 1

// Col(2) will be filled with adjacent value at each point. Note that
// col(2)[9] = (col(1)[9]+col(1)[10])/2 and col(1)[10] = col(2)[10]

for(ii=1;ii<=10;ii++) col(1)[ii] = ii;
col(2)=movavg(col(1),0, 2);

Example 2

// The subrange [10:20] in col(2) will be filled with adjacent average
// of the given subrange [10:20] in col(1).

range aa=col(1)[10:20];
range bb=col(2)[10:20];
bb = movavg(aa, 3, 3);

Example 3

newbook;
col(A) = {4,8,6,-1,NAN,-3,-1, 3, 4, 5};
col(B) = movavg(col(A),3,0,0); //returns [4 6 6 4.25 4.33333 0.66667 -1.66667 -0.33333 0.75 2.75]
col(C) = movavg(col(A),3,0,1); //returns [4 6 6 4.25 -- -- -- -- 0.75 2.75]
col(D) = movavg(col(A),3,0,2); //returns [4 6 6 4.25 -- 0.66667 -1.66667 -0.33333 0.75 2.75]