Skip to content

Instantly share code, notes, and snippets.

@raddy
Created September 19, 2013 21:50
Show Gist options
  • Save raddy/6630348 to your computer and use it in GitHub Desktop.
Save raddy/6630348 to your computer and use it in GitHub Desktop.
weighted convex opt svi -- just multiplying through by weights... (thx kevin)
def optimize_weighted(y,max_vi,weights,market_vols,tte,s):
# {{{
y = y[market_vols>0]
vols = market_vols[market_vols>0]
w = weights[market_vols>0]
n = len(y)
# R ends up n by 3
R = np.concatenate([np.ones(n), np.sqrt(y**2+1), y]).reshape(3,n).T
# W is n by n
W = np.diag(w)
Q = 2*matrix(R.T.dot(W).dot(R))
#Y1 = np.sum(y)
#Y2 = np.sum(y**2)
#Y3 = np.sum(np.sqrt(y**2+1))
#Y4 = np.sum(y*np.sqrt(y**2+1))
#Y5 = np.sum(y**2+1)
var_t = vols**2*tte
#v = np.sum(var_t)
#vsq = np.sum(var_t*var_t)
#vY = np.sum(var_t*y)
#vY2 = np.sum(var_t*np.sqrt(y**2+1))
q = -2*matrix(var_t.T.dot(W).dot(R))
h = matrix([0.0, 4.0*s, 0.0, 0.0, 4.0*s, 4.0*s, 0.0, max_vi])
# G is in column-major order
G = matrix([[0.0,0.0,0.0,0.0,0.0,0.0,-1.0,1.0],[-1.0,1.0,-1.0,-1.0,1.0,1.0,0.0,0.0],[0.0,0.0,-1.0,1.0,-1.0,1.0,0.0,0.0]])
cvxopt.solvers.options['show_progress'] = False
sol = solvers.qp(Q,q,G,h)
# I convert back to the notation in the paper here
# to verify that the constraints are satisfied:
a = sol['x'][0]
c = sol['x'][1]
d = sol['x'][2]
if(c < 0 or c > 4*s):
print "Failed first constraint\n"
if(math.fabs(d) > c or math.fabs(d) > ((4*s) -c)):
print "Failed second constraint\n"
# This constraint differs in the paper from the one Josh originally sent;
# I use the one from the paper
if(a < 0.0 or a > max_vi):
print "Failed third constraint\n"
return sol['x']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment