forked from Rarlegend/cs182
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lasso.py
71 lines (61 loc) · 2.37 KB
/
lasso.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from sklearn.linear_model import Lasso
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_boston
from pprint import pprint
import myownq
import random
import util
import math
xData = []
yData = []
selectedKeys = ['"ACCOCI"', '"ASSETS"', '"ASSETSC"', '"ASSETSNC"', '"BVPS"', '"CAPEX"', '"CASHNEQ"', '"COR"', '"CURRENTRATIO"', '"DE"', '"DEBT"', '"DEPAMOR"', '"DILUTIONRATIO"', '"DPS"', '"EBIT"', '"EBITDA"', '"EBT"', '"EPS"', '"EPSDIL"', '"EQUITY"', '"FCF"', '"FCFPS"', '"GP"', '"INTANGIBLES"', '"INTEXP"', '"INVENTORY"', '"LIABILITIES"', '"LIABILITIESC"', '"LIABILITIESNC"', '"NCF"', '"NCFCOMMON"', '"NCFDEBT"', '"NCFDIV"', '"NCFF"', '"NCFI"', '"NCFO"', '"NCFX"', '"NETINC"', '"NETINCCMN"', '"NETINCDIS"', '"PAYABLES"', '"PB"', '"PREFDIVIS"', '"PRICE"', '"RECEIVABLES"', '"RETEARN"', '"REVENUE"', '"RND"', '"SGNA"', '"SHARESWA"', '"SHARESWADIL"', '"TANGIBLES"', '"TAXEXP"', '"TBVPS"', '"WORKINGCAPITAL"']
for i, line in enumerate(f):
lineo += 1
if (lineo == 10000):
break
fList = line.split(",")
date = fList[0]
value = float(fList[1])
ticker = str(fList[2])
#add the -1 thing to get rid of newline character
fundamental = str(fList[3])[:-1]
if (ticker_previous == None):
ticker_previous = ticker
elif (ticker_previous != ticker):
isFirstObservation = True
ticker_previous = ticker
if (fundamental == '"PRICE"'):
currentDateList.append(date)
currentPriceDict[hash(date)] = value
else:
if (date_previous != date):
if (not isFirstObservation):
try:
index = currentDateList.index(date)
except:
index = None
if (index):
observation['"PRICE"'] = currentPriceDict[hash(currentDateList[index])]
if (index + 3 >= len(currentDateList)):
index = len(currentDateList) - 1
else:
index = index + 3
observation['"PRICENEXT"'] = currentPriceDict[hash(currentDateList[index])]
observedData = gradientParser.getObservation(observationPrevious, observation, selectedKeys)
curState = state(observedData)
priceChange = curState.getScore()
xData.append(observedData[0])
yData.append(priceChange)
else:
isFirstObservation = False
observationPrevious = copy.deepcopy(observation)
observation = {}
date_previous = date
observation[fundamental] = value
X = scaler.fit_transform(xData)
Y = yData
names = selectedKeys
lasso = Lasso(alpha=.3)
lasso.fit(X, Y)
print lasso.coef_
print names