#Practical Assignment #1. This assignment is worth 150 points.
#Useful hints: Utilize book examples and make sure to download
#and library appropriate packages (FRAPO, zoo, fBasics, evir)
#Under each of the items provide the relevant R code
#1) Check you working directory
getwd()
## [1] "/Users/nischal/Documents/ANLY/ANLY 515/Assignment 1"
#2) Set your working directory to RStudio folder
# that you have created inside the ANLY 515 forlder"
setwd("~/Documents/ANLY/ANLY 515/Assignment 1")
#3) Download the 3 data set, and lable them
# BTC, LTC, ETH. These data sets reperesnet daily prices
# of three chryptocurrencies: Bitcoin, Litcoin, and Ethereum.
# Set the first column in each data set to the date format
# and the remaining columns in numerical format.
library(readxl)
btc<- read_excel("~/Documents/ANLY/ANLY 515/Assignment 1/data/btc-1-1.xlsx",col_types = c("date",
"numeric", "numeric", "numeric", "numeric","numeric",
"numeric","numeric", "numeric","numeric", "numeric",
"numeric", "numeric","numeric", "numeric","numeric", "numeric"))
ltc<- read_excel("~/Documents/ANLY/ANLY 515/Assignment 1/data/ltc-1-1.xlsx", col_types = c("date",
"numeric", "numeric", "numeric", "numeric","numeric",
"numeric","numeric", "numeric","numeric", "numeric",
"numeric", "numeric","numeric", "numeric","numeric", "numeric"))
eth<- read_excel("~/Documents/ANLY/ANLY 515/Assignment 1/data/eth-1-1.xlsx", col_types = c("date",
"numeric", "numeric", "numeric", "numeric","numeric",
"numeric","numeric", "numeric","numeric", "numeric",
"numeric", "numeric","numeric", "numeric","numeric"))
#4) Create three new datasets that are subsets of the original
# datasets, to include dates only after "2015-08-31".
# Name these datasets btcnew, ltcnew, ethnew,
btc<- na.omit(btc)
ltc<- na.omit(ltc)
eth<- na.omit(eth)
btc2<-btc[btc$date>"2015-08-31",]
ltc2<-ltc[ltc$date>"2015-08-31",]
eth2<-eth[eth$date>"2015-08-31",]
#5) Create 4 variables: date (represents dates of observation) ,
# BTCPrice (price of Bitcoin), LTCPrice (Price of Litcoin),
# ETHPrice (Price of Ethereum)
date<-btc2$date
BTCPrice<-btc2$`price(USD)`
LTCPrice<-ltc2$`price(USD)`
ETHPrice<-eth2$`price(USD)`
#6) Check the format of these variable by using str() command
str(date)
## POSIXct[1:1235], format: "2015-09-01" "2015-09-02" "2015-09-03" "2015-09-04" "2015-09-05" ...
str(BTCPrice)
## num [1:1235] 230 228 229 227 230 ...
str(LTCPrice)
## num [1:1235] 2.85 2.82 2.8 2.64 2.7 2.92 3.04 3.05 3.05 2.92 ...
str(ETHPrice)
## num [1:1239] 1.35 1.35 1.3 1.26 1.28 1.34 1.3 1.25 1.24 1.21 ...
#7) Use date variable to create attribute "time" for
# BTCPrice, LTCPrice, and ETHPrice by using attr() function
attr(BTCPrice,"time")<-date
attr(LTCPrice,"time")<-date
attr(ETHPrice,"time")<-date
#8) Create three variables that represent daily returns on all three coins
# by using returnseries()(part of FRAPO package) function.
# Call these variables BTCRet, LTCRet, and ETHRet.
library(FRAPO)
## Loading required package: cccp
## Loading required package: Rglpk
## Loading required package: slam
## Warning: package 'slam' was built under R version 4.1.2
## Using the GLPK callable library version 4.65
## Loading required package: timeSeries
## Loading required package: timeDate
## Financial Risk Modelling and Portfolio Optimisation with R (version 0.4-1)
BTCRet<- returnseries(BTCPrice)
LTCRet<- returnseries(LTCPrice)
ETHRet<- returnseries(ETHPrice)
#9) Use date variable to create attribute "time" for
# BTCRet, LTCRet, and ETHRet
attr(BTCRet,"time")<-date
attr(LTCRet,"time")<-date
attr(ETHRet,"time")<-date
#10) Create a character variable CoinDates which extracts the dates
# from the BTCRet variable by using
# as.character(format(as.POSIXct(attr()),"%Y-%m-%d")) function
CoinDates <- as.character(format(as.POSIXct(attr(BTCRet,"time")),"%Y-%m-%d"))
#11) Create time seriese called BTCReturns by using BTCRet varible
# and timeSeries() function
BTCReturns <- timeSeries(BTCRet, charvec = CoinDates)
#12) Rename the column of BTCReturns to "BTCReturns" by using colnames()
colnames(BTCReturns)<- "BTCReturns"
#13) Devide the output window into 2 by 2 matrix by using par() function
par(mfrow=c(2,2))
#14) Generate a time series plot of Daily Returns of Bitcoin
# (requires fBasics library)
library(fBasics)
seriesPlot(BTCReturns)
#15) Generate a box plot of Returns of Bitcoin
boxPlot(BTCReturns)
#16) Generate a acf and pacf of Bitcoin Returns.
# Make sure to omit missing values
library(fBasics)
acf(BTCReturns,lag.max=20,na.action = na.omit,plot = TRUE)
pacf(BTCReturns, na.action = na.pass,plot = TRUE)
#17) Generate a QQ plot of Bitcoin.
# You may have to generate a variable that omits missing values.
# use na.omit() function
qqnormPlot(na.omit(BTCReturns))
#18) Generate acf and pcf of the absolute returns of Bitcoin returns
BTCreturn2 <- abs(BTCReturns)
acf(BTCreturn2, type = c("correlation", "covariance", "partial"), plot = TRUE, na.action = na.omit)
pacf(BTCreturn2, plot = TRUE, na.action = na.omit)
#19) Generate Volotility Clustering Plot of absolute daily returns
# of Bitcoin
plot(BTCreturn2)
#Part II
#20) Create new data set called PriceCoins that includes prices
# of all three coins by using cbind()
PriceCoins<- cbind(BTCPrice, LTCPrice, ETHPrice)
## Warning in cbind(BTCPrice, LTCPrice, ETHPrice): number of rows of result is not
## a multiple of vector length (arg 1)
#21) Check the names of the colums and the format of each of the variable
str(PriceCoins)
## num [1:1239, 1:3] 230 228 229 227 230 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:3] "BTCPrice" "LTCPrice" "ETHPrice"
#22) by using the package zoo create an element of class "zoo"
# labled PriceCoinsZoo, and includes Prices of all three Coints:
# BTCPrice", "LTCPrice", and "ETHPrice"
library(zoo)
##
## Attaching package: 'zoo'
## The following object is masked from 'package:timeSeries':
##
## time<-
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
PriceCoinsZoo<- as.zoo(PriceCoins)[,c("ETHPrice","LTCPrice","BTCPrice")]
#23) Plot a time series graph of prices of all three coins
plot(PriceCoinsZoo)
#24) Create a variable that calculates daily returns
# (first difference of natural logs) on different coins.
# Call the variable ReturnCoins by using diff(log()) function
ReturnCoins<-diff(log(PriceCoinsZoo))*100
#25) Plot a time series graph of returns of all three coins
plot(ReturnCoins)
#26) Plot cross covariance functions between returns and between
# absolute returns of a) BTC and LTC, BTC and ETC, and ETC and LTC.
# You should have 6 graphs.
par(mfrow=c(3,2),mar=c(1, 2, 1, 1), oma=c(0, 0, 0, 0), mgp=c(2.4, 0.8, 0), las=1)
ccf(ReturnCoins[,3], ReturnCoins[,2])
ccf(ReturnCoins[,3], ReturnCoins[,1])
ccf(ReturnCoins[,1], ReturnCoins[,2])
ccf(ReturnCoins[,1], ReturnCoins[,3])
ccf(ReturnCoins[,2], ReturnCoins[,1])
ccf(ReturnCoins[,2], ReturnCoins[,3])
#27) Generate plots of rolling correlations between
# BTC<C, BTCÐ, and ETH<C
rollc <- function(x){ dim <- ncol(x)
rcor <- cor(x)[lower.tri(diag(dim), diag = FALSE)]
return(rcor)}
rcor <- rollapply(ReturnCoins, width = 500, rollc, align = "right", by.column = FALSE)
colnames(rcor) <- c("B-L", "B-E", "L-E")
plot(rcor, main = "", xlab = "")
# Part III
# 28-30) Export all graphs to a word document and briefly analyze
# whether data on cryptocurrency returns resembles the
# "stylized facts" of the financial data
Yes, the data on cryptocurrency returns resembles the “stylized facts” of the financial data.
-> The first one is ‘volatility clustering’. It means periods of large returns alternate with periods with small returns, suggesting that volatility is not constant. This is the same as our first plot for BTC Return. As the representative of cryptocurrency, bitcoin shows an unstable return from 2016 to 2019 and appeared both the highest and lowest return value at the end of 2017.
-> Second fact, the distribution of returns is not normal. From the QQ plot, the light grey line represents the Standard Normal distribution. In the case when the returns followed Gaussian distribution, those two lines would be aligned. However, we see that there are differences, mostly in the tails.
-> Third fact, No (or almost no) significant autocorrelation in returns. Autocorrelation measures the degree of similarity between a given time series and the lagged version of the same series over successive time intervals. It is analogous to the correlation between two time-series: the first one in its original form and one lagged by n periods. In this experiment, we used the AR model for BTC return, and then use the absolute return for ARMA model, which exactly matches the normal financial fact.
-> Finally, Financial products have their internal connections, it might be alternative or complementary relationships. In final picture, we compared the relationship between these three cryptocurrencies, and we can see BTC VS. ETC and BTC VS. LTC almost have the same tendency. That means, for BTC, ETC, and LTC it doesn’t have much difference.