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"
start_date <- "2022-11-30"
end_date <- "2022-12-31"
symbol <-"T"
# Download the data for AT&T (ticker symbol: T)
getSymbols(symbol, src = "yahoo", from = start_date, to = end_date, auto.assign = TRUE)
[1] "T"
# 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, no lags seem to be statistically significant (at 5 percent significance level). Remember, it can by just demonstration of the Type 1 Error !!! 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.[3] 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, runs = 11, n1 = 10, n2 = 10, n = 20, p-value = 1
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
[^3] the details of the Runs test can be found in Bujang MA, Sapri FE. An application of the runs test to test for randomness of observations obtained from a clinical survey in an ordered population. The Malaysian Journal of Medical Sciences: MJMS. 2018 Jul;25(4):146.
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 10 autocorelation statistics, approximately 0.05 x 10 = 0.5 (is about 1?) autocorelations can be demonstrated as significant. In our case, there are 0 calculated as significant.↩︎
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.↩︎