This is for some software I'm writing.
I have a number of fields, all of which have a score input which allows a percentage of 0-100. Each field has a "weight" property, which determines its significance, and should affect the overall calculated average of all the fields.
Example:
Field | Weight | Score %
--------------------------------
A | 1.0 | 30
B | 2.0 | 57
C | 3.5 | 84
D | 1.0 | 91
E | 2.0 | 77
F | 4.0 | 12
The normal average of the scores would be 58.5
but the weight of each field should be applied to proportionally influence the average.
- Field B's score should be twice as important as Field A
- Field F's score should be four times as important as Field A
- etc.
I would appreciate any explanation / pseudo-maths as to the most efficient way to approach this, and I will apply it to my software as appropriate.
If somebody could please tag this post appropriately – I'm not sure what to tag it with.
Best Answer
The weighted average of a set of $N$ numbers $x_k$ (in your case, the scores) where each number has weight $w_k$ is simply $$ \bar{x} = \frac{w_1x_1 + w_2x_2 + w_3x_3 + \ldots + w_Nx_N}{w_1 + w_2 + w_3 + \ldots + w_N} = \frac{\sum_{k\,=\,1}^{N} w_kx_k}{\sum_{k\,=\,1}^{N} w_k} $$
In words, you multiply each score by its corresponding weight, then sum all of those values, then divide that result by the sum of the weights. You can do both sums in a single loop then do the final division after the loop ends.
In pseudo-code, it looks like this: