RATS 10.1
RATS 10.1

Instructions /

MVSTATS Instruction

Home Page

← Previous Next →

MVSTATS( options )   series start end newseries newstart

Computes the mean, variance, maximum, minimum, median, and/or general fractiles for a moving window of data. That is, it generates these statistics for a set of shifting windows of constant width. Note that the capabilities of the old MVFRACTILE instruction have now been added to MVSTATS.

Wizard

You can use the Data/Graphics—Moving Window Statistics wizard to compute these statistics.

Parameters

series

series for which to calculate statistics

start, end

Range of entries to transform. Unless you choose one of the EXTEND options, start and end need to allow for the span of data required by the window. For instance, a centered window of width 11 requires that start and end be at least 5 entries in from the beginning and end of series. If you have not set a SMPL, these default to the maximum usable range of series.

newseries

Resulting series, used only when using the FRACTILE option to compute a single fractile. Use the RESULTS option when computing more than one fractile.

newstart

start for newseries, by default start

Options

WIDTH=Number of entries in each statistics window [5]

SPAN=(same as WIDTH: an older name for the same option) [5]

CENTERED/[NOCENTERED]

The WIDTH is the number of entries considered at any one time. If you choose a CENTERED window (the default is current and lagging), this should be odd. If it’s even, MVSTATS will make it odd by adding one. If you choose CENTERED, the results for an entry use a window centered at that entry. If not (the default), the window consists of the entry and preceding ones.

 

You can use as many of these output options as you would like.

 

MEANS=(output) series for means

VARIANCES=(output) series for variances

MAXIMUMS=(output) series for maximum values

MINIMUMS=(output) series for minimum values

MEDIANS=(output) series for medians

IQRANGE=(output) series of interquartile ranges (75%-25%)

Use these options to save the moving means, variances, maximum, minimum, median and interquartile range into data series. You can use any combination of these. VARIANCES are calculated using a divisor of N–1.

 

FRACTILE=other fractile, vector of fractiles, or ||list of other fractiles|| (between 0 and 1)

RESULTS=VECTOR[SERIES] of fractiles

Use FRACTILE to compute one more sets of moving fractiles (quantiles). If you just want a single fractile, use the newseries parameter to save the result. If you want to compute multiple fractiles, supply a list of the fractiles in the form FRACTILE=||f1,f2,...,fn|| and use the RESULTS option to save the results into an n–vector of series.

 

EXTEND=ZEROS/REPEAT/SHORTEN

By default, MVSTATS won’t compute an output value for observations where the window goes outside the input range. These options allow the output series to cover the same range as the input series. EXTEND=ZEROS extends the input series out-of-sample with zeros. EXTEND=REPEAT extends by repeating the first value (at the lower end) and the final value (at the upper end). EXTEND=RESCALE (EXTEND=SHORTEN means the same) uses a window truncated to the actual data–for instance, an uncentered window at the left endpoint will use just that one data point.

Algorithm for Computing Fractiles

To compute fractile f, the data in the window are sorted. (This is done by sequential insertions and deletions). If there are N points in the window, the quantity \(\left( {N - 1} \right)f + 1\) gives the position in the ranking for the desired fractile. If this isn’t an integer, a weighted sum of the values on either side is used. For instance, if \(\left( {N - 1} \right)f + 1\) is 3.75, the result is \(.25{X_{(3)}} + .75{X_{(4)}}\).

Missing Values/Global SMPL

Any missing values and entries skipped because of a global SMPL are dropped from the calculation of the statistics. Each will, however, have an output value, computed using the surrounding data.

Examples

This takes moving 36 period means of the series TRES (that is the mean of TRES for the current and the previous 35 entries) and divides TRES and NBREC by the lagged value of this to produce the series TR1 and NBREC1.

 

mvstats(means=trma,span=36) tres 1961:12 1996:12

set TR1    1962:1 1996:12 = tres/trma{1}

set NBREC1 1962:1 1996:12 = nbrec/trma{1}


 

This computes a moving 21 period maximum and minimum of IBMLOG into MAXS and MINS.

 

mvstats(min=mins,max=maxs,span=21) ibmlog

 


 

This does a centered maximum and minimum of the series X. This will lose SPAN data points at each end.

 

mvstats(max=max,min=min,centered,width=2*span+1) x startl+span endl-span


 


Copyright © 2025 Thomas A. Doan