Reputation: 33
I have a portfolio with, lets say, 3 investment choices. I'd like to figure out how to allocate my portfolio (what percentage of each investment should I hold), based on a metric (or metrics).
What I'd like to do is generate a list that contained every possible allocation (in terms of percentage) for my portfolio. Ex (I'm only showing 5 options; there are a lot more possibilities):
Is there an R function that will create a list of all the possible allocations possible? I don't need it to be down to the 1% level; maybe the 5% level would be nice. Or, would I need to create my own function? And if so, how would I go about this? I'm "intermediate" on my R knowledge at this point. I can create my own functions, but I'm not super fast at it.
Thanks!
Upvotes: 2
Views: 61
Reputation: 145775
expand.grid
works well for generating all possible combinations of variables. We can generate all combinations of 2 variables, filter out the times they exceed 1, and then define the third:
inputs = seq(0, 100, 5)
result = expand.grid(asset_A = inputs, asset_B = inputs)
result = subset(result, asset_A + asset_B <= 100)
result$asset_C = 100 - (result$asset_A + result$asset_B)
Upvotes: 3