@@ -78,9 +78,7 @@ def main():
7878 domain = i ,
7979 description = "Indicator variable for assets included in the portfolio" ,
8080 )
81- PortVariance = Variable (
82- m , name = "PortVariance" , description = "Portfolio variance"
83- )
81+ PortVariance = Variable (m , name = "PortVariance" , description = "Portfolio variance" )
8482 PortReturn = Variable (m , name = "PortReturn" , description = "Portfolio return" )
8583
8684 # In case short sales are allowed these bounds must be set properly.
@@ -168,10 +166,8 @@ def main():
168166 if lamda_loop > 1 :
169167 break
170168 lamda [...] = lamda_loop
171- MeanVarMip .solve (
172- options = Options (minlp = "SHOT" , relative_optimality_gap = 0 )
173- )
174- MeanVarianceMIP += f"{ round (lamda_loop ,1 )} ,{ round (MeanVarMip .objective_value ,4 )} ,{ round (PortVariance .records .level [0 ],4 )} ,{ round (PortReturn .records .level [0 ],4 )} ,"
169+ MeanVarMip .solve (solver = "SHOT" , options = Options (relative_optimality_gap = 0 ))
170+ MeanVarianceMIP += f"{ (lamda_loop )} ,{ round (MeanVarMip .objective_value , 4 )} ,{ round (PortVariance .records .level [0 ], 4 )} ,{ round (PortReturn .records .level [0 ], 4 )} ,"
175171 x_recs = [str (round (x_rec , 4 )) for x_rec in x .records .level .tolist ()]
176172 MeanVarianceMIP += "," .join (x_recs )
177173 MeanVarianceMIP += "\n "
@@ -277,23 +273,15 @@ def main():
277273 if lamda_loop > 1 :
278274 break
279275 lamda [...] = lamda_loop
280- MeanVarWithCost .solve (
281- options = Options (minlp = "SHOT" , relative_optimality_gap = 0 )
282- )
283- MeanVarianceWithCost += f"{ round (lamda_loop ,1 )} ,{ round (MeanVarWithCost .objective_value ,4 )} ,{ round (PortVariance .records .level [0 ],4 )} ,{ round (PortReturn .records .level [0 ],4 )} ,"
284- x0_recs = [
285- str (round (x_rec , 4 )) for x_rec in x_0 .records .level .tolist ()
286- ]
287- x1_recs = [
288- str (round (x_rec , 4 )) for x_rec in x_1 .records .level .tolist ()
289- ]
276+ MeanVarWithCost .solve (solver = "SHOT" , options = Options (relative_optimality_gap = 0 ))
277+ MeanVarianceWithCost += f"{ round (lamda_loop , 1 )} ,{ round (MeanVarWithCost .objective_value , 4 )} ,{ round (PortVariance .records .level [0 ], 4 )} ,{ round (PortReturn .records .level [0 ], 4 )} ,"
278+ x0_recs = [str (round (x_rec , 4 )) for x_rec in x_0 .records .level .tolist ()]
279+ x1_recs = [str (round (x_rec , 4 )) for x_rec in x_1 .records .level .tolist ()]
290280 MeanVarianceWithCost += "," .join (x0_recs ) + ","
291281 MeanVarianceWithCost += "," .join (x1_recs ) + "\n "
292282 lamda_loop += 0.1
293283
294- with open (
295- "MeanVarianceWithCost.csv" , "w" , encoding = "UTF-8"
296- ) as FrontierHandleTwo :
284+ with open ("MeanVarianceWithCost.csv" , "w" , encoding = "UTF-8" ) as FrontierHandleTwo :
297285 FrontierHandleTwo .write (MeanVarianceWithCost )
298286
299287 # ***** Portfolio Revision *****
@@ -304,9 +292,7 @@ def main():
304292
305293 BuyLimits = Parameter (m , name = "BuyLimits" , domain = [Bound , i ])
306294 SellLimits = Parameter (m , name = "SellLimits" , domain = [Bound , i ])
307- InitHold = Parameter (
308- m , name = "InitHold" , domain = i , description = "Current holdings"
309- )
295+ InitHold = Parameter (m , name = "InitHold" , domain = i , description = "Current holdings" )
310296
311297 # We set the curret holding to the optimal unconstrained mean-variance portfolio
312298 # with lamda = 0.5
@@ -407,9 +393,7 @@ def main():
407393 objective = ObjDef ,
408394 )
409395
410- MeanVarianceRevision = (
411- '"Model status","Lambda","z","Variance","ExpReturn",'
412- )
396+ MeanVarianceRevision = '"Model status","Lambda","z","Variance","ExpReturn",'
413397
414398 MeanVarianceRevision += "," .join (i_recs ) + ","
415399 MeanVarianceRevision += "," .join (i_recs ) + ","
@@ -420,25 +404,17 @@ def main():
420404 if lamda_loop > 1 :
421405 break
422406 lamda [...] = lamda_loop
423- MeanVarRevision .solve (
424- options = Options (minlp = "SHOT" , relative_optimality_gap = 0 )
425- )
426- MeanVarianceRevision += f"{ MeanVarRevision .status } ,{ round (lamda_loop ,1 )} ,{ round (MeanVarRevision .objective_value ,4 )} ,{ round (PortVariance .records .level [0 ],4 )} ,{ round (PortReturn .records .level [0 ],4 )} ,"
407+ MeanVarRevision .solve (solver = "SHOT" , options = Options (relative_optimality_gap = 0 ))
408+ MeanVarianceRevision += f"{ MeanVarRevision .status } ,{ round (lamda_loop , 1 )} ,{ round (MeanVarRevision .objective_value , 4 )} ,{ round (PortVariance .records .level [0 ], 4 )} ,{ round (PortReturn .records .level [0 ], 4 )} ,"
427409 x_recs = [str (round (x_rec , 4 )) for x_rec in x .records .level .tolist ()]
428- buy_recs = [
429- str (round (x_rec , 4 )) for x_rec in buy .records .level .tolist ()
430- ]
431- sell_recs = [
432- str (round (x_rec , 4 )) for x_rec in sell .records .level .tolist ()
433- ]
410+ buy_recs = [str (round (x_rec , 4 )) for x_rec in buy .records .level .tolist ()]
411+ sell_recs = [str (round (x_rec , 4 )) for x_rec in sell .records .level .tolist ()]
434412 MeanVarianceRevision += "," .join (x_recs ) + ","
435413 MeanVarianceRevision += "," .join (buy_recs ) + ","
436414 MeanVarianceRevision += "," .join (sell_recs ) + "\n "
437415 lamda_loop += 0.1
438416
439- with open (
440- "MeanVarianceRevision.csv" , "w" , encoding = "UTF-8"
441- ) as FrontierHandleThree :
417+ with open ("MeanVarianceRevision.csv" , "w" , encoding = "UTF-8" ) as FrontierHandleThree :
442418 FrontierHandleThree .write (MeanVarianceRevision )
443419
444420
0 commit comments