Returns the covariance of two equally long numeric arrays: how much their items move together.
A positive result means the two arrays tend to rise and fall together, a negative one means they move opposite ways. Pairs where either side is na are skipped.
By default it divides by the number of pairs; pass biased = false for the sample form with one less.
Syntax
array.covariance(id1: array<int/float>, id2: array<int/float>, biased: series bool (optional)) -> series float
array.covariance(id2: array<int/float>, biased: series bool (optional)) -> series floatArguments
| Name | Type | Default | Description |
|---|---|---|---|
| id1 | array<int/ | required | The array that is read, or that receives the result. |
| id2 | array<int/ | required | The second numeric array. It must hold as many items as the first one. |
| biased | series bool | true | true divides by the count (population); false divides by the count minus one (sample). |
Returns
series float: The covariance of the paired items.
Remarks
- The arrays must have the same size, or the script stops with an error that quotes both sizes.
Example
//@version=6
indicator("array.covariance: price and volume", overlay = false)
var px = array.new_float()
var vx = array.new_float()
array.push(px, close)
array.push(vx, volume)
if array.size(px) > 20
array.shift(px)
array.shift(vx)
plot(array.covariance(px, vx), "Covariance")See also
Runs on Sidechair