#Using the getSymbols command, download MONTHLY prices (from Yahoo) for Amazon, Inc (AMZN) from Jan 2019 to Feb 2021.
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
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
getSymbols(Symbols = "AMZN", from="2019-01-01",
to="2021-02-01", periodicity="monthly", src = "yahoo")
## '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.
## [1] "AMZN"
#Delete any NA values with the na.omit function:
r = na.omit(AMZN)
#Adjusted Prices
prices= AMZN$AMZN.Adjusted
names(prices) = c("AMZN")
#Calculate simple returns of AMZN stock
R = na.omit(prices / lag(prices,n=1) - 1)
r = diff(log(prices))
r = na.omit(r)
hist(R, main="Histogram of Amazon simple returns",
xlab="Simple returns", col="sky blue")
# INTERPRETATION OF THE HISTOGRAM THE Y AXIS REPRESENTS THE FREQUENCY, AND THE X AXIS IS THE RANGE OF THE MONTHLY RETURNS FOR THE AMAZON STOCK, THE HISTOGRAM IS SKEWED. THE MOST FREQUENT RETURNS OF AMAZON STOCK ARE BETWEEN -5% TO +5%. THE MEAN IS ARROUND 0.05 AND WE CAN SEE HOW THE RETURNS BETWEEN 15% AND 25% ARE NEGATIVE.
# H0: AMZN=1
# hα: AMZN>1
N <- nrow(r)
se_AMZN <- sd(r) / sqrt(N)
se_AMZN
## [1] 0.01577447
MANUAL CALCULATION t-tests #Mean of returns
mean_r_AMZN<- mean(r)
t_value_AMZN <- (mean_r_AMZN -0.01) / se_AMZN
t_value_AMZN
## [1] 1.012978
HYPOTHESIS TEST USING t.test function
ttest_AMZN<- t.test(as.numeric(r), nu=0.01, alternative = "greater")
ttest_AMZN
##
## One Sample t-test
##
## data: as.numeric(r)
## t = 1.6469, df = 23, p-value = 0.05659
## alternative hypothesis: true mean is greater than 0
## 95 percent confidence interval:
## -0.001056222 Inf
## sample estimates:
## mean of x
## 0.0259792
##PART II Donwload from Yahoo Finance monthly prices and returns for Alfa(ALFAA.MX) and the IPCyC Mexican market index (^MXX) from Jan, 2016 to date Jan 2021.
getSymbols(c("ALFAA.MX", "^MXX"), from="2016-01-01", to= "2021-01-01", periodicity="monthly", src="yahoo")
## [1] "ALFAA.MX" "^MXX"
# CC RETURNS FOR ALFA
r_ALFAA <- na.omit(diff(log(ALFAA.MX$ALFAA.MX.Adjusted)))
# IPC:
r_MXX <- na.omit(diff(log(MXX$MXX.Adjusted)))
# MERGE INTO SAME OBJECT:
all_rets <- merge(r_ALFAA, r_MXX)
# RENAME THE COLUMNS:
colnames(all_rets) <- c("ALFAA", "MXX")
#MARKET REGRESSION MODEL FOR ALFAA
reg <- lm(r_ALFAA ~ r_MXX)
sumreg<- summary(reg)
sumreg
##
## Call:
## lm(formula = r_ALFAA ~ r_MXX)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.32170 -0.04475 -0.01576 0.02992 0.34625
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.01464 0.01254 -1.167 0.248
## r_MXX 1.89300 0.27447 6.897 4.72e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.09632 on 57 degrees of freedom
## Multiple R-squared: 0.4549, Adjusted R-squared: 0.4453
## F-statistic: 47.57 on 1 and 57 DF, p-value: 4.725e-09
#(E[Ri]−Rf)=β1(RM−Rf)
# (r_ALFAA) -0.01464 + 1.89300 =r_MXX
# ALFAA is riskier than the market, since it has a B1 higher than 1.
# the returns are under the market, as we saw previously in the regression output bO is negative 0.014, aprox. -1.5% less than the market.
# This value corresponds to the systematic error.