TSRunningVar function

The TSRunningVar function computes the variance of a time series over a running window.

Syntax

TSRunningVar(value      double precision, 
              num_values integer) 
returns double precision;

TSRunningVar(value      real, 
              num_values integer) 
returns double precision;
value
The first input value to use to calculate the running correlation.
num_values
The number of values to include in the running variance, k.

Description

Use the TSRunningVar function within the Apply function.

This function runs over a fixed number of elements, not over a fixed length of time; therefore, it might not be appropriate for irregular time series.

The first (num_values - 1) outputs are exceptions because they result from shorter windows (the first output is derived from the first input time, the second output is derived from the first two input times, and so on). Null elements in the input also result in shortened windows.

The TSRunningVar function can take parameters that are columns of a time series. Use the same parameter format that the Apply function accepts.

Returns

A DOUBLE PRECISION running variance of the last k values.

Example

This statement produces a time series with the same length and calendar as stock_data but with one data column other than the time stamp. Element n of the output is the variance of column 1 of stock_bar elements n-19, n-18, ... n. The first 19 elements of the output are a bit different: the first element is NULL, because variance is undefined for a series of 1. The second output element is the variance of the first two input elements, and so on.

If element i of stock_data is NULL, or if column 1 of element i of stock_data is NULL, output elements i, i + 1, ... i + 19, are variances of just 19 numbers (assuming that there are no other null values in the input window).
select stock_name, Apply('TSRunningVar($0.high, 20)', 
                stock_data::TimeSeries(stock_bar))::
                TimeSeries(one_real)
from daily_stocks;