Reputation: 1
Suppose that I have a data for U.S sector indices such as Non Durable, Durable, Manufacturing,..., etc and for the companies within each of these sectors. I am aiming to see if for the given sectors, does adding and increasing number of companies affect the portfolio risk adjusted return.
Is there a way in R that write a code to:
1- Obtain weights to be allocated for each of these sectors based on max decorrelation, 2- Create several portfolios based on investing for assets in these sectors. For example, a portfolio with 4 assets and measure its Sharpe Ratio, a portfolio with 8 assets,..., etc.
I have tried computing the max decorrelation weights for the sectors using RiskPortfolios package in the following code:
sigma =covEstimation(return_sectoral_data)
weights_max_decorrel<-round(optimalPortfolio(Sigma = sigma,
control = list(type = 'maxdec', constraint = 'lo')),2)
print(weights_max_decorrel)
Durable Manufacturing Energy Tech NonDurable
0.41 0.06 0.34 0.00 0.16
Tech Shops Health
0.00 0.03 0.00
But after getting these weights, I am not sure how can I create different portfolios based on investing in the companies of the portfolios with Non-zero weights.
Upvotes: 0
Views: 104
Reputation: 1
# Assuming you have the return_sectoral_data and weights_max_decorrel
library(RiskPortfolios)
library(PortfolioAnalytics)
# Set seed for reproducibility
set.seed(123)
# Number of portfolios to create with different numbers of assets
portfolio_sizes <- c(4, 8, 12)
# Initialize a data frame to store results
portfolio_results <- data.frame(Size = numeric(),
SharpeRatio = numeric(),
stringsAsFactors = FALSE)
# Loop through different portfolio sizes
for (size in portfolio_sizes) {
# Randomly select 'size' number of assets from each sector based on weights
selected_assets <- lapply(names(weights_max_decorrel),
function(sector) sample(names(return_sectoral_data[[sector]]), size, replace = FALSE))
# Flatten the list of selected assets
selected_assets <- unlist(selected_assets)
# Extract returns for the selected assets
selected_returns <- return_sectoral_data[selected_assets]
# Calculate portfolio returns and covariance matrix
portfolio_return <- colMeans(selected_returns)
portfolio_covariance <- cov(selected_returns)
# Calculate portfolio volatility
portfolio_volatility <- sqrt(t(weights_max_decorrel) %*% portfolio_covariance %*% weights_max_decorrel)
# Calculate Sharpe Ratio
portfolio_sharpe <- SharpeRatio(portfolio_return, portfolio_volatility)
# Store results in the data frame
portfolio_results <- rbind(portfolio_results, c(size, portfolio_sharpe))
}
# Print the results
print(portfolio_results)
Upvotes: 0