Modeling with GAMS: Part I
University of ChicagoBooth School of Business
Kipp Martin
February 15, 2012
1
Motivation
Where we have been:
I Learned the fundamentals of VBA - ranges, if-then logic,looping, arrays, etc.
I Learned the fundamentals of MATLAB - matrices, if-thenlogic, looping, arrays, etc
Where we are going:
I Use VBA and MATLAB to gather data to populate and solvea GAMS model
I Learn some interesting applications
4
Motivation
Consider the following scenario:
I Take a fixed amount of money and invest it among six mutualfunds
I Future returns are uncertain for each mutual fund
I We would like to maximize our expected return
I We want to protect our downside risk
6
Motivation
A Model: define the following decision variables:
FS = PROPORTION OF PORTFOLIO INVESTED IN FOREIGN STOCK FUND
IB = PROPORTION OF PORTFOLIO INVESTED IN INTERMEDIATE-TERM BOND FUND
LG = PROPORTION OF PORTFOLIO INVESTED IN LARGE-CAP GROWTH FUND
LV = PROPORTION OF PORTFOLIO INVESTED IN LARGE-CAP VALUE FUND
SG = PROPORTION OF PORTFOLIO INVESTED IN SMALL-CAP GROWTH FUND
SV = PROPORTION OF PORTFOLIO INVESTED IN SMALL-CAP VALUE FUND
For example, if FS = .25, LV = .5, and SG = .25 then we wouldtake our money (however much that happens to be) and invest25% in the foreign stock fund, 50% in the large-cap value fund,and 25% in the small-cap growth fund.
The variable values should sum to 1.0.
8
Motivation
KEY ASSUMPTIONS: We treat each year as a scenario andconsider these to be the five possible states of nature into thefuture. We also assume each scenario to be equally likely (occurwith probability .20).
Now introduce an additional set of variables that measure thereturn of the portfolio under each scenario. That is:
Ri is the the return of the portfolio under scenario i .
For example, the return of the portfolio with FS = .25, LV = .5,and SG = .25 under scenario 2 (Year2) is:
R2 = .25*13.12 + .5*20.61 + .25*19.4 = 18.43
9
Motivation
In terms of the variables not fixed at any value we have forscenario (year) 2:
R2 = 13.12*FS + 3.25*IB + 18.71*LG + 20.61*LV + 19.40*SG+ 25.32*SV
Likewise we have, for say scenario 5,
R5 = -21.93*FS + 7.36*IB - 23.26*LG - 5.37*LV - 9.02*SG +17.31*SV
We could write such an equation for each scenario (year).
10
Motivation
Then for all five scenarios we have the constraints:
10.06*FS+17.64*IB+32.41*LG+32.36*LV+33.44*SG+24.56*SV = R1
13.12*FS+ 3.25*IB+18.71*LG+20.61*LV+19.40*SG+25.32*SV = R2
S13.47*FS+7.51*IB+33.28*LG+12.93*LV+3.85*SG-6.70*SV = R3
45.42*FS-1.33*IB+41.46*LG+7.06*LV+58.68*SG+5.43*SV = R4
-21.93*FS+7.36*IB-23.26*LG-5.37*LV-9.02*SG+17.31*SV = R5
11
Motivation
We also assume that we are fully invested, in other words, weinvest 100% percent of our available capital into the mutual funds.
This gives us another contraint.
In portfolio models it is often called the unity constraint.
FS + IB + LG + LV + SG + SV = 1
12
Motivation
The objective is to maximize expected return.
The expected return is found by summing up the return for eachscenario weighted by the probability that the scenario occurs.
The expected value is:
.2*R1 + .2*R2 + .2*R3 + .2*R4 + .2*R5
Then the objective function is:
MAX .2*R1 + .2*R2 + .2*R3 + .2*R4 + .2*R5
13
Motivation
Optimal Solution: Put everything in the small-cap growth fund!
SG = 1.0
The expected return is 21.27%
Any problem with this solution?
15
Motivation
Consider Risk! Now consider risk.
There are a lot of way to measure risk!
Let’s do something easy and specify a minimum return for eachscenario.
We rebuild and solve the model with a constraint of a return of atleast 2% in every period.
16
Motivation
New Solution: The expected value (return) is 17.33%
It is optimal to make the following investments:
Large-CapGrowth – 0.108,Small-CapGrowth – 0.415,Small-CapValue – 0.477
The scenario returns are:
Year1– 29.093,Year2 – 22.149,Year3 – 2.000,Year4 – 31.417,Year5 – 2.000
18
Install and Test GAMS
GAMS: We are going to use the General Algebraic ModelingSystem (GAMS) for solving models like the one just developed.See http://en.wikipedia.org/wiki/General_Algebraic_
Modeling_System.
This is a modeling language originally developed at the WorldBank for solving optimization problems.
Please download the relevant version for your platform. The MacOS X version does not have a GUI.
19
Install and Test GAMS
Step 1: Select the correct Windows version to download at:
http://www.gams.com/download/
Step 2: Download (do not install from the browser) theexecutable installation program windows x86 32.exe (assume a32 bit Windows machine – if you have 64 bit machine downloadwindows x86 64.exe) to your hard drive. If you are not sure ifyour machine is 32 bit or 64 bit, get the 32 bit program. It will runjust fine on 64 bit machines.
20
Using GAMS
Step 3: Now install GAMS.
I Windows XP – you are the lucky ones (well, not really) – justdouble click on windows x86 32.exe and do a default install.
I Vista ( and maybe Windows 7) – you need to install asadministrator. See the Figure on the next page. At thecommand prompt, run windows x86 32.exe. (You need toconnect to the directory where this is stored).
I Mac – if you are a Mac users, install the Mac version on yourmachine. Follow directions at the download site.
21
Install and Test GAMS
GAMS Installation: GAMS with Vista
At the command prompt, connect to the drive where you have theinstallation exe and run.
22
Install and Test GAMS
GAMS Installation: The Command Prompt (find it throughStart)
Make sure you are in the directory with the windows x86 32.exeexecutable, type this into the prompt, and hit return to start theinstallation.
23
Install and Test GAMS
GAMS Installation (continued): You should end up with aGAMS icon on your desktop. If the installation does not open theprogram directly, click on the GAMS icon and you should seesomething like:
24
Install and Test GAMS
Testing: Just to make sure things are working, do the following:
1. Download the example portfolio optimization modelportOpt.gms athttp://faculty.chicagobooth.edu/kipp.martin/root/
htmls/coursework/36104/datafiles/portOpt.gms
2. Install the file into the directory
C:\gamsdir\projdir
3. Open the GAMS IDE and then under File, openportOpt.gms.
4. Under the File menu item select run. (If you are asked toselect the solvers, go with the defaults.) Hopefully, you willsee something like the Figure on the following page.
25
Install and Test GAMS - Mac OS X
Mac OS X:
I Go to the link:http://support.gams.com/doku.php?id=installation:
how_do_i_install_the_gams_version_for_macintosh
I Watch the Video
In the Applications/Utilities folder double click on Terminal.app
You will need to type in a command like (assume you are in thedirectory holding the file portOpt.gms):
/Applications/GAMS/gams portOpt.gms
27
Using GAMS
There are three major sections (for now) in defining a GAMSmodel.
I VARIABLES: In this section we declare the models. In thiscase the investment variables (FS, etc), z, the optimal valueof the portfolio, and the return variables R1, ..., R6. Weactually define the investment variables as POSITIVEVARIABLES which means they must be nonnegative.
I EQUATIONS define the objective function and theconstraints in the model
I MODEL – define the equations to use
28
Using GAMS
A few notes:
I I will try to setoff GAMS keywords by using all capital letters– however GAMS keywords are not case sensitive.
I Variable and constraint name are not case sensitive.
I In equations, we use =e= for equality, =l= for ≤, and =g=for ≥
I You can separate a list of variables or equations by a commaor a new line
I You can put in comments by using an * in column 1
29
Using GAMS
Solving the Model: Now solve the model. First give the solveroptions (not necessary – you could always go with defaults)
Execute the SOLVE command
SOLVE portOpt USING LP MAXIMIZING z ;
The LP that you see in SOLVE stands for Linear Program. Ourobjective function and constraints are linear. If we had somenonlinear, anywhere, we would replace LP with NLP.
30
Using GAMS
Model Solution: Here is how to display the model solution.
The optimal objective function value:
DISPLAY z.l
The value of the investment variables:
DISPLAY FS.l, IB.l, LG.l, LV.l, SG.l, SV.l;
The value of the scenario returns:
DISPLAY R1.l, R2.l, R3.l, R4.l, R5.l;
The .l suffix stands for level – it is often called the primal value.
31
Using GAMS
When you run GAMS, in the IDE, a window will pop up indicatingwhether or not there was a successful run. We mentioned thisbefore. It will look like:
32
Using GAMS
GAMS Output: The result of running a GAMS model alsoincludes listing of the output. If the you input a filemodelName.gms the output will be modelName.lst.
You will get this output even if there are errors in file.gms.
The file.lst will have:
I a solution report
I error diagnostics if there were errors
I results of the display command
I an echo of the input file (turned off by $OFFLISTING)
I variable and constraint information
33
Using GAMS
GAMS Output: In the GAMS listing file, for each variable youwill see information like
IB
(.LO, .L, .UP, .M = 0, 0, +INF, 0)
17.64 Scenario1Ret
3.25 Scenario2Ret
7.51 Scenario3Ret
-1.33 Scenario4Ret
7.36 Scenario5Ret
1 Total
and for each equation, information such as
Scenario1Ret.. - R1 + 10.06*FS + 17.64*IB + 32.41*LG + 32.36*LV
+ 33.44*SG + 24.56*SV =E= 0 ; (LHS = 0)
34
Using GAMS
Debugging: So when was the last time you were working withcomputers and things went well? Uh well, let me see, never?
Murphy’s Law: If something can go wrong it will.
Martin’s Corollary: When it comes to computers, Murphy was anoverly idealistic optimist.
Jun Ma: It is never easy.
Fortunately, GAMS has nice debugging features.
36
Using GAMS
Debugging: More often than not, you will see something like thefollowing. I produced this deleting the definition of variable IB, butthen using it in the equation.
37
Using GAMS
The file portOpt.lst will contain debugging information. You willsee which lines in the input file contains errors and error numbers.
29 *Calculate the return under each scenario;
30 Scenario1Ret .. 10.06*FS + 17.64*IB + 32.41*LG + 32.36*LV + 33.44*SG + 2
**** $140
4.56*SV =e= R1 ;
31 Scenario2Ret .. 13.12*FS + 3.25*IB + 18.71*LG + 20.61*LV + 19.40*SG + 25
.32*SV =e= R2
The first problem is with line 30 in the input file and there is anerror $140.
38
Using GAMS
Debugging: GAMS will explain each error.
140 Unknown symbol
141 Symbol neither initialized nor assigned
A wild shot: You may have spurious commas
in the explanatory
text of a declaration.
Check symbol reference list.
257 Solve statement not checked because
of previous errors
The culprit is the first error, number 140. I used variable IB
without defining it.
39
Using GAMS
Documentation:
I See the tutorial, Tutorial.pdf, by Rick Rosenthal in thedocs/gams directory. Definitely the place to start!
I For all the gory details see the User’s Guide,GAMSUsersGuide.pdf in the docs/bigdocs directory.
I GAMS model libraryhttp://www.gams.com/modlib/modlib.htm
A great set of examples to follow.
I Other Web documentationhttp://www.gams.com/docs/document.htm
40