# portfolio weights generation

Hi!!
I would like to model 3000 portfolios weights that incorporate various constraints using Rstudio.

I have four assets with the following constraints
0 < 1 < 0.5
0 < 2 < 0.5
0 < 3 < 0.5
0,2 <= 4 < 0.5
1 + 2 + 3 + 4 = 1

Thank you very much for the help!!!

Perhaps you could make your question more concrete; to me it reads quite ambiguously.
Its often good when seeking help, to show how you can do a simpler thing, but are missing the knowledge to add a complexity.
In that spirit, could you show how you would model one portoflio weight not incorporating constraints ?

If you are trying to use R to select a model portfolio, have a look at the quantmod package. There are functions there that are designed specifically for determining "optimal" portfolios.

I am not trying to find an optimal portfolio yet. I am just trying to generate random sets of weights following my constraints.

how do we interpret this ? was the comma supposed to be a decimal point ?

``````n <- 3000

buffnum <- 10 # make this more values than we need so we can throw away stuff we dont like

x1 <- runif(n*buffnum ,min = 0,max=0.5)
x2 <- runif(n*buffnum ,min = 0,max=0.5)
x3 <- runif(n*buffnum ,min = 0.2,max=0.5)

x4 <- 1 - (x1+x2+x3)

conform_1 <- which(x4>0)
length(conform_1)

picks <- sample(conform_1,
size = n,
replace = FALSE)

myweights <- data.frame(
x1 = x1[picks],
x2 = x2[picks],
x3 = x3[picks],
x4 = x4[picks]
)``````

this is perfect thank you very much !

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.