introduction to gams...gams lives for a moment: report writing commands always use values from the...

29
Introduction to GAMS: Comparative analysis Dhazn Gillig & Bruce A. McCarl Department of Agricultural Economics Texas A&M University

Upload: others

Post on 27-Jan-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

  • Introduction to GAMS:Comparative analysis

    Dhazn Gillig&

    Bruce A. McCarl

    Department of Agricultural EconomicsTexas A&M University

  • Comparative Analysis

    Models once built are almost always subjected to comparative analysis where a number of alternative scenarios are run then the analyst compares the results across scenarios to see the effect is of the scenario assumptions.

    ! Two ways to conduct comparative analysis

    • Use multiple GAMS submissions or multiple solves generating report writing output and then manually comparing the analysis results

    • Use the GAMS LOOP procedure and set up a comparative scenario analysis system that creates cross scenario comparison tables

  • Let use an example from McCarl and Spreen book

    • GAMS implementation of the Unified MOTAD

    example

    • Section 14.3.1.3

    • Book Chapter 14 - Risk

    (http://ageco.tamu.edu/faculty/mccarl/mccspr/new14.pdf)

    Example on Unified MOTAD

  • Example on Unified MOTAD

  • Example on Unified MOTAD

  • Example on Unified MOTAD

  • Example on Unified MOTAD

  • Suppose we wish to simulate 3 scenarios involved with varying the risk aversion parameter. One will have a zero risk aversion parameter and the others with “1.5” and “5.0”. We could do this using repeated solves at the end of the GAMS program as follows.

    … data section …… model structure …RAP=0;SOLVE MTDPortfol USING LP MAXIMIZING OBJ ;

    RAP = 1.5 ;SOLVE MTDPortfol USING LP MAXIMIZING OBJ ;

    RAP = 5 ;SOLVE MTDPortfol USING LP MAXIMIZING OBJ ;

    Multiple GAMS Submissions

    The model is first solved at the original RAP = 0.

    The RAP is changed to 1.5 and model is solved again

    The RAP is changed to 5 and model is solved again

  • GAMS lives for a MOMENT: Report writing commands always use values from the most recent solution. One must save the data if comparative reports are desired by creating a parameter to store the report data and then actively storing what is needed.

    PARAMETER OUTPUT(*) RESULTS FROM MODELVAR Variance ;

    VAR = SUM(EVENTS,(POSDEV.L(EVENTS)+NEGDEV.L(EVENTS))**2)/ CARD(EVENTS ) ;

    OUTPUT("OBJ") = OBJ.L ;OUTPUT("RAP") = RAP ;OUTPUT(STOCKS) = INVEST.L(STOCKS) ;OUTPUT("VAR") = VAR ;OUTPUT("SHADPRICE") = INVESTAV.M ;OPTION Output:2:0:1; DISPLAY Output;

    We will save the calculation parts of this program as another file called report.gms so that it can be used by “$include” command.

    Multiple GAMS Submissions

  • Here are steps:

    1. Set up and solve the original model

    2. Setup parameters

    3. Construct a report

    4. Alter the risk aversion parameter to 1.5

    5. Solve altered model

    6. Construct a report

    7. Alter the risk aversion parameter to 5

    8. Solve altered model

    9. Construct a report

    Multiple GAMS Submissions

    SOLVE MTDPortfol USING LP MAXIMIZING OBJ ;

    PARAMETER OUTPUT(*) Model RESULTS VAR Variance ;

    $include report.gms

    RAP = 1.5 ;

    SOLVE MTDPortfol USING LP MAXIMIZING OBJ ;

    $include report.gms

    RAP = 5 ;

    SOLVE MTDPortfol USING LP MAXIMIZING OBJ ;

    $include report.gms

  • Multiple GAMS Submissions

    Output from RAP = 0---- 162 PARAMETER OUTPUT RESULTS FROM MODEL

    BUYSTOCK3 17.86OBJ 148.21MEAN 148.21VAR 19709.82STD 140.39SHADPRICE 0.30

    Output from RAP = 1.5---- 162 PARAMETER OUTPUT RESULTS FROM MODEL

    BUYSTOCK1 2.82BUYSTOCK2 5.62BUYSTOCK3 1.82BUYSTOCK4 8.40OBJ 106.09RAP 1.50MEAN 119.80VAR 83.89STD 9.16SHADPRICE 0.21

  • This approach does not yield cross scenario comparative report writing and output is widely dispersed. Let’s fix that.

    Step 1 Create a parameter in which to save report with a place for a scenario name in the set dimensions

    SOLVE MTDPortfol USING LP MAXIMIZING OBJ ;

    PARAMETER OUTPUT1(*,*) RESULTS FROM MODELVAR Variance ;

    Step 2 Compute a report using the scenario name in it’s index position (note that in this example the 2nd set refers to scenarios)

    VAR = SUM(EVENTS,(POSDEV.L(EVENTS)+NEGDEV.L(EVENTS))**2)/ CARD(EVENTS ) ;

    OUTPUT("OBJ","RAP = 0.0") = OBJ.L ;OUTPUT("RAP","RAP = 0.0") = RAP ;OUTPUT(STOCKS,"RAP = 0.0") = INVEST.L(STOCKS) ;OUTPUT("VAR","RAP = 0.0") = VAR ;OUTPUT("SHADPRICE","RAP = 0.0") = INVESTAV.M ;

    Multiple GAMS Submissions

  • Step 3 After the second solve statement, Compute a report using the scenario name in it’s index position, but this time use a name indicative of the second scenario such as “RAP = 1.5”

    Rap=1.5;

    SOLVE MTDPortfol USING LP MAXIMIZING OBJ ;

    VAR = SUM(EVENTS,(POSDEV.L(EVENTS)+NEGDEV.L(EVENTS))**2)/ CARD(EVENTS ) ;

    OUTPUT("OBJ","RAP = 1.5") = OBJ.L ;OUTPUT("RAP","RAP = 1.5 ") = RAP ;OUTPUT(STOCKS,"RAP = 1.5 ") = INVEST.L(STOCKS) ;OUTPUT("VAR","RAP = 1.5 ") = VAR ;OUTPUT("SHADPRICE","RAP = 1.5 ") = INVESTAV.M ;

    Multiple GAMS Submissions

    Why do we re-calculate VAR parameter?

  • Display the output

    OPTION OUTPUT:4:1:1 ;DISPLAY OUTPUT;

    places of decimals: # of indices in R: # of indices in C

    Multiple GAMS Submissions

  • Multiple GAMS Submissions – percentage change

    Percentage changes often desirable in a cross scenario comparison. Suppose we use the following code:

    ! The first conditional ($) prevents us from dividing through by zero and rounds to get rid of excessively small numbers.

    ! The second conditional tells us where percentage changes couldn’t be calculated because the base solution was zero and uses a special value which is NA.

  • Output without using conditional ($)

    Output with using conditional ($)

    Multiple GAMS Submissions – percentage change

  • Cautions about calculations !!GAMS treats calculations in three distinctly different ways.

    ! Dynamic – calculations repeated every time the model is generated.

    Only calculations in the model .. Statements are dynamic

    ! Static – calculations executed once only at the place the GAMS instruction appears in the code.

    ! Repeated Static – static calculations in a loop executed every time we go through the loop

    Loop(I, land=land*2; ) ;

    Multiple GAMS Submissions – percentage change

  • Because GAMS lives for the moment, when a calculation is issued then all prior values are overwritten. If you one want to revert to original values then one has to instruct GAMS to do that.

    Multiple GAMS Submissions – percentage change

  • The better way to conduct comparative analysis is to use the LOOP procedure. Here the code contains a LOOP which causes GAMS to repeat execution of statement enclosed in the parentheses defining the LOOP for each value of the defining set.

    Using GAMS LOOP Procedure

  • There are several steps.Step 1: Define Scenario setStep 2: Define parameters that will be altered by scenario

    (i.e. risk aversion parameters)Step 3: Define parameters used in the LOOP (i.e. output, sRap)

    Using GAMS LOOP Procedure for Comparative Runs

    STEP1

    STEP2

    STEP3

  • Using GAMS LOOP ProcedureReset RAP values to original values

    Set RAP values by scenarios

  • Using GAMS LOOP Procedure – GAMS output

  • OPTION Output :4:1:1 ;DISPLAY Output ;

    Using GAMS LOOP Procedure – GAMS output

  • Many comparative studies involve model structure modification. One can introduce context sensitive structure by making constraints or terms conditionals.

    sameas(i,j) a statement that is True if text for set element name of I = that for j

    sameas(resource, “ Land”) => A statement is true if text for the element of resource is Land otherwise, it is false.

    Using GAMS LOOP Procedure – structural modification

  • Using GAMS LOOP Procedure – structural modification

  • ResourceEq(Resource)$( (ResourceAvailable(Resource) GT 100)and sameas(Resource,"Land") ).. would impose the

    constraint in a conditional manner.

    Base Scenario:

    Scenario 1:

    Using GAMS LOOP Procedure – structural modification

  • OPTION Output :3:2:1 ;DISPLAY Output ;

    Using GAMS LOOP Procedure – structural modification

  • Increasing run efficiency using the Save and Restart procedure.

    In the IDE a box is available just to the right of execute button where we can associate a set of execution time parameters with a file and the IDE will remember these whenever the file is opened in this project.-s = .\t\a1 => save work files-r = .\t\a1 => restart work files

    Using GAMS LOOP Procedure

  • ReferencesMcCarl, B. A. Basic GAMS class.

    (http://agecon.tamu.edu/faculty/mccarl/mccarl.htm).