Weighted median computation
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
The weighted median is a statistical measure that generalizes the concept of the median for datasets where individual observations have varying degrees of importance or reliability, denoted as weights. This computation is widely used in fields such as economics, computer science, and statistics, particularly in robust data analysis and image processing.
Understanding the Weighted Median
The median of a dataset is the value that separates the higher half from the lower half. In a similar way, the weighted median takes into account both the values of the data points and their associated weights. The intuition behind the weighted median is to identify a point that splits the dataset into two parts of equal weighted mass.
Definition
Given a dataset with elements and corresponding non-negative weights , the weighted median is a value from the dataset such that:
and
where is the total weight.
This condition ensures that the weighted sum of elements less than the median and the weighted sum of elements greater than the median is each no greater than half the total weight.
Example Calculation
Consider a simple example with the dataset and respective weights . We need to compute the weighted median:
- Sort the data by the values: with sorted weights .
- Compute the cumulative weights: • 2: 4 • 3: 6 • 5: 9 • 8: 10 • 10: 15
- Total weight .
- Weighted median condition: cumulative weight just exceeds at the value 5.
Thus, the weighted median in this example is 5.
Properties and Applications
• Robustness: The weighted median is robust to outliers, as extreme values with low weights have limited influence on the result. • Non-uniqueness: When cumulative weight passes half at multiple points, any of these can qualify as a weighted median. • Applicability: Weighted median is suitable for datasets with reliability differences among observations, commonly seen in sensor fusion, computational imaging, and survey data analysis.
Algorithm for Efficient Computation
The weighted median can be computed efficiently using an algorithm similar to the QuickSelect algorithm for the median. Key steps include:
- Sort the Dataset: Sort the data entries by their values.
- Cumulative Sum of Weights: Compute the cumulative sum of weights as you iterate through values.
- Locate Weighted Median: Find the smallest index where the cumulative weight equals or surpasses .
A well-informed choice of pivot in each iteration can ensure a time complexity of on average for finding the weighted median, similar to the time complexity for finding the standard median.
Pseudocode for Weighted Median

