(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.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With