EFrontier.py

Python Example:


from mplpy import mpl
from numpy import arange, float32
from matplotlib import pyplot as plot


CPLEX_OPTIMAL = 1
iterCount = 100
origTarget = 4.0
deltaTarget = 0.1

model = mpl.model
model.WorkingDir = mpl.ModelDirectory
modelFilename = mpl.ModelDirectory + "Portfolio.mpl"
result = model.ReadModel(modelFilename)

VarianceMacro = model.Variance
MeetTargetCon = model.MeetTarget

TargetArray = arange(iterCount, dtype=float32)
VarianceArray = arange(iterCount, dtype=float32)

for i in range(iterCount):
   targetReturn = origTarget + i * deltaTarget
   MeetTargetCon.RHSValue = targetReturn
   model.Solve(mpl.cplex)
   print(str(i) + ") " + str(targetReturn) + ", " + str(VarianceMacro.Value))
   if model.Solution.ResultCode == CPLEX_OPTIMAL:
      TargetArray[i] = targetReturn / 100.0
      VarianceArray[i] = VarianceMacro.Value
   else:
      solCount = i
      print(str(solCount+1) + ") " + model.Solution.ResultString + " (" + str(model.Solution.ResultCode) + ")")
      break


TargetArray = TargetArray[0:solCount]
VarianceArray = VarianceArray[0:solCount]
print(TargetArray)
print(VarianceArray)

plot.plot(VarianceArray, TargetArray, marker='.', c='r')
plot.title("Efficient Frontier with MPL OptiMax")
plot.grid(True)
plot.xlabel("Risk")
plot.ylabel("Return")
plot.axis([0, 100, 0, 0.14])
plot.show()

        

Back To Top | Maximal Home Page | List of Samples | Previous Page | Next Page