In statistics, the mean, median, and mode are three fundamental measures of central tendency. They each provide a different way to represent the "typical" value in a dataset, and understanding their differences is crucial for interpreting data accurately.
The Mean (Average)
The mean is what most people refer to as the "average." It's calculated by summing up all the values in a dataset and then dividing by the total number of values.
Mean = (Sum of all values) / (Number of values)
The mean is sensitive to outliers (extremely high or low values) because every value in the dataset contributes to its calculation.
The Median
The median is the middle value in a dataset that has been ordered from least to greatest. If the dataset has an odd number of values, the median is the single middle value. If the dataset has an even number of values, the median is the average of the two middle values.
To find the median:
Sort the data points in ascending order.
If there's an odd number of data points, the median is the middle number.
If there's an even number of data points, add the two middle numbers together and divide by 2.
The median is a more robust measure of central tendency than the mean when dealing with datasets that contain outliers, as it is not affected by extreme values.
The Mode
The mode is the value that appears most frequently in a dataset. A dataset can have one mode (unimodal), more than one mode (multimodal), or no mode at all if all values appear with the same frequency.
If multiple values share the highest frequency, they are all considered modes. If every value appears only once, there is no mode.
When to Use Which Measure?
Mean: Useful for datasets where the values are relatively close together and not skewed by extreme outliers. It's commonly used in many statistical analyses.
Median: Ideal for datasets with outliers or when you want to find the "typical" value that isn't distorted by extreme numbers. Income and housing prices are often reported using the median.
Mode: Helpful for identifying the most common occurrence in a dataset, particularly for categorical data or when you're interested in the most popular item or value.
Example Calculation
Let's consider the dataset: 5, 10, 15, 10, 20
Mean: (5 + 10 + 15 + 10 + 20) / 5 = 60 / 5 = 12
Median: First, sort the data: 5, 10, 10, 15, 20. The middle value is 10.
Mode: The number 10 appears twice, which is more than any other number. So, the mode is 10.
function calculateStats() {
var dataInput = document.getElementById("dataInput").value;
var resultsDiv = document.getElementById("result");
var meanResultSpan = document.getElementById("meanResult");
var medianResultSpan = document.getElementById("medianResult");
var modeResultSpan = document.getElementById("modeResult");
// Clear previous results
meanResultSpan.textContent = "-";
medianResultSpan.textContent = "-";
modeResultSpan.textContent = "-";
resultsDiv.style.backgroundColor = "#f8f9fa"; // Reset to default background
if (!dataInput) {
alert("Please enter some data points.");
return;
}
// Parse the input string into an array of numbers
var numbers = dataInput.split(',')
.map(function(item) {
return parseFloat(item.trim());
})
.filter(function(item) {
// Remove any non-numeric values or NaN
return !isNaN(item);
});
if (numbers.length === 0) {
alert("No valid numbers were found in the input. Please check your entries.");
return;
}
// — Calculate Mean —
var sum = 0;
for (var i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
var mean = sum / numbers.length;
meanResultSpan.textContent = mean.toFixed(2);
// — Calculate Median —
// Sort the numbers for median calculation
var sortedNumbers = numbers.slice().sort(function(a, b) {
return a – b;
});
var median;
var mid = Math.floor(sortedNumbers.length / 2);
if (sortedNumbers.length % 2 === 0) {
// Even number of elements
median = (sortedNumbers[mid – 1] + sortedNumbers[mid]) / 2;
} else {
// Odd number of elements
median = sortedNumbers[mid];
}
medianResultSpan.textContent = median.toFixed(2);
// — Calculate Mode —
var mode = [];
var frequencyMap = {};
var maxFrequency = 0;
for (var j = 0; j maxFrequency) {
maxFrequency = frequencyMap[num];
}
}
// Collect all numbers with the maximum frequency
for (var key in frequencyMap) {
if (frequencyMap[key] === maxFrequency) {
mode.push(parseFloat(key)); // Ensure it's a number
}
}
// Handle cases:
// 1. All numbers appear once (no mode)
// 2. Multiple numbers share the same highest frequency (multimodal)
// 3. Single number with highest frequency (unimodal)
if (maxFrequency === 1 && numbers.length > 1) {
modeResultSpan.textContent = "No distinct mode";
} else if (mode.length === numbers.length) { // All numbers have same frequency
modeResultSpan.textContent = "No distinct mode";
}
else {
modeResultSpan.textContent = mode.map(function(m) { return m.toFixed(2); }).join(', ');
}
// Change background color to indicate success
resultsDiv.style.backgroundColor = "var(–success-green)";
}