-
Notifications
You must be signed in to change notification settings - Fork 1
/
saved.R
165 lines (141 loc) · 4.66 KB
/
saved.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#Housekeeping
rm(list = ls())
#Set Seed
set.seed(2346)
#installing packages
# install.packages("xlsx")
# install.packages("MultipleBubbles")
# install.packages("moments")
# install.packages("corrgram")
# install.packages("forecast")
# install.packages("tseries")
# install.packages("rtadfr")
# install.packages("psymonitor")
# install.packages("ggplot2")
# install.packages("knitr")
# install.packages("lubridate")
# install.packages("markdown")
# install.packages("spelling")
# install.packages("testthat")
# install.packages("PerformanceAnalytics")
# install.packages("xts")
# install.packages("expm")
install.packages('aTSA')
#Loading packages
library("xlsx")
library("MultipleBubbles")
library("moments")
library("corrgram")
library("forecast")
library("tseries")
library("remotes")
library("psymonitor")
library("ggplot2")
library("knitr")
library("lubridate")
library("markdown")
library("spelling")
library("testthat")
library("PerformanceAnalytics")
library("xts")
library("expm")
library('aTSA')
#Loading and Defining data
ethereumdata <- read_excel("Desktop/ethereumdata.xlsx")
View(ethereumdata)
P = ethereumdata$Open
IQR(P)
logP = ethereumdata$`ln open`
date = ethereumdata$Date
date
acf(P, lag.max = 20,main='ACF for ETH Price Series')
pacf(P, lag.max = 20, main='Partial ACF for ETH Price Series')
arch.test(logP, output=TRUE)
plot(P, xaxt ="n", type='l')
axis(1, at = date, labels = FALSE)
plot(date,P,type="l")
axis.Date(1,at=date,las=2)
pt + scale_x_date(breaks = as.Date(c("2017-01-01","2018-01-01")))
#Returns for homoskedasticity
P_xts <- xts(P, order.by = date)
Return <- Return.calculate(P_xts, method = "discrete")
plot.ts(Return)
Return2 <- Return^2
plot.ts(Return2)
acf(Return2, lag.max = 100, plot = TRUE, main = "ACF for log Prices", ylab = "Autocorrelation", type = c("correlation", "covariance"))
McLeod. "partial"))
#Describing Data
skewness(logP)
kurtosis(logP)
summary(logP)
sd(logP)
acf(logP, lag.max = 100, main = "ACF for log Prices", ylab = "Autocorrelation", type = c("correlation", "covariance",
"partial"))
pacf(logP, lag.max = 100)
hist(logP)
#Calculate the Dickey Fuller with a Fixed Lag Order
#ADF without a constant
#Can't calculate lags without a constant
adf.test(logP, alternative = c("explosive"))
ADF_FL(logP, adflag = 0, mflag = 3)
#ADF with constant
ADF_FL(logP, adflag = 0, mflag = 1)
ADF_FL(logP, adflag = 1, mflag = 1)
ADF_FL(logP, adflag = 2, mflag = 1)
#ADF with trend
ADF_FL(logP, adflag = 0, mflag = 2)
ADF_FL(logP, adflag = 1, mflag = 2)
ADF_FL(logP, adflag = 2, mflag = 2)
#Calculate the ADF which considers AIC and BIC
#constant no trend
ADF_IC(logP, adflag = 20, mflag = 1, IC = 1)
ADF_IC(logP, adflag = 20, mflag = 1, IC = 2)
#constant trend
ADF_IC(logP, adflag = 20, mflag = 2, IC = 1)
ADF_IC(logP, adflag = 20, mflag = 2, IC = 2)
#Non-recursive ADF
adf.test(logP, alternative = "explosive" , k=1)
#Defining vars
obs <- length(logP)
swindow0 <- floor(obs*(0.01 + 1.8/sqrt(obs)))
dim <- obs - swindow0 + 1
Tb <- 24 + swindow0 - 1
# Estimate PSY statistics and CVs
# bsadf <- PSY(logP, swindow0)
bsadf <- readRDS('bsadf.rds')
quantilesBsadf <- cvPSYwmboot(logP, swindow0, Tb=Tb, nboot = 49, nCores = 2)
quantiles <- quantilesBsadf %*% matrix(1, nrow = 1, ncol = dim)
quantiles90 <- quantiles[1,]
quantiles95 <- quantiles[2,]
quantiles99 <- quantiles[3,]
# locate bubble/crisis dates, CI99
ind99 <- (bsadf > t(quantiles99)) * 1
monitorDates <- date[swindow0:obs]
OT <- locate(ind99, monitorDates)
OT
# locate bubble/crisis dates, CI95
ind95 <- (bsadf > t(quantiles95)) * 1
monitorDates <- date[swindow0:obs]
OT <- locate(ind95, monitorDates)
# locate bubble/crisis dates, CI90
ind90 <- (bsadf > t(quantiles90)) * 1
monitorDates <- date[swindow0:obs]
OT <- locate(ind90, monitorDates)
# Show bubble/crisis periods
print(OT, obs)
disp(OT, obs)
#Locate bubble/crisis periods
date[ind99==1]
dateStampDf1 <- ts(bsadf)
dateStampDf2 <- ts(quantiles90)
dateStampDf3 <- ts(quantiles95)
dateStampDf4 <- ts(quantiles99)
ts.plot(dateStampDf1, plot.type = "single", col="blue")
ts.plot(dateStampDf2, plot.type = "single", col="red")
ts.plot(dateStampDf3, plot.type = "single", col="#005100")
ts.plot(dateStampDf4, plot.type = "single", col="purple")
indbubble <- (dateStampDf1 > dateStampDf4) * 1
bubble_line <- indbubble - 6
dateStampDf <- cbind(dateStampDf1, dateStampDf4, bubble_line)
write.csv(dateStampDf,'301_citation_misa.csv')
# ts.plot(dateStampDf, plot.type = "single", list(xlab="date", ylab=NULL), lty=c(1,2,1), col=c("blue", "red", "black"))