-
Notifications
You must be signed in to change notification settings - Fork 3
/
stat_models.py
55 lines (41 loc) · 1.84 KB
/
stat_models.py
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
import numpy as np
from sklearn.neighbors import KNeighborsRegressor
from numba import jit
# //TODO K* see why -1 doesn't work.
@jit(forceobj=True)
def knn(distances, column):
predictions = np.zeros(10)
for neighbours in range(1, 11):
neigh = KNeighborsRegressor(n_neighbors=neighbours, weights='distance')
neigh.fit(np.array(distances[column]).reshape(-1, 1), np.array(distances.returns))
predictions[neighbours - 1] = neigh.predict([[np.mean(np.sort(np.array(distances[column]))[:neighbours])]])
if np.sum(predictions > 0) >= 5:
return 1
else:
return -1
# https://github.com/kfirkfir/k-Star-Nearest-Neighbors/blob/master/kStarNN.m
@jit(forceobj=True)
def kstar(distances, column):
l_c = [0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10]
predictions = np.zeros(len(l_c))
for count, lc in enumerate(l_c):
distances = distances.sort_values(by=column)
n = len(distances)
beta = lc * distances[column]
l_lambda = beta[1] + 1 # Otherwise it will never go into the while loop - go figure research papers...
k, sum_beta, sum_beta_square = 0, 0, 0
np.seterr(invalid='ignore') # High change sqrt has negative value in it.
while l_lambda > beta[k + 1] and k <= n - 3: # -3 because otherwise will run into issues.
k += 1
sum_beta = sum_beta + beta[k]
sum_beta_square = sum_beta_square + (beta[k]) ** 2
l_lambda = (1 / k) * (sum_beta + np.sqrt(k + sum_beta ** 2 - k * sum_beta_square))
alpha = np.zeros(n)
for i in range(n):
alpha[i] = np.max(l_lambda - lc * distances[column][i], 0)
alpha[i] = alpha[i] / np.sum(alpha[i])
predictions[count] = np.sum(alpha * distances.returns)
if np.sum(predictions > 0) >= 5:
return 1
else:
return -1