Function Repository Resource:

RoundedPercentageAllocation

Source Notebook

Split a value into list of rounded numbers according to a list of target proportions

Contributed by: Jon McLoone

ResourceFunction["RoundedPercentageAllocation"][total,proportions]

creates a list of integers that sum to total and are also as close to the given proportions as possible.

ResourceFunction["RoundedPercentageAllocation"][total,proportions,d]

creates a list of numbers that sum to total and are multiples of d.

Examples

Basic Examples (2) 

Split 100 into proportions of 1/3 and 2/3 to the nearest integer:

In[1]:=
ResourceFunction["RoundedPercentageAllocation"][100, {1/3, 2/3}]
Out[1]=

In most financial currencies, one would typically allocate it to the nearest 0.01:

In[2]:=
ResourceFunction["RoundedPercentageAllocation"][100, {1/3, 2/3}, 0.01]
Out[2]=

Properties and Relations (2) 

As long as the total is a multiple of the allocation amount, the result will always sum to the original total:

In[3]:=
proportions = RandomReal[1, 10];
proportions = proportions/Total[proportions]
Out[2]=
In[4]:=
result = ResourceFunction["RoundedPercentageAllocation"][97, proportions]
Out[4]=
In[5]:=
Total[result]
Out[5]=
In[6]:=
result = ResourceFunction["RoundedPercentageAllocation"][97, proportions, 0.01]
Out[6]=
In[7]:=
Total[result]
Out[7]=

However, the proportions in the result are usually not exactly the requested proportions:

In[8]:=
result/Total[result]
Out[8]=

Possible Issues (2) 

If the input number is not divisible by the allocation size then the result will add up to less then the original:

In[9]:=
ResourceFunction[
 "RoundedPercentageAllocation"][100.5, {1/3, 2/3}, 0.2]
Out[9]=
In[10]:=
Total[%]
Out[10]=

If you allocate an amount to proportions that do not sum to one, the proportions will be rescaled so that they do sum to 1:

In[11]:=
ResourceFunction["RoundedPercentageAllocation"][100, {0.2, 0.2}]
Out[11]=

Publisher

Jon McLoone

Version History

  • 1.0.0 – 05 February 2020

Related Resources

License Information