```\$TITLE Appendix C: OLG exchange model -- constant discount rate

*=====================================================================
* Introduce intertemporal sets
*=====================================================================

* The model captures all generations alive in the first model period
* (year 0) and all those born in the span of the subsequent 150
* years, where generations are labeled according to the year in which
* they are born. The model is solved in 1-year intervals with each
* new generation being born at the start of a period and living to
* the age of 55.

SCALARS
TIMINT          Single period time interval             /1/,
INIYEAR         Year in with oldest generation was born /-54/;
SETS
G               Generations in the model                /
"-54","-53","-52","-51",
"-50","-49","-48","-47","-46","-45","-44","-43","-42","-41",
"-40","-39","-38","-37","-36","-35","-34","-33","-32","-31",
"-30","-29","-28","-27","-26","-25","-24","-23","-22","-21",
"-20","-19","-18","-17","-16","-15","-14","-13","-12","-11",
"-10","-9","-8","-7","-6","-5","-4","-3","-2","-1", 0*150/,
T(G)            Time periods in the model          /0*150/,
A(T)            Typical life-cycle /0*54/;

* We need some special sets to identify key time periods and generations.

SETS    TFIRST(T)       First period in the model,
TLAST(T)        Last period in the model,
ATGEN(G)        Generations with terminal assets;

* These special sets are identified by their order in the declaration.

TFIRST(A)       = YES\$(ORD(A) EQ 1);
TLAST(T)        = YES\$(ORD(T) EQ CARD(T));
ATGEN(G)        = YES\$((CARD(G)-CARD(A)+1) LT ORD(G));

* Aliases used to manipulate sets.

ALIAS (G,GG,YR), (T,TT), (A,AA);

*=====================================================================
* Introduce fundamental parameters
*=====================================================================

SCALARS RBAR_A          Annual interest rate                    /0.05/,
GAMMA_A         Annual population growth rate           /0.01/,
THETA           Exponent in intertemporal utility       /4.00/,
TDEF0           Base year trade deficit                 /0.01/;

* Modify annual rates of change to 5-year interval between solution
* periods

SCALARS RBAR            Periodic interest rate,
GAMMA           Periodic population growth rate;

RBAR    = (1+RBAR_A)**TIMINT  - 1;
GAMMA   = (1+GAMMA_A)**TIMINT - 1;

*=====================================================================
* Time profiles
*=====================================================================

* Declare variables relating values to intertemporal sets and use
* annual growth and interest rates to create time profiles consistent
* with the 1-year interval between solution periods.

PARAMETERS
YEAR(G)         Point in time,
AGE(A)          Age at a given point in the life cycle,
PREF(G)         Reference price path (present value price index),
QREF(G)         Reference quantity path (population size index),
OMEGA0(A)       Initial endowment profile;

* Time periods and ages can be identified by the order of the
* relevant set using the fact that each period has a length of 5
* years, and that generations are labeled according to the year they
* are born and live for 55 years.

YEAR(G)        = INIYEAR + TIMINT * (ORD(G)-1);
AGE(A)         = TIMINT * (ORD(A)-1);

* Declare indices for population size and present value prices.

QREF(G)        = (1+GAMMA_A)**YEAR(G);
PREF(G)        = 1 /(1+RBAR_A)**YEAR(G);

* Use ages and years to set up correspondence between generations,
* age, and year.

SET     MAPG(G,A,YR) Assignment from generation and age to time period;

MAPG(G,A,YR) = YES\$(YEAR(G)+AGE(A) EQ YEAR(YR));

* Endowment profile as in Auerbach and Kotlikoff (1987):

OMEGA0(A) = EXP(4.47 + 0.033*AGE(A) - 0.00067 * AGE(A)**2);

* Endowment profiles are scaled to an economy-wide level of 1 in the
* base year

OMEGA0(A) = OMEGA0(A) / SUM(AA, (1+GAMMA)**(1-ORD(AA)) * OMEGA0(AA));

*=====================================================================
* Calibration model: Solve for benchmark steady state of reference
* generation, with calibration model for endogenous consumption
*=====================================================================

* Find utility discount rate, RHO, to set implied aggregate
* consumption equal to aggregate endowments plus the trade deficit.
* We use the equations arising from the household utility
* maximization problem to set up a mixed complementarity problem
* (MCP) and use the solver to find the value of RHO that satisfies
* all the equations in the system. Alternatively, since the
* first-order conditions in this simple setting dictate that
* consumption is growing over the life cycle at a constant rate
* one could solve for rho analytically as it is done in appendixd.gms

VARIABLES
RHO     Period utility discount rate;

POSITIVE VARIABLES
CC(T)   Consumption profile of generation born in year 0,
LAMDA   Shadow price of income (present value utils);

EQUATIONS
EQC(A)  First order condition for consumption,
EQCCC   Base year aggregate consumption,
EQLAMDA First order condition for price of income;

* First order conditions.

EQC(A)..        LAMDA*PREF(A) =E= (1+RHO)**(1-ORD(A))*CC(A)**(-THETA);
EQLAMDA..       SUM(A,PREF(A)*CC(A)) =E= SUM(A,PREF(A)*OMEGA0(A));

* Aggregate consumption in the base year is implied by the
* consumption profile of the generation born in year 0 since the
* steady state assumption determines the relative sizes of each
* generation. We require that aggregate consumption equals total
* endowments plus the trade deficit.

EQCCC..          SUM(A,CC(A)/QREF(A)) =E= 1 + TDEF0;

* Associate variables with equations.

MODEL BENCH     /EQC.CC, EQLAMDA.LAMDA, EQCCC.RHO/;

* Initialize variables and set bounds to prevent operation errors.

RHO.L           = 0.01;
RHO.LO          = -0.99;
CC.L(A)         = 1/CARD(A);
CC.LO(A)        = 1E-9;
LAMDA.L         = 1;
LAMDA.LO        = 1E-9;

* Solve calibration model.

BENCH.ITERLIM=50000;
SOLVE BENCH USING MCP;

* Derived utility discount rate on an annual basis.

PARAMETER RHO_A   Annual utility discount (%);

RHO_A           = 100 * ((1+RHO.L)**(1/TIMINT) - 1); DISPLAY RHO_A;

*=====================================================================
* Use endowments and the calibrated consumption profile for
* generation 0 to install baseline values for all generations
*=====================================================================

PARAMETERS
EREF(G,T)       Baseline endowment profile,
CREF(G,T)       Baseline consumption profile,
PREF_T(A)       Baseline post-terminal price path,
CREF_T(G,A)     Baseline post-terminal consumption profile,
MREF(G)         Baseline present value of consumption;

* We assign demand and income profiles for generation G at time T
* based on endowments and the calibrated consumption profile for
* generation 0. The trick here is to use a GAMS loop over the mapping
* which relates generations (G), ages (A) and time periods (T):

LOOP(MAPG(G,A,T),
EREF(G,T) = QREF(G) * OMEGA0(A);
CREF(G,T) = QREF(G) * CC.L(A);  );

* The last model generation is born in year 150 which means that in
* order to capture the full life cycle of all model generations we
* need to cover a 50-year "post-terminal" period. We index these
* post-terminal periods by the same index (A) that we use to index
* ages in a life cycle.

* Present value prices in post-terminal periods are extrapolated from
* the value of the reference price index in the terminal period.

LOOP((A,TLAST)\$AGE(A), PREF_T(A) = PREF(TLAST) / (1 + RBAR_A)**AGE(A); );

* Consumption profiles in post-terminal periods for generation G at
* age AA are inferred from the consumption levels in the initial
* period of generations that have the same age.

LOOP((G,A,TLAST)\$(YEAR(G)+AGE(A) GT YEAR(TLAST)),
CREF_T(G,AA)\$(AGE(AA)+YEAR(TLAST) EQ AGE(A)+YEAR(G))
= CC.L(A) * QREF(G); );

* Present value of consumption by generation, including post-terminal
* consumption by generations who live beyond the model horizon.

MREF(G) = SUM(T, PREF(T)*CREF(G,T)) + SUM(A,  PREF_T(A)*CREF_T(G,A));

*=====================================================================
* Use endowments and the calibrated consumption profiles for
* generation 0 to back out the evolution of asset holdings and
* distinguish between domestic and foreign debt
*=====================================================================

PARAMETERS
ASSETS(A)       Present value of assets over the life cycle,
MA(A)           Assets held in year 0 by age of generation,
ASSETH          Positive asset holdings by age in year 0,
DEBT            Net debt by age in year 0;

SCALAR  THETAD          Ratio of total assets to total debt;

* The present value of assets equals the sum of the value of
* endowments less consumption in all previous periods of the life
* cycle. The asset profile of the representative generation can then
* be used to find the distribution of asset holdings across
* generations alive in the base year.

ASSETS(A) = SUM(AA\$(ORD(AA) LT ORD(A)), PREF(AA)*(OMEGA0(AA) - CC.L(AA)));
MA(A)     = ASSETS(A)/(QREF(A)*PREF(A));

* We assume that negative asset positions reflect holdings of both
* domestic and foreign debt, while positive asset positions reflect
* holdings of domestic assets. We assume that all age groups with
* negative assets hold foreign and domestic debt in the same
* proportion which means that we can use the ratio of total assets to
* total debt to decompose the asset holdings by type

THETAD         = -SUM(A\$(MA(A) GT 0), MA(A))/ SUM(A\$(MA(A) LT 0), MA(A));

ASSETH(A,"DOMESTIC")\$(MA(A) GT 0)       =  MA(A);

* We model baseline assets and debt positions as inital endowments
* for generations alive in year 0.

PARAMETERS
DASSET(G)       Initial endowment of domestic assets,
FASSET(G)       Initial endowment of foreign assets;

DASSET(G) = SUM(MAPG(G,A,"0"), ASSETH(A,"DOMESTIC") - DEBT(A,"DOMESTIC"));
FASSET(G) = SUM(MAPG(G,A,"0"), - DEBT(A,"FOREIGN"));

*=====================================================================
* Model in GAMS/MCP. This model solves for the equilibrium transition
* path subject to terminal conditions that assume the presence of a
* steady state. If there are no exogenous changes the model
* replicates the calibrated consumption profiles. We use this feature
* to check the calibration and then solve for the results of en
* exogenous change in the endowment profile
*=====================================================================

POSITIVE VARIABLES

* Production activities. These determine how inputs are converted
* into outputs according to the technology implied by the benchmark
* data. The variables here are activity levels and an equilibrium
* requires that each active sector earns zero profit.

U(G)            Utility,
X(T)            Export,
M(T)            Import,

* Prices. The variables here are the prices that are associated with
* each commodity. An equilibrium requires that prices are such that
* supply equals demand.

PC(T)           Price of private consumption,
PCT(G,A)        Price of post-terminal consumption of goods,
PU(G)           Price of intertemporal utility,
PFX             Price of foreign exchange,

* Consumer income levels. These are agents that receive income from
* endowments or taxes and spend it to maximize utility. The variables
* here are income levels and an equilibrium requires that total
* income equals total expenditure.

RA(G)           Representative agents by generation

* These are endogenous variables associated with model constraints
* that relate the transition to the steady state.

CT(G,A)         Post-terminal consumption of goods,
AT(G)           Terminal assets;

* Equations asscociated with model variables. These fall into three
* classes. PRF which ensure zero profit in each activity, MKT which
* ensure no excess demand for each commodity, and DEF which ensure
* income balance for each agent.

EQUATIONS
PRF_U(G)        Utility,
PRF_X(T)        Export,
PRF_M(T)        Import,
MKT_PC(T)       Price of private consumption,
MKT_PCT(G,A)    Price of post-terminal consumption of goods,
MKT_PU(G)       Price of intertemporal utility,
MKT_PFX         Price of foreign exchange,
DEF_RA(G)       Representative agents by generation,
EQU_CT(G,A)     Post-terminal consumption of goods,
EQU_AT(G)       Terminal assets;

* Utility is treated of as a commodity demanded by the different
* generations which implies that the utility function is modeled as
* any other production activity. The activity level here is initialized
* at unity implying an overall output level equal to the present value
* of consumption, MREF(G).

PRF_U(G)..      SUM(T, PREF(T)   * CREF(G,T) * (PC(T)/PREF(T))**(1-1/THETA))
+ SUM(A, PREF_T(A) * CREF_T(G,A) *
(PCT(G,A)/PREF_T(A))**(1-1/THETA))
=E= MREF(G) * PU(G)**(1-1/THETA);

* These equations represent zero profit in import and export activities.
* The assumption of a small open economy and perfect capital mobility
* implies that the price of imports is constant in current value terms.
* We therefore do not need to distinguish between years, but only
* operate with a single present value price for foreign exchange. The
* import activity is initialized at the reference quantity path implying
* an overall output level equal to the baseline trade deficit while the
* export activity is initialized at zero.

PRF_M(T)..      PFX * PREF(T) =G= PC(T);

PRF_X(T)..      PC(T) =G= PFX * PREF(T);

* Supply equals demand for consumption, post-terminal consumption,
* "utility", and foreign exchange.

MKT_PC(T)..     SUM(G, EREF(G,T) + DASSET(G)\$TFIRST(T)) + M(T) - X(T) =E=

SUM(G, CREF(G,T)*U(G)*(PU(G)*PREF(T)/PC(T))**(1/THETA));

MKT_PCT(G,A)\$CREF_T(G,A)..
CT(G,A) =E= U(G)*(PU(G)*PREF_T(A)/PCT(G,A))**(1/THETA);

MKT_PU(G)..     U(G)*MREF(G)*PU(G) =E= RA(G);

MKT_PFX..       SUM(T, X(T)*PREF(T)) + SUM(G, FASSET(G))
=E= SUM(T, M(T)*PREF(T));

* Income balance for each generation. Each generation demands "utility"
* and is endowed with an amount of the consumption good in each period.
* In addition, generations alive in the initial period are endowed with
* domestic and foreign assets. To terminate the model generations alive
* in the terminal period are required to leave an amount of assets and
* are also endowed with goods for consumption in the post-terminal periods.

DEF_RA(G)..     RA(G) =E= SUM(T, PC(T)*(EREF(G,T) + DASSET(G)\$TFIRST(T)))
+ PFX * FASSET(G) + (PFX * AT(G))\$ATGEN(G)
+ SUM(A, PCT(G,A) * CREF_T(G,A) * CT(G,A));

* Select the levels of post-terminal consumption so that the present
* value price declines with the steady state interest rate.

EQU_CT(G,A)\$CREF_T(G,A)..
SUM(TLAST, PC(TLAST)) =E= PCT(G,A)*(1+RBAR)**(ORD(A)-1);

* Select terminal asset position so that all generations living past
* the terminal period achieve the same equivalent variation.

EQU_AT(G)\$ATGEN(G)..
U(G) =E= U(G-1);

* Define the equations entering the model and their complementary
* slackness relationship with variables in the model:

MODEL EXCHANGE /PRF_U.U, PRF_X.X, PRF_M.M , MKT_PC.PC, MKT_PCT.PCT,
MKT_PU.PU, MKT_PFX.PFX, DEF_RA.RA ,EQU_CT.CT, EQU_AT.AT/;

* explicitly below:

*=====================================================================
* auxiliary variables
*=====================================================================

U.L(G)          = 1;
PU.L(G)         = 1;
PFX.L           = 1;
RA.L(G)         = MREF(G);
X.L(T)          = 0;
M.L(T)          = QREF(T)*TDEF0;
PC.L(T)         = PREF(T);
PCT.L(G,A)      = PREF_T(A);
CT.L(G,A)       = 1\$CREF_T(G,A);
AT.L(G)         = SUM(T, (CREF(G,T) -EREF(G,T)) * PREF(T))\$ATGEN(G);
AT.LO(G)        = -INF;

* Numeraire:

PC.FX(TFIRST)   = 1;

*=====================================================================
* Replicate the benchmark equilibrium
*=====================================================================

EXCHANGE.ITERLIM=0;
SOLVE EXCHANGE USING MCP;
DISPLAY "Benchmark tolerance CHK:",EXCHANGE.OBJVAL;

*=====================================================================
* Run counterfactual
*=====================================================================

* Modify the endowment profile, and scale it to maintain an economy-wide
* level of unity in the base year.

PARAMETER       OMEGA(A)        Counterfactual endowment profile;

OMEGA(A)        = EXP(4.47 + 0.02*AGE(A) - 0.0007*(AGE(A))**2);
OMEGA(A)        = OMEGA(A) / SUM(AA, (1+GAMMA)**(1-ORD(AA)) * OMEGA(AA));

LOOP(A,EREF(G,T)\$MAPG(G,A,T) = QREF(G) * OMEGA(A); );

* Solve the model.

EXCHANGE.ITERLIM=10000;
SOLVE EXCHANGE USING MCP;

* Parameters for reporting results from the counterfactual experiment.

PARAMETERS
WCHANGE Welfare change (% equivalent variation by year of birth),
TDEF    Trade deficit (change from baseline level);

* Due to production activities being homogenous of degree 1 the
* equivalent variation is the percentage change in output from sector
* U. The trade deficit in year T is the difference between aggregate
* consumption and aggregate endowment.

WCHANGE(G)      = 100 * (U.L(G) - 1);
TDEF(T)         = M.L(T) - X.L(T);

*=====================================================================
* Display statements
*=====================================================================

DISPLAY YEAR, AGE, PREF, PREF_T, QREF, OMEGA0, OMEGA, EREF, CREF, CREF_T,
MREF, ASSETS, MA, ASSETH, DEBT, DASSET, FASSET, WCHANGE, TDEF;
```