How to compute mean average robustly?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Computing the mean or average of a set of data points is a fundamental statistical operation. However, calculating a simple arithmetic mean can sometimes be misleading, especially in the presence of outliers or skewed data. To obtain a more robust estimate of the mean, various methods can be employed. This article explores several techniques and considerations for computing a robust mean.
Traditional Arithmetic Mean
The traditional arithmetic mean is given by:
where represents each data point and is the total number of data points. While this formula is straightforward and works well with symmetric distributions, it is sensitive to outliers.
Limitations of Arithmetic Mean
The arithmetic mean can be greatly impacted by extreme values. For example, consider a dataset representing the incomes of a neighborhood: `$30,000, 32,000, \31,000, 35,000, and \1,500,000. The mean would be $`325,600, which does not accurately represent the earnings of the majority.
Techniques for Robust Mean Estimation
To mitigate the influence of outliers, several alternative techniques can be employed:
1. Trimmed Mean
The trimmed mean involves removing a specified percentage of the smallest and largest values before calculating the mean. Typically, 5% or 10% of the data points from both ends are excluded.
For a dataset of 20 values, if a 10% trim is applied, the smallest 2 and largest 2 values are removed. Thus, the mean is calculated using the remaining 16 values.
Advantages:
• Reduces the influence of outliers. • Easy to understand and compute.
Disadvantages:
• Requires a subjective decision about what percentage to trim.
• Loss
of data points.
2. Winsorized Mean
Similar to the trimmed mean, the Winsorized mean replaces outliers with the nearest remaining values instead of discarding them.
For a 10% Winsorized mean: • The smallest 2 values are replaced with the 3rd smallest value. • The largest 2 values are replaced with the 3rd largest value.
Advantages:
• Retains all data points. • Reduces effects of extreme values more gently than trimming.
Disadvantages:
• Still requires choosing a trimming percentage. • More complex to calculate than a simple mean.
3. Median
The median, the middle value of a dataset, is extremely robust to outliers. For an odd number of data points, it is the central value; for an even number, it is the average of the two central values.
Advantages:
• Completely robust to outliers. • Simple to understand and compute.
Disadvantages:
• Does not take full data variability into account. • Less informative than mean in symmetric distributions.
4. Weighted Mean
In a weighted mean, each data point is assigned a weight, reflecting its importance or reliability.
where represents the weight of each data point.
Advantages:
• Allows customization of data points' influence. • Useful for aggregated data from different sources.
Disadvantages:
• Selecting appropriate weights can be subjective. • Not specifically designed to reduce outlier impact.
5. Huber Mean
The Huber mean is a compromise between the mean and median, controlled by a parameter . Values within of the mean are averaged normally. Values outside are weighted less.
Advantages:
• Adjusts smoothly between mean and median-like behavior. • Provides a continuum of robustness depending on .
Disadvantages:
• Requires parameter tuning. • Computationally more intensive.
Summary of Robust Mean Techniques
| Method | Robustness to Outliers | Data Loss | Complexity | Subjectivity |
| Trimmed Mean | Moderate | Yes | Low | Yes |
| Winsorized Mean | Moderate | No | Moderate | Yes |
| Median | High | No | Low | None |
| Weighted Mean | Low to Moderate (depends on weights) | No | Moderate | Yes | |
| Huber Mean | High | No | High | Yes |
Conclusion
Choosing the right technique to compute a robust mean depends on the nature of the data and the analysis goals. Each method comes with trade-offs in terms of simplicity, robustness, and data retention. By understanding these methods, analysts can select the most appropriate one, ensuring a more reliable representation of central tendency even in the presence of outliers or skewed data distributions.
Related reading
- How to concatenate two tensors horizontally in TensorFlow?
- How to convert a dataframe to a dictionary
- How to convert a NumPy array to PIL image applying matplotlib colormap
- How to convert an array of strings to an array of floats in numpy?
- How to compute shortest unique prefixes of a set of strings?
- How to compute the intersection points of a line and an arbitrary shape?
- How to convert index of a pandas dataframe into a column
- How to convert index of a pandas dataframe into a column

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.