Mean Square Log Error: Definition & Formula
In this article
Mean square log error (MSLE) is a regression metric that measures the average squared difference between the natural logarithms of the predicted and actual values. You enter two lists of numbers (actual and predicted), and the calculator returns the MSLE, which penalizes underestimates more heavily than overestimates. This is especially useful when your target variable spans several orders of magnitude, like house prices, population counts, or sales figures.
in actual | Actual values (comma-separated) | - | 150, 200, 250, 300, 350 | 0 | 1000000 in predicted | Predicted values (comma-separated) | - | 140, 210, 240, 310, 340 | 0 | 1000000 out msle | Mean Square Log Error | - | mean((log(predicted) - log(actual))^2) | 4 out rmse | RMSE (same units as log) | - | sqrt(msle) | 4
Formula
The mean square log error is defined as:
MSLE = (1/n) * Σ (log(pi + 1) - log(ai + 1))2
Where:
- n = number of observations (count of pairs)
- pi = predicted value for the i-th observation
- ai = actual (true) value for the i-th observation
- log = natural logarithm (base e)
The +1 inside the log is a common convention to avoid the undefined log(0) when a value is zero. Some definitions omit the +1, but for practical use with real data that includes zeros, the +1 version is safer. The result is always a non-negative number; a perfect model gives MSLE = 0.
Worked Example
Using the default values from the calculator: actual = [150, 200, 250, 300, 350], predicted = [140, 210, 240, 310, 340].
Step 1: Add 1 to each value and take the natural log.
- log(151) = 5.0173, log(141) = 4.9488
- log(201) = 5.3033, log(211) = 5.3519
- log(251) = 5.5255, log(241) = 5.4848
- log(301) = 5.7071, log(311) = 5.7398
- log(351) = 5.8608, log(341) = 5.8319
Step 2: Compute the squared differences.
- (4.9488 - 5.0173)² = 0.00469
- (5.3519 - 5.3033)² = 0.00236
- (5.4848 - 5.5255)² = 0.00166
- (5.7398 - 5.7071)² = 0.00107
- (5.8319 - 5.8608)² = 0.00084
Step 3: Average the squared differences.
(0.00469 + 0.00236 + 0.00166 + 0.00107 + 0.00084) / 5 = 0.01062 / 5 = 0.0021
The calculator rounds to 4 decimal places, giving MSLE = 0.0021. The RMSE (square root of MSLE) is sqrt(0.0021) = 0.0459, which represents the typical log-error in natural log units.
When to Use MSLE Instead of MSE or MAE
MSLE is the right choice when your target variable is strictly positive and spans several orders of magnitude. For example, predicting house prices (ranging from $50k to $5M), population sizes, or insurance claim amounts. In such cases, a model that predicts $500k for a $1M house might be acceptable, but predicting $50k would be terrible. MSLE treats both errors symmetrically in log space, so an underestimate and an overestimate of the same relative size are penalized equally.
In contrast, mean squared error (MSE) penalizes large absolute errors heavily, which can be dominated by a few high-value outliers. Mean absolute error (MAE) is more robust but still not scale-invariant. MSLE is effectively the MSE computed on log-transformed targets, which makes it equivalent to assuming that the target follows a log-normal distribution.
Use MSLE when:
- The target is always positive (e.g., prices, counts, durations).
- The target spans multiple orders of magnitude (e.g., 10 to 100,000).
- Relative errors matter more than absolute errors.
- You want to penalize underestimates more than overestimates—actually, MSLE penalizes both equally in relative terms, but because the log compresses large values, an underestimate of a large value is penalized less than an overestimate of a small value.
How to Interpret MSLE Values
MSLE is not directly interpretable in the original units of your target. A value of 0.01 means that, on average, the squared difference between log(predicted+1) and log(actual+1) is 0.01. Taking the square root gives the RMSE in log units, which is approximately the typical relative error. For example, an RMSE of 0.05 in log space means the typical prediction is off by about 5% (since e^0.05 ≈ 1.051).
To convert MSLE to a percentage error, you can compute: percentage error ≈ (e^sqrt(MSLE) - 1) × 100%. For MSLE = 0.0021, sqrt = 0.0458, e^0.0458 = 1.0469, so about 4.7% typical error. This is a rough rule of thumb; the actual distribution may vary.
There is no universal “good” MSLE threshold—it depends on your data’s scale and variability. Compare MSLE across models on the same dataset; the lower, the better. A model with MSLE = 0 is perfect, but that’s almost never achievable with real data.
Common Mistakes and Pitfalls
One frequent mistake is forgetting to add 1 to both actual and predicted values when zeros are present. If a value is zero, log(0) is undefined (negative infinity), which breaks the calculation. The +1 convention avoids this, but be consistent: if you use +1, apply it to both actual and predicted.
Another mistake is using MSLE on data that contains negative values. Since the log of a negative number is undefined, MSLE is only valid for non-negative targets. If your target can be negative (e.g., profit margins), use MSE or a different metric.
Rounding errors can also creep in when you compute logs manually. Always use sufficient precision (e.g., 6 decimal places) in intermediate steps. The calculator here rounds the final result to 4 decimals, but the internal computation uses full precision.
Finally, do not compare MSLE values across different datasets with different scales. A dataset with values in the thousands will naturally have a different MSLE than one with values in the millions. Always evaluate MSLE on a validation set that reflects the real-world distribution.
MSLE vs. Other Metrics: A Quick Comparison
Here’s how MSLE stacks up against common alternatives:
- MSE (Mean Squared Error): Penalizes absolute errors; sensitive to outliers; not scale-invariant. Good for targets with bounded range.
- MAE (Mean Absolute Error): Robust to outliers; interpretable in original units; but does not penalize large errors as heavily as MSE.
- RMSLE (Root Mean Squared Logarithmic Error): The square root of MSLE; same properties, but in the same units as log(target). Often used in Kaggle competitions.
- MAPE (Mean Absolute Percentage Error): Interpretable as a percentage; but can be infinite if actual values are zero, and it penalizes overestimates more than underestimates (asymmetric).
In practice, MSLE is a good middle ground when you care about relative error and your data is positive and skewed. It is also less sensitive to outliers than MSE because the log compresses large values.
Limitations and When Not to Use MSLE
MSLE is not a universal solution. Avoid it when:
- Your target contains zeros or negative values (use MSE or a custom metric).
- Your target has a narrow range (e.g., 10 to 20)—then MSE is simpler and more interpretable.
- You need to interpret errors in the original units—MAE or RMSE are better.
- You have extreme outliers in the log space—they can still dominate the squared log error.
Also, MSLE assumes that the log transformation makes the error distribution roughly symmetric. If that assumption is badly violated, you might consider other transformations (e.g., Box-Cox) or robust metrics.
Finally, remember that MSLE is a metric, not a loss function. You can use it to evaluate a model, but if you want to train a model to minimize MSLE, you need to use a compatible loss function (e.g., in neural networks, you can set loss = MSLE). Many libraries support this directly.
Frequently Asked Questions
Q: Can MSLE be negative?
No. Since it’s an average of squared differences, it’s always non-negative. A negative value would indicate a calculation error.
Q: What is the difference between MSLE and RMSLE?
RMSLE is simply the square root of MSLE. RMSLE is in the same units as the log of the target, making it slightly more interpretable. For example, an RMSLE of 0.1 means the typical prediction is off by about 10% (e^0.1 ≈ 1.105).
Q: Why do we add 1 inside the log?
To handle zero values. Without the +1, log(0) is undefined. Adding 1 shifts all values up by 1, so zeros become log(1)=0. This is a standard convention in many libraries, but be consistent—if you use it, apply it to both actual and predicted.
Q: Is MSLE the same as MSE on log-transformed data?
Yes, if you apply the natural log to both actual and predicted (with the +1 shift) and then compute MSE, you get MSLE. That’s exactly what the formula does.
Q: How do I choose between MSLE and MAPE?
MAPE is more interpretable (a percentage), but it can blow up if any actual value is zero. MSLE is more stable with zeros and gives symmetric treatment in log space. If your data has zeros, MSLE is safer.
Q: Can I use MSLE for classification problems?
No. MSLE is for regression problems with continuous targets. For classification, use accuracy, precision, recall, F1, or log loss.