0

I've been breaking my head in trying to calculate the proportional scaling of percentages, in which the end result should always quantify to 100%.

For example:

Person A - 60%
Person B - 40%

When adding person C with 50%, the following scaling occurs, in proportion to their original percentage of what's left:

Person A: 30%
Person B: 20%
Person C: 50%

When amending an entry; for example, setting Person A to 10%, the following occurs:

Person A: 10%
Person B: 30%
Person C: 60%

I was able to calculate this off the top of my head (strange - that never works), however I'm uncertain as to how to apply this in mathematical formula, and in its simplest form; in such a way that the calculation works in all scenarios, whether adding, amending or removing persons and their percentages.

At every calculation, I have existing reference to the following:

  • How many items (persons) there currently are
  • The original percentage allocated to that person

Your help would be greatly appreciated.

Edit

I'm not sure how to explain it in a better form, but I hope the following tables may explain a bit better:

Where ? are the values to be calculated

The closest I've come so far:

var beneficiaries = [
    100
]; // Current state of beneficiaries

const calculateBeneficiaryRatio = (action, value = 0, index = 0) => { let results = []; let cur_percentage, new_percentage;

switch(action) {
case "add":
        for(let i in beneficiaries) {
      cur_percentage = beneficiaries[i];
      new_percentage = (100 - value) * (cur_percentage / 100);
      beneficiaries[i] = new_percentage;
    } 
        beneficiaries.push(value);
    return beneficiaries;
    break;
case "remove":
        value = beneficiaries[index];
    beneficiaries.splice(index, 1);
        for(let i in beneficiaries) {
      cur_percentage = beneficiaries[i];
      new_percentage = cur_percentage + (value / beneficiaries.length);
      beneficiaries[i] = new_percentage;
    }
    return beneficiaries;
    break;
case "update":
        beneficiaries[index];
        for(let i in beneficiaries) {
      cur_percentage = beneficiaries[i];
      new_percentage = (100 - value) * (cur_percentage / 100) + (value / beneficiaries.length);
      beneficiaries[i] = new_percentage;
    }
    return beneficiaries;
    break;

} }

beneficiaries = calculateBeneficiaryRatio('add', 29); console.log(beneficiaries); beneficiaries = calculateBeneficiaryRatio('add', 20); console.log(beneficiaries); beneficiaries = calculateBeneficiaryRatio('remove', null, 1); console.log(beneficiaries); beneficiaries = calculateBeneficiaryRatio('update', 20, 0); console.log(beneficiaries);

https://jsfiddle.net/74Lf2dhk/1/

  • I think you've gotten your final entry wrong. Should be 25 5/7% for person B and 64 2/7% for person C, if you're trying to maintain a 2:5 ratio between B and C. – Kyan Cheung Jan 26 '21 at 07:07
  • Thank you Kyky. The total distribution should scale in proportion to the person's original %, after which, when all are added up, it's always 100% allocation. My math is terrible, hence why I need some guidance. – RookieSA Jan 26 '21 at 07:15
  • 1
    It will help to have a clear idea of what's happening here. In your last example, you're amending $A$. You say that the "total distribution should scale in proportion to the person's original %" — what does this mean, exactly? $C$ doesn't have an initial value; what should happen? – Théophile Jan 26 '21 at 07:20
  • 1
    In your latest example, when $C$ is added, the values go from $(90,10)$ to $(85,5,10)$. Both $A$ and $B$ have decreased by $5$; this changes the ratio $A:B$ significantly: $90:10 \neq 85:5$. Then, when $D$ is added, this time the others are adjusted so that the ratio $A:B:C$ is maintained. What's going on? – Théophile Jan 26 '21 at 14:21

1 Answers1

0

Neither of your two examples matches your description: instead of $(85,5,10)$, I would have expected $(81,9,10)$, because that preserves the ratio $a:b$, i.e., $90:10=81:9$. Maybe there was a miscalculation?

If that's the case, then here's the idea: suppose you have some percentages $(a,b,c,d)$ and you're changing $d$ to $d+\Delta$. Then we need to remove a total of $\Delta$ from the other three while preserving the ratio $a:b:c$.

Looking at $a$ as a fraction of $a+b+c$, we have $\frac a{a+b+c}$. This is the fraction of $\Delta$ by which $a$ must decrease, so the new value of $a$ will be $$a - \left(\frac a{a+b+c}\right)\Delta,$$ which after factoring out the $a$ we can rewrite as $$a\left(1 - \frac \Delta{a+b+c}\right).$$

To put it more simply, let $T$ be the total of the first three values: $T=a+b+c$. Then the new value of $a$ will be $a\left(\frac{T-\Delta}T\right).$

The changes to $b$ and $c$ are similar; therefore, the new values of $(a,b,c,d)$ will be $$\left(a\left(\frac{T-\Delta}T\right), b\left(\frac{T-\Delta}T\right), c\left(\frac{T-\Delta}T\right), d + \Delta\right).$$

Finally, note that the case $a=b=c=0$ will have to be given special treatment.

Théophile
  • 24,627
  • Thanks for your patience, Théophile. That's the thing, I'm not too sure what I'm doing myself. I'm trying though, but I can't seem to grasp the concept. Let me explain the actual objective; then perhaps it would make some sort of sense rather than confusing myself and everyone: – RookieSA Jan 26 '21 at 14:49
  • Suppose I have an insurance policy, and as per the policy, it states that the amount of cover across beneficiaries should always equate and amount to 100%, irrespective of how many beneficiaries have been specified.

    So, upon adding my first beneficiary, it's automatically allocated the full 100% to conform to the requirements. Now, I wish to add another beneficiary with 40% cover; this should automatically bring down my first beneficiary to 60% and the new beneficiary will then be added as 40%.

    – RookieSA Jan 26 '21 at 14:53
  • I wish to add another beneficiary, with 30% cover. Beneficiary 1 and 2 already have 60% and 40% respectively. To accommodate the new beneficiary, the other two beneficiaries need to automatically adjust in proportion to what their original cover percentage was. And so with this 3rd beneficiary added, there's 70% cover left to share between the 2 already-existing beneficiaries, however should honor the ratio of what their cover was out of 100% - only, the 100% now is suddenly 70%. – RookieSA Jan 26 '21 at 14:56
  • This logic, being applied when a beneficiary's cover amount is amended or when a beneficiary is removed. Inasmuch as I try, I can't seem to grasp the concept. All I have, at all times, are the amount of beneficiaries and what their current cover percentages are. This is the objective that is expected, though, from a programming perspective, I wouldn't know how to replicate your formula. And so, with limited standard math calculations, I can only calculate in the following manner:

    input_percentage = 70% difference = (100 - 70) = 30 new_percentage = difference * (input_percentage / 100)

    – RookieSA Jan 26 '21 at 15:05
  • @RookieSA Okay, it sounds like we're talking about the same thing. I updated the formula to express it more succinctly. You simply need to multiply $a,b$, and $c$ by $\frac{T-\Delta}T$, where $T$ is the total of their original values, and $\Delta$ is the change in $d$. This generalizes to any number of variables. For example, let's take your earlier situation where $(a,b,c)=(30,20,50)$, and you decrease $a$ by $20$. Then $\frac{T-\Delta}T = \frac{70-(-20)}{70} = \frac97 \approx 1.29$, so $b$ and $c$ need to be scaled by this amount. This should give $(10, 25.7, 64.3)$. – Théophile Jan 26 '21 at 19:24
  • You can verify that $10+25.7+64.3=100$, as required, and the ratio of $b:c$ has remained the same, i.e., $20:50 = 25.7:64.3$. These are the values that @Kyky arrived at also in the comment above. – Théophile Jan 26 '21 at 19:25
  • Thanks so much, Théophile! Works a treat! You're a genius. For the sake of interest, is there any specific terminology for this type of calculation? – RookieSA Jan 26 '21 at 19:43
  • @RookieSA Glad to help! Off the top of my head, what comes to mind is barycentric co-ordinates. The article is fairly technical, but if you look at the triangle in the section I linked to, notice that the corners are labelled $(1,0,0), (0,1,0)$, and $(0,0,1)$: in percentages, this would correspond to $(100,0,0), (0,100,0)$, and $(0,0,100)$. Any other point in the triangle is a mix of three numbers that will always add to $1$ (multiply by $100$ to get percentage). – Théophile Jan 26 '21 at 20:44
  • Moving directly towards or away from one of the corners is equivalent to adjusting one of your percentages and keeping the others in a correct ratio. – Théophile Jan 26 '21 at 20:45