>>> from math import isclose >>> isclose(expectation([1, 2], [0.7, 0.3]), 1.3) True
(values: list, probabilities: list)
| 29 | |
| 30 | # Function to calculate the expectation (mean) |
| 31 | def 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 |
no outgoing calls
no test coverage detected