Mean, typically denoted by , measures the central value of the data set. It is the sum of all the values divided by the number of values.
Example
For example, let’s say in a class of 4 children, Alex, Beth, Charlie and Dawn, scored 3, 8, 21 and 9 for a test. As a teacher, you want to find out the average score for the class. We can use the mean formula to find this. In this case, for the sample , the sample mean is
Code
In code, this can be done as follows.
// create an array of doubles for our dataset
val values = doubleArrayOf(3.0, 8.0, 21.0, 9.0)
// get the array size
val n = values.size
var total = 0.0
var sample_mean : Double
// find the sum of all the values
for (i in 0..n-1) {
total += values.get(i)
}
sample_mean = total / n
println("Sample size: " + n)
println("Sample mean: " + sample_mean)
Sample size: 4
Sample mean: 10.25
In NM Dev, we can simply compute the sample of a dataset using the class Mean without having to come up with the algorithm ourselves.
// create an array of doubles for our dataset
val values = doubleArrayOf(3.0, 8.0, 21.0, 9.0)
// create the Mean object using the array that we created
val mean = Mean(values)
println("Sample size: " + mean.N())
println("Sample mean: " + mean.value())
Sample size: 4
Sample mean: 10.25
Weighted mean allows certain values in the dataset to weigh more than others. Each value in the dataset can have its own weight . It is the sum of all the values multiplied by each of their weights divided by the sum of all their weights.
Example
Let’s say for example, a student took 5 subjects, English, Physics, Math, Economics and Computing and each subject has a different weight when calculating the student’s overall grade. The weights are as follows.
Subject | Weight |
English | 2 |
Physics | 3 |
Math | 4 |
Economics | 1 |
Computing | 5 |
The student’s grades for each subject are as follows.
Subject | Grade |
English | 88 |
Physics | 94 |
Math | 69 |
Economics | 66 |
Computing | 80 |
In order to find the student’s overall grade through the weighted mean formula, we must first multiply the grade for each subject with their respective weights.
Subject | Weighted Grade |
English | 2 * 88 = 176 |
Physics | 3 * 94 = 282 |
Math | 4 * 69 = 276 |
Economics | 1 * 66 = 66 |
Computing | 5 * 80 = 400 |
We can then find the average of the weighted grades by finding the sum of all the weighted grades and then dividing it by the total weight.
Sum of weighted grades
Total weight
Weighted mean
Hence, the student’s overall grade is 80.
The above example can be defined mathematically as follows. The student’s grades for each subject are and each subject weighs differently in calculating the overall grade, , the weighted sample mean is
Code
Translating this into code, we can use NM Dev’s class WeightedMean.
// create arrays for our dataset and our weights
val values = doubleArrayOf(88.0, 94.0, 69.0, 66.0, 80.0)
val weights = doubleArrayOf(2.0, 3.0, 4.0, 1.0, 5.0)
// create the WeightedMean object
val weighted_mean = WeightedMean(values, weights)
println("Sample weighted mean: " + weighted_mean.value())
Sample weighted mean: 80.0