mardi 24 mai 2016

R how to create random portfolios out of a predefined range?

I want to create random portfolios made up of 10 funds each. The portfolios need to fulfill this constraint:

  • Maximum vintage year of funds in portfolio - Minimum vintage year of fund in portfolio = 5 years (5 years is the investment period). So a portfolio with the first fund with vintage 1990 can only include funds from 1990-1995.

I have a dataset of 5,000 funds from 1982 to 2010 which looks (simplified) like this:

 Fund.ID Vintage Type Region.Focus Net.Multiple  Size
[1,] 4716  2003  2    US           1.02          Small
[2,] "2237 1998  25   Europe       0.03          Medium
[3,] 1110  1992  2    Europe       1.84          Medium
[4,] 12122 1997  25   Asia         2.04          Large 
[5,] 5721  2006  25   US           0.86          Mega
[6,] 730   1998  2    Europe       0.97          Small

So far I have solved the problem by using a loop function in which I can alternate the investment period from 1-6 years or change the number of funds.

##10 funds
for(i in 1982:2005)
{
  t <- i:(i+5)
  x <- subset(dataset, Vintage == i) ##instead of dataset, you can use any other subset
  pf <- subset(dataset, Vintage %in% t) ##change dataset here as well then
  m <- 1000 %*% nrow(x) %/% nrow(dataset) ##function simulates portfolios proportional to number of funds per year, change dataset here as well then
  n <- nrow(pf)
  weight <- rlongonly(m = m, n = n, k = 10, x.t = 1, x.l = 0.01, x.u = 0.3, max.iter = 1000)
  year.fof <- paste("fof.tvpi.10", i, sep = ".")
  assign(year.fof, weight %*% pf$Net.Multiple..X.)
  colnames(weight) <- pf[, 3]
  year.weight <- paste("weight.10", i, sep =".")
  assign(year.weight, weight)
}

I am using subset to distinguish style factors. rlongonly is a function from the rportfolios package and gives out a weight matrix, which I can then multiple e.g. with the Net Multiple vector.

However, is there a simpler way of doing this? Can I use an If() function or some kind of constraint vector/matrix? I want to include other constraints as well: A portfolio can only consist of funds with one size, a portfolio can only consist of funds from one region.




Aucun commentaire:

Enregistrer un commentaire