Overview

Purpose

Compute the covariance between two variables, with optional support for weights.

Parameters

  • x: first variable.

  • y: second variable.

  • w: optional weights compatible with x and y.

Returns

A float with the covariance between x and y.

Errors and exceptions

  • ValueError if x and y do not have the same length.

  • ValueError if the inputs contain non-finite values.

  • ValueError if w is 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

The implemented formula is E[xy] - E[x]E[y]. In the weighted case, the means are weighted with the same w vector.