Initializing help system before first use

Folio - example from 'Getting started' executed in MATLAB


Type: Portfolio optimization
Rating: 2 (easy-medium)
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
File(s): foliomat.m, foliomat.mos, foliomat2.m, foliomat2.mos


foliomat.m
%*******************************************************
%  Mosel Matlab Example Problems
%  =============================
%
%  file foliomat.m
%  `````````````````````
%  Modeling a small LP problem 
%  to perform portfolio optimization.
%  
% (c) 2014 Fair Isaac Corporation
%     author: L.Bertacco, Apr. 2014
%*******************************************************

RET  = [5 17 26 12 8 9 7 6 31 21];
RISK = [2 3 4 9 10];
NA   = [1 2 3 4];
[r,e]=moselexec('foliomat.mos');
objval
frac'

foliomat.mos
(!******************************************************
   Mosel Matlab Example Problems
   =============================

   file foliomat.mos
   `````````````````````
   Modeling a small LP problem 
   to perform portfolio optimization.
   
  (c) 2014 Fair Isaac Corporation
      author: L.Bertacco, Apr. 2014
*******************************************************!)

model "Portfolio optimization with LP - MATLAB"
 uses "mmxprs"

 declarations
  SHARES: range
  RISK: set of integer             
  NA: set of integer               
  RET: array(SHARES) of real       
  frac: array(SHARES) of mpvar   
  simplexiter: integer
 end-declarations

 initializations from "matlab.mws:"
   RISK NA RET
 end-initializations 
 
 Return:= sum(s in SHARES) RET(s)*frac(s) 
 sum(s in RISK) frac(s) <= 1/3
 sum(s in NA) frac(s) >= 0.5
 sum(s in SHARES) frac(s) = 1
 forall(s in SHARES) frac(s) <= 0.3
 maximize(Return)
 simplexiter:=getparam("XPRS_simplexiter")

 initializations to "matlab.mws:"
  simplexiter
  evaluation of getobjval as "objval"
  evaluation of array(s in SHARES) frac(s).sol as "frac"
 end-initializations 
 exit(getprobstat)

end-model 

foliomat2.m
%*******************************************************
%  Mosel Matlab Example Problems
%  =============================
%
%  file foliomat2.m
%  ``````````````````
%  Running a small LP problem with different parameters
%  and postprocessing results
%  
% (c) 2014 Fair Isaac Corporation
%     author: L.Bertacco, Apr. 2014
%*******************************************************

NAMES  ={'treasury' 'hardware' 'theater' 'telecom' 'brewery' 'highways' 'cars'    'bank'      'software' 'electronics'};
RET    =[ 5          17         26        12        8         9          7         6           31         21 ];
DEV    =[ 0.1        19         28        22        4         3.5        5         0.5         25         16 ];
COUNTRY={'Canada'   'USA'      'USA'     'USA'     'UK'      'France'   'Germany' 'Luxemburg' 'India'     'Japan'};

RISK_N  ={'hardware' 'theater' 'telecom' 'software' 'electronics'};
NA_N    ={'treasury' 'hardware' 'theater' 'telecom'};

RISK=cellfun(@(n) strmatch(n,NAMES,'exact'), RISK_N); % find indices of high-risk values among shares
NA  =cellfun(@(n) strmatch(n,NAMES,'exact'), NA_N);   % find indices of shares issued in N.-America

for m=1:9
  moselexec('foliomat2.mos',['MAXRISK=' num2str(m/10)]);
  obj(m)=objval;
  optimal(m)=optsol;
  fracm(m,:)=frac;
end

disp('Results');
disp('Estimated returns:');
disp(table([1:9]'/10,obj','VariableNames',{'MaxRisk' 'Return'}))

disp('Average share utilization:');
disp(table(NAMES',mean(fracm)','VariableNames',{'Share' 'AverageUsage'}))

ribbon(fracm)
title('Share utilization')
set(gca,'XTick',[1:size(fracm,2)])
set(gca,'XTickLabel',NAMES)
set(gca,'YDir','reverse')
set(gca,'YTick',[1:9])
set(gca,'YTickLabel',[1:9]/10)
set(gca,'ZLim',[0,max(reshape(fracm,1,[]))])

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