Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions maths/average_weighted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from __future__ import annotations


def weighted_mean(values: list[float], weights: list[float]) -> float:
"""
Return the weighted mean of a list of values given their weights.
Wiki: https://en.wikipedia.org/wiki/Weighted_arithmetic_mean

>>> weighted_mean([10, 20, 30], [1, 2, 3])
23.333333333333332
>>> weighted_mean([5, 5, 5], [1, 1, 1])
5.0
>>> weighted_mean([], [])
Traceback (most recent call last):
...
ValueError: values and weights cannot be empty
>>> weighted_mean([1, 2], [1])
Traceback (most recent call last):
...
ValueError: values and weights must have the same length
>>> weighted_mean([1, 2], [0, 0])
Traceback (most recent call last):
...
ValueError: sum of weights cannot be zero
"""
if len(values) == 0:
raise ValueError("values and weights cannot be empty")
if len(values) != len(weights):
raise ValueError("values and weights must have the same length")
if sum(weights) == 0:
raise ValueError("sum of weights cannot be zero")
return sum(x * w for x, w in zip(values, weights)) / sum(weights)


if __name__ == "__main__":
import doctest

doctest.testmod()
44 changes: 44 additions & 0 deletions maths/weighted_average.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from __future__ import annotations


def weighted_average(values: list[float], weights: list[float]) -> float:
"""
Return the weighted average of a list of values given their corresponding weights.

https://en.wikipedia.org/wiki/Weighted_arithmetic_mean

>>> weighted_average([1, 2, 3], [1, 1, 1])
2.0
>>> weighted_average([10, 20, 30], [1, 2, 3])
23.333333333333332
>>> weighted_average([5, 15], [1, 3])
12.5
>>> weighted_average([100], [0.5])
100.0
>>> weighted_average([], [])
Traceback (most recent call last):
...
ValueError: Inputs cannot be empty
>>> weighted_average([1, 2], [1])
Traceback (most recent call last):
...
ValueError: Values and weights must have the same length
>>> weighted_average([1, 2, 3], [0, 0, 0])
Traceback (most recent call last):
...
ValueError: Sum of weights cannot be zero
"""
if not values:
raise ValueError("Inputs cannot be empty")
if len(values) != len(weights):
raise ValueError("Values and weights must have the same length")
total_weight = sum(weights)
if total_weight == 0:
raise ValueError("Sum of weights cannot be zero")
return sum(value * weight for value, weight in zip(values, weights)) / total_weight


if __name__ == "__main__":
import doctest

doctest.testmod()