-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFrom_probabilities_to_bet_size.py
60 lines (40 loc) · 1.83 KB
/
From_probabilities_to_bet_size.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
def getSignal(events,stepSize,prob,pred,numClasses,numThreads,**kargs):
#get signals from predictions
if prob.shape[0]==0:return pd.Series()
#1)generate signals from multinomial classification (one-vs-rest,OvR)
signal0=(prob-1./numClasses)/(prob*(1.-prob))**.5#t_value of OvR
signal0=pred*(2*norm.cdf(signal0)-1)# signal=side*size
if 'side' in events:signal0*=events.loc[signal0.index,'side']#meta-labeling
#2)compute acerage signal among those concurrently open
df0=signal0.to_frame('signal').join(events[['t1']],how='left')
df0=avgActiveSignals(df0,numThreads)
signal1=discreteSignal(signal0=df0,stepSize=stepSize)
return signal1
def acgActiveSignals(signals,numThreads):
#compute the average signal among those active
#1)time points where signals change (either one starts or one ends)
tPnts=set(signals['t1'].dropna().values)
tPnts=tPnts.union(signals.index.values)
tPnts=list(tPnts);tPnts.sort
out=mpPandasObj(mpAvgActiveSignals,('molecule',tPnts),numThreads,signals=signals)
return out
def mpAvgActiveSignals(signals,molecule):
'''
at time loc,average signal among those still active
Signal is active if:
a) issued before or at loc AND
b) loc before signal's endtime,or endtime is still unknown (NaT)
'''
out=pd.Series()
for loc in molecule:
df0=(signals.index.values<=loc)&((loc<signals['t1'])|pd.isnull(signals['t1']))
act=signals[df0].index
if len(act)>0:out[loc]=signals.loc[act,'signal'].mean()
else:out[loc]=0#no signals active at this time
return out
def discreteSignal(signal0,stepSize):
#discretize signal
signal1=(signal0/stepSize).round()*stepSize#discretize
signal1[signal1>1]=1# cap
signal1[signal1<-1]=-1# floor
return signal1