Coding with Maths: Statistics
Use code to calculate mean, median, mode, and more -- doing in seconds what takes minutes by hand!
Why Combine Coding and Maths?
Imagine calculating the average of 5 numbers by hand. Easy enough. Now imagine doing it for 10,000 numbers. That's where code becomes powerful.
By Hand
5 numbers: ~2 minutes
100 numbers: ~30 minutes
10,000 numbers: days!
With Code
5 numbers: instant
100 numbers: instant
10,000 numbers: still instant!
This is why data science and AI rely on both maths and coding working together.
Calculating the Mean (Average)
The mean is the sum of all values divided by how many values there are: Mean = Sum / Count
function calculateMean(numbers) {
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum = sum + numbers[i];
}
return sum / numbers.length;
}
let data = [4, 8, 15, 16, 23, 42];
display("Mean = " + calculateMean(data));
Finding Maximum and Minimum
function findMax(numbers) {
let max = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
return max;
}
function findMin(numbers) {
let min = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] < min) {
min = numbers[i];
}
}
return min;
}
Full Statistics Report
This function calculates mean, median, mode, min, max, and range all at once!
// Full statistics: mean, median, mode, min, max, range
function statistics(numbers) {
// Sort the numbers
numbers.sort((a, b) => a - b);
// Mean
let sum = 0;
for (let n of numbers) sum += n;
let mean = sum / numbers.length;
// Median
let mid = Math.floor(numbers.length / 2);
let median = numbers.length % 2 !== 0
? numbers[mid]
: (numbers[mid-1] + numbers[mid]) / 2;
// Mode
let counts = {};
for (let n of numbers) {
counts[n] = (counts[n] || 0) + 1;
}
return { mean, median, mode, min, max, range };
}
Key Vocabulary
Mean (Average)
Sum of all values divided by the count. Gives the "typical" value.
Median
The middle value when data is sorted. Not affected by extreme values.
Mode
The value that appears most often in the data set.
Range
The difference between the largest and smallest values. Range = Max - Min.
Array
A list of values stored in code. E.g., [4, 8, 15, 16]
Data Science
Using code + maths + statistics to find patterns and insights in data.
Worked Examples
Calculate the mean of [10, 20, 30, 40, 50]
Step 1: Sum = 10 + 20 + 30 + 40 + 50 = 150
Step 2: Count = 5 numbers
Answer: Mean = 150 / 5 = 30
Find the median of [3, 7, 1, 9, 5]
Step 1: Sort: [1, 3, 5, 7, 9]
Step 2: 5 numbers (odd count), so median is the middle value
Answer: Median = 5 (the 3rd value in sorted list)
What is the mode of [2, 4, 4, 6, 4, 8, 2]?
Step 1: Count each value: 2 appears 2 times, 4 appears 3 times, 6 appears 1 time, 8 appears 1 time
Step 2: Which appears most? 4 (appears 3 times)
Answer: Mode = 4
Knowledge Check
Select the correct answer for each question.
Question 1
What is the mean of [2, 4, 6, 8, 10]?
Question 2
What is the median of [12, 3, 7, 15, 9]?
Question 3
What does this code display?
let data = [5, 10, 15, 20];
let sum = 0;
for (let i = 0; i < data.length; i++) {
sum = sum + data[i];
}
display(sum);
Question 4
What is the range of [3, 17, 8, 24, 11]?
Question 5
What is the mode of [5, 3, 5, 7, 3, 5, 9]?
Key Concepts Summary
- ●Code can calculate statistics (mean, median, mode, range) instantly -- even for huge data sets.
- ●Mean = sum / count. Median = middle value (sorted). Mode = most frequent value.
- ●Arrays store lists of data that loops can process one item at a time.
- ●Combining coding and maths is the foundation of data science and artificial intelligence.
- ●Code makes maths scalable -- the same function works for 5 numbers or 5 million.