pacman::p_load(pacman, tidyverse, tseries, psych, here, knitr, randtests)
A. Using the R function get.hist.quote, download the adjusted prices needed to calculate five years of monthly returns for the period ending December 31, 2015, and compute the log-prices.
#adjusted monthly price of best buy shares
x <- get.hist.quote(instrument = "BBY",
start = "2009-12-15",
end = "2015-12-31",
quote = "AdjClose",
compression = "m")
## '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.
## time series starts 2010-01-01
## time series ends 2015-12-01
bby_m_logp <- log(x)
bby_m_logp
## Adjusted
## 2010-01-01 3.326492
## 2010-02-01 3.322391
## 2010-03-01 3.475523
## 2010-04-01 3.543230
## 2010-05-01 3.471783
## 2010-06-01 3.250413
## 2010-07-01 3.273765
## 2010-08-01 3.178811
## 2010-09-01 3.441739
## 2010-10-01 3.493057
## 2010-11-01 3.490670
## 2010-12-01 3.270856
## 2011-01-01 3.266745
## 2011-02-01 3.213593
## 2011-03-01 3.097979
## 2011-04-01 3.181444
## 2011-05-01 3.203521
## 2011-06-01 3.192440
## 2011-07-01 3.067877
## 2011-08-01 2.992263
## 2011-09-01 2.898515
## 2011-10-01 3.023550
## 2011-11-01 3.055810
## 2011-12-01 2.908099
## 2012-01-01 2.939595
## 2012-02-01 2.970430
## 2012-03-01 2.928257
## 2012-04-01 2.857846
## 2012-05-01 2.700569
## 2012-06-01 2.813592
## 2012-07-01 2.674490
## 2012-08-01 2.654953
## 2012-09-01 2.624041
## 2012-10-01 2.510448
## 2012-11-01 2.361870
## 2012-12-01 2.260823
## 2013-01-01 2.591053
## 2013-02-01 2.600236
## 2013-03-01 2.900182
## 2013-04-01 3.067768
## 2013-05-01 3.126059
## 2013-06-01 3.118041
## 2013-07-01 3.220410
## 2013-08-01 3.399735
## 2013-09-01 3.440558
## 2013-10-01 3.577300
## 2013-11-01 3.523297
## 2013-12-01 3.506636
## 2014-01-01 2.983557
## 2014-02-01 3.106895
## 2014-03-01 3.098599
## 2014-04-01 3.086814
## 2014-05-01 3.151401
## 2014-06-01 3.265724
## 2014-07-01 3.229456
## 2014-08-01 3.299592
## 2014-09-01 3.351528
## 2014-10-01 3.373698
## 2014-11-01 3.517248
## 2014-12-01 3.506277
## 2015-01-01 3.409481
## 2015-02-01 3.488649
## 2015-03-01 3.480480
## 2015-04-01 3.399277
## 2015-05-01 3.400719
## 2015-06-01 3.338598
## 2015-07-01 3.335467
## 2015-08-01 3.464575
## 2015-09-01 3.474865
## 2015-10-01 3.423096
## 2015-11-01 3.325728
## 2015-12-01 3.282977
B. Compute five years of monthly log-returns corresponding to the log-prices obtained in Part (a).
bby_m_log <- as.vector(bby_m_logp)
bby_m_logret <- diff(bby_m_log)
bby_m_logret
## [1] -0.004101284 0.153132391 0.067706968 -0.071447111 -0.221369947
## [6] 0.023351666 -0.094953716 0.262927932 0.051317931 -0.002387151
## [11] -0.219813512 -0.004110834 -0.053152396 -0.115613854 0.083464750
## [16] 0.022077393 -0.011081369 -0.124562778 -0.075614286 -0.093748231
## [21] 0.125034928 0.032260596 -0.147711611 0.031496356 0.030834951
## [26] -0.042172777 -0.070411428 -0.157277080 0.113023303 -0.139101467
## [31] -0.019537418 -0.030912321 -0.113592462 -0.148578069 -0.101047109
## [36] 0.330229683 0.009182957 0.299946211 0.167586179 0.058290782
## [41] -0.008017745 0.102368387 0.179325819 0.040822161 0.136742075
## [46] -0.054002245 -0.016660854 -0.523079024 0.123337404 -0.008295942
## [51] -0.011784713 0.064586906 0.114322723 -0.036267979 0.070136023
## [56] 0.051935940 0.022170230 0.143550082 -0.010971145 -0.096796308
## [61] 0.079168082 -0.008169208 -0.081202749 0.001441795 -0.062120714
## [66] -0.003131345 0.129108469 0.010289970 -0.051769130 -0.097367623
## [71] -0.042751177
C. Using the function summary, calculate the summary statistics for the returns computed in Part (b).
summary(bby_m_logret)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -0.5230790 -0.0709293 -0.0041108 -0.0006129 0.0661469 0.3302297
D. Using the function acf, calculate the sample autocorrelation function for the log-returns computed in Part (b); use a maximum lag of 12.
print(acf(bby_m_logret, lag.max = 12))
##
## Autocorrelations of series 'bby_m_logret', by lag
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1.000 0.027 0.096 -0.113 -0.032 -0.110 0.083 0.152 -0.135 -0.043 -0.304
## 11 12
## -0.065 -0.134
For the data calculated in Exercise 11, use the Box–Ljung test to test the hypothesis.
Where ρ(⋅) denotes the autocorrelation of the stochastic process corresponding to the log-returns on Best Buy stock; see Example 3.3. Is this result consistent with the hypothesis that the log-prices of Best Buy stock follow RW3? Why or why not?
Box.test(bby_m_logret, lag = 12, type = "L")
##
## Box-Ljung test
##
## data: bby_m_logret
## X-squared = 16.597, df = 12, p-value = 0.1654
Yes, the result of the Box-Ljung test is consistent with the hypothesis that the log-price of Best Buy stock follow R3 because the p-value is greater than 0.05, meaning that there is a high probability that our result will be consistent with the null hypothesis.
For the log-prices of Best Buy stock calculated in Exercise 11, calculate the p-value of the runs test; see Example 3.5.
Based on your results, what do you conclude regarding the random walk hypothesis as applied to the log-prices of Best Buy stock?
runs.test(bby_m_logret)
##
## Runs Test
##
## data: bby_m_logret
## statistic = -1.4448, runs = 30, n1 = 35, n2 = 35, n = 70, p-value =
## 0.1485
## alternative hypothesis: nonrandomness
Based on the runs test results, I conclude that there is no evidence to reject the null hypothesis that random walk hypothesis can be applied to Best Buy stock prices.
For the log-returns on Best Buy stock calculated in Exercise 11, calculate the rescaled-range statistic H; see Example 3.6.
Based on this result, what do you conclude regarding random walk hypothesis as applied to the log-prices of Best Buy stock?
Significance_level <- c(0.1,0.05,0.025,0.005)
Critical_Value <- c(1.620, 1.747, 1.862, 2.098)
tibble(Significance_level,Critical_Value)
## # A tibble: 4 x 2
## Significance_level Critical_Value
## <dbl> <dbl>
## 1 0.1 1.62
## 2 0.05 1.75
## 3 0.025 1.86
## 4 0.005 2.10
H1 <- max(cumsum(bby_m_logret - mean(bby_m_logret)))
H1
## [1] 0.2783874
H2 <- min(cumsum((bby_m_logret - mean(bby_m_logret))))
H2
## [1] -1.044218
H <- (H1 - H2) / (sd(bby_m_logret)*(60^0.5))
H
## [1] 1.35818
When comparing the H value to that of the critical values we see that the p-value is greater than 0.10. Therefore there is not enough evidence to reject the null hypothesis that random walk applies to Best Buy share price.