Date: The date of the stock price.
Open: The opening price of the stock on that day.
High: The highest price of the stock during the day.
Low: The lowest price of the stock during the day.
Close: The closing price of the stock on that day.
Adj Close: The adjusted closing price of the stock, accounting for splits and dividends.
Volume: The number of shares traded on that day.
# Load required libraries
library(quantmod)
library(rugarch)
library(tseries)
library(ggplot2)
library(gridExtra)
library(forecast)
# Load and prepare the data
data <- read.csv("GOOGL.csv")
data$Date <- as.Date(data$Date)
data <- data[order(data$Date), ]
# Calculate log returns
log_returns <- diff(log(data$Close))
data <- data[-1, ]
data$LogReturn <- log_returns
# === 1. Time Series Plots ===
## Closing Prices
ggplot(data, aes(x = Date, y = Close)) +
geom_line(color = "blue") +
labs(
title = "GOOGL Closing Prices Over Time",
x = "Date", y = "Closing Price (USD)",
caption = "Data source: GOOGL stock"
) +
theme_minimal()
## Log Returns
ggplot(data, aes(x = Date, y = LogReturn)) +
geom_line(color = "darkred") +
labs(
title = "Log Returns of GOOGL",
x = "Date", y = "Log Return",
caption = "Calculated from adjusted closing prices"
) +
theme_minimal()
# === 2. Histogram of Log Returns ===
ggplot(data, aes(x = LogReturn)) +
geom_histogram(bins = 50, fill = "darkgreen", color = "white") +
labs(
title = "Histogram of Log Returns",
x = "Log Return", y = "Frequency"
) +
theme_minimal()
# === 3. GARCH(1,1) Model Fitting ===
spec <- ugarchspec(
variance.model = list(model = "sGARCH", garchOrder = c(1, 1)),
mean.model = list(armaOrder = c(0, 0), include.mean = TRUE),
distribution.model = "norm"
)
fit <- ugarchfit(spec = spec, data = data$LogReturn)
print(fit)
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,1)
## Mean Model : ARFIMA(0,0,0)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001008 0.000238 4.241 2.2e-05
## omega 0.000012 0.000000 39.022 0.0e+00
## alpha1 0.079798 0.003479 22.938 0.0e+00
## beta1 0.887481 0.005395 164.512 0.0e+00
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.001008 0.000257 3.9286 8.5e-05
## omega 0.000012 0.000001 16.0009 0.0e+00
## alpha1 0.079798 0.007896 10.1057 0.0e+00
## beta1 0.887481 0.011637 76.2620 0.0e+00
##
## LogLikelihood : 11690.1
##
## Information Criteria
## ------------------------------------
##
## Akaike -5.2759
## Bayes -5.2701
## Shibata -5.2759
## Hannan-Quinn -5.2739
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.9987 0.3176
## Lag[2*(p+q)+(p+q)-1][2] 1.0207 0.4916
## Lag[4*(p+q)+(p+q)-1][5] 1.9313 0.6348
## d.o.f=0
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.02498 0.8744
## Lag[2*(p+q)+(p+q)-1][5] 0.49184 0.9589
## Lag[4*(p+q)+(p+q)-1][9] 1.36199 0.9661
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.1668 0.500 2.000 0.6830
## ARCH Lag[5] 0.4048 1.440 1.667 0.9113
## ARCH Lag[7] 1.1626 2.315 1.543 0.8857
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 20.1658
## Individual Statistics:
## mu 0.1719
## omega 4.7613
## alpha1 0.4870
## beta1 0.5742
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 1.07 1.24 1.6
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 0.8441 0.3987
## Negative Sign Bias 0.6744 0.5001
## Positive Sign Bias 0.5742 0.5659
## Joint Effect 2.2643 0.5194
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 280.4 2.073e-48
## 2 30 290.4 6.386e-45
## 3 40 306.5 3.085e-43
## 4 50 319.6 2.212e-41
##
##
## Elapsed time : 3.996087
# === 4. Conditional volatility plot (Estimated volatility) ===
data$Volatility <- sigma(fit) # In-sample volatility
ggplot(data, aes(x = Date, y = Volatility)) +
geom_line(color = "orange") +
labs(
title = "Estimated Volatility from GARCH(1,1)",
x = "Date", y = "Estimated Volatility",
caption = "Volatility computed from the fitted GARCH(1,1) model"
) +
theme_minimal()
# === 5. ACF of Standardized Residuals (Model Adequacy Check) ===
resid_std <- residuals(fit, standardize = TRUE) # Standardized residuals
acf(resid_std, main = "ACF of Standardized Residuals")