'I'm trying to build a Markowits portfolio in R and I'm having some issues with my codes
I am trying to build and optimize two kind of portfolios:
- Markowitz portfolio;
- Global Minimum-Variance Portfolio.
But some errors appears, when trying to run code. I got this code from.
There is my code:
library(xts) # to manipulate time series of stock data
library(quantmod) # to download stock data
library(PerformanceAnalytics) # to compute performance measures
library(dplyr)
#install.packages("quantmod")
#install.packages("PerformanceAnalytics")
# download data from YahooFinance
stock_namelist <- c("AAPL", "AMD", "ADI", "ABBV", "AEZS", "A", "APD", "AA","CF")
prices <- xts()
for (i in 1:length(stock_namelist)) {
tmp <- Ad(getSymbols(stock_namelist[i], from = "1990-01-01", to = "2022-05-06", auto.assign = FALSE))
tmp <- na.approx(tmp, na.rm = FALSE) # interpolate NAs
prices <- cbind(prices, tmp)
}
colnames(prices) <- stock_namelist
tclass(prices) <- "Date"
str(prices)
head(prices)
tail(prices)
# compute log-returns and linear returns
X_log <- diff(log(prices))[-1]
X_lin <- (prices/lag(prices) - 1)[-1]
# or alternatively...
X_log <- CalculateReturns(prices, "log")[-1]
X_lin <- CalculateReturns(prices)[-1]
plot(prices/rep(prices[1, ], each = nrow(prices)), col = rainbow10equal, legend.loc = "topleft",
main = "Normalized prices")
T_trn <- round(0.7*T) # 70% of data
X_log_trn <- X_log[1:T_trn, ]
X_log_tst <- X_log[(T_trn+1):T, ]
X_lin_trn <- X_lin[1:T_trn, ]
X_lin_tst <- X_lin[(T_trn+1):T, ]
mu <- colMeans(X_log_trn)
Sigma <- cov(X_log_trn)
library(CVXR) # interface for convex optimization solvers
# define portfolio formulations
portolioMarkowitz <- function(mu, Sigma, lmd = 0.5) {
w <- Variable(nrow(Sigma))
prob <- Problem(Maximize(t(mu)%*% w - lmd*quad_form(w, Sigma)),
constraints = list(w >= 0, sum(w) == 1))
result <- solve(prob)
return(as.vector(result$getValue(w)))
}
portolioGMVP <- function(Sigma) {
w <- Variable(nrow(Sigma))
prob <- Problem(Minimize(quad_form(w, Sigma)),
constraints = list(w >= 0, sum(w) == 1))
result <- solve(prob)
return(as.vector(result$getValue(w)))
}
# compute portfolios
w_Markowitz <- portolioMarkowitz(mu, Sigma)
w_GMVP <- portolioGMVP(Sigma)
When I am trying to run this code:
w_Markowitz <- portolioMarkowitz(mu, Sigma)
The following error comes up:
Error in if (!is_hermitian(object)) return(FALSE) :
missing value where TRUE/FALSE needed
And running this line of code:
w_GMVP <- portolioGMVP(Sigma)
Another error appears:
Error in if (!is_hermitian(object)) return(FALSE) :
missing value where TRUE/FALSE needed
How can i solve those errors in my code?
Here is my session info:
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)
Matrix products: default
locale:
[1] LC_COLLATE=Russian_Kazakhstan.1251 LC_CTYPE=Russian_Kazakhstan.1251 LC_MONETARY=Russian_Kazakhstan.1251
[4] LC_NUMERIC=C LC_TIME=Russian_Kazakhstan.1251
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] dplyr_1.0.7 CVXR_1.0-1 devtools_2.4.3 usethis_2.1.5
[5] PerformanceAnalytics_2.0.4 quantmod_0.4.20 TTR_0.24.2 xts_0.12.1
[9] zoo_1.8-9
loaded via a namespace (and not attached):
[1] tinytex_0.35 tidyselect_1.1.1 xfun_0.28 slam_0.1-50 remotes_2.4.2 purrr_0.3.4 lattice_0.20-41
[8] vctrs_0.3.8 generics_0.1.1 testthat_3.1.4 gmp_0.6-5 utf8_1.2.2 rlang_1.0.2 pkgbuild_1.3.1
[15] pillar_1.6.4 DBI_1.1.1 glue_1.6.2 withr_2.4.3 Rmpfr_0.8-7 bit64_4.0.5 sessioninfo_1.2.2
[22] lifecycle_1.0.1 memoise_2.0.1 callr_3.7.0 fastmap_1.1.0 ps_1.6.0 curl_4.3.2 fansi_0.5.0
[29] Rcpp_1.0.7 cachem_1.0.6 desc_1.4.1 pkgload_1.2.4 fs_1.5.1 bit_4.0.4 brio_1.1.3
[36] processx_3.5.2 grid_4.0.5 rprojroot_2.0.2 Rglpk_0.6-4 quadprog_1.5-8 cli_3.3.0 tools_4.0.5
[43] magrittr_2.0.1 tibble_3.1.6 crayon_1.4.2 pkgconfig_2.0.3 ellipsis_0.3.2 Matrix_1.3-2 prettyunits_1.1.1
[50] assertthat_0.2.1 R6_2.5.1 compiler_4.0.5
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
