#Calling the packages
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
library(ggplot2)
#Fetching the Stock prices data
quantmod::getSymbols("AMZN",from="2018-02-01",to="2023-02-01",src="yahoo")
## [1] "AMZN"
#Plotting the time sereies chart of the data
ggplot(data = AMZN, aes(x=index(AMZN), y=AMZN$AMZN.Close))+geom_line(colour="blue")+ labs(title = "Amazon stock Closing price 2018-2024", x="Date",y="Daily closing prices")
## Don't know how to automatically pick scale for object of type <xts/zoo>.
## Defaulting to continuous.

#computing daily close log returns
AMZN$log_returns<-diff(log(AMZN$AMZN.Close))
#Plotting Daily close log returns
ggplot(data = AMZN, aes(x=index(AMZN), y= log_returns))+ geom_line(color="red")+labs(title = "Amazon Stock daily log returns", x="Date", y="log returns")
## Warning: Removed 1 row containing missing values (`geom_line()`).

#commenting on the chart. As it shows in the plot, the log return of stock is not stationary. Approximately the mean is 0, but the volatility seems to be random.
#Question d i)
#presence of correlation tests
#carrying out Autocorrelation tests
acf(AMZN$AMZN.Close,lag.max = 10)

#PACF test
pacf(AMZN$AMZN.Close, lag.max = 10)

#commenting on both tests.An outcome closely around 2 means a very low level of autocorrelation. An outcome closer to 0 suggests a stronger positive autocorrelation, and an outcome closer to 4 suggests a stronger negative autocorrelation. In our case daily stock closing prices of Amazon are strongly correlated. For PACF Large spike at lag 1 that decreases on the latter lags.
#Calling required library
library(tseries)
# Question d ii)
#Asymmetry test
#jarque.bera.test
jarque.bera.test(AMZN$AMZN.Close)
##
## Jarque Bera Test
##
## data: AMZN$AMZN.Close
## X-squared = 139.57, df = 2, p-value < 2.2e-16
#Commenting. A p-value less than the significance level (usually set at 0.05) indicates that the null hypothesis should be rejected and the data does not follow a normal distribution. In our case we got p-value < 2.2e-16. Therefore the data does not follow normal distribution. Hence the stock for Amazon between the period is asymmetric
#question d iii)
#fat tail test
#Shapiro.test failed to run so we did not include it
#plotting a histogram to show kurtosis and skewness
hist(AMZN$AMZN.Close)

#commenting on the histogram. From the diagram, it is clear that the data is not normally distributed hence data is fat tailed
#question d iv)
#Augmented dicky fuller test
adf.test(AMZN$AMZN.Close)
##
## Augmented Dickey-Fuller Test
##
## data: AMZN$AMZN.Close
## Dickey-Fuller = -1.0624, Lag order = 10, p-value = 0.9287
## alternative hypothesis: stationary
#Commenting on it.The augmented Dickey–Fuller (ADF) statistic, used in the test, is Dickey-Fuller = -1.7618 a negative number. The more negative it is, the stronger the rejection of the hypothesis that there is a unit root at some level of confidence. It has a Lag order = 11, p-value = 0.6791 alternative hypothesis: stationary. Stationary series indicate absence of leverage effect.
Box.test(AMZN$AMZN.Close, lag=10, type = "Ljung-Box")
##
## Box-Ljung test
##
## data: AMZN$AMZN.Close
## X-squared = 12116, df = 10, p-value < 2.2e-16
#commenting on test . p-value < 2.2e-16 indicate absence of leverage effect.
#question d v)
#volatilty clustering
garch(AMZN$AMZN.Close, lags.multi= c(5,10,15))
##
## ***** ESTIMATION WITH ANALYTICAL GRADIENT *****
##
##
## I INITIAL X(I) D(I)
##
## 1 1.106003e+03 1.000e+00
## 2 5.000000e-02 1.000e+00
## 3 5.000000e-02 1.000e+00
##
## IT NF F RELDF PRELDF RELDX STPPAR D*STEP NPRELDF
## 0 1 9.351e+03
## 1 2 6.626e+03 2.91e-01 3.74e+00 4.5e-04 3.5e+04 1.0e+00 6.54e+04
## 2 4 6.616e+03 1.45e-03 1.34e-03 2.3e-05 2.0e+00 5.0e-02 2.56e+00
## 3 5 6.602e+03 2.21e-03 2.19e-03 3.6e-05 2.8e+00 1.0e-01 5.52e-03
## 4 6 6.599e+03 4.40e-04 7.93e-04 8.8e-05 1.5e+00 2.0e-01 1.00e-03
## 5 7 6.598e+03 8.34e-05 1.40e-04 6.5e-05 0.0e+00 1.7e-01 1.40e-04
## 6 10 6.598e+03 3.04e-07 3.04e-07 3.9e-08 7.7e+01 1.1e-04 1.18e-05
## 7 12 6.598e+03 6.02e-07 6.02e-07 7.9e-08 1.7e+01 2.3e-04 2.13e-05
## 8 14 6.598e+03 1.19e-07 1.19e-07 1.6e-08 3.1e+02 4.6e-05 2.07e-05
## 9 16 6.598e+03 2.37e-07 2.37e-07 3.1e-08 3.9e+01 9.2e-05 2.06e-05
## 10 19 6.598e+03 4.73e-09 4.73e-09 6.3e-10 7.6e+03 1.8e-06 2.04e-05
## 11 21 6.598e+03 9.46e-10 9.46e-10 1.3e-10 3.8e+04 3.7e-07 2.04e-05
## 12 23 6.598e+03 1.89e-09 1.89e-09 2.5e-10 4.8e+03 7.4e-07 2.04e-05
## 13 25 6.598e+03 3.78e-10 3.78e-10 5.0e-11 9.5e+04 1.5e-07 2.03e-05
## 14 27 6.598e+03 7.56e-10 7.56e-10 1.0e-10 1.2e+04 2.9e-07 2.03e-05
## 15 29 6.598e+03 1.51e-09 1.51e-09 2.0e-10 6.0e+03 5.9e-07 2.03e-05
## 16 31 6.598e+03 3.03e-10 3.03e-10 4.0e-11 1.2e+05 1.2e-07 2.03e-05
## 17 33 6.598e+03 6.05e-11 6.05e-11 8.1e-12 6.0e+05 2.4e-08 2.03e-05
## 18 35 6.598e+03 1.21e-10 1.21e-10 1.6e-11 7.4e+04 4.7e-08 2.03e-05
## 19 37 6.598e+03 2.42e-11 2.42e-11 3.2e-12 1.5e+06 9.4e-09 2.03e-05
## 20 39 6.598e+03 4.84e-12 4.84e-12 6.4e-13 7.4e+06 1.9e-09 2.03e-05
## 21 41 6.598e+03 9.68e-12 9.68e-12 1.3e-12 9.3e+05 3.8e-09 2.03e-05
## 22 43 6.598e+03 1.94e-12 1.94e-12 2.6e-13 1.9e+07 7.5e-10 2.03e-05
## 23 46 6.598e+03 -1.52e+06 3.87e-14 5.2e-15 9.3e+08 1.5e-11 2.03e-05
##
## ***** FALSE CONVERGENCE *****
##
## FUNCTION 6.598208e+03 RELDX 5.160e-15
## FUNC. EVALS 46 GRAD. EVALS 23
## PRELDF 3.872e-14 NPRELDF 2.035e-05
##
## I FINAL X(I) D(I) G(I)
##
## 1 1.106001e+03 1.000e+00 2.277e-03
## 2 9.273068e-01 1.000e+00 1.107e+01
## 3 8.728749e-12 1.000e+00 1.286e+01
##
## Call:
## garch(x = AMZN$AMZN.Close, lags.multi = c(5, 10, 15))
##
## Coefficient(s):
## a0 a1 b1
## 1.106e+03 9.273e-01 8.729e-12