-
Notifications
You must be signed in to change notification settings - Fork 15
/
functions.py
40 lines (32 loc) · 1002 Bytes
/
functions.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
import numpy as np
import math
# prints formatted price
def formatPrice(n):
return ("-$" if n < 0 else "$") + "{0:.2f}".format(abs(n))
# returns the vector containing stock data from a fixed file
def getStockDataVec(key):
vec = []
lines = open("data/" + key + ".csv", "r").read().splitlines()
for line in lines[1:]:
vec.append(float(line.split(",")[4]))
return vec
# returns the sigmoid
def sigmoid(x):
try:
if x < 0:
return 1 - 1 / (1 + math.exp(x))
return 1 / (1 + math.exp(-x))
except OverflowError as err:
print("Overflow err: {0} - Val of x: {1}".format(err, x))
except ZeroDivisionError:
print("division by zero!")
except Exception as err:
print("Error in sigmoid: " + err)
# returns an an n-day state representation ending at time t
def getState(data, t, n):
d = t - n + 1
block = data[d:t + 1] if d >= 0 else -d * [data[0]] + data[0:t + 1] # pad with t0
res = []
for i in range(n - 1):
res.append(sigmoid(block[i + 1] - block[i]))
return np.array([res])