0

In regards to this question Portfolio Optimisation under weight constraints,

I have a long unbalanced panel data of company returns as well as sector dummy variables taking values of 1 if that company belongs to this sector and 0 otherwise. The data looks like this:

CompanyID Date PR Sector1 Sector2
1 00003 2000-01-31 0.00947 0 1

2 00003 2000-07-31 0.0473 0 1

3 00003 2000-09-30 0.0399 0 1

I want to perform portfolio diversification with the aim to diversify my portfolio across the sectors and achieve maximum Sharpe ratio or achieve maximum diversification. My weights should sum to 1 and be non negative. I want to see the output as several combination between the assets and the sectors and to see the return, risk and Sharpe

Is there a way that I can do so?

I had the following code on R but It is not working:

 assets=unique(return_dat$CompanyID) asset_min=5 # Minimum amount of Assets to go in the portfolio asset_max=205 #max amount of Assets to go in the portfolio asset_step=5 asset_range=seq(from=asset_min,to=asset_max,by=asset_step) sectors=unique(return_dat$Sector) sector_min=2 #Minimum amount of Sectors to go in the portfolio sector_max=10 #max Minimum amount of Sectors to go in the portfolio sector_step=2 sector_range=seq(from=sector_min,to=sector_max,by=sector_step) # Initialize an empty data frame to store results Results <- data.frame() # Loop through the range of sectors and assets for (k in sector_range) { for (i in asset_range) { # Select random sectors sector_sample <- sample(sectors, k, replace = TRUE) asset_dat <- return_data %>% filter(Sector %in% sector_sample) # Select random assets within those sectors asset_sample <- sample(asset_dat$CompanyID, i, replace = TRUE) asset_dat <- asset_dat %>% filter(CompanyID %in% asset_sample) # Prepare asset returns data asset_returns <- asset_dat %>% select(CompanyID, Date, PR) %>% pivot_wider(names_from = CompanyID, values_from = PR) asset_returns <- xts(asset_returns[,-1], order.by = as.Date(asset_returns$Date)) # Calculate expected returns and covariance matrix exp_returns <- colMeans(asset_returns, na.rm = TRUE) * 12 # annualized returns cov_matrix <- cov(asset_returns, use = "complete.obs") * 12 # annualized covariance # Set up the optimization problem Dmat <- 2 * cov_matrix dvec <- rep(0, ncol(asset_returns)) Amat <- cbind(1, diag(ncol(asset_returns))) bvec <- c(1, rep(0, ncol(asset_returns))) meq <- 1 # Solve the quadratic programming problem solution <- solve.QP(Dmat, dvec, t(Amat), bvec, meq) weights <- solution$solution # Calculate portfolio metrics portfolio_return <- sum(exp_returns * weights) portfolio_risk <- sqrt(t(weights) %*% cov_matrix %*% weights) sharpe_ratio <- (portfolio_return - risk_free_rate) / portfolio_risk # Append the results Results <- rbind(Results, c(k, i, portfolio_return, portfolio_risk, sharpe_ratio)) } } colnames(Results) <- c("num_sectors", "num_assets", "return", "risk", "sharpe_ratio") print(Results) 

I get the following error:

Error in solve.QP(Dmat, dvec, t(Amat), bvec, meq) : Amat and dvec are incompatible!

2
  • change the codes in "Set up the optimization problem" section to: Amat <- cbind(rep(1, i), matrix(0, nrow = i, ncol = ncol(asset_returns))) and bvec <- c(1, rep(0, i)) so that Amat matches the length of bvec and the constant term is included for each combination of sectors and assets. try and let me know. Commented Dec 24, 2023 at 7:41
  • Hi @ManojKumar It still giving me the same error message. Error in solve.QP(Dmat, dvec, t(Amat), bvec, meq) : Amat and dvec are incompatible! Commented Dec 24, 2023 at 10:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.