Last updated
Descriptive Statistics Overview
Descriptive statistics summarize and describe the main features of a dataset. Unlike inferential statistics (which draw conclusions about populations from samples), descriptive statistics simply describe what the data shows. The key measures are central tendency (mean, median, mode) and dispersion (range, variance, standard deviation).
Measures of Central Tendency
| Measure | Formula | Best For |
|---|---|---|
| Mean | Σx / n | Symmetric distributions without outliers |
| Median | Middle value when sorted | Skewed distributions, income data |
| Mode | Most frequent value | Categorical data, finding peaks |
| Geometric mean | (x₁ × x₂ × ... × xₙ)^(1/n) | Growth rates, ratios |
Full Statistics in JavaScript
JavaScript
function statistics(data) {
const sorted = [...data].sort((a, b) => a - b);
const n = data.length;
const sum = data.reduce((a, b) => a + b, 0);
const mean = sum / n;
// Median
const mid = Math.floor(n / 2);
const median = n % 2 ? sorted[mid] : (sorted[mid-1] + sorted[mid]) / 2;
// Mode
const freq = {};
data.forEach(x => freq[x] = (freq[x] || 0) + 1);
const maxFreq = Math.max(...Object.values(freq));
const mode = Object.keys(freq).filter(k => freq[k] === maxFreq).map(Number);
// Variance & std dev
const variance = data.reduce((s, x) => s + Math.pow(x - mean, 2), 0) / (n - 1);
const stdDev = Math.sqrt(variance);
return {
count: n, sum, mean, median, mode,
min: sorted[0], max: sorted[n-1],
range: sorted[n-1] - sorted[0],
variance: variance.toFixed(4),
stdDev: stdDev.toFixed(4),
q1: sorted[Math.floor(n * 0.25)],
q3: sorted[Math.floor(n * 0.75)]
};
}