1

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):

enter image description here

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!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • I might not be looking at this correctly. The matrix that is generated does not have rows (or columns) that each add up to 100. I think I'm following the concept of `combn` now, but I can't quite wrap my head around how to get this all to add up to 100. – R_novice_AJB Apr 06 '21 at 22:00

1 Answers1

3

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)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Ah, thank you. Would this also work if I were to, say, have 4 choices instead of 3, would I just do an embedded `expand.grid` inside `expand.grid`? – R_novice_AJB Apr 06 '21 at 22:02
  • You wouldn't need another expand.grid, you could just add it as a 3rd argument to the one that's there. It takes as many arguments as you need. – Gregor Thomas Apr 07 '21 at 02:27