In statistics, the Mean and Standard Deviation are the two most fundamental measures used to describe a data set. While the mean provides a central value, the standard deviation tells you how "spread out" the data is from that central value.
What is the Arithmetic Mean?
The mean (or average) is calculated by adding all values in a collection and dividing by the count of those values. It represents the equilibrium point of your data.
Formula: x̄ = Σx / n
What is Standard Deviation?
Standard deviation measures the amount of variation or dispersion. A low standard deviation indicates that the data points tend to be very close to the mean, while a high standard deviation indicates that the data points are spread out over a wide range of values.
Sample vs. Population
It is crucial to choose the correct type for your calculation:
Population: Used when you have data for every single member of the group you are studying (e.g., test scores for every student in a specific class).
Sample: Used when you are using a small group to represent a larger population (e.g., polling 100 people to represent an entire city). The sample calculation uses "n-1" (Bessel's correction) to provide an unbiased estimate.
Realistic Example
Imagine you are measuring the height of 5 plants in centimeters: 12, 15, 12, 18, 13.
Mean: (12+15+12+18+13) / 5 = 14 cm.
Variance (Sample): Calculated by finding the squared difference of each height from 14, summing them, and dividing by 4 (n-1). Sum of squares = 26. Variance = 6.5.
Standard Deviation: Square root of 6.5 ≈ 2.55 cm.
function calculateStatistics() {
var rawData = document.getElementById("dataset").value;
var errorDiv = document.getElementById("error-message");
var resultsDiv = document.getElementById("results-section");
// Reset displays
errorDiv.style.display = "none";
resultsDiv.style.display = "none";
// Clean data: split by commas, spaces, or newlines
var dataArray = rawData.split(/[,\s\n]+/)
.map(function(item) { return item.trim(); })
.filter(function(item) { return item !== ""; })
.map(Number);
// Validate inputs
if (dataArray.length === 0) {
errorDiv.innerText = "Please enter some numbers to calculate.";
errorDiv.style.display = "block";
return;
}
for (var i = 0; i < dataArray.length; i++) {
if (isNaN(dataArray[i])) {
errorDiv.innerText = "One or more entries are not valid numbers. Please check your data.";
errorDiv.style.display = "block";
return;
}
}
var isPopulation = document.getElementById("population").checked;
var n = dataArray.length;
if (!isPopulation && n <= 1) {
errorDiv.innerText = "Sample standard deviation requires at least two numbers.";
errorDiv.style.display = "block";
return;
}
// 1. Calculate Sum
var sum = 0;
for (var j = 0; j < n; j++) {
sum += dataArray[j];
}
// 2. Calculate Mean
var mean = sum / n;
// 3. Calculate Variance
var sumOfSquares = 0;
for (var k = 0; k < n; k++) {
sumOfSquares += Math.pow(dataArray[k] – mean, 2);
}
var divisor = isPopulation ? n : n – 1;
var variance = sumOfSquares / divisor;
var stdDev = Math.sqrt(variance);
// Display results
document.getElementById("res-count").innerText = n;
document.getElementById("res-mean").innerText = mean.toLocaleString(undefined, {maximumFractionDigits: 4});
document.getElementById("res-sum").innerText = sum.toLocaleString(undefined, {maximumFractionDigits: 4});
document.getElementById("res-variance").innerText = variance.toLocaleString(undefined, {maximumFractionDigits: 4});
document.getElementById("res-sd").innerText = stdDev.toLocaleString(undefined, {maximumFractionDigits: 4});
resultsDiv.style.display = "block";
}