FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

Folio - example from 'Getting started' executed in MATLAB

Description
MATLAB interfacing with a Mosel portfolio optimization problem:
  • foliomat.m: MATLAB script to execute the model
  • foliomat.mos: corresponding Mosel model
  • foliomat2.m: extended version with chart display
  • foliomat2.mos: corresponding Mosel model


Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
foliomat.m[download]
foliomat.mos[download]
foliomat2.m[download]
foliomat2.mos[download]





foliomat2.mos

(!******************************************************
   Mosel Matlab Example Problems
   =============================

   file foliomat2.mos
   ``````````````````
   Modeling a small LP problem 
   to perform portfolio optimization.
   -- Parameters, data input from MATLAB, 
      result output to MATLAB --
   
  (c) 2014 Fair Isaac Corporation
      author: L.Bertacco, Apr. 2014
*******************************************************!)

model "Portfolio optimization with LP"
 uses "mmxprs"

 parameters
  DATAFILE= "matlab.mws:"            ! File with problem data
  MAXRISK = 1/3                      ! Max. investment into high-risk values
  MAXVAL = 0.3                       ! Max. investment per share
  MINAM = 0.5                        ! Min. investment into N.-American values
 end-parameters

 writeln("Solving for MAXRISK: ", MAXRISK)
 declarations
  SHARES: range                      ! Set of shares
  NAMES: array(SHARES) of string     ! Names of the shares
  RISK: set of integer               ! Set of high-risk values among shares
  NA: set of integer                 ! Set of shares issued in N.-America
  RET: array(SHARES) of real         ! Estimated return in investment
 end-declarations

 initializations from DATAFILE
  NAMES RISK RET NA
 end-initializations

 declarations
  frac: array(SHARES) of mpvar       ! Fraction of capital used per share
 end-declarations

! Objective: total return
 Return:= sum(s in SHARES) RET(s)*frac(s) 

! Limit the percentage of high-risk values
 sum(s in RISK) frac(s) <= MAXRISK

! Minimum amount of North-American values
 sum(s in NA) frac(s) >= MINAM

! Spend all the capital
 sum(s in SHARES) frac(s) = 1
 
! Upper bounds on the investment per share
 forall(s in SHARES) frac(s) <= MAXVAL

! Solve the problem
 maximize(Return)

! Solution printing to a file
 writeln("Total return: ", getobjval)
 forall(s in SHARES) 
  writeln(strfmt(NAMES(s),-12), ": \t", strfmt(getsol(frac(s))*100,5,2), "%")
  
 initializations to "matlab.mws:"
  evaluation of getobjval as "objval"
  evaluation of getprobstat=XPRS_OPT as "optsol"
  evaluation of array(s in SHARES) frac(s).sol as "frac"
 end-initializations 

end-model 

Back to examples browserPrevious exampleNext example