Overview
Purpose
Compute the covariance between two variables, with optional support for weights.
Parameters
x: first variable.y: second variable.w: optional weights compatible withxandy.
Returns
A float with the covariance between x and y.
Errors and exceptions
ValueErrorifxandydo not have the same length.ValueErrorif the inputs contain non-finite values.ValueErrorifwis incompatible or not positive.
Example
from mespy import covariance
x = [1.0, 2.0, 3.0]
y = [2.0, 4.0, 6.0]
print(covariance(x, y))
Notes
Covariance is defined as E[(x - E[x])(y - E[y])], normalized by n or by sum(w). In the weighted case, the means are weighted with the same vector w. The computation centers the data before multiplying them, so it stays accurate even with large offsets.