-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlphaInvest_batch.py
54 lines (41 loc) · 1.87 KB
/
AlphaInvest_batch.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
import numpy as np
class ALPHA_proc_batch:
def __init__(self, alpha0, numhyp, startfac):
self.alpha0 = alpha0
self.wealth_vec = np.zeros(numhyp+1)
self.wealth_vec[0] = startfac*self.alpha0
self.alpha = np.zeros(numhyp + 1)
self.phi_fac = 0.25
self.phi_k = self.phi_fac*self.wealth_vec[0]
#self.alpha[0:2] = [0, self.phi_k/(1 + self.phi_k)] # preset alpha_1 for usage. note alpha vec always one longer than wealth vec
self.alpha[0:2] = [0, self.wealth_vec[0]/2]
self.b0 = self.alpha0 - self.wealth_vec[0]
def run_fdr(self, pvec):
numhyp = len(pvec)
last_rej = 0
rej = np.zeros(numhyp+1)
for k in range(0, numhyp-1):
if self.wealth_vec[k] > 0:
# Get rejection
this_alpha = self.alpha[k + 1] # make sure first one doesn't do bullshit
rej[k + 1] = (pvec[k] < this_alpha)
# Calc wealth
if (rej[k + 1] == 1):
last_rej = k + 1
# # Update wealth
wealth = self.wealth_vec[k] - (1-rej[k + 1])*this_alpha/(1-this_alpha) + rej[k + 1]*self.b0
self.wealth_vec[k + 1] = wealth
# Calc new alpha
next_alpha = min(wealth/(1 + k + 2 - last_rej), ((1-self.phi_fac)*wealth)/((1-self.phi_fac)*wealth+1))
# # Update wealth and alpha
# wealth = self.wealth_vec[k] - self.phi_k + rej[k + 1]*(self.b0 + self.phi_k)
# self.wealth_vec[k + 1] = wealth
# self.phi_k = self.phi_fac*wealth
# next_alpha = self.phi_k/(1+self.phi_k)
self.alpha[k + 2] = next_alpha
else:
break
#pdb.set_trace()
self.alpha = self.alpha[1:]
rej = rej[1:]
return rej