1 / 22

Curso Teoría de Portafolios Taller: “CAPM con librería R PerfomanceAnalytics” N. Giraldo, EIO – UN Mayo 2009

Curso Teoría de Portafolios Taller: “CAPM con librería R PerfomanceAnalytics” N. Giraldo, EIO – UN Mayo 2009. ###### CARGAR LIBRERIAS library(PerformanceAnalytics) library(fAssets) library(fPortfolio). ###### LEER DATOS D = read.table("C:/R/Portafolios/8 acciones 2001 2003.prn",

wilona
Download Presentation

Curso Teoría de Portafolios Taller: “CAPM con librería R PerfomanceAnalytics” N. Giraldo, EIO – UN Mayo 2009

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Curso Teoría de PortafoliosTaller: “CAPM con librería R PerfomanceAnalytics”N. Giraldo, EIO – UNMayo 2009

  2. ###### CARGAR LIBRERIAS library(PerformanceAnalytics) library(fAssets) library(fPortfolio)

  3. ###### LEER DATOS D = read.table("C:/R/Portafolios/8 acciones 2001 2003.prn", header=TRUE,stringsAsFactors=FALSE) ###### PREPARAR DATOS x.Date = as.Date(D[,1],"%m/%d/%Y") x = zoo(D[,2:11], x.Date)

  4. # convierte columna 10 = cdt90 en tasa # efectiva diaria r = (1+x[2:731,10]/100)^(1/252)-1 # calcula los rendimientos de 8 acciones + # columna 9 = igbc Ret = Return.calculate(x[,1:9], method="compound")

  5. # el dato 121 del rendimiento de igbc hay # que corregirlo Ret[121,9] = ( as.numeric(Ret[120,9]) + as.numeric(Ret[122,9]) ) /2 # creamos variable igbc columna 9 igbc = Ret[,9]

  6. # calcular un beta por regresion lineal # calcular la prima de riesgo de las acciones Ret.ex = Ret - r # calcular la prima de riesgo del mercado igbc.ex = igbc - r

  7. # calcular un beta por regresion lineal beta1 = lm(Ret.ex[,1] ~ igbc.ex) summary(beta1) beta2 = lm(Ret.ex[,2] ~ igbc.ex) summary(beta2) beta3, beta4, etc, …

  8. # calcular betas con la libreria PerfomanceAnalytics # primero creamos vectores… beta = double(8) alfa = beta risk.prima = beta SR = beta # asignamos nombres a las entradas de los # vectores names(beta)=colnames(Ret[,1:8]) names(alfa)=colnames(Ret[,1:8]) names(risk.prima)=colnames(Ret[,1:8])

  9. # CAPM.RiskPremium is the premium returned # to the investor over the risk free asset # CAPM.RiskPremium = for(j in 1:8){ beta[j] = CAPM.beta(Ret[,j], igbc, r) alfa[j] = CAPM.alpha(Ret[,j], igbc, r) risk.prima[j] = CAPM.RiskPremium(Ret[,j], r) SR[j] = SharpeRatio(Ret[,j],r) }

  10. # la linea del mercado para las acciones SML es # se estima regresando la variable de la prima de riesgo # de las acciones contra beta: # CAPM.RiskPremium = a + b CAPM.beta + error sml = lm(risk.prima ~ beta) # Los parámetros a y b estimados están en el vector best = sml$coef

  11. (cbind(beta,alfa,risk.prima,SR)) beta alfa risk.prima SR argos 0.7379387 0.0004334918 0.0012893807 0.08886241 exito 0.2878942 -0.0007373311 -0.0004034207 -0.02569264 bbog 0.5615122 0.0011660906 0.0018173534 0.10399453 bancol 0.8194878 0.0009649049 0.0019153776 0.10243814 bav 0.9013191 -0.0004622399 0.0005831438 0.03728501 fabri 0.1818629 0.0014131070 0.0016240385 0.05485461 choco 0.5188485 0.0006082874 0.0012100673 0.09353360 noel 0.2469992 0.0008681223 0.0011546012 0.09703422

  12. SEGUNDA PARTE: Calcular los betas de varios portafolios

  13. # convertir la matriz Ret de 9 columnas en # un objeto “timeSeries” para poder aplicar # las funciones de la librería fPortfolio Ds = as.timeSeries(Ret[,1:8]) colnames(Ds) # especificaciones iniciales: Spec= portfolioSpec() setType(Spec) = "MV" Constraints = "LongOnly" setSolver(Spec) = "solveRquadprog"

  14. # 50 portafolios frontera fp = portfolioFrontier(Ds, Spec, Constraints) # pesos de los portafolios wp = getWeights(fp) # rendimiento esperado y promedio (matriz N x 2 ) fRet = getTargetReturn(fp) # volatilidad var y cvar (matriz N x 4: cov,sigma,cvar,var) fRisk = getTargetRisk(fp)

  15. # portafolio minima varianza fm = minvariancePortfolio(Ds, Spec, Constraints) (wm = getWeights(fm)) (um = getTargetReturn(fm)[1] ) (sm = getTargetRisk(fm)[2] ) # um = rendimiento medio min var = 0.00122 r = 0.0008 setRiskFreeRate(Spec) = r

  16. # portafolio tangente ft = tangencyPortfolio(Ds, Spec, Constraints) ( ut = getTargetReturn(ft)[1] ) ( st = getTargetRisk(ft)[2] ) (wt = getWeights(ft))

  17. # otro portafolio porcentajes iguales • we = rep(1/8,8) • ue = sum(we*getMean(fp)) • se = sqrt(t(we)%*%getCov(fp)%*%we)

  18. # escoger un portafolio eficiente: # meta de rendimiento igual a la media del IGBC uIgbc = mean(igbc) sIgbc = sd(igbc) names(sIgbc) = "igbc" setTargetReturn(Spec) = uIgbc fI = efficientPortfolio(Ds, Spec, Constraints) (wI = getWeights(fI)) (sI = getTargetRisk(fI)[2] ) names(sI) = "efi-igbc"

  19. # comparar los cuatro portafolios W = rbind(wm,wt,wI,we) (beta.p = W%*%beta) rownames(W) = c("minvar", "tangente", "igbc-eff", "iguales") W2 = cbind(W,beta.p) colnames(W2)[9]="betas" (t(W2))

  20. minvar tangente igbc-eff iguales argos 0.062260779 0.04276399 0.08010210 0.1250000 exito 0.206368850 0.00000000 0.09480452 0.1250000 bbog 0.075641357 0.28475348 0.13402230 0.1250000 bancol 0.004390349 0.23918113 0.06183030 0.1250000 bav 0.116837415 0.00000000 0.05609914 0.1250000 fabri 0.047827960 0.08543070 0.06007729 0.1250000 choco 0.166090165 0.10665685 0.18282807 0.1250000 noel 0.320583126 0.24121385 0.33023628 0.1250000 betas 0.430793727 0.51791082 0.45024565 0.5319828

More Related