-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment_rebalancing.py
110 lines (104 loc) · 4.33 KB
/
experiment_rebalancing.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
MA: "Heuristic Portfolio Rebalancing Optimization Under Transaction Cost"
Gregor Lenhard - Spring 2019
CODE: Experiment
"""
import os
os.chdir(os.path.dirname(os.path.realpath('__file__')))
import numpy as np
import pandas as pd
import standard_functions as func
import portfolio_functions as pf
import multiprocessing as mp
import time
# =============================================================================
# == Data
setting = {'plot_data': False,
'data': "germanstocks.csv",
'i_stock' : np.arange(25),
'i_date': np.arange(3739)}
# == Parameters
para = {'strategy' : [1,2,3,4,5,9],
# 1 = DEx; 2 = DEdx; 3 = NH; 4 = SLSQP, 5=SLSQP_ignore_cost, 9 = EW
'perfectForesight' : [True, False],
'rS' : 0.01/250,
'w0' : 50000,
'T_prior' : 1250,
'T_invest' : 750,
'BL' : 10,
'cvar' : [0, 0.001, 0.005, 0.01, 0.02, 0.05]}
# == Experimental Setting
Experiment = np.arange(100)
Results = pd.DataFrame(columns=['Experiment','cvar','perfForesight', 'strategy',
'sr_Rebal','sr_noRebal','wealth','cost',
'wealth_noRebal','trades','gini','hhi','mu',
'sigma'])
## load data
df = pd.read_csv(setting['data'], sep=";", index_col=0)
y = df.iloc[setting['i_date'], setting['i_stock']]
r = np.diff(np.log(y), axis=0)
# =============================================================================
# Experimental support functions
# =============================================================================
# == write experiment to csv
def write_results():
if os.path.isfile('Results.csv'):
with open('Results.csv', 'a',newline='') as csvFile:
Results.to_csv(csvFile, header=False, index=False)
else:
with open('Results.csv', 'a',newline='') as csvFile:
Results.to_csv(csvFile, header=True, index=False)
# == Log experimental results in Dataframe "Results"
def log_result(PF):
global Results
Results = Results.append({'Experiment': exp,
'cvar': PF['cvar'],
'perfForesight' : PF['perfFore'],
'strategy': PF['strategy'],
'sr_Rebal': PF['sr_Rebal'],
'sr_noRebal': PF['sr_noRebal'],
'wealth': PF['final_w'],
'cost': PF['cost'],
'wealth_noRebal': PF['final_w_noRebal'],
'trades': PF['trades'],
'gini': PF['gini'],
'hhi': PF['hhi'],
'mu': PF['mu_Rebal'],
'sigma': PF['sigma_Rebal']
} , ignore_index=True)
# == run multiple setting simultan
def run_multi_process(strategy, perfectForesight, c_vals,exp):
pool = mp.Pool()
for cvar in c_vals:
pool.apply_async(pf.rebal_SR,
args = (strategy, rSim,para['w0'],
para['T_invest'], para['T_prior'], cvar,
para['rS'],
setting['plot_data'], perfectForesight,
mu, sigma, ),
callback = log_result)
pool.close()
pool.join()
print(f"Experimental Setting No. {exp}. Strategy {strategy},",
f"foresight=={perfectForesight}. Done.")
# =============================================================================
# RUN EXPERIMENT
# =============================================================================
start = time.time()
for exp in Experiment:
np.random.seed(exp)
# simulate data
ySim, rSim = func.bootstrap_y(y, n=para['T_invest'] + para['T_prior'],
BL=para['BL'])
rSim, mu, sigma = func.GBM_PF(rSim, T=para['T_invest'] + para['T_prior'])
# =========================================================================
for strategy in para['strategy']:
for perfectForesight in para['perfectForesight']:
if __name__ == '__main__':
run_multi_process(strategy, perfectForesight, para['cvar'],exp)
# write df to csv
write_results()
# delete results from df
Results = Results.iloc[0:0]
end = time.time()
print(f"Done! Time needed: {np.round((end-start)/60,2)} mins")