0
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

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:

  • num = 0 // num stands for the numerator
  • den = 0 // den stands for the denominator
  • for k = 1 to N do
    • num += w[k] * score[k]
    • den += w[k]
  • avg = num / den // avg stands for the weighted average
$\endgroup$
  • $\begingroup$ Great, thanks. Was close to what I had, but was trying to divide by the average of the weights or something like that. $\endgroup$ – BadHorsie Aug 25 '15 at 16:16
  • $\begingroup$ You're welcome. :) $\endgroup$ – wltrup Aug 25 '15 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.