The Mathematical Trade-Off Between the Mean and the Median in Loss Function Optimization
The choice between using a mean or a median to summarize data is fundamentally a choice of which mathematical penalty to apply to errors. The mean minimizes squared errors (L2 loss), while the median minimizes absolute errors (L1 loss).
By Harper Lane
- Classical Optimization Theorists
- Favor L2 loss and the mean for its mathematical smoothness, unique solutions, and computational efficiency in gradient descent.
- Robust Statistics Advocates
- Favor L1 loss and the median for real-world, noisy data because it prevents outliers from distorting the underlying trend.
Perspectives this story doesn't cover
- Machine Learning Practitioners prioritizing Huber Loss
- L1
- Mean Absolute Error (MAE) penalty
- L2
- Mean Squared Error (MSE) penalty
- 0
- Derivative required for optimization
- 1
- Constant gradient of L1 loss
When summarizing a dataset, the mode identifies the most frequent value by applying a strict zero-one penalty—it only rewards exact matches and punishes all deviations equally, regardless of distance. The mean and the median, by contrast, both seek the center of a distribution by measuring the distance of every data point from a proposed estimate. The single respect in which they differ is how they penalize that distance.[3]
This distinction is not merely a descriptive quirk; it is a fundamental property of mathematical optimization. In statistical modeling and machine learning, an estimator is chosen to minimize a specific loss function—a mathematical formula that quantifies the cost of being wrong. The choice of loss function dictates the shape of the resulting model.[4]
The L2 loss function, commonly known as Mean Squared Error (MSE), squares the distance between the estimate and each data point. Because the penalty grows quadratically, an error of 4 units is punished 16 times as severely as an error of 1 unit. To find the optimal estimate that minimizes this total squared penalty, one takes the derivative of the loss function and sets it to zero.[1][3]
The calculus reveals a clean, elegant result: the derivative of the squared distance yields a linear equation where the optimal value is simply the sum of all data points divided by the number of points. This is the exact definition of the arithmetic mean. Therefore, the mean is the unique mathematical minimizer for L2 loss.[4]
Therefore, the mean is the unique mathematical minimizer for L2 loss.
The L1 loss function, or Mean Absolute Error (MAE), takes a radically different approach. It measures the absolute distance between the estimate and the data points without squaring it. Under L1, an error of 4 units is punished exactly 4 times as severely as an error of 1 unit. The penalty scales linearly rather than exponentially.[2]
Optimizing the L1 loss function requires finding the point where the sum of absolute deviations is minimized. The derivative of an absolute value is a step function: it returns +1 for points above the estimate and -1 for points below it. This creates a constant gradient that ignores the magnitude of the error and only registers its direction.[1]
To set this derivative to zero and find the minimum, the number of +1s must exactly balance the number of -1s. In other words, the optimal estimate must have exactly as many data points above it as it has below it. This is the precise definition of the median. The median is the unique minimizer for L1 loss.[3][4]
This mathematical reality dictates how algorithms behave in the presence of outliers. Because the mean minimizes squared errors, a single massive outlier will exert a gravitational pull on the estimate, dragging the mean toward it to avoid the catastrophic quadratic penalty. The median, minimizing absolute errors, only registers the outlier as a single +1 in its balancing equation, entirely ignoring its magnitude.[2]
Different angles
L2 Loss (Mean Squared Error)
Optimizes for the arithmetic mean by applying a quadratic penalty to errors.
For: Mathematically smooth and continuously differentiable, making it highly efficient for gradient descent algorithms in machine learning. It guarantees a unique solution and heavily penalizes large errors, ensuring no single prediction is wildly off. Against: Highly sensitive to outliers. A single anomalous data point (e.g., a $10 million house in a $300,000 neighborhood) will severely skew the model. Evidence: The derivative of x-squared is 2x, meaning the gradient scales proportionally with the error magnitude. Fits well when: The data is normally distributed, outliers are genuine errors that must be corrected, or the business cost of a large error is exponentially worse than a small one. Does not fit when: The dataset contains extreme, unrepresentative anomalies that should be ignored.
L1 Loss (Mean Absolute Error)
Optimizes for the median by applying a linear, constant penalty to errors.
For: Highly robust to outliers. Because the penalty scales linearly, a massive anomaly exerts no more pull on the median than a minor deviation on the same side of the center. Against: The absolute value function is not differentiable at zero, complicating optimization for some machine learning algorithms. It can also yield multiple optimal solutions if the dataset has an even number of points with a gap in the middle. Evidence: The derivative of an absolute value is a constant +1 or -1, meaning the optimization only counts the direction of the error, not its magnitude. Fits well when: The dataset is heavily skewed (e.g., income distributions, real estate prices) and the goal is to find the typical experience rather than the mathematical center of mass. Does not fit when: Small errors and large errors carry fundamentally different business costs, or when computational efficiency in complex neural networks is paramount.
Sources
[1]Stats StackExchangeRobust Statistics AdvocatesL1 regression estimates median whereas L2 regression estimates mean?
Read on Stats StackExchange →
[2]PracHubClassical Optimization TheoristsExplain median vs mean for L1/L2
Read on PracHub →
[3]SAS BlogsClassical Optimization TheoristsMeans and medians as minimizers of a loss function
Read on SAS Blogs →
[4]Factlen Editorial TeamClassical Optimization TheoristsSynthesis by Factlen editorial team
Read on Factlen Editorial Team →
Comments
More in Data & Analysis
See all →Clinical Trial Design
How Intention-to-Treat Analysis Preserves Randomization and Prevents Selection Bias
9 sources
Statistical Modeling
How the Expectation and Maximization Steps Iteratively Converge to Maximum Likelihood Estimates for Latent Variables
4 sources
Time-Series Analysis
How the Damping Parameter in Exponential Smoothing Balances Responsiveness to New Data and Stability
6 sources
Economic Sentiment
The Evidence Pack: Why 50% of Americans Report Struggling With Basics Despite a Growing Economy
5 sources
Every angle. Every day.
Get Data & Analysis stories with full source coverage and perspective breakdowns delivered to your inbox.




