List.Covariance
ListReturns the sample covariance of two lists of numbers.
Syntax
List.Covariance(numberList1 as list, numberList2 as list) as nullable numberParameters
| Name | Type | Required | Description |
|---|---|---|---|
numberList1 | list | Yes | The first list of numbers. |
numberList2 | list | Yes | The second list of numbers. Must have the same length as numberList1. |
Return Value
nullable number — The sample covariance between the two lists, or null if the lists are empty.
Remarks
List.Covariance computes the sample covariance between two numeric lists. Covariance measures how much two variables change together: a positive value means they tend to increase together, while a negative value means one tends to increase as the other decreases. A covariance near zero suggests little linear relationship.
The formula is the sample covariance: the sum of (x - mean_x) * (y - mean_y) divided by n - 1 (Bessel's correction), where n is the number of paired observations. This is appropriate when the lists represent a sample from a larger population.
Both lists must have the same number of elements; mismatched lengths will cause an error. Returns null if either list is empty. Null values in the lists cause errors — clean the data with List.RemoveNulls first if needed. To normalize covariance into a more interpretable [-1, 1] scale, divide by the product of the two standard deviations to get the Pearson correlation coefficient.
Examples
Example 1: Positive covariance — two variables move together
List.Covariance({2, 4, 6, 8}, {1, 3, 5, 7})Result | |
|---|---|
| 1 | 5.33 |
Example 2: Negative covariance — price increases as volume decreases
let
Prices = {10, 20, 30, 40, 50},
Volumes = {500, 420, 350, 260, 180},
Cov = List.Covariance(Prices, Volumes)
in
#table({"Covariance"}, {{Cov}})The final output — presents the covariance value in a single-row table labeled Covariance.
Covariance | |
|---|---|
| 1 | -2,050 |