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.