Let us first read data
VAR<- read.csv2(file.choose())
The data, are the loss days during the year of a given stock
Let us see the data ’ View(VAR)’
Let us sort the data by return
sort(VAR$DailyReturnt)
## [1] -0.99683 -0.99179 -0.99058 -0.96588 -0.96455 -0.94323 -0.93869 -0.92546
## [9] -0.88123 -0.86096 -0.83487 -0.83365 -0.83030 -0.81467 -0.80294 -0.78881
## [17] -0.78814 -0.75650 -0.74385 -0.73966 -0.72084 -0.71931 -0.71552 -0.70428
## [25] -0.70153 -0.67847 -0.67554 -0.67458 -0.67127 -0.66273 -0.65917 -0.63452
## [33] -0.62600 -0.61303 -0.61282 -0.61170 -0.60729 -0.57777 -0.57334 -0.56834
## [41] -0.54594 -0.52486 -0.51906 -0.51629 -0.50329 -0.48185 -0.47791 -0.47196
## [49] -0.46626 -0.46485 -0.45425 -0.45304 -0.45021 -0.44586 -0.43843 -0.43811
## [57] -0.40703 -0.37668 -0.36853 -0.36710 -0.36134 -0.34231 -0.33687 -0.33551
## [65] -0.33046 -0.31138 -0.30550 -0.29772 -0.29614 -0.28959 -0.28678 -0.26959
## [73] -0.26597 -0.26486 -0.26084 -0.25924 -0.25844 -0.25557 -0.24785 -0.24648
## [81] -0.24550 -0.24202 -0.24118 -0.23954 -0.23088 -0.21764 -0.21518 -0.21043
## [89] -0.20873 -0.20858 -0.20012 -0.19752 -0.18696 -0.16620 -0.16307 -0.16175
## [97] -0.13293 -0.12029 -0.05691 -0.03905
Let us check the ‘95%’ VAR
head(sort(VAR$DailyReturnt))
## [1] -0.99683 -0.99179 -0.99058 -0.96588 -0.96455 -0.94323
Practical issues
Few basic analisiis
Let us see the histogram distibution of the values
hist(VAR$DailyReturnt)
Let us calculate the risk
var(VAR$DailyReturnt)
## [1] 0.06475425
What the difference between Variance and Standar Deviation?
sd(VAR$DailyReturnt)
## [1] 0.2544686
Do we have any assumptions on our data?
Let us calculate the quantiles
quantile(VAR$DailyReturnt)
## 0% 25% 50% 75% 100%
## -0.996830 -0.684235 -0.459550 -0.260440 -0.039050
#install.packages("quantmod") /if you do not have installed perviously
library(quantmod) # Load the package
## Ładowanie wymaganego pakietu: xts
## Ładowanie wymaganego pakietu: zoo
##
## Dołączanie pakietu: 'zoo'
## Następujące obiekty zostały zakryte z 'package:base':
##
## as.Date, as.Date.numeric
## Ładowanie wymaganego pakietu: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
getSymbols(“AAPL”) # Download daily prices of Apple stock from Yahoo for the year 2022
#getSymbols("AAPL") # Download daily prices of Apple stock from Yahoo
getSymbols("AAPL",from="2022-01-02", to="2022-12-31")
## [1] "AAPL"
let us calculate the return of apple
AAPL_returns <- Delt(AAPL$AAPL.Adjusted)
# alternatively we migh calcuate manually:
# # Or using the diff function and dividing by the previous day's price
#AAPL_returns_diff <- diff(AAPL$AAPL.Adjusted) / #AAPL$AAPL.Adjusted[-length(AAPL$AAPL.Adjusted)]
Let us see it
plot(AAPL_returns)
In practice we use the log return instead of simple return. For the
difference between compare:
https://robotwealth.com/the-intuition-of-log-returns/
So let us now work on the log returns
# Calculate the log difference between consecutive adjusted closing prices
log_returns <- diff(log(AAPL$AAPL.Adjusted))
plot(log_returns)
let us see the distibution of returns
hist(log_returns)
What you cna observer imidietelly? Doest this histogram is simillar to the dencity of any distribution function.
# let us compare it to the normal distribution
# Q-Q plot
qqnorm(log_returns)
qqline(log_returns)
Let us check more formally
# Formal normality tests
# Kolmogorov-Smirnov test (less powerful for larger sample sizes)
ks.test(log_returns, "pnorm")
##
## Asymptotic one-sample Kolmogorov-Smirnov test
##
## data: log_returns
## D = 0.50443, p-value < 2.2e-16
## alternative hypothesis: two-sided
We have a normal distribution of the returns, what is important for us in term of the stationary and more advanced analysis of the return process.
sd<- sd(log_returns)
var<- var(log_returns)
cat("Standard deviation is equal to", round(sd,3), "\n")
## Standard deviation is equal to NA
cat("Variance is equal to", round(var,3), "\n")
## Variance is equal to NA
Let us see the maximum spread (potential)
spread<- max(log_returns) - min(log_returns)
spread
## [1] NA
And let us see the VAR@ 95%
head(sort(log_returns))
## AAPL.Adjusted
## 2022-01-04 -0.0127729107
## 2022-01-05 -0.0269600338
## 2022-01-06 -0.0168342434
## 2022-01-07 0.0009880081
## 2022-01-10 0.0001160663
## 2022-01-11 0.0166447103
Do you see the error? Why is this so?
You can’t change the ordering of a zoo object and since an xts object is a zoo object it applies to xts too. The oo in zoo stands for ordered observations. Such an object is always stored and shown in the order of its time index.
We will convert the zoo object innto dataframe
return_frame<- as.data.frame(log_returns)
head(sort(return_frame$AAPL.Adjusted))
## [1] -0.06047157 -0.05807326 -0.05732833 -0.05323322 -0.05036640 -0.04798746
Now we can view ‘View(sort(return_frame$AAPL.Adjusted))’