Skip to main content
re-etiquetado en este caso se emplea multiplicadores de lagrange a un problema de optimización de programación lineal mediante algebra lineal y derivadas parciales para facilitar su resolución en una función mediante la inversa de una matriz de covarianza
Enlace
específico el problemo del inverso de la matriz
Origen Enlace

Pero me dacalculando el inverso de la matriz obtengo:

Pero me da

Pero calculando el inverso de la matriz obtengo:

Origen Enlace

Solve(covMat) retorna que system is computationally singular: reciprocal condition number

Quiero obtener la cartera de variación mínima. Los rendimientos esperados mu son:

> mu
       .SXQR        .SXTR        .SXNR        .SXMR        .SXAR        .SX3R 
 0.100496686  0.068652744  0.065081570  0.013155820  0.086947540  0.103143934 
       .SX6R        .SXFR        .SXOR        .SXDR        .SX4R        .SXRR 
 0.054990629  0.088484620  0.085435533  0.068080455  0.098365460  0.023932074 
       .SXER        .SXKR        .SX7R        .SX8R        .SXIR        .SXPR 
 0.037525561 -0.000400454  0.024776148  0.007051037  0.042215791  0.116013074

y la matriz de covarianza covMat esta :

> covMat
           .SXQR      .SXTR      .SXNR      .SXMR      .SXAR      .SX3R      .SX6R
.SXQR 0.03345763 0.03498086 0.04185753 0.03245136 0.03497776 0.02324828 0.02081399
.SXTR 0.03498086 0.04974472 0.04619830 0.04159817 0.04420657 0.02401824 0.02689768
.SXNR 0.04185753 0.04619830 0.06097311 0.04537355 0.05011720 0.02686316 0.03057380
.SXMR 0.03245136 0.04159817 0.04537355 0.04287405 0.04017447 0.02191462 0.02405812
.SXAR 0.03497776 0.04420657 0.05011720 0.04017447 0.05465295 0.02184147 0.02625680
.SX3R 0.02324828 0.02401824 0.02686316 0.02191462 0.02184147 0.02318865 0.01733667
.SX6R 0.02081399 0.02689768 0.03057380 0.02405812 0.02625680 0.01733667 0.03490965
.SXFR 0.04086303 0.05423479 0.05599539 0.04781758 0.04785298 0.03177379 0.03971822
.SXOR 0.03468033 0.04174388 0.04790487 0.03778244 0.03958789 0.02265676 0.03305845
.SXDR 0.01823918 0.02550628 0.02203189 0.02453928 0.01603731 0.01815483 0.01477641
.SX4R 0.03342966 0.03293408 0.04631396 0.03373207 0.03772380 0.02582638 0.02640921
.SXRR 0.03033003 0.03421856 0.04020604 0.03334523 0.03561357 0.01863272 0.02276777
.SXER 0.02051229 0.01525291 0.02833009 0.01743750 0.01769105 0.01464734 0.01775119
.SXKR 0.02694061 0.03196615 0.03950701 0.03682066 0.03574271 0.01394396 0.02498871
.SX7R 0.03635913 0.04453817 0.04871591 0.03789661 0.03882093 0.02804067 0.03282608
.SX8R 0.03991513 0.04959444 0.05847244 0.04934120 0.04838590 0.02522826 0.02852557
.SXIR 0.03508348 0.04831080 0.04905516 0.04555107 0.04046836 0.02949463 0.03263901
.SXPR 0.04456718 0.03223030 0.06505796 0.03031465 0.04557004 0.02648925 0.03399364
           .SXFR      .SXOR       .SXDR      .SX4R      .SXRR      .SXER      .SXKR
.SXQR 0.04086303 0.03468033 0.018239181 0.03342966 0.03033003 0.02051229 0.02694061
.SXTR 0.05423479 0.04174388 0.025506282 0.03293408 0.03421856 0.01525291 0.03196615
.SXNR 0.05599539 0.04790487 0.022031890 0.04631396 0.04020604 0.02833009 0.03950701
.SXMR 0.04781758 0.03778244 0.024539282 0.03373207 0.03334523 0.01743750 0.03682066
.SXAR 0.04785298 0.03958789 0.016037311 0.03772380 0.03561357 0.01769105 0.03574271
.SX3R 0.03177379 0.02265676 0.018154833 0.02582638 0.01863272 0.01464734 0.01394396
.SX6R 0.03971822 0.03305845 0.014776414 0.02640921 0.02276777 0.01775119 0.02498871
.SXFR 0.07060256 0.05433736 0.029870028 0.04370332 0.04173824 0.02543286 0.03772980
.SXOR 0.05433736 0.05018950 0.019754192 0.03724791 0.03814745 0.02642396 0.03293653
.SXDR 0.02987003 0.01975419 0.024627383 0.01758262 0.01499347 0.01045468 0.01481692
.SX4R 0.04370332 0.03724791 0.017582623 0.04093423 0.03116615 0.02429916 0.02793648
.SXRR 0.04173824 0.03814745 0.014993470 0.03116615 0.03504357 0.01910385 0.03251937
.SXER 0.02543286 0.02642396 0.010454676 0.02429916 0.01910385 0.02722964 0.01322536
.SXKR 0.03772980 0.03293653 0.014816920 0.02793648 0.03251937 0.01322536 0.04505911
.SX7R 0.05938230 0.05169770 0.023760635 0.04122183 0.03709132 0.02779035 0.02632013
.SX8R 0.05506206 0.04556889 0.027748962 0.04155255 0.03723654 0.02359730 0.04231239
.SXIR 0.05991709 0.04424346 0.033142798 0.03888777 0.03233359 0.02232765 0.03104647
.SXPR 0.04889303 0.05858437 0.006954027 0.05553596 0.04425855 0.05085459 0.02653357
           .SX7R      .SX8R      .SXIR       .SXPR
.SXQR 0.03635913 0.03991513 0.03508348 0.044567182
.SXTR 0.04453817 0.04959444 0.04831080 0.032230303
.SXNR 0.04871591 0.05847244 0.04905516 0.065057961
.SXMR 0.03789661 0.04934120 0.04555107 0.030314650
.SXAR 0.03882093 0.04838590 0.04046836 0.045570043
.SX3R 0.02804067 0.02522826 0.02949463 0.026489254
.SX6R 0.03282608 0.02852557 0.03263901 0.033993645
.SXFR 0.05938230 0.05506206 0.05991709 0.048893031
.SXOR 0.05169770 0.04556889 0.04424346 0.058584372
.SXDR 0.02376063 0.02774896 0.03314280 0.006954027
.SX4R 0.04122183 0.04155255 0.03888777 0.055535956
.SXRR 0.03709132 0.03723654 0.03233359 0.044258552
.SXER 0.02779035 0.02359730 0.02232765 0.050854589
.SXKR 0.02632013 0.04231239 0.03104647 0.026533566
.SX7R 0.06443470 0.04622553 0.04997693 0.062958984
.SX8R 0.04622553 0.06557348 0.05449832 0.047301440
.SXIR 0.04997693 0.05449832 0.06063113 0.032824422
.SXPR 0.06295898 0.04730144 0.03282442 0.143337184

He visto que hay este articulo que da la ecuación y el código:

introducir la descripción de la imagen aquí

Entonces utilizo el código siguiente.

assetSymbols <- colnames(yearly_return)

mu <- colMeans(yearly_return,na.rm = TRUE) # expected returns
covMat <- cov(yearly_return) # covariance matrix
corMat <- cor(yearly_return) # correlation matrix


## Minimum Variance Portfolio function ####
getMinVariancePortfolio <- function(mu,covMat,assetSymbols) {
  U <- rep(1, length(mu)) # vector of 1
  O <- solve(covMat)     # inverse of covariance matrix
  w <- O%*%U /as.numeric(t(U)%*%O%*% U)
  Risk <- sqrt(t(w) %*% covMat %*% w)
  ExpReturn <- t(w) %*% mu
  Weights <- `names<-`(round(w, 5), assetSymbols)
  list(Weights = t(Weights),
       ExpReturn = round(as.numeric(ExpReturn), 5),
       Risk = round(as.numeric(Risk), 5))
}

Pero me da

 Error in solve.default(covMat) : 
  system is computationally singular: reciprocal condition number = 1.06734e-19