covariance — aggregate function to return the covariance of two ranges of of random values.


covariance (double field-x, double field-y)


Covariance is a measure of how much two variables change together. This function takes the name of two fields of type double in the aggregate window, or two expressions that resolve to the names of two such fields, and returns a value of type double, whose value and sign represents the covariance of the two ranges.

Covariance can be used to measure the degree to which returns on two risky assets move in tandem, as a indicator to help balance risk in a portfolio of owned assets. A positive covariance means that asset returns move together. A negative covariance means returns vary inversely.

For example, if you owned one asset that had a high covariance with another asset that you did not own, then you would receive very little increased diversification by adding the second asset. The opposite is also true, that adding an asset with low covariance to a portfolio would lower the overall portfolio risk.

If the query returns no rows to the aggregate's window, the function returns null. If the query returns a row count of 1 or −1, the result is also null.

This function calculates its return value based on the unbiased or n-1 method, and is usually used when the incoming data represents a random sample of data. Use covariancep for a similar function that calculates its return value using the biased or n method, which is used when the incoming data represents an entire population of data.


This LiveView aggregate function is based on the covariance aggregate function in the StreamBase expression language.