#Get data for the Microsoft stock price GOOG/NASDAQ_MSFT and construct the log return yt = ∆logMSFTt.
library("Quandl")
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library("tseries")
msp<-Quandl("GOOG/NASDAQ_MSFT", type="zoo")
mspt<-msp$Close
lmspt<-log(mspt)
dlmspt <- diff(lmspt,1)
par(mfrow=c(1,2))
plot(lmspt,xlab="Time", ylab="lmspt")
plot(dlmspt,xlab="Time", ylab="dlmspt")

#a) Estimate an ARMA model: use ACF, PACF, Ljung-Box statistics, to #identify and then estimate an appropriate model for log return
Quandl.auth(" 3HQPzReHC1WfzuSG41gy")
## Warning: 'Quandl.auth' is deprecated.
## Use 'Quandl.api_key' instead.
## See help("Deprecated")
plot(acf(coredata(dlmspt), type='correlation', lag=24, plot=FALSE), ylab="", main=expression(paste("ACF for dlmspt")))

acf(coredata(dlmspt),type="partial",lag=24,ylab="",main="PACF-dlmspt")

# estimate model -

m1 <- arima(dlmspt,order=c(2,0,2))
m1
## 
## Call:
## arima(x = dlmspt, order = c(2, 0, 2))
## 
## Coefficients:
##           ar1      ar2     ma1     ma2  intercept
##       -0.5344  -0.2307  0.4640  0.1463      9e-04
## s.e.   0.1690   0.1444  0.1717  0.1494      2e-04
## 
## sigma^2 estimated as 0.0005335:  log likelihood = 17803.19,  aic = -35594.37
#Check model m1 for adequacy
tsdiag(m1,gof.lag=24)

BIC(m1)
## [1] -35552.77
AIC(m1)
## [1] -35594.37
# try another model

m2 <- arima(dlmspt,order=c(4,0,4))
## Warning in arima(dlmspt, order = c(4, 0, 4)): possible convergence problem:
## optim gave code = 1
m2
## 
## Call:
## arima(x = dlmspt, order = c(4, 0, 4))
## 
## Coefficients:
##          ar1      ar2      ar3     ar4      ma1     ma2     ma3      ma4
##       0.6334  -0.4169  -0.0674  0.2443  -0.7075  0.4161  0.0978  -0.3218
## s.e.  0.2263   0.2177   0.2020  0.1604   0.2246  0.2281  0.2080   0.1559
##       intercept
##           8e-04
## s.e.      2e-04
## 
## sigma^2 estimated as 0.0005307:  log likelihood = 17815.3,  aic = -35610.6
#Check model m1 for adequacy
tsdiag(m2,gof.lag=24)

BIC(m2)
## [1] -35541.27
AIC(m2)
## [1] -35610.6

m2 is better no correlated resdulas and the value of BIC is lower than BIC for m1.

#(b) Use ACF, PACF, Ljung-Box statistics for squared residuals from (a) to identify and then estimate an ARCH(m) model with normal innovations
#combines the ARCH model for volatility with ARMA model for mean