Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Delgado authored and Dominic Delgado committed May 31, 2016
2 parents 8049585 + 0801206 commit cea085e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 33 deletions.
2 changes: 1 addition & 1 deletion expert_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ExpertPool(Portfolio):
def __init__(self, market_data, experts, start=0, stop=None, init_weights=None,
rebal_interval=1, tune_interval=None,
init_b=None, init_dollars=init_dollars,
weighting_strategy='exp_window', windows=[10], ew_alpha=0.5, ew_eta=0.1,
weighting_strategy='exp_window', windows=[10], ew_alpha=0.5, ew_eta=0.8,
verbose=False, silent=False, past_results_dir=None, new_results_dir=None, repeat_past=False):

if not isinstance(market_data, MarketData):
Expand Down
13 changes: 6 additions & 7 deletions olmar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ class OLMAR(Portfolio):
Online Moving Average Reversion (OLMAR) Portfolio
Introduced by Li and Hoi. "On-Line Portfolio Selection with Moving Average Reversion"
http://icml.cc/2012/papers/168.pdf
"""
def __init__(self, market_data, market_data_train=None, start=0, stop=None, window=20, eps=1.1, rebal_interval=1,
window_range=range(16, 26, 2), eps_range=[1.1, 1.2, 1.3, 1.4, 1.5], tune_interval=None,
def __init__(self, market_data, market_data_train=None, start=0, stop=None, window=10, eps=1.3, rebal_interval=1,
window_range=range(5, 30, 3), eps_range=np.arange(1.1, 5.1, 0.2), tune_interval=15,
init_b=None, verbose=False, silent=False, past_results_dir=None, new_results_dir=None, repeat_past=False):
"""
Expand Down Expand Up @@ -138,7 +139,6 @@ def get_new_allocation(self, day, init=False):

# limit lambda to avoid numerical problems from acting too aggressively.
# (referenced from marigold's implementation: https://github.com/Marigold/universal-portfolios)
# TODO: check if this is necessary
lam = min(100000, lam)

# Note: we don't perform simplex project b/c negative values (shorting) is allowed.
Expand All @@ -156,8 +156,8 @@ def tune_hyperparams(self, cur_day):
# Create new instances of this portfolio with various hyperparameter settings
# to find the best constant hyperparameters in hindsight

tune_duration = 20
if cur_day >= tune_duration:
tune_duration = 10 # Tune over the last 2 weeks
if cur_day > tune_duration:
start_day = cur_day - tune_duration
else:
# Not worth tuning yet
Expand All @@ -166,8 +166,7 @@ def tune_hyperparams(self, cur_day):
hyperparam_space = [self.window_range, self.eps_range]
hyp_combos = list(itertools.product(*hyperparam_space))

#init_b = self.b_history[-tune_duration] # Allocation used at beginning of tuning period
init_b = None
init_b = self.b_history[:,cur_day-tune_duration] # Allocation used at beginning of tuning period

# Compute sharpe ratios for each setting of hyperparams
sharpe_ratios = []
Expand Down
7 changes: 4 additions & 3 deletions portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(self, market_data, market_data_train=None, start=0, stop=None, reba
past_b_history, past_dollars_history = self.load_previous_results(past_results_dir)
self.past_b_history = past_b_history
self.past_dollars_history = past_dollars_history
self.len_past = past_b_history.shape[1]
self.len_past = past_b_history.shape[1] - 1
self.b = past_b_history[:, -1] # Use previous b as initialization (overrides |init_b| argument)
else:
self.past_b_history = None
Expand All @@ -85,7 +85,7 @@ def update(self, cur_day, init=False):

# Check if we need to tune hyperparameters today
if self.tune_interval and not self.repeat_past:
if cur_day > self.start and cur_day % self.tune_interval == 0:
if cur_day > self.start and cur_day % self.tune_interval == 1:
self.tune_hyperparams(cur_day)

self.update_allocation(cur_day, init)
Expand Down Expand Up @@ -212,7 +212,8 @@ def save_results(self):
save_dir = self.new_results_dir

util.save_dollars_history(save_dir=save_dir, dollars=self.dollars_op_history, portfolio_type=self.portfolio_type)
util.save_b_history(save_dir=save_dir, b_history=self.b_history, portfolio_type=self.portfolio_type)
full_b = np.concatenate((self.b_history, self.b.reshape(-1, 1)), axis=1)
util.save_b_history(save_dir=save_dir, b_history=full_b, portfolio_type=self.portfolio_type)
util.save_hyperparams(save_dir=save_dir, hyperparams_dict=self.get_hyperparams_dict(), portfolio_type=self.portfolio_type)
return

Expand Down
29 changes: 7 additions & 22 deletions rmr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class RMR(OLMAR):
http://www.ijcai.org/Proceedings/13/Papers/296.pdf
"""
def __init__(self, market_data, market_data_train=None, start=0, stop=None, window=20, eps=1.3, tau=0.01, max_iter=100,
rebal_interval=1, window_range=range(14, 26, 2), eps_range=[1.1, 1.3, 1.5, 1.7, 1.9, 2.1, 2.3, 2.5],
tune_interval=None, init_b=None, verbose=False, silent=False,
def __init__(self, market_data, market_data_train=None, start=0, stop=None, window=20, eps=1.5, tau=0.001, max_iter=100,
rebal_interval=1, window_range=range(5, 30, 3), eps_range=np.arange(1.1, 5.1, 0.2),
tune_interval=25, init_b=None, verbose=False, silent=False,
past_results_dir=None, new_results_dir=None, repeat_past=False):

self.portfolio_type = 'RMR'
Expand Down Expand Up @@ -99,12 +99,7 @@ def predict_price_relatives(self, day):
mu_avail_full_window = self.T_func(mu_avail_full_window, window_pr_avail_full_window)
L1_dist = np.linalg.norm((prev_mu-mu_avail_full_window), ord=1)
thresh = self.tau * np.linalg.norm(mu_avail_full_window, ord=1)
"""
prev_mu = mu
mu = self.T_func(mu, window_pr_avail_today)
L1_dist = np.linalg.norm((prev_mu-mu), ord=1)
thresh = self.tau * np.linalg.norm(mu, ord=1)
"""

if L1_dist <= thresh:
break

Expand Down Expand Up @@ -156,22 +151,13 @@ def T_func(self, mu, window_cl):

T_tilde = s2 * 1.0 / s1
return T_tilde
"""
gamma = np.linalg.norm(R_tilde, ord=2)
gamma_inv = 1.0 / gamma
return T_bar + min(1.0, gamma_inv) * mu
"""


def tune_hyperparams(self, cur_day):
# Create new instances of this portfolio with various hyperparameter settings
# to find the best constant hyperparameters in hindsight

# TODO: use previous history, if available

tune_duration = 20
if cur_day >= tune_duration:
tune_duration = 10 # Tune over the last 2 weeks
if cur_day > tune_duration:
start_day = cur_day - tune_duration
else:
# Not worth tuning yet
Expand All @@ -180,8 +166,7 @@ def tune_hyperparams(self, cur_day):
hyperparam_space = [self.window_range, self.eps_range]
hyp_combos = list(itertools.product(*hyperparam_space))

#init_b = self.b_history[-tune_duration] # Allocation used at beginning of tuning period
init_b = None
init_b = self.b_history[:,cur_day-tune_duration] # Allocation used at beginning of tuning period

# Compute sharpe ratios for each setting of hyperparams
sharpe_ratios = []
Expand Down

0 comments on commit cea085e

Please sign in to comment.