library("quantmod") 
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
library("ggfortify")
## Loading required package: ggplot2
activos<-c("^GSPC","IBM")
getSymbols(activos, from="1989-12-29", to="2018-12-31")
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## 
## WARNING: There have been significant changes to Yahoo Finance data.
## Please see the Warning section of '?getSymbols.yahoo' for details.
## 
## This message is shown once per session and may be disabled by setting
## options("getSymbols.yahoo.warning"=FALSE).
## [1] "GSPC" "IBM"
head(IBM)
##            IBM.Open IBM.High  IBM.Low IBM.Close IBM.Volume IBM.Adjusted
## 1989-12-29 23.56250 23.65625 23.50000  23.53125    6549600     12.33237
## 1990-01-02 23.68750 24.53125 23.62500  24.50000    7041600     12.84008
## 1990-01-03 24.68750 24.87500 24.59375  24.71875    9464000     12.95472
## 1990-01-04 24.75000 25.09375 24.71875  25.00000    9674800     13.10213
## 1990-01-05 24.96875 25.40625 24.87500  24.93750    7570000     13.06938
## 1990-01-08 24.81250 25.21875 24.81250  25.09375    4625200     13.15126

Precios de cierre mensual

ibm.mensual<-to.monthly(IBM)
head(ibm.mensual)
##          IBM.Open IBM.High  IBM.Low IBM.Close IBM.Volume IBM.Adjusted
## Dec 1989 23.56250 23.65625 23.50000  23.53125    6549600     12.33237
## Jan 1990 23.68750 25.40625 23.62500  24.65625  156385200     12.92197
## Feb 1990 24.65625 26.37500 24.37500  25.96875  133848800     13.77932
## Mar 1990 25.93750 27.34375 25.84375  26.53125  128318400     14.07778
## Apr 1990 26.28125 27.81250 26.25000  27.25000  110108400     14.45917
## May 1990 27.37500 30.53125 26.96875  30.00000  149241200     16.09662
sp500.mensual<-to.monthly(GSPC)
head(sp500.mensual)
##          GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted
## Dec 1989    350.68    353.41   350.67     353.40   145940000        353.40
## Jan 1990    353.40    360.59   319.83     329.08  3793250000        329.08
## Feb 1990    329.08    336.09   322.10     331.89  2961970000        331.89
## Mar 1990    331.89    344.49   331.08     339.94  3283280000        339.94
## Apr 1990    339.94    347.30   327.76     330.80  2801220000        330.80
## May 1990    330.80    362.26   330.80     361.23  3596680000        361.23

Precios ajustados mensuales

sp500.adj<-sp500.mensual[,6]
head(sp500.adj)
##          GSPC.Adjusted
## Dec 1989        353.40
## Jan 1990        329.08
## Feb 1990        331.89
## Mar 1990        339.94
## Apr 1990        330.80
## May 1990        361.23
ibm.adj<-ibm.mensual[,6]
head(ibm.adj)
##          IBM.Adjusted
## Dec 1989     12.33237
## Jan 1990     12.92197
## Feb 1990     13.77932
## Mar 1990     14.07778
## Apr 1990     14.45917
## May 1990     16.09662

Retornos mensuales

sp500.retorno.mensual<-monthlyReturn(sp500.adj,type="log")
head(sp500.retorno.mensual)
##          monthly.returns
## Dec 1989     0.000000000
## Jan 1990    -0.071299700
## Feb 1990     0.008502791
## Mar 1990     0.023965503
## Apr 1990    -0.027255210
## May 1990     0.088000978
ibm.retorno.mensual<-monthlyReturn(ibm.adj,type="log")
head(ibm.retorno.mensual)
##          monthly.returns
## Dec 1989      0.00000000
## Jan 1990      0.04670098
## Feb 1990      0.06424019
## Mar 1990      0.02142896
## Apr 1990      0.02673073
## May 1990      0.10728064
sp500.retorno.mensual<-sp500.retorno.mensual[-1,]
ibm.retorno.mensual<-ibm.retorno.mensual[-1,]
head(ibm.retorno.mensual)
##          monthly.returns
## Jan 1990      0.04670098
## Feb 1990      0.06424019
## Mar 1990      0.02142896
## Apr 1990      0.02673073
## May 1990      0.10728064
## Jun 1990     -0.02105238

Tasa libre de riesgo

Tasa de Treasury Bill a 3 meses.

getSymbols('TB3MS',src='FRED',from="1990-01-01", to="2018-12-31")
## [1] "TB3MS"
head(TB3MS)
##            TB3MS
## 1934-01-01  0.72
## 1934-02-01  0.62
## 1934-03-01  0.24
## 1934-04-01  0.15
## 1934-05-01  0.16
## 1934-06-01  0.15
TB3MS_2<-TB3MS["1990/2018"] 
head(TB3MS_2)
##            TB3MS
## 1990-01-01  7.64
## 1990-02-01  7.74
## 1990-03-01  7.90
## 1990-04-01  7.77
## 1990-05-01  7.74
## 1990-06-01  7.73
tail(TB3MS_2)
##            TB3MS
## 2018-07-01  1.96
## 2018-08-01  2.03
## 2018-09-01  2.13
## 2018-10-01  2.25
## 2018-11-01  2.33
## 2018-12-01  2.37
TB3MS_2<-to.monthly(TB3MS_2) 
TB3MS.m<- (1+TB3MS_2[,1]/100)^(1/12)-1 
head(TB3MS.m)
##          TB3MS_2.Open
## Jan 1990  0.006154037
## Feb 1990  0.006231899
## Mar 1990  0.006356340
## Apr 1990  0.006255245
## May 1990  0.006231899
## Jun 1990  0.006224116
autoplot(sp500.retorno.mensual)

autoplot(ibm.retorno.mensual)

autoplot(TB3MS.m)

dim(ibm.retorno.mensual)
## [1] 348   1
dim(sp500.retorno.mensual)
## [1] 348   1
dim(TB3MS.m)
## [1] 348   1

Cálculo del beta usando la covarinza

\[ \beta_i=\frac{Cov(R_i,R_m)}{var(R_m)} \]

CAPM1<-cov(ibm.retorno.mensual,sp500.retorno.mensual)/var(sp500.retorno.mensual) 
CAPM1
##                 monthly.returns
## monthly.returns        1.012649

Cálculo del beta usando la regresión

ri_rf <-ibm.retorno.mensual-TB3MS.m 
rm_rf <-sp500.retorno.mensual-TB3MS.m 
regresion<-lm(ri_rf ~ rm_rf) 
summary(regresion)
## 
## Call:
## lm(formula = ri_rf ~ rm_rf)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.314431 -0.031437 -0.000612  0.033169  0.240931 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.0006333  0.0034683   0.183    0.855    
## rm_rf       1.0078179  0.0834378  12.079   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.06449 on 346 degrees of freedom
## Multiple R-squared:  0.2966, Adjusted R-squared:  0.2946 
## F-statistic: 145.9 on 1 and 346 DF,  p-value: < 2.2e-16

Modelo de Mercado

\[ r_i=\alpha+\beta r_m \]

regresion2 <- lm(ibm.retorno.mensual ~ sp500.retorno.mensual)
summary(regresion2)
## 
## Call:
## lm(formula = ibm.retorno.mensual ~ sp500.retorno.mensual)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.314456 -0.031391 -0.000494  0.033228  0.240778 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           0.0005888  0.0034883   0.169    0.866    
## sp500.retorno.mensual 1.0126494  0.0834588  12.134   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.06449 on 346 degrees of freedom
## Multiple R-squared:  0.2985, Adjusted R-squared:  0.2965 
## F-statistic: 147.2 on 1 and 346 DF,  p-value: < 2.2e-16

Cálculo del beta y alfa usando PerformanceAnalytics

El paquete PerformanceAnalytics tiene la función CAPM.beta(Ra, Rb, Rf = 0) para calcular el valor beta y CAPM.alpha(Ra, Rb, Rf = 0) para calcular el valor alfa.

Donde:

library("PerformanceAnalytics")
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
CAPM.beta(ibm.retorno.mensual,sp500.retorno.mensual,TB3MS.m)
## [1] 1.007818
CAPM.alpha(ibm.retorno.mensual,sp500.retorno.mensual,TB3MS.m)
## [1] 0.0006333284