-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.tex
More file actions
324 lines (267 loc) · 10.8 KB
/
code.tex
File metadata and controls
324 lines (267 loc) · 10.8 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
\section {Appendix}
% ------
% R Code
% ------
\subsection{Bitcoin - Time Series Linear Regression}
\begin{minted}{r}
require(jsonlite)
##############################################
# get.polo.url()
#
# Creates a poloniex api call url
# Either for training (train=T) or testing (train=F)
# Testing end is 90 days past training end
#
# @param start, default=1
# The start of your training dataset
# @param end, default=1
# The end of your training dataset
# @param pair, default="USDT_BTC"
# Token pairs, taken from the market list on https://poloniex.com/
# @param period, default=86400
# Period of dataset in seconds, default=1 day
# @param train, default=TRUE
# Whether you want the training or testing url
# @returns string
# API URL for accessing poloniex data
##############################################
get.polo.url <- function(start=1, end=1, pair="USDT_BTC", period=86400, train=TRUE) {
# Returns OHLC + Volume
POLO_URL <- "https://poloniex.com/public?command=returnChartData¤cyPair=%s&start=%d&end=%d&period=%i"
# Returns tick by tick trades (max 50000)
# POLO_URL2 <- "https://poloniex.com/public?command=returnTradeHistory¤cyPair=%s&start=%d&end=%d"
START <- as.numeric(as.POSIXct(sprintf("2013-%d-01", start)), tz="GMT")
END <- as.numeric(as.POSIXct(sprintf("2017-%d-01", end)), tz="GMT")
PRED_START <- as.numeric(as.POSIXct(sprintf("2017-%d-01", end)), tz="GMT")
PRED_END <- as.numeric(as.POSIXct(sprintf("2017-%d-01", end)), tz="GMT")+7776000
# PERIOD_VALUES <- c(300, 7200, 86400)
# 5min, 2hours, 24hours
PREDICTION_LENGTH <- 90*86400/PERIOD # days
if(train) {
return(sprintf(POLO_URL, pair, START, END, period))
}
else {
return(sprintf(POLO_URL, pair, PRED_START, PRED_END, period))
}
}
# -------------------------------
# Data Creation and Visualization
# -------------------------------
df <- fromJSON(get.polo.url(start=1, end=1))
pred_df <- fromJSON(get.polo.url(start=1,end=1,train=F))
train <- ts(df[8])
test <- ts(pred_df[8])
plot(log(c(train, test)), type="l", xlab="Time (Days)", ylab="Log of Volume Weighted Average Price", main="VWAP vs Time")
lines(x=seq(length(train)+1, length(train)+length(test)), y=log(test), col="red")
# -----------
# Differences
# -----------
plot(diff(train), ylab="Lagged Difference of VWAP", xlab="Time (Days)", main="diff(VWAP) vs Time")
abline(h=0)
# --------------
# ACFs and PACFs
# --------------
acf(diff(train), lag.max=50, main="ACF of diff(train)")
pacf(diff(train), lag.max=50, main="PACF of diff(train)")
acf(diff(diff(train)), lag.max=50, main="ACF of diff(diff(train))")
pacf(diff(diff(train)), lag.max=50, main="PACF of diff(diff(train))")
# --------------------
# Model and Prediction
# --------------------
ARIMA <- arima(train, order=c(1,1,0), seasonal=list(order=c(1,1,1), period=41))
print(AIC(ARIMA))
print(BIC(ARIMA))
ARIMA.pred <- predict(ARIMA, n.ahead=PREDICTION_LENGTH)
error <- abs((c(test)-ARIMA.pred$pred))
error <- ((error/max(error))*(min(log(test))/max(log(test))))+min(log(test))
plot(log(test), lwd=2, xlab="Time (Days)", ylab="Log of VWAP", main="Log of Bitcoin VWAP\nPrediction compared to Actual Prices")
lines(x=seq(along=test), log(ARIMA.pred$pred), col="blue", lwd=1)
plot(log(ts(df2[8])), type='l', main="Log of Bitcoin VWAP with ARIMA prediction", ylab="Log of VWAP")
lines(x=seq(length(series)+1, length(series)+length(ARIMA.pred$pred)), y=log(ARIMA.pred$pred), col="red")
# ------------------------
# Recurrent Neural Network
# ------------------------
library(rnn)
df <- fromJSON(get.polo.url(start=1, end=1))
X = matrix(1:680, nrow=34)
Y = matrix(df[1:680,8], nrow=34)
# Conform to interval [0,1]
X <- (X-min(X)) / (max(X)-min(X))
Y <- (Y-min(Y)) / (max(Y)-min(Y))
# Transpose
X <- t(X)
Y <- t(Y)
train <- 1:15
test <- 16:20
model <- trainr(Y = Y[train,],
X = X[train,],
learningrate = 0.05,
hidden_dim = c(16,8),
numepochs = 1000)
Y.pred <- predictr(model, X)
plot(as.vector(t(Y)), col="red", type="l", main="Actual vs Predicted", ylab="Y, Y.pred")
lines(as.vector(t(Y.pred)), type="l", col="blue")
\end{minted}
% ------------
% Python Codes
% ------------
\subsection{\textit{k} and \textit{k}++ means clustering}
\begin{minted}{python}
import numpy as np
import scipy
import matplotlib.pyplot as plt
class Kmeans(object):
def __init__(self, data_as_txt, k, seed=None):
self.k = k
self.data_as_txt = data_as_txt
self.seed = seed
def load_dataset(self, name):
return np.loadtxt(name)
def euclidian(slef, a, b):
return np.linalg.norm(a-b) # magnitude between two points
def plot(self, dataset, history_centroids, belongs_to):
colors = ['r', 'g', 'y', 'c']
fig, ax = plt.subplots()
for index in range(dataset.shape[0]):
instances_close = [i for i in range(len(belongs_to)) if belongs_to[i] == index]
for instance_index in instances_close:
ax.plot(dataset[instance_index][0], dataset[instance_index][1], (colors[index] + 'o'))
history_points = []
for index, centroids in enumerate(history_centroids):
for inner, item in enumerate(centroids):
if index == 0:
history_points.append(ax.plot(item[0], item[1], 'bo')[0])
else:
history_points[inner].set_data(item[0], item[1])
plt.pause(0.8)
''' num_instances: number of rows in data (i.e how many points)
dataset: the data
return: list of k randomly selected data points in a list: [[x1 y1] [x2 y2] ... [xk yk]]
'''
def initialization(self, dataset):
if self.seed is not None:
np.random.seed(self.seed)
num_instances, num_features = dataset.shape # 45, 2
return dataset[np.random.choice(num_instances - 1, self.k, replace=False)]
#return dataset[np.random.randint(0, num_instances - 1, size = self.k)]
def kmeans(self, k, epsilon=0, distance='euclidian'):
history_centroids = []
if distance == 'euclidian':
dist_method = self.euclidian
dataset = self.load_dataset(self.data_as_txt)
# dataset = dataset[:, 0:dataset.shape[1] - 1]
num_instances, num_features = dataset.shape # 45, 2
#prototypes = dataset[np.random.randint(0, num_instances - 1, size=k)]
prototypes = self.initialization(dataset)
history_centroids.append(prototypes)
prototypes_old = np.zeros(prototypes.shape)
belongs_to = np.zeros((num_instances, 1))
norm = dist_method(prototypes, prototypes_old)
iteration = 0
while norm > epsilon:
iteration += 1
norm = dist_method(prototypes, prototypes_old)
prototypes_old = prototypes
for index_instance, instance in enumerate(dataset):
dist_vec = np.zeros((k, 1))
for index_prototype, prototype in enumerate(prototypes):
dist_vec[index_prototype] = dist_method(prototype,
instance)
belongs_to[index_instance, 0] = np.argmin(dist_vec)
tmp_prototypes = np.zeros((k, num_features))
for index in range(len(prototypes)):
instances_close = [i for i in range(len(belongs_to)) if belongs_to[i] == index]
prototype = np.mean(dataset[instances_close], axis=0)
# prototype = dataset[np.random.randint(0, num_instances, size=1)[0]]
tmp_prototypes[index, :] = prototype
prototypes = tmp_prototypes
history_centroids.append(tmp_prototypes)
#self.plot(dataset, history_centroids, belongs_to)
return prototypes, history_centroids, belongs_to
def execute(self, graph=True):
dataset = self.load_dataset(self.data_as_txt)
centroids, history_centroids, belongs_to = self.kmeans(self.k)
if(graph):
self.plot(dataset, history_centroids, belongs_to)
return centroids
class Kpp(Kmeans):
def initialization(self, dataset):
X = dataset
print(X)
C = [X[0]]
print(C)
for k in range(1, self.k):
# find shortest distantce squared to center
D2 = scipy.array([min([scipy.inner(c-x, c-x) for c in C]) for x in X])
probs = D2/D2.sum()
cumprobs = probs.cumsum()
print(cumprobs)
r = scipy.rand()
for j, p in enumerate(cumprobs):
if r < p:
i = j
break
C.append(X[i])
return np.array(C)
if __name__ == "__main__":
kpp_means = Kpp("pts2.txt", 4)
#centroids = kpp_means.execute(graph=True)
kmeans = Kmeans("pts2.txt", 4)
#centroids = kmeans.execute(graph=True)
all_kmeans_centroids = []
all_kpp_means_centroids = []
for _ in range(1, 1000):
kmeans_centroids = kmeans.execute(graph=False)
all_kmeans_centroids.append(kmeans_centroids)
for _ in range(1, 1000):
kpp_centroids = kpp_means.execute(graph=False)
all_kpp_means_centroids.append(kpp_centroids)
with open("KmeansCentroids.txt", mode='w') as File:
for i in all_kmeans_centroids:
for j in i:
File.write("{} {} ".format(j[0], j[1]))
File.write("\n")
with open("KppCentroids.txt", mode='w') as File:
for i in all_kpp_means_centroids:
for j in i:
File.write("{} {} ".format(j[0], j[1]))
File.write("\n")
\end{minted}
% ------
% R code
% ------
\subsection {Centroid Plots and Distribution of SSE}
\begin{minted}{r}
# --------------
# Plot centroids
# --------------
points <- read.table("pts2.txt")
kmeansCentroids <- read.csv("KmeansCentroids.txt", sep=" ", header=FALSE, na.strings="nan")[1:8]
kppCentroids <- read.csv("KppCentroids.txt", sep=" ", header=FALSE, na.strings="nan")[1:8]
plot(points, col=rgb(0,0,0,0.5))
for(i in 1:4) {
points(x=kmeansCentroids[,1*i], y=kmeansCentroids[,2*i], pch=21, col=i+1, bg=rgb(0.1,0.1,0.1,0.1))
}
plot(points, col=rgb(0,0,0,0.5))
for(i in 1:4) {
points(x=kppCentroids[,1*i], y=kppCentroids[,2*i], pch=25, col=i+1, bg=rgb(0.1,0.1,0.1,0.1))
# ------------------------
# Show distribution of SSE
# ------------------------
x <- read.csv("kmeans_sse.csv", sep=" ", header=FALSE)
x <- unlist(x, use.names=FALSE)
hist(x, main="Frequency of Sum of Mean Squared\n Errors in 100 k means trials", xlab="Sum of Mean Squared Errors", breaks=100)
mu <- mean(x)
variance <- var(x)
print(mu)
print(variance)
print(IQR(x))
x <- read.csv("kpp_sse.csv", sep=" ", header=FALSE)
x <- unlist(x, use.names=FALSE)
hist(x, main="Frequency of Sum of Mean Squared\n Errors in 100 k++ trials", xlab="Sum of Mean Squared Errors", breaks=100)
mu <- mean(x)
variance <- var(x)
print(mu)
print(variance)
print(IQR(x))
\end{minted}