If making the financial analysis of the stock exchange data, the following packages are needed:
#install.packages("PerformanceAnalytics", repos = "http://cran.us.r-project.org")
#install.packages("dplyr", repos = "http://cran.us.r-project.org")
#install.packages("tidyquant", repos = "http://cran.us.r-project.org")
#install.packages("quantmod", repos = "http://cAs iran.us.r-project.org")
#install.packages("tseries", repos = "http://cran.us.r-project.org")
#install.packages("tidyverse", repos = "http://cran.us.r-project.org")
library(PerformanceAnalytics) #useful package !!!
library(quantmod) # useful package
library(tidyquant) # useful
library(randtests) # for runs test
# other libraries
library(ggplot2)
library(dplyr)
library(tseries)
library(tidyverse)
library(plotly)
library(hrbrthemes)
library(xts)
library(knitr)
library(kableExtra)
library(car)
library(mathjaxr)
library(zoo)
rm(list=ls())
If downloading the stock-exchange data, we use the quantmod package command “getSymbols”. COmmand “Ad” enables extraction of just Adjusted closing prices of the day.
# Load the required package
library(quantmod)
# Set dates for data retrieval
start_date <- "2010-01-01"
end_date <- "2022-12-31"
symbol <-"F"
# Download the data for Ford (ticker symbol: F)
getSymbols(symbol, src = "yahoo", from = start_date, to = end_date, auto.assign = TRUE)
[1] "F"
# Extract the adjusted close prices
adot.close <- Ad(getSymbols(symbol, src = "yahoo", from = start_date, to = end_date, auto.assign = FALSE))
title <- paste(symbol)
plot(adot.close, col="red", main=title)
valtozas <- diff(adot.close)
median_valtozas <- median(valtozas, na.rm = TRUE) # Compute median excluding NA
valtozas[is.na(valtozas)] <- median_valtozas # replacing missing returns by their estimates by median
acf(valtozas,main="Autocorrelations of Price differences")
\[H_0: \text{markets are weakly information efficient}\] \[H_1: \text{markets are not weakly information efficient}\] The above given Autocorrelation function is a function, which assigns each lag a correlation between actual price differences and a lagged price differences by a specified number of periods (varying from 1 to 35 ) - \(corr(P_{t}-P_{t-1},{P_{t-i}-P_{t-i-1}})\). If the correlations would be out the band between two blue dotted lines, we could accept the hypothesis that price movements of the F are not information weakly efficient. In our case, lags 7 and 15 and 31 seem to be statistically significant (at 5 percent significance level). Remember, it can by just demonstration of the Type 1 Error !!! There are just 3 cases of 35 - that is why we are prone to reject the alternative hypothesis.1 We do not have any information to reject \(H_0\), prices can be considered as weakly efficient.
\[H_0: \text{markets are weakly information efficient}\] \[H_1: \text{markets are not weakly information efficient}\] To test the weak form of the efficiency, we can also use Runs test. We are using the diferenced time series as in the previous example. Therafter, we substitute
# Load the required package
library(randtests)
# Perform the Runs Test
runs_test_result <- runs.test(as.vector(valtozas))
# Print the result
print(runs_test_result)
Runs Test
data: as.vector(valtozas)
statistic = -0.49106, runs = 1585, n1 = 1612, n2 = 1584, n = 3196, p-value =
0.6234
alternative hypothesis: nonrandomness
In our case p-value is high, we reject the alternative, we deny the alternative hypothesis of absenting the weak form of the efficiency.2
All public (but not inside) information is calculated into a stock’s current price. Neither fundamental nor technical analysis can be used to achieve superior gains.
Test Event Studies: These studies examine the stock price reaction to new public information, such as earnings announcements, to determine how quickly and accurately prices adjust.
Both the public and inside information is calculated into a stock current price.
If speaking about 5 percent significance level, than in case of 35 autocorelation statistics, approximately 0.05 x 35 = 2 autocorelations can be demonstrated as significant. In our case, there are 3 calculated as significant (with lags 7,15,31).↩︎
p-value is in fact probability of the Type I error in statistical testing. It means, accepting the alternative hypothesis in case it is not really true. In our case, if p-value is high, than the statement that the prices of the stock are not informationally weak efficient, meets the probability of error of p-value x 100 percent.↩︎