Advanced Statistics with R code 90% doen just check it and make a summary

profilearoojch
doc.docx

EXPLORATORY ANALYSIS

nobs 469.000000

NAs 0.000000

Minimum -0.166652

Maximum 0.164808

1. Quartile -0.022391

3. Quartile 0.029000

Mean 0.003843

Median 0.005614

Sum 1.802469

SE Mean 0.002079

LCL Mean -0.000242

UCL Mean 0.007929

Variance 0.002027

Stdev 0.045027

Skewness -0.040829

Kurtosis 1.569306

C:\Users\XXX\Desktop\PLOTS\Rplot.jpeg

Title:
 Jarque - Bera Normalality Test
Test Results:
  STATISTIC:
    X-squared: 49.4632
  P VALUE:
    Asymptotic p Value: 1.816e-11 

The distribution of log(returns) is not normal distribution, with symmetry close to 0 but kurtosis statistics is 1.5 which is far from normal distribution statistic. The Jarque-Bera test is significant (p-value < 0.05) indicating that hypothesis of normal distribution for log(returns) can be rejected.

The following plot is time series plot of weekly price of Google shares which shows increasing trend.

( Time series Plot of Google Prices )

Time plot of returns show that google stock returns shows periods of high volatility at various times. Most returns are between +/- 10%. Sample moments show that distribution of log returns is somewhat symmetric with very thin tails (kurtosis = 1.56). There is various times where the returns produce +/-15% with higher volatility also. Volatility doesn’t seem to die down quickly and it continues for a while after a volatile period.

C:\Users\XXX\Desktop\PLOTS\Rplot03.jpeg

To check null hypothesis of non stationarity:

Dickey Fuller Test is expressed as H0: φ1=1 vs Ha: φ1<>1 where the null hypothesis indicates unit-root non-stationarity.

The Dickey-Fuller test shows that the earnings time series is unit-root stationary. The test p-values for lags 7 is less than 0.05, therefore the null hypothesis of non stationarity can be rejected (small p-values< 0.05).

Augmented Dickey-Fuller Test
Test Results:
  PARAMETER:
    Lag Order: 7
  STATISTIC:
    Dickey-Fuller: -7.1331
  P VALUE:
    0.01 

alternative hypothesis: stationary

ACF TESTS

The ACF plots below show the Google stock returns are not correlated indicating a constant mean model for rt. Both the squared returns time series and the absolute time series show large autocorrelations. We can conclude that the log returns process has a strong non-linear dependence.

C:\Users\XXX\Desktop\PLOTS\Rplot02.jpeg

Ljung Box testing for ARCH effects for Lags of 6-12-18 for squared returns and absolute returns

Box-Ljung test
data:  coredata(rets^2)
X-squared = 48.3339, df = 6, p-value = 1.013e-08
Box-Ljung test
data:  coredata(rets^2)
X-squared = 71.9976, df = 12, p-value = 1.352e-10
Box-Ljung test
data:  coredata(rets^2)
X-squared = 98.7945, df = 18, p-value = 3.68e-13

The Ljung Box tests confirm that the Ljung Box tests on the squared returns are autocorrelated. The analysis shows a strong ARCH effect. The squared returns are strongly correlated. The Ljung-Box tests on squared residuals are highly significant with p-values < 0.005, and autocorrelation plots shows large autocorrelations for the first 18 lags.

Plot of PACF: There is no correlation till lag 13 which shows there is no AR lag.

C:\Users\XXX\Desktop\PLOTS\Rplot04.jpeg

MODEL FITTING

*---------------------------------*
*          GARCH Model Fit        *
*---------------------------------*
Conditional Variance Dynamics 	
-----------------------------------
GARCH Model	: sGARCH(1,1)
Mean Model	: ARFIMA(0,0,0)
Distribution	: std 
Optimal Parameters
------------------------------------
        Estimate  Std. Error  t value Pr(>|t|)
mu      0.004432    0.001778   2.4924 0.012689
omega   0.000103    0.000053   1.9574 0.050295
alpha1  0.094169    0.032716   2.8784 0.003997
beta1   0.856326    0.044635  19.1850 0.000000
shape   6.797338    2.000925   3.3971 0.000681

To fit an ARMA(0,0)-GARCH(2,1) model for the log returns using a normal distribution for the error terms:

Fitted Model:

with 7 degrees of freedom

Residual Analysis

Q-Statistics on Standardized Residuals
------------------------------------
              statistic p-value
Lag[1]           0.0339  0.8539
Lag[p+q+1][1]    0.0339  0.8539
Lag[p+q+5][5]    2.4766  0.7800
d.o.f=0
H0 : No serial correlation
Q-Statistics on Standardized Squared Residuals
------------------------------------
              statistic p-value
Lag[1]           0.1274  0.7212
Lag[p+q+1][3]    1.4245  0.2327
Lag[p+q+5][7]    3.4346  0.6333
d.o.f=2

Above output shows there is no evidence of autocorrelation in residuals. They behave like white noise. Also, there is no evidence of serial correlation in squared residuals. They also behave like white noise.

Adjusted Pearson Goodness-of-Fit Test:
------------------------------------
  group statistic p-value(g-1)
1    20      8465            0
2    30     13148            0
3    40     17834            0
4    50     22521            0

Test for Goodness of fit. Normal Distribution rejected.

To fit ARMA(0,0)-eGARCH(1,1) model with Gaussian distribution:

Conditional Variance Dynamics 	
-----------------------------------
GARCH Model	: eGARCH(1,1)
Mean Model	: ARFIMA(0,0,0)
Distribution	: norm 
Optimal Parameters
------------------------------------
        Estimate  Std. Error  t value Pr(>|t|)
mu      0.004814    0.001837   2.6205 0.008780
omega  -0.427600    0.165861  -2.5781 0.009936
alpha1 -0.086690    0.033886  -2.5583 0.010518
beta1   0.931657    0.026273  35.4604 0.000000
gamma1  0.177187    0.048248   3.6724 0.000240

Fitted Model is:

Residual Analysis:

Q-Statistics on Standardized Residuals
------------------------------------
              statistic p-value
Lag[1]          0.03655  0.8484
Lag[p+q+1][1]   0.03655  0.8484
Lag[p+q+5][5]   2.73813  0.7403
d.o.f=0
H0 : No serial correlation
Q-Statistics on Standardized Squared Residuals
------------------------------------
              statistic p-value
Lag[1]           0.2255  0.6348
Lag[p+q+1][3]    2.6812  0.1015
Lag[p+q+5][7]    4.1362  0.5300
d.o.f=2
ARCH LM Tests
------------------------------------
             Statistic DoF P-Value
ARCH Lag[2]      2.224   2  0.3290
ARCH Lag[5]      4.682   5  0.4559
ARCH Lag[10]     7.053  10  0.7204

Above output shows there is no evidence of autocorrelation in residuals. They behave like white noise. Also, there is no evidence of serial correlation in squared residuals. They also behave like white noise.

Adjusted Pearson Goodness-of-Fit Test:
------------------------------------
  group statistic p-value(g-1)
1    20     26.65       0.1131
2    30     32.04       0.3179
3    40     43.67       0.2798
4    50     47.74       0.5243

Above output shows that error terms are normally distributed as all the P values are greater than 0.05.

To fit ARMA(0,0)-tGARCH(1,1) model with Gaussian distribution:

Conditional Variance Dynamics 	
-----------------------------------
GARCH Model	: gjrGARCH(2,1)
Mean Model	: ARFIMA(0,0,0)
Distribution	: norm 
Optimal Parameters
------------------------------------
        Estimate  Std. Error   t value Pr(>|t|)
mu      0.004928    0.001860  2.649926 0.008051
omega   0.000120    0.000053  2.257334 0.023987
alpha1  0.000000    0.000877  0.000011 0.999991
alpha2  0.072854    0.037335  1.951388 0.051011
beta1   0.828559    0.053752 15.414339 0.000000
gamma1  0.397469    0.144242  2.755564 0.005859
gamma2 -0.315459    0.131445 -2.399922 0.016399

Fitted Model is:

Residual Analysis:

Q-Statistics on Standardized Residuals
------------------------------------
              statistic p-value
Lag[1]           0.2311  0.6307
Lag[p+q+1][1]    0.2311  0.6307
Lag[p+q+5][5]    2.9412  0.7090
d.o.f=0
H0 : No serial correlation
Q-Statistics on Standardized Squared Residuals
------------------------------------
              statistic p-value
Lag[1]            2.137 0.14380
Lag[p+q+1][4]     4.191 0.04063
Lag[p+q+5][8]     4.680 0.45620
d.o.f=3
ARCH LM Tests
------------------------------------
             Statistic DoF P-Value
ARCH Lag[2]      2.461   2  0.2921
ARCH Lag[5]      4.904   5  0.4277
ARCH Lag[10]     7.140  10  0.7122

Above output shows there is no evidence of autocorrelation in residuals. They behave like white noise. Also, there is no evidence of serial correlation in squared residuals. They also behave like white noise.

Adjusted Pearson Goodness-of-Fit Test:
------------------------------------
  group statistic p-value(g-1)
1    20     20.00       0.3947
2    30     32.43       0.3014
3    40     30.87       0.8203
4    50     54.56       0.2714

Above output shows that error terms are normally distributed as all the P values are greater than 0.05.

BEST FIT FOR THE MODEL:

               garch11  egarch11 gjrgarch11
Akaike       -3.479720 -3.455538  -3.474405
Bayes        -3.435471 -3.411288  -3.412455
Shibata      -3.479944 -3.455762  -3.474841
Hannan-Quinn -3.462310 -3.438127  -3.450030

From above output it can be said that AR(0)-EGARCH(1,1) have the (smallest) values for all best fit criterion and hence can be considered as best model.

Forecast Analysis:

RE-FIT MODELS LEAVING 100 OUT-OF-SAMPLE OBSERVATIONS FOR FORECAST and EVALUATE STATISTICS for 5 Step Ahead Forecast.

    garch11     egarch11     tgarch21    
MSE 0.000950199 0.0009498531 0.0009499473
MAE 0.02257449  0.0225407    0.02255243  
DAC 0.6         0.6          0.6  

Since MSE is smallest for eGARCH model. We should consider this model for forecasting.

CODE:

#Tomas Georgakopoulos CSC 425 Final Project

setwd("C:/Course/CSC425")

# Analysis of Google weekly returns from

library(forecast)

library(TSA)

# import data in R and compute log returns

# import libraries for TS analysis

library(zoo)

library(tseries)

myd= read.table('Weekly-GOOG-TSDATA.csv', header=T, sep=',')

pricets = zoo(myd$price, as.Date(as.character(myd$date), format=c("%m/%d/%Y")))

#log return time series

rets = log(pricets/lag(pricets, -1))

# strip off the dates and just create a simple numeric object

ret = coredata(rets);

#compute statistics

library(fBasics)

basicStats(rets)

#HISTOGRAM

par(mfcol=c(1,2))

hist(rets, xlab="Weekly Returns", prob=TRUE, main="Histogram")

#Add approximating normal density curve

xfit<-seq(min(rets,na.rm=TRUE),max(rets,na.rm=TRUE),length=40)

yfit<-dnorm(xfit,mean=mean(rets,na.rm=TRUE),sd=sd(rets,na.rm=TRUE))

lines(xfit, yfit, col="blue", lwd=2)

#CREATE NORMAL PROBABILITY PLOT

qqnorm(rets)

qqline(rets, col = 'red', lwd=2)

# creates time plot of log returns

par(mfrow=c(1,1))

plot(rets)

#Perform Jarque-Bera normality test.

normalTest(rets,method=c('jb'))

#SKEWNESS TEST

skew_test=skewness(rets)/sqrt(6/length(rets))

skew_test

print("P-value = ")

2*(1-pnorm(abs(skew_test)))

#FAT-TAIL TEST

k_stat = kurtosis(rets)/sqrt(24/length(rets))

print("Kurtosis test statistic")

k_stat

print("P-value = ")

2*(1-pnorm(abs(k_stat)))

#COMPUTE DICKEY-FULLER TEST

library(fUnitRoots)

adfTest(rets, lags=7, type=c("c"))

# Computes Ljung-Box test on squared returns to test non-linear independence at lag 6 and 12

Box.test(coredata(rets^2),lag=6,type='Ljung')

Box.test(coredata(rets^2),lag=12,type='Ljung')

Box.test(coredata(rets^2),lag=18,type='Ljung')

# Computes Ljung-Box test on absolute returns to test non-linear independence at lag 6 and 12

Box.test(abs(coredata(rets)),lag=6,type='Ljung')

Box.test(abs(coredata(rets)),lag=12,type='Ljung')

Box.test(abs(coredata(rets)),lag=18,type='Ljung')

# Plots ACF function of vector data

par(mfrow=c(3,1))

acf(ret)

# Plot ACF of squared returns to check for ARCH effect

acf(ret^2)

# Plot ACF of absolute returns to check for ARCH effect

acf(abs(ret))

#plot returns, square returns and abs(returns)

# Plots ACF function of vector data

par(mfrow=c(3,1))

plot(rets, type='l')

# Plot ACF of squared returns to check for ARCH effect

plot(rets^2,type='l')

# Plot ACF of absolute returns to check for ARCH effect

plot(abs(rets),type='l')

par(mfrow=c(1,1))

# plots PACF of squared returns to identify order of AR model

pacf(coredata(rets),lag=30)

#GARCH Models

library(rugarch)

#Fit AR(0,0)-GARCH(1,1) model

garch11.spec=ugarchspec(variance.model=list(garchOrder=c(1,1)), mean.model=list(armaOrder=c(0,0)),distribution.model = "std")

#estimate model

garch11.fit=ugarchfit(spec=garch11.spec, data=rets)

garch11.fit

#Fit AR(0,0)-eGARCH(1,1) model with Gaussian distribution

egarch11.spec=ugarchspec(variance.model=list(model = "eGARCH",garchOrder=c(1,1)), mean.model=list(armaOrder=c(0,0)))

#estimate model

egarch11.fit=ugarchfit(spec=egarch11.spec, data=rets)

egarch11.fit

#Fit AR(0,0)-TGARCH(1,1) model with norm-distribution

gjrgarch11.spec=ugarchspec(variance.model=list(model = "gjrGARCH",garchOrder=c(2,1)), mean.model=list(armaOrder=c(0,0)), distribution.model = "norm")

#estimate model

gjrgarch11.fit=ugarchfit(spec=gjrgarch11.spec, data=rets)

gjrgarch11.fit

# compare information criteria

model.list = list(garch11 = garch11.fit,

egarch11 = egarch11.fit,

gjrgarch11 = gjrgarch11.fit)

info.mat = sapply(model.list, infocriteria)

rownames(info.mat) = rownames(infocriteria(garch11.fit))

info.mat

# RE-FIT MODELS LEAVING 100 OUT-OF-SAMPLE OBSERVATIONS FOR FORECAST

# EVALUATION STATISTICS

garch11.fit = ugarchfit(spec=garch11.spec, data=rets, out.sample=100)

egarch11.fit = ugarchfit(egarch11.spec, data=rets, out.sample=100)

tgarch11.fit = ugarchfit(spec=gjrgarch11.spec, data=rets, out.sample=100)

garch11.fcst = ugarchforecast(garch11.fit, n.roll=100, n.ahead=1)

egarch11.fcst = ugarchforecast(egarch11.fit, n.roll=100, n.ahead=1)

tgarch11.fcst = ugarchforecast(tgarch11.fit, n.roll=100, n.ahead=1)

fcst.list = list(garch11=garch11.fcst,

egarch11=egarch11.fcst,

tgarch21=tgarch11.fcst)

fpm.mat = sapply(fcst.list, fpm)

fpm.mat