Random Number Generator with Restrictions

Asked

Viewed 36 times

1

I need to define randomly the weight of an investment portfolio that is divided into three types and cm the following limitations:

  1. Fixed income [0-100%]
  2. Variable Income [0-30%]
  3. Investment abroad 0-10%]

Restrictions:

  1. All investments must be greater than or equal to 0;
  2. And the sum of the weights should be 1 + 2 + 3 = 100%.

Generating the numbers separately is not the problem and obeying the first constraint is easy, but I cannot do the second.

rendaFixa <- runif(1, 0, 1)
rendaVariavel <- runif(1, 0, 0.3)
rendaIE <- runif(1, 0, 0.1)

1 answer

2


I would draw first the values of rendaIE and rendaVariavel. The way these proportions are defined, their sums will never exceed 40%. From there, rendaFixa would be the difference between 100% and the sum rendaIE + rendaVariavel. Computationally, it would be something like this:

set.seed(1)

rendaIE <- runif(1, 0, 0.1)
rendaVariavel <- runif(1, 0, 0.3)
rendaFixa <- 1 - rendaIE - rendaVariavel

rendaIE
#> [1] 0.02655087
rendaVariavel
#> [1] 0.1116372
rendaFixa
#> [1] 0.861812

rendaIE + rendaVariavel + rendaFixa
#> [1] 1

Created on 2021-02-22 by the reprex package (v1.0.0)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.