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

Function expectation

maths/joint_probability_distribution.py:31–37  ·  view source on GitHub ↗

>>> from math import isclose >>> isclose(expectation([1, 2], [0.7, 0.3]), 1.3) True

(values: list, probabilities: list)

Source from the content-addressed store, hash-verified

29
30# Function to calculate the expectation (mean)
31def expectation(values: list, probabilities: list) -> float:
32 """
33 >>> from math import isclose
34 >>> isclose(expectation([1, 2], [0.7, 0.3]), 1.3)
35 True
36 """
37 return sum(x * p for x, p in zip(values, probabilities))
38
39
40# Function to calculate the variance

Callers 3

varianceFunction · 0.85
covarianceFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected