# Step 1: Ensure you’ve got the necessary packages installed and loaded:

if (!require(quadprog)) install.packages("quadprog")
if (!require(PerformanceAnalytics)) install.packages("PerformanceAnalytics")
library(quadprog)
library(PerformanceAnalytics)


# Step 2: Prepare your data based on the selected assets from both strategies.

# For demonstration purposes, let's assume selected_returns1 and selected_returns2 are data frames of daily returns for the selected assets under Strategy 1 and Strategy 2, respectively.
# You should replace these placeholders with your own data as obtained from previous steps.

# Step 3: Implement Global Minimum Variance Portfolio without short selling:

calculate_GMVP <- function(returns) {
  Dmat <- 2 * cov(returns)
  dvec <- rep(0, ncol(returns))
  Amat <- rbind(c(1, rep(0, ncol(returns))), diag(ncol(returns)))
  bvec <- c(1, rep(0, ncol(returns)))
  meq <- 1
  solve.QP(Dmat, dvec, Amat, bvec, meq)$solution
}

calculate_GMVP <- function(returns) {
  # This is a placeholder, adjust according to the actual function definition
  # Ensure this function is capable of calculating and returning the GMVP weights based on returns
}

# Calculate for both strategies
weights_GMVP1 <- calculate_GMVP(selected_returns1)
weights_GMVP2 <- calculate_GMVP(selected_returns2)


# Step 4: Implement Tangency Portfolio with short selling:

calculate_tangency_portfolio <- function(returns) {
  avg_returns <- colMeans(returns)
  inv_cov_matrix <- solve(cov(returns))
  ones_vector <- rep(1, ncol(returns))
  
  portfolio_weights <- inv_cov_matrix %*% avg_returns
  portfolio_weights <- portfolio_weights / (ones_vector %*% inv_cov_matrix %*% avg_returns)
  return(portfolio_weights)
}

calculate_tangency_portfolio <- function(selected_returns) {
  avg_returns <- colMeans(selected_returns)
  cov_matrix <- cov(selected_returns)
  inv_cov_matrix <- solve(cov_matrix)
  ones_vector <- matrix(1, nrow = ncol(selected_returns), ncol = 1)
  
  numerator <- inv_cov_matrix %% avg_returns
  denominator <- t(ones_vector) %% inv_cov_matrix %*% avg_returns
  
  weights <- numerator / denominator
  
  return(weights)
}


# Calculate for both strategies
weights_TP1 <- calculate_tangency_portfolio(selected_returns1)
weights_TP2 <- calculate_tangency_portfolio(selected_returns2)


# Step 5: Output the weight allocations and optionally calculate portfolio return and risk measures.

cat("Global Minimum Variance Portfolio Weights (Strategy 1):", weights_GMVP1, "\n")
cat("Tangency Portfolio Weights (Strategy 1):", weights_TP1, "\n")
cat("Global Minimum Variance Portfolio Weights (Strategy 2):", weights_GMVP2, "\n")
cat("Tangency Portfolio Weights (Strategy 2):", weights_TP2, "\n")

# Optional: Calculate Portfolio Return and Risk for a demonstration
portfolio_return_GMVP1 <- sum(colMeans(selected_returns1) * weights_GMVP1)
portfolio_risk_GMVP1 <- sqrt(t(weights_GMVP1) %*% cov(selected_returns1) %*% weights_GMVP1)

cat("Portfolio Return (GMVP Strategy 1):", portfolio_return_GMVP1, "\n")
cat("Portfolio Risk (GMVP Strategy 1):", portfolio_risk_GMVP1, "\n")
# Calculate for both strategies
> weights_TP1 <- calculate_tangency_portfolio(selected_returns1)
Error in t(ones_vector)%%inv_cov_matrix : non-conformable arrays
>