Hi Pravin,
There are a bunch of references here:
https://www.quantopian.com/posts/on-line-portfolio-selection-from-grant-k
Here are some code snippets:
###########################
# Inside of OLMAR (algo 2)
x_bar = x_tilde.mean()
# Calculate terms for lambda (lam)
dot_prod = np.dot(b_t, x_tilde)
num = context.eps - dot_prod
denom = (np.linalg.norm((x_tilde-x_bar)))**2
# test for divide-by-zero case
if denom == 0.0:
lam = 0 # no portolio update
else:
lam = max(0, num/denom)
b = b_t + lam*(x_tilde-x_bar)
b_norm = simplex_projection(b)
weight = np.dot(b_norm,x_tilde)
return (b_norm, weight)
def simplex_projection(v, b=1):
"""Projection vectors to the simplex domain
Implemented according to the paper: Efficient projections onto the
l1-ball for learning in high dimensions, John Duchi, et al. ICML 2008.
Implementation Time: 2011 June 17 by [email protected] AT pmail.ntu.edu.sg
Optimization Problem: min_{w}\| w - v \|_{2}^{2}
s.t. sum_{i=1}^{m}=z, w_{i}\geq 0
Input: A vector v \in R^{m}, and a scalar z > 0 (default=1)
Output: Projection vector w
:Example:
>>> proj = simplex_projection([.4 ,.3, -.4, .5])
>>> print proj
array([ 0.33333333, 0.23333333, 0. , 0.43333333])
>>> print proj.sum()
1.0
Original matlab implementation: John Duchi ([email protected])
Python-port: Copyright 2012 by Thomas Wiecki ([email protected]).
"""
v = np.asarray(v)
p = len(v)
# Sort v into u in descending order
v = (v > 0) * v
u = np.sort(v)[::-1]
sv = np.cumsum(u)
rho = np.where(u > (sv - b) / np.arange(1, p+1))[0][-1]
theta = np.max([0, (sv[rho] - b) / (rho+1)])
w = (v - theta)
w[w<0] = 0
return w
So, you just need to compute your x_tilde
(I think).
Note also that the so-called BAH(OLMAR) is a simple matter of doing a weighted average over expected returns over an expanding trailing window. Not sure how this maps onto your strategy, but you should probably consider some way of sampling/smoothing, since otherwise, you could end up with choppy returns.
Hope this helps. Just let me know if you need anything else. This weekend, I might get the chance to work up a simple running example.
Grant