2

I am trying to better understand how various parts of the mosek optimizer work and cannot quite understand the logic of the following constraints etc.

It I have the following code:

n = 3
x0 = [-20.0, -50.0, -10.0]
t = [0.01, 0.01, 0.01]

TC = flattenBook(n, x0, t)

def flattenBook(n, x0, t):
    M = Model("Simple Portfolio")
    M.setLogHandler(sys.stdout)

    ## can be long and short
    x = M.variable("x", n, Domain.unbounded())

    ## helper variable for buy/sell positions
    z = M.variable("z", n, Domain.unbounded())

    ## find long positions
    l = M.variable("l", n, Domain.greaterThan(0.0))
    M.constraint('long1', Expr.sub(l,x0), Domain.greaterThan(0.0))

    M.constraint('buy',  Expr.sub(z,Expr.sub(x,x0)),  Domain.greaterThan(0.0))
    M.constraint('sell', Expr.sub(z,Expr.sub(x0,x)),  Domain.greaterThan(0.0))

    M.constraint("longeqshort",  Expr.sum(x), Domain.equalsTo(0.0))
    M.objective('obj', ObjectiveSense.Minimize, Expr.dot(z, t))

    M.solve()
    if True:
        print "x:"
        print x.level()

The results are as follows:

[60.0, -50.0, -10.0]

Which are correct but can someone confirm the logic for the l variable. My understanding is that the long1 constraint forces l to be only the positive values from the x0 array, is that correct? And if so why?

Based on the examples on the Mosek website I have taken this logic from the buy/sell constraints for the costs of trading.

bpython
  • 241
  • 5
  • 15
  • Don't define n in line 1. $n$ is just len(x0). Your logic is hard to understand. You seem to have an initial position x0 and try to move from there to x. This new position should be neutral (sum x=0) and you try to get there in the cheapest way possible( min z'*t ). You have defined z = abs(x -x0). The variable l is a bit weird here. You can comment out the two lines without changing anything. – tschm Jun 30 '16 at 09:32
  • Also note that your problem does not have a unique solution. You need to buy 80 to get to sum(x) = 0. Currently you go from -20 to +60 in the first asset. But you could also go from -10 to +70 in the third asset, or +40 in the first, +40 in the third, etc..... You would have to modify t or enfore some balancing in the move – tschm Jun 30 '16 at 09:34

0 Answers0