Enter your raw numerical data here. Non-numeric characters will be ignored.
Percentage of data to remove from BOTH the top and bottom. E.g., 10% removes the lowest 10% and highest 10%.
Total Data Points (n):–
Points Removed (Total):–
Standard Mean (Average):–
Trimmed Mean Result:–
function calculateTrimmedMean() {
// Reset UI
var errorDiv = document.getElementById('errorMessage');
var resultsDiv = document.getElementById('resultsArea');
errorDiv.style.display = 'none';
resultsDiv.style.display = 'none';
// 1. Get and Parse Data Input
var rawData = document.getElementById('datasetInput').value;
var trimPercentInput = document.getElementById('trimPercentage').value;
if (!rawData.trim()) {
errorDiv.innerHTML = "Please enter a dataset.";
errorDiv.style.display = 'block';
return;
}
// Split by comma, space, newline, or tab
var items = rawData.split(/[ , \n\t]+/);
var numbers = [];
for (var i = 0; i < items.length; i++) {
var val = parseFloat(items[i]);
if (!isNaN(val)) {
numbers.push(val);
}
}
if (numbers.length === 0) {
errorDiv.innerHTML = "No valid numbers found in the dataset.";
errorDiv.style.display = 'block';
return;
}
if (numbers.length < 3) {
errorDiv.innerHTML = "Dataset must contain at least 3 numbers to perform a meaningful trim.";
errorDiv.style.display = 'block';
return;
}
// 2. Validate Percentage
var percent = parseFloat(trimPercentInput);
if (isNaN(percent) || percent = 50) {
errorDiv.innerHTML = "Please enter a valid percentage between 0 and 49.9.";
errorDiv.style.display = 'block';
return;
}
// 3. Sort Array (Numeric Sort)
numbers.sort(function(a, b) {
return a – b;
});
// 4. Calculate Trim Logic
var n = numbers.length;
// Number of items to remove from ONE end
var k = Math.floor(n * (percent / 100));
// Total removed items (top + bottom)
var totalRemoved = k * 2;
// Ensure we don't remove everything (though p = n) {
errorDiv.innerHTML = "Calculation Error: Trim percentage is too high for the dataset size, resulting in 0 remaining items.";
errorDiv.style.display = 'block';
return;
}
// Slice the array
// slice(start, end) -> start is inclusive, end is exclusive.
// We want to skip the first k items, and stop before the last k items.
// Last k items start at index n – k.
var trimmedData = numbers.slice(k, n – k);
// 5. Calculate Means
var sumStandard = 0;
for (var j = 0; j < n; j++) {
sumStandard += numbers[j];
}
var standardMean = sumStandard / n;
var sumTrimmed = 0;
for (var m = 0; m < trimmedData.length; m++) {
sumTrimmed += trimmedData[m];
}
var trimmedMean = sumTrimmed / trimmedData.length;
// 6. Output Results
document.getElementById('resTotalCount').innerText = n;
document.getElementById('resRemovedCount').innerText = totalRemoved + " (" + k + " from each end)";
document.getElementById('resStandardMean').innerText = standardMean.toFixed(4);
document.getElementById('resTrimmedMean').innerText = trimmedMean.toFixed(4);
resultsDiv.style.display = 'block';
}
Understanding the Trimmed Mean
The trimmed mean is a statistical measure of central tendency, similar to the mean and median. It is designed to be more robust against outliers than the standard arithmetic mean. By removing a small designated percentage of the largest and smallest values before calculating the average, the trimmed mean provides a more accurate reflection of the "center" of a dataset when extreme values might skew the results.
Why Use a Trimmed Mean?
In many real-world datasets, extreme outliers can heavily influence the average. For example, in competitive sports scoring (like gymnastics or diving), judges' scores are often trimmed (the highest and lowest scores are discarded) to prevent bias or error from a single judge affecting the athlete's final score.
Similarly, in economic data like income distribution, a few billionaires can skew the average income significantly upwards, making the "average" misleading for the typical person. A trimmed mean smooths out these anomalies.
How the Calculation Works
The process of calculating a trimmed mean involves four distinct steps:
Sorting: Organize all data points in the set from smallest to largest.
Determining the Trim Count (k): Calculate how many observations to remove. If you select a 10% trimmed mean, you calculate 10% of the total count of items.
Trimming: Remove k items from the bottom of the list and k items from the top of the list.
Averaging: Calculate the standard arithmetic mean of the remaining numbers.
Formula
If $n$ is the number of observations and $p$ is the percentage to trim from each end:
$$ k = \lfloor n \times \frac{p}{100} \rfloor $$
Where $k$ is the number of data points removed from each end. The mean is then calculated on the remaining $n – 2k$ data points.
Example Calculation
Let's calculate a 20% trimmed mean for the following dataset of 10 numbers:
Raw Data: 12, 55, 14, 18, 10, 100, 16, 14, 11, 2
Step 1: Sort the data
2, 10, 11, 12, 14, 14, 16, 18, 55, 100
Step 2: Determine trim count
Total items ($n$) = 10.
Percentage ($p$) = 20%.
$k = 10 \times 0.20 = 2$.
We must remove 2 numbers from the bottom and 2 numbers from the top.
Comparison: The standard mean of the original dataset was 25.2, which was heavily skewed by the "100" outlier. The trimmed mean (14.16) is much closer to the bulk of the data.
Frequently Asked Questions
What is the difference between a 10% and 20% trimmed mean?
A 10% trimmed mean removes the top 10% and bottom 10% of values. A 20% trimmed mean removes more data (the top and bottom 20%). The higher the percentage, the closer the result typically moves toward the median. In fact, if you trim nearly 50%, the trimmed mean becomes the median.
Is "Trimmed Mean" the same as "Truncated Mean"?
Yes, the terms are often used interchangeably in statistics. Both refer to the process of discarding the tails of a probability distribution or sample.
How are decimal trim counts handled?
If the percentage results in a non-integer number of items to remove (e.g., 10% of 15 items is 1.5), standard practice is usually to round down to the nearest whole integer (floor), ensuring that at least that percentage of data remains. However, some advanced statistical software may interpolate values, but this calculator uses the standard "floor" method for simplicity and clarity.