TSRunningCor function

The TSRunningCor function computes the running correlation of two time series over a running window. The TSRunningCor function returns NULL if the variance of either input is zero or NULL over the window.

Syntax

TSRunningCor(value1     double precision, 
             value2     double precision,
              num_values integer) 
returns double precision;

TSRunningCor(value1     real, 
             value2     real,
              num_values integer) 
returns double precision;
value1
The column of the first time series to use to calculate the running correlation.
value2
The column of the second time series to use to calculate the running correlation.
num_values
The number of values to include in the running correlation, k.

Description

Use the TSRunningCor function within the Apply function.

The TSRunningCor 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 set of (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.

The TSRunningCor 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 correlation of the last k values.

Example

This statement finds the running correlation between stock data for HCLTECH and AA1 over a 20 element window. Again, the first 19 output elements are exceptions because they result from windows of fewer than 20 elements. The first is NULL because correlation is undefined for just one element.
select Apply('TSRunningCor($0.high, $1.high, 20)', 
                ds1.stock_data::TimeSeries(stock_bar), 
                ds1.stock_data::TimeSeries(stock_bar))::TimeSeries(one_real)
from daily_stocks ds1, daily_stocks ds2 
where ds1.stock_name = 'HCLTECH' 
and ds2.stock_name = 'AA1';
Tip: When a start date is supplied to the Apply function, the first (num_values - 1) output elements are still formed from incomplete windows. The Apply function never looks at data before the specified start date.