Trusted answers to developer questions

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

We use the ** covariance()** method in

**Covariance** is a measure of the directional relationship between two random variables in statistics. The covariance between two random variables can have the following values:

**Positive**: A positive covariance value suggests that both random variables move in the*same*direction. If one random variable increases, the other random variable also increases. Similarly, if one random variable decreases, the other also decreases.**Negative**: A negative covariance value suggests that both random variables move in the*opposite*direction. If one random variable increases, then the other random variable decreases. Similarly, if one random variable decreases, the other increases.**Zero**: When the two random variables are independent of each other, the covariance between them is zero.

When we use the `covariance()`

method, the length of both the inputs has to be the same.

This method was introduced in Python version 3.10.

```
covariance(x, y, /)
```

: It is the first input.`x`

: It is the second input.`y`

The method returns the covariance value.

import statistics x = [2, 3, 4, 2] y = [3, 5, 9, 0] print("Covariance - ", statistics.covariance(x, y))

Positive covariance of two variables

**Line 1**: We import themodule.`statistics`

**Line 3**: We define the first input`x`

.**Line 4**: We define the second input`y`

.**Line 6**: We calculate the covariance of the inputs using the`covariance()`

method.

Here, the covariance value is positive. This indicates that both the random variables/inputs move in the same direction.

import statistics x = [2, 3, 4, 2] y = [1, -1, -2, 0] print("Covariance - ", statistics.covariance(x, y))

Negative covariance of two variables

**Line 1**: We import the`statistics`

module.**Line 3**: We define the first input`x`

.**Line 4**: We define the second input`y`

.**Line 6**: We calculate the covariance of the inputs using the`covariance()`

method.

Here, the covariance value is negative. This indicates that both the random variables/inputs move in the opposite direction.

RELATED TAGS

python

communitycreator

covariance

statistics

CONTRIBUTOR

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring

Related Courses