I am trying to find all possible portfolio-allocations for n
number of equities with weighting steps of s
. The function expand.grid()
is used for calculating all variations, and a subset is done using rowSums()
, reducing the output to all variations where the weightings are 100.
The problem:
This way doesn't work for "larger" numbers. Using the subset after expand.grid()
doesn't seem to be the best way. Any ideas?
Here is the code:
n <- 5 #number equities
s <- 20 #weighting steps
Ptf <- function(n, s){
m <- expand.grid(rep(list(seq(0, 100, s)), n))
subset(m, rowSums(m)==100)
}
Ptfs <- Ptf(n, s)
Result:
head(Ptfs)
Var1 Var2 Var3 Var4 Var5
6 100 0 0 0 0
11 80 20 0 0 0
16 60 40 0 0 0
21 40 60 0 0 0
26 20 80 0 0 0
31 0 100 0 0 0
> tail(Ptfs)
Var1 Var2 Var3 Var4 Var5
4321 0 0 0 40 60
5186 20 0 0 0 80
5191 0 20 0 0 80
5221 0 0 20 0 80
5401 0 0 0 20 80
6481 0 0 0 0 100
Increasing the number of equities n <- 10
delivers an error message:
> n <- 10 #number equities
> s <- 20 #weighting steps
>
...
>
> Ptfs <- Ptf(n, s)
Error: cannot allocate vector of size 461.3 Mb
Any help would be really appreciated!