TSRunningMed function

The TSRunningMed function computes the median of a time series over a running window. This function is useful only when used within the Apply function.

Syntax

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

TSRunningMed(value      real, 
              num_values integer) 
returns double precision;
value
The first input value to use to calculate the running median. Typically, the name of a DOUBLE, FLOAT, or REAL column in your time series.
num_values
The number of values to include in the running median, k.

Description

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 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.

Returns

A DOUBLE PRECISION running median of the last k values.

Example

This statement produces a time series from the running median over a 10-element window of the column high of stock_data. You can refer to the columns of a time series as $colname or $colnumber: for example, $high, or $1.
select stock_name, Apply('TSRunningMed($high, 10)', 
                stock_data::TimeSeries(stock_bar))::
      TimeSeries(one_real)
from daily_stocks;