MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / variance

Function variance

maths/joint_probability_distribution.py:41–48  ·  view source on GitHub ↗

>>> from math import isclose >>> isclose(variance([1,2],[0.7,0.3]), 0.21) True

(values: list[int], probabilities: list[float])

Source from the content-addressed store, hash-verified

39
40# Function to calculate the variance
41def variance(values: list[int], probabilities: list[float]) -> float:
42 """
43 >>> from math import isclose
44 >>> isclose(variance([1,2],[0.7,0.3]), 0.21)
45 True
46 """
47 mean = expectation(values, probabilities)
48 return sum((x - mean) ** 2 * p for x, p in zip(values, probabilities))
49
50
51# Function to calculate the covariance

Calls 1

expectationFunction · 0.85

Tested by

no test coverage detected