Page 1 of 1

sensitivity analysis using excel and GAMS

Posted: Sun Mar 24, 2019 12:16 pm
by mapo17af
Hi,

We would like to run the DICE-2016R2 model in GAMS, for which the code is publicly available here:
http://www.econ.yale.edu/~nordhaus/home ... 1916ap.gms

Then, in order to make an assessment of the parameters in the model, we will do a sensitivity analysis. One thing we for example want to look at is:
- changing the parameter "prstp" for an entire range of values from 0.001 to 0.015, what values will that create for the main variables of interest:

Code: Select all

CPRICE(t)
and

Code: Select all

scc(t)
?

Can someone explain how to do this?

For simplification and convenience, one might want to refer to the the mean-variance model from the GAMS model library (attached as file) as example instead of the DICE model. In the last part of the code of that model we see several loops. Instead of using loops, we would like to create a gdx(?) file such that we can get a nice-looking table in excel as output, for our sensitivity analysis.
Basically we want to create a table with in the first column the analysed investment(s) 'i', there below its variance, its return, and finally it's lambda, and we want to display those values for various portfolios (portfolio 1 till 10), which should hence be displayed in the rows of the table. Preferably in excel.
I thought this could be done with the following code, but I cannot get it to work. Can someone explain how to do this?

Code: Select all

SummaryReport(*,*)

Code: Select all

SummaryReport (i,p)

Code: Select all

SummaryReport (variance,p)

Code: Select all

SummaryReport (return,p)

Code: Select all

SummaryReport (lambda,p)
Thanks a lot!

Re: sensitivity analysis using excel and GAMS

Posted: Tue Apr 09, 2019 10:17 am
by Fred
Hi,

I am not sure if the detour via the meanvar model really helps us here. Also, I am not sure what you try to illustrate with the ReportSummary code snippets. Hence, I'll try to answer directly referring to the dice model.

Changing parameter "prstp" in the range from 0.001 to 0.015 means that you want to solve the model 15 times (assuming that you choose a step size of 0.001). You mention that instead of using loops you would liek to use a gdx file... I don't think there is an "instead". To solve the 15 scenarios, a loop would be natural to use in my opinion. To store the results GDX/Excel files can be used. I took the dice model and added a little code section that does this:

Code: Select all

set iter iterations / i1*i15 /;
parameter report(*,*,*) report parameter;
loop(iter,
* change parameter prstp 
  prstp = ord(iter)*0.001;
* recalculate parameters/fixed variables that depend on prstp (no guarantee that I covered them all)
  rr(t) = 1/((1+prstp)**(tstep*(t.val-1)));
  optlrsav = (dk + .004)/(dk + .004*elasmu + prstp)*gama;
  S.FX(lag10(t)) = optlrsav;
* solve model
  solve co2 maximizing utility using nlp;
* save values that ar of interest
  report('CPRICE',t,iter) = CPRICE.l(t);
  report('scc',t,iter)    = -1000*eeq.m(t)/(.00001+cc.m(t)); 
);
* unload report parameter to gdx
execute_unload 'rep.gdx', report;
* export report data to excel via GDDXXRQ
execute 'gdxxrw rep.gdx par=report rdim=2 cdim=1';
abort$(errorlevel<>0) 'problems wirth GDXXRW';
The results of the 15 scenarios are stored in rep.gdx and rep.xlsx. Of course, this is just a quick and dirty example that can be adjusted as needed.

I hope this helps!

Fred

Re: sensitivity analysis using excel and GAMS

Posted: Thu Apr 11, 2019 2:15 pm
by mapo17af
Thank you Fred, that definitely helped a lot!
You were right about the loops, I am very new to GAMS so I did not know how to specify the question correctly.

In a similar way, I would like to change the parameter value for "ga0", but not stepwise like in my previous question. Instead, I generated 1000 random numbers in an excel file, based on a normal distribution with mean 2.1% and standard deviation of 1.1%.
I would like to use a loop such that GAMS takes the random number from the excel file, runs the DICE model, saves the output, and then goes again into the excel file, takes the next random number as value for the parameter "ga0", runs the DICE again, saves the output in turn, and so on and so forth.

Could you kindly help me with coding that ?
Please find the excel file with random numbers generated attached, and here the DICE 2016R2 code: http://www.econ.yale.edu/~nordhaus/home ... 1916ap.gms

Thanks a lot in advance!

Re: sensitivity analysis using excel and GAMS

Posted: Thu Apr 11, 2019 4:25 pm
by Fred
Hi,

Instead of reading/writing 1000 times from Excel, I suggest to rather read/write from/to Excel and manage the 1000 scenarios in GAMS (for the start you might want to use a much smaller number). Basically what you have to do is similar to the previous example but you also read data from Excel. I hope the following code snippet gives you some hints on how to proceed.

Code: Select all

[...]
set s scenarios;
* scenario set implicitly defined via parameter ga0_s
parameter ga0_s(s<);
* read data from excel and store in GDX file
$call gdxxrw Question2Gams.xlsx par=ga0_s rng=Sheet1!a2 rdim=1 cdim=0
$ife errorlevel<>0 $abort Problem with GDXXRW
* load data from gdx file
$gdxin Question2Gams
$load ga0_s
$gdxin
display s, ga0_s;
[...]
* loop over scenarios and updata data as needed
loop(s,
  [...]
  ga0 = ga0_s(s);
* update other symbols if there are any in the model that depend ga0
  [...]
  solve co2 maximizing utility using nlp;  
  [...]
* process solution as needed
);
Best,
Fred