-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitcoin ARMA model.Rmd
168 lines (133 loc) · 2.69 KB
/
bitcoin ARMA model.Rmd
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
166
---
title: "STAT4181 hw4 Min Yang"
output:
word_document: default
html_notebook: default
pdf_document: default
html_document:
df_print: paged
---
###A
```{r}
library(astsa)
library(base)
ts.plot(cmort)
```
2.
```{r}
acf(cmort)
pacf(cmort)
```
3.
```{r}
x <- cmort
AIC_arma_seq <- function(x, max_p=5,
max_q=5
){
MA_AIC<-rep(0,max_q)
for(i in 1:max_q){
temp <- arima(x,order=c(0,0,i))
MA_AIC[i]<-AIC(temp)
}
AR_AIC <- rep(0,max_p)
for(i in 1:max_p){
temp <- arima(x,order=c(i,0,0))
AR_AIC[i]<-AIC(temp)
}
return(list(MA_AIC,AR_AIC))
}
AIC_arma_seq(x,max_p=5,max_q=5)
```
4.
```{r}
AIC_arma_seq <- function(x,
max_p=10,
max_q=10
){
MA_AIC<-rep(0,max_q)
for(i in 1:max_q){
temp <- arima(x,order=c(0,0,i))
MA_AIC[i]<-AIC(temp)
}
AR_AIC <- rep(0,max_p)
for(i in 1:max_p){
temp <- arima(x,order=c(i,0,0))
AR_AIC[i]<-AIC(temp)
}
return(list(AR_AIC, MA_AIC))
}
AIC_arma_seq(cmort,max_p=10,max_q=10)
```
With the smalles value of 3217.429, the best model is AR[2].
```{r}
predict(arima(cmort,order=c(2,0,0)),n.ahead=12)
```
###B
####1.
```{r}
bit<-read.csv("~/Desktop/HW2Bitcoin.csv")
bit$Date <- as.Date(bit$Date, format="%m / %d / %y ")
```
####2.
```{r}
library(zoo)
library(xts)
bit_zoo <- zoo(bit$Last,order.by = bit$Date)
bit_xts <- as.xts(bit_zoo)
bit_log<- diff(log(bit_xts), lag = 1)
plot.ts(bit_log)
```
###3.
```{r}
bitpre17 <- na.omit(bit_log["/2016"])
str(bitpre17)#check if get rid of NA
```
###4.
```{r}
AR_MAX_ORDER <- 10
AR_aic <- rep(0,AR_MAX_ORDER)
for(i in 1:AR_MAX_ORDER){
temp <- arima(bitpre17,order = c(i,0,0))
AR_aic[i] <- AIC(temp)
}
AR_aic
```
```{r}
MA_MAX_ORDER <- 10
MA_aic <- rep(0,MA_MAX_ORDER)
for(i in 1:MA_MAX_ORDER){
temp <- arima(bitpre17,order = c(0,0,i))
MA_aic[i] <- AIC(temp)
}
MA_aic
```
With AIC of -3900.012, the best model is MA[9].
####5.
```{r}
bit_logg <- na.omit(bit_log)
TT <- length(bit_logg)
T0 <- 974 #975 data before 2017 - 1 data NA
```
```{r}
#I directly plug in the data and order in the function
backtest_arma <- function(bit_logg,
arima_order=c(0,0,9),
T0=975){
eps <- coredata(bit_logg)
eps[1:T0] <- 0
for(t in (T0+1):TT){ #at every time step
#evaluate previous prediction
if(t!=(T0+1)){
eps[t] <- coredata(prev_pred)-coredata(bit_logg[t])
}
#fit model to past data
temp_model <- arima(bit_logg[1:t],order=arima_order)
#predict from the model
prev_pred <- predict(temp_model,n_ahead=1)$pred
}
eps <- eps[(T0+1):TT]
return(c(eps))
}
plot(c(eps))
```
From the plot, all eps are within 0.2, so I conclude this model performs pretty well.