Recent

Author Topic: adjust relative weights in order that sum of relative weights is always 1  (Read 1955 times)

tatamata

  • Hero Member
  • *****
  • Posts: 804
    • ZMSQL - SQL enhanced in-memory database
There is a dataset with records for which a user can set a relative weight for each row. The weight is a float value between 0 an 1.
When user changes particular weight, weights in all other rows should be automatically adjusted in order to maintain rule that sum of relative weights of all records must be 1.
Can you suggest algorithm or point to a code example how to accomplish this?
Thanks.

Nesto

  • New member
  • *
  • Posts: 9
Re: adjust relative weights in order that sum of relative weights is always 1
« Reply #1 on: February 21, 2016, 12:34:39 pm »
take the old and new weight and subtract both values from 1.0, then divide the new with old result. with this factor multiply each weight in the other rows.

Quote
e.g.
input
0.40 -> 0.75 changed weight
0.40
0.20

calc
1.0 - 0.40 = 0.60 -> old sum of the other weights
1.0 - 0.75 = 0.25 -> new sum of the other weights

0.25 / 0.60 = 0.4166 factor for all the other rows

result
0.75 (set by user)
0.40 * 0.4166 = 0.16664
0.20 * 0.4166 = 0.08332
« Last Edit: February 21, 2016, 12:39:51 pm by Nesto »

balazsszekely

  • Guest
Re: adjust relative weights in order that sum of relative weights is always 1
« Reply #2 on: February 21, 2016, 12:47:13 pm »
I would use a trigger for this. You have a new, old value for the current, changed row. With a for select, you can loop through the rows and change the values accordingly.

 

TinyPortal © 2005-2018