-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharima-models3.Rmd
More file actions
185 lines (90 loc) · 2.96 KB
/
Copy patharima-models3.Rmd
File metadata and controls
185 lines (90 loc) · 2.96 KB
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
---
title: "R Notebook"
output: html_notebook
---
# Libraries
```{r}
library(quantmod)
library(tseries)
library(timeSeries)
library(forecast)
library(xts)
library(dplyr)
library(foreach)
library(doParallel)
```
```{r}
df <- read.csv("./data/btc/btc-final.csv", sep = "\t", header = TRUE)
data <- df
data$date <- as.POSIXct(data$date)
data <- data %>% filter(date > "2015-01-27")
head(data)
```
Ran adf.test. Both close price differences and log of differences are significant and therefore stationary. close_price_returns is stationary.
found best model to be the ARIMA(4,0,5) based on auto.arima search.
```{r}
m1_arima45_dif_price_close <- arima(diff(data$price_close), order=c(4,0,5))
m2_arima45_returns <- arima(data$close_price_returns, order = c(4,0,5))
m3_arima3030 <- arima(diff(data$price_close), order=c(30,0,30))
```
```{r}
actual_series <- xts(0, as.POSIXct("2015-01-27 00:05:00","%Y-%m-%d %H:%M:%S"))
actual_series
# Initialzing a dataframe for the forecasted return series
forecasted_series <- data.frame(Forecasted = numeric())
breakpoint <- floor(nrow(stock)*(.8))
stock.train <-
```
```{r}
m1 <- auto.arima(stock$diff)
m2 <- auto.arima(stock$diff, max.P = 30, max.Q = 30, max.order = 60)
```
```{r}
summary(m2)
m3 <- auto.arima(stock_train, max.P = 30, max.Q = 30, max.order = 60)
```
# sliding windows
```{r}
# for (b in breakpoint:(nrow(stock)-1)) {
cat(paste0("\rWorking on itteration: ", (b-breakpoint+1), "/", (nrow(stock)-1-breakpoint)))
b <- breakpoint
stock_train = stock[1:b, "diff"]
stock_test = stock[(b+1):nrow(stock), "diff"]
# Summary of the ARIMA model using the determined (p,d,q) parameters
fit = ARIMA(stock_train, order = c(4, 0, 5))
summary(fit)
# plotting a acf plot of the residuals
acf(fit$residuals,main="Residuals plot")
# Forecasting the log returns
# arima.forecast = forecast.Arima(fit, h = 1,level=99)
arima.forecast = forecast(fit, h = 1,level=99)
summary(arima.forecast)
# plotting the forecast
par(mfrow=c(1,1))
plot(arima.forecast, main = "ARIMA Forecast")
# Creating a series of forecasted returns for the forecasted period
forecasted_series = rbind(forecasted_series,arima.forecast$mean[1])
colnames(forecasted_series) = c("Forecasted")
# Creating a series of actual returns for the forecasted period
actual_return = stock[(b+1),]
actual_series = c(Actual_series,xts(Actual_return))
rm(actual_return)
print(stock_prices[(b+1),])
print(stock_prices[(b+2),])
# }
```
# Parallel Sliding windows
```{r}
no_cores <- detectCores() - 2 # 8 cores on server
cluster <- makeCluster(no_cores)
registerDoParallel(cluster)
```
```{r}
#Vector output
foreach(exponent = 1:5, .combine = c) %dopar% base^exponent
#Matrix output
foreach(exponent = 1:5, .combine = rbind) %dopar% base^exponent
#List output
foreach(exponent = 1:5, .combine = list, .multicombine=TRUE) %dopar% base^exponent
stopImplicitCluster()
```