Syntax Supported by the IML Procedure and the iml Action

MOVEWIN Function

MOVEWIN (y, param, <, statistic> <, windowPos> <, opt> ) ;

This function is supported by the IML procedure and the iml action.

The MOVEWIN function enables you to compute moving averages and other moving-window statistics for values of the column vector, y. You specify a moving statistic by using the statistic argument. You specify the width and orientation of the moving window by using the param and windowPos arguments, respectively. You specify how to handle missing values and incomplete time windows by using the opt argument.

For an n-element column vector, y, the function returns an n-element column vector of statistics.

The arguments to the MOVEWIN function are as follows:

y

is an n-element numerical column vector. The values of y define a time series, which means that they correspond to a sequence of equally spaced and strictly increasing times.

param

specifies the observation weights, the width of the moving window, or both, as follows:

  • For an exponentially weighted moving average statistic (statistic="EWMA"), param is a scalar value in the interval . The value specifies the alpha parameter.

  • For statistics other than EWMA, if param is a scalar value, it must be a positive integer. The value specifies the width of the window.

  • If param is a vector of J nonnegative values, then J determines the width of the window, and the kth element of param is the weight that is applied to the kth element of y in the moving window. You do not need to standardize the weights; they are internally standardized so that the weights sum to 1. Weights are supported only for the mean and standard deviation statistics.

statistic

is an optional string that specifies the moving statistic. The default value is "MEAN". You can also specify "EWMA", "MAD", "MAX", "MEDIAN", "MIN", or "STD". The EWMA statistic does not use a moving window. The other statistics that you can specify use moving windows. See the section Details of Moving Statistics for more information.

windowPos

is an optional character that specifies the relative position of the moving window. This argument applies only to the statistics that use moving windows. Let J be the window width. You can specify the following values for windowPos:

"B"

Uses a backward (or lagging) moving window. The ith moving window contains the observations . This is the default.

"C"

Uses a centered moving window. The ith moving window contains the observations , where and J is an odd integer.

"F"

Uses a forward (or leading) moving window. The ith moving window contains the observations .

opt

is an optional vector that contains two elements. This argument applies only to the statistics that use moving windows. A default value is used if an element of opt is omitted or is a missing value. You can specify the elements of opt as follows:

opt[1]

indicates how to compute the moving-window statistic near the ends of the vector y when a moving window does not contain J observations.

  • For a backward window of width J, the window for observation does not contain J observations when . The opt[1] element specifies how to handle that situation. If opt[1]=1 (the default), then the window is truncated and the observations are used to compute the statistic. If opt[1]=0, then the statistic is missing.

  • For a centered window, the opt[1] element applies when or , where . If opt[1]=1, then the observations are used to compute the statistic when , and the observations are used when . If opt[1]=0, then the statistic is missing.

  • For a forward window, the window for observation does not contain J observations when . If opt[1]=1, then the observations are used to compute the statistic. Otherwise, the statistic is missing.

opt[2]

specifies how to compute the moving-window statistic when an element of y within a moving window is missing. Specify opt[2]=1 to use only the nonmissing observations; this is the default. Specify opt[2]=0 to return a missing value whenever the window contains a missing value.

The following examples show how to compute unweighted moving means for each type of window:

y = {1, 3, 5, 4, 6, 3, 3, 10, 12};           /* observed data */
bMean3 = movewin(y, 3);                      /* moving mean, backward window */
cMean3 = movewin(y, 3, "MEAN", "C", {0, 1}); /* centered window, specify opt */
fMean3 = movewin(y, 3, "MEAN", "F");         /* forward window, default opt */
print y bMean3 cMean3 fMean3;

Figure 228: Moving Means Using Different Window Types

y bMean3 cMean3 fMean3
1 1 . 3
3 2 3 4
5 3 4 5
4 4 5 4.3333333
6 5 4.3333333 4
3 4.3333333 4 5.3333333
3 4 5.3333333 8.3333333
10 5.3333333 8.3333333 11
12 8.3333333 . 12


Details of Moving Statistics

Exponentially Weighted Moving Average

The EWMA statistic is the only supported statistic that does not use a moving window of fixed width. Instead, it is defined recursively. Let be the ith EWMA statistic. For the first data point, . For , . If or is a missing value, then will be a missing value.

Intuitively, is a weighted average of the observations , where observations that are closer in time to the ith period in the time series receive larger weights. You can compute the explicit weight for each observation by expanding the recursive definition of the EWMA statistic: , for .

Statistics That Use Moving Windows

All the supported statistics other than EWMA use moving windows. Let s be a statistic for a moving window of width J. For simplicity, denote the values of y within the window .

The maximum, median, minimum, and median absolute deviation are unweighted. They are computed as follows:

"MAX"

The statistic s is the largest of the values in the moving window.

"MEDIAN"

The statistic s is the median of the values in the moving window.

"MIN"

The statistic s is the smallest of the values in the moving window.

"MAD"

The statistic s is the median absolute deviation of the values in the moving window. Let m be the median of the values in the moving window. Let , , be the absolute difference between the kth element of the window and m. Then s is the median of those absolute differences: .

The mean and standard deviation can be weighted or unweighted. Let be the weights that are associated with , where for all k if the statistic is unweighted. Let W be the sum of the weights. The statistic s is computed as follows:

"MEAN"

.

"STD"

, where is the weighted mean. Notice that when the standard deviation is unweighted, the denominator for the statistic is . This is different from the usual sample standard deviation, whose denominator is .

Last updated: November 20, 2025