[Solved] How to create 4 random defined percentages that sum 1


Let

ll <- c(0.2, 0.2, 0.15, 0.15)
ul <- c(0.5, 0.5, 0.4, 0.3)

where ll and ul correspond to lower and upper limits for each of the random variables. Next,

x <- runif(length(ll), 0, ul - ll)

is a vector of uniform random draws from intervals [0,0.3], [0,0.3], [0,0.25], and [0,0.15]. The reason for this is that our final vector is

if(sum(x) > 1 - sum(ll)) {
  ll + x / sum(x) * (1 - sum(ll))
} else {
  ll + x + (ul - ll - x) / sum(ul - ll - x) * (1 - sum(x) - sum(ll))
}
# [1] 0.3112532 0.2927185 0.2347163 0.1613119

So, if sum(x) > 1 - sum(ll), this means that x is too large. In that case we renormalize it by making it smaller. In this way all the constraints are satisfied. Otherwise, x is too small. In that case we are going to add a portion of ul - ll - x. Now the key is that necessarily

sum(ul - ll - x) > (1 - sum(x) - sum(ll))

which means that ul - ll - x doesn’t get increased and, hence, again all the restrictions are satisfied.

Kernel densities of the respective elements are as follows

enter image description here

solved How to create 4 random defined percentages that sum 1