On this page Purpose

API Reference / mespy.covariance

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

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.