-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGs_contour.R
77 lines (59 loc) · 3.21 KB
/
Gs_contour.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
library(quantmod)
library(qrmtools)
df_BTC <- getSymbols('BTC-EUR',src='yahoo',auto.assign=FALSE, from = "2017-11-11",
to = "2024-05-10")
df_ETH <- getSymbols('ETH-EUR',src='yahoo',auto.assign=FALSE, from = "2017-11-11",
to = "2024-05-10")
BTC_ETH <- cbind(df_BTC$`BTC-EUR.Adjusted`,df_ETH$`ETH-EUR.Adjusted`)
colnames(BTC_ETH) <- c("BTC-EUR","ETH-EUR")
R_BTCETH <- returns(BTC_ETH) # calculate log-returns
library(rugarch)
# For BTC: garch.fit_BTC
# GARCH Model : eGARCH(3,4)
# Mean Model : ARFIMA(0,0,0) WITH MEAN
# Distribution : sstd
ctrl = list(RHO = 1,DELTA = 1e-8,MAJIT = 100,MINIT = 650,TOL = 1e-6)
## Specify model: ARMA(0,0)-eGARCH(3,4) WITH MEAN and skew Student t innovations
spec_BTC = ugarchspec(variance.model = list(model="eGARCH",garchOrder = c(3, 4)),mean.model = list(armaOrder = c(0,0),include.mean=TRUE),distribution.model = "sstd")
## Fit marginal ARMA-GARCH model for BTC-EUR returns
garch.fit_BTC = ugarchfit(data = R_BTCETH$`BTC-EUR`, spec = spec_BTC, solver = "solnp", solver.control = ctrl)
residuals_BTC <- residuals(garch.fit_BTC,standardize=TRUE)
# For ETH: garch.fit_ETH
# GARCH Model : eGARCH(4,3)
# Mean Model : ARFIMA(0,0,0) WITH ZERO MEAN
# Distribution : sstd
## Specify model: ARMA(0,0)-eGARCH(4,3) WITH ZERO MEAN and skew Student t innovations
spec_ETH = ugarchspec(variance.model = list(model="eGARCH",garchOrder = c(4, 3)),mean.model = list(armaOrder = c(0,0),include.mean=FALSE),distribution.model = "sstd")
## Fit marginal ARMA-GARCH model for ETH-EUR returns
garch.fit_ETH = ugarchfit(data = R_BTCETH$`ETH-EUR`, spec = spec_ETH, solver = "solnp", solver.control = ctrl)
residuals_ETH <- residuals(garch.fit_ETH,standardize=TRUE)
# Put together stand. residuals from BTC and ETH ARMA-GARCH models
residuals_BTCETH <- cbind(residuals_BTC,residuals_ETH)
colnames(residuals_BTCETH) <- c("residuals_BTC","residuals_ETH")
df_residuals_BTCETH <- fortify.zoo(residuals_BTCETH) # convert to data frame
library(copula)
library(sn)
# find marginal distributions
st.BTC.fit <- st.mple(y=residuals_BTCETH[,1])
st.ETH.fit <- st.mple(y=residuals_BTCETH[,2])
# fitted skew-t margins
BTC.ETH.margins <- c("st", "st")
BTC.ETH.parms <- list(list(dp = st.BTC.fit$dp),
list(dp = st.ETH.fit$dp))
# fitted Gumbel survival copula
tau_gumbel_s = cor(residuals_BTCETH[,1], residuals_BTCETH[,2], method = "kendall")[1]
gumbel_s.tau2theta <- 1/(1 - tau_gumbel_s) # calculate parameter theta
BTC.ETH.cop <- rotCopula(gumbelCopula(param = gumbel_s.tau2theta, dim = 2))
myBvd.BTC.ETH.fit <- mvdc(copula = BTC.ETH.cop,
margins = BTC.ETH.margins,
paramMargins = BTC.ETH.parms)
# contour plot
par(pty="s") # to make the plot region square
xylim = 2.5 # it is advised to keep the xylim, in order that the contour lines can be recognized
plot(df_residuals_BTCETH[,2], df_residuals_BTCETH[,3], col = "brown1", pch = 20,
xlim = c(-xylim, xylim), ylim = c(-xylim, xylim),
xlab = expression(hat(epsilon)["1t"]), ylab = expression(hat(epsilon)["2t"]),
cex.lab = 1, bg = 'transparent')
contour(myBvd.BTC.ETH.fit, dMvdc, lwd = 3,
xlim = c(-xylim, xylim), ylim = c(-xylim, xylim),
add = TRUE, labcex = 0.7)