R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(quadprog)

returns_data <- matrix(c(
  0.01, 0.02, -0.01, 0.005,  
  0.015, 0.018, -0.012, 0.008,  
  0.012, 0.025, -0.015, 0.007,
  0.009, 0.021, -0.008, 0.006  
), nrow = 4, byrow = TRUE)

colnames(returns_data) <- c("0050", "0056", "006205", "00646")

cov_matrix <- cov(returns_data)
mean_returns <- colMeans(returns_data)

epsilon <- 1e-10
cov_matrix_reg <- cov_matrix + epsilon * diag(ncol(returns_data))

objective_function <- function(w, cov_matrix) {
  portfolio_risk <- sqrt(t(w) %*% cov_matrix %*% w)
  return(portfolio_risk)
}

constraints <- matrix(1, nrow = 1, ncol = ncol(returns_data))
bounds <- list(lower = rep(0, ncol(returns_data)), upper = rep(1, ncol(returns_data)))

optimal_weights <- solve.QP(Dmat = 2 * cov_matrix_reg, dvec = rep(0, ncol(cov_matrix)),
                            Amat = t(constraints), bvec = 1, meq = 1)$solution

GMVP_return <- sum(optimal_weights * mean_returns)
GMVP_std_dev <- sqrt(t(optimal_weights) %*% cov_matrix %*% optimal_weights)

print(optimal_weights)
## [1]  0.7336424  0.4253957  0.4679659 -0.6270040
print(GMVP_return)
## [1] 0.008030055
print(GMVP_std_dev)
##              [,1]
## [1,] 2.860615e-07
returns_data_monthly <- matrix(c(
  0.05, 0.06, -0.03, 0.04,  
  0.08, 0.07, -0.02, 0.05,
  0.06, 0.09, -0.04, 0.03,  
  0.07, 0.08, -0.03, 0.06  
), nrow = 4, byrow = TRUE)

colnames(returns_data_monthly) <- c("0050", "0056", "006205", "00646")

cov_matrix_monthly <- cov(returns_data_monthly)
mean_returns_monthly <- colMeans(returns_data_monthly)

epsilon <- 1e-10
cov_matrix_reg_monthly <- cov_matrix_monthly + epsilon * diag(ncol(returns_data_monthly))

optimal_weights_monthly <- solve.QP(Dmat = 2 * cov_matrix_reg_monthly, dvec = rep(0, ncol(cov_matrix_monthly)),
                                    Amat = t(constraints), bvec = 1, meq = 1)$solution

GMVP_return_monthly <- sum(optimal_weights_monthly * mean_returns_monthly)
GMVP_std_dev_monthly <- sqrt(t(optimal_weights_monthly) %*% cov_matrix_monthly %*% optimal_weights_monthly)

print(optimal_weights_monthly)
## [1] -4.999983e-01  4.999996e-01  9.999981e-01  5.624986e-07
print(GMVP_return_monthly)
## [1] -0.02499984
print(GMVP_std_dev_monthly)
##              [,1]
## [1,] 1.704729e-08
rf <- 0  
excess_returns <- returns_data_monthly - rf

mean_excess_returns <- colMeans(excess_returns)
cov_matrix_monthly <- cov(excess_returns)
tangency_weights <- solve(cov_matrix_monthly + epsilon * diag(ncol(cov_matrix_monthly)), mean_excess_returns) / 
  sum(solve(cov_matrix_monthly + epsilon * diag(ncol(cov_matrix_monthly)), mean_excess_returns))


tangency_return <- sum(tangency_weights * mean_excess_returns)

tangency_std_dev <- sqrt(tangency_weights %*% cov_matrix_monthly %*% tangency_weights)

print(tangency_weights)
##          0050          0056        006205         00646 
## -5.000028e-01  4.999994e-01  1.000006e+00 -2.362507e-06
print(tangency_return)
## [1] -0.02500051
print(tangency_std_dev)
##              [,1]
## [1,] 4.854012e-08

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.