Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to keep a list of percentages to add up to 100%

(code examples are python)
Lets assume we have a list of percentages that add up to 100:

mylist = [2.0, 7.0, 12.0, 35.0, 21.0, 23.0]  

Some values of mylist may be changed, others must stay fixed.
Lets assume the first 3 (2.0, 7.0, 12.0) must stay fixed and the last three (35.0, 21.0, 23.0) may be changed.

fix = mylist[:3]
vari = mylist[3:]

The goal is to add a new item to mylist, while sum(mylist) stays 100.0 and vari
items keep their relations to each other. For that we need to substract a CERTAIN
PERCENTAGE from each vari item. Example: lets assume we want to add 4.0 to mylist.
Using an ugly aproximation loop I found out that i need to substract ca. 5.0634%
of each vari item (CERTAIN PERCENTAGE = 5.0634):

adjusted =[]
for number in vari:
    adjusted.append(number-(number*(5.0634/100.0)))
adjusted.extend(fix)
adjusted.append(4.0)

adjusted now contains my desired result.

My question is how to calculate CERTAIN PERCENTAGE ;.)

like image 318
Titusz Avatar asked Jan 05 '09 12:01

Titusz


2 Answers

How's this?

def adjustAppend( v, n ):
    weight= -n/sum(v)
    return [ i+i*weight for i in v ] + [n]

Given a list of numbers v, append a new number, n. Weight the existing number to keep the sum the same.

 sum(v) == sum( v + [n] )

Each element of v, i, must be reduced by some function of i, r(i) such that

sum(r(i)) == -n

or

sum( map( r, v ) ) == -n

Therefore, the weighting function is -(n*i)/sum(v)

like image 83
S.Lott Avatar answered Sep 19 '22 01:09

S.Lott


you're being silly.

let's say you want to add 4.0 to the list. You don't need to subtract a certain amount from each one. What you need to do is multiply each item.

100 - 4 = 96. therefore, multiply each item by 0.96

you want to add 20.0 as an item. so then you multiply each item by 0.8, which is (100-20)*0.01

update: Hrmn I didn't read carefuly enough.

think of it like this. (fixed)+(vari)= 100; (fixed)+(vari * x) + newitem = 100;

so basically like what we did before except with just the vari portion. if vari totals to 50, and the new item you're adding is 3.0, then multiply each item in vari by (47/50)

like image 32
Breton Avatar answered Sep 19 '22 01:09

Breton