Skip to content

Instantly share code, notes, and snippets.

@trialsolution
Created May 31, 2014 15:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save trialsolution/4947a59cdda558994bad to your computer and use it in GitHub Desktop.
Save trialsolution/4947a59cdda558994bad to your computer and use it in GitHub Desktop.
2x2 HOS model for a small open economy
*
* --- Small country trading equilibrium
*
* Different variants of the same model:
*
*
* (1) explicit trade balance condition [import value = export value] or 'nested' budget constraint [max. income = production value]
* (2) 1-step or 2-step optimization:
* The two-step optimization includes the subsequent solves of:
* (i) max. output value,
* (ii) max. utility subject to a budget constratint which in turn is defined by the optimal output
*
*
* --- The code builds on the small country example of Gilbert and Tower, link:
* http://ideas.repec.org/c/uth/gt2013/2012-14.html
*
* Original heading, credits go to Gilbert and Tower and their excellent book:
* Introduction to Numerical Simulation for Trade Theory and Policy
*
* This file contains a small country version of the HOS model, as described in Chapter 12 of
* Gilbert and Tower: Introduction to Numerical Simulation for Trade Theory and Policy.
* Contact: jgilbert@usu.edu or tower@econ.duke.edu.
$offlisting
* Define the indexes for the problem
SET I Goods /1,2/;
alias(i,ii);
SET J Factors /K,L/;
ALIAS (J, JJ);
* Create names for parameters
PARAMETERS
ALPHA Shift parameters in utility
BETA(I) Share parameters in utility
P(I) Prices
UO Initial utility level
CO(I) Initial consumption levels
XO(I) Initial trade levels
GAMMA(I) Shift parameters in production
DELTA(J,I) Share parameters in production
RHO(I) Elasticity parameters in production
FBAR(J) Endowments
QO(I) Initial output levels
RO(J) Initial factor prices
FO(J,I) Initial factor use levels
GDPO Initial gross domestic product;
* Assign values to the parameters
P(I)=1;
RO(J)=1;
QO(I)=100;
CO(I)=QO(I);
XO(I)=QO(I)-CO(I);
FO('L','1')=20;
FO('L','2')=80;
FO('K',I)=(QO(I)*P(I)-FO('L',I)*RO('L'))/RO('K');
FBAR(J)=SUM(I, FO(J,I));
GDPO=SUM(I, P(I)*QO(I));
RHO(I)=0.1;
DELTA(J,I)=(RO(J)/FO(J,I)**(RHO(I)-1))/(SUM(JJ, RO(JJ)/FO(JJ,I)**(RHO(I)-1)));
GAMMA(I)=QO(I)/(SUM(J, DELTA(J,I)*FO(J,I)**RHO(I)))**(1/RHO(I));
UO=GDPO;
BETA(I)=CO(I)/GDPO;
ALPHA=UO/PROD(I, CO(I)**BETA(I));
* Create names for variables
VARIABLES
U Utility index
X(I) Trade levels
C(I) Consumption levels
Q(I) Output levels
R(J) Factor prices
F(J,I) Factor use levels
GDP Gross domestic product;
* Assign initial values to variables, and set lower bounds
U.L=UO;
X.L(I)=XO(I);
C.L(I)=CO(I);
Q.L(I)=QO(I);
R.L(J)=RO(J);
F.L(J,I)=FO(J,I);
GDP.L=GDPO;
C.LO(I)=0;
Q.LO(I)=0;
R.LO(J)=0;
F.LO(J,I)=0;
GDP.LO=0;
* Create names for equations
EQUATIONS
UTILITY Utility function
DEMAND(I) Demand functions
MAT_BAL(I) Market closure
PRODUCTION(I) Production functions
RESOURCE(J) Resource constraints
FDEMAND(J,I) Factor demand functions
INCOME Gross domestic product;
* Assign the expressions to the equation names
* Cobb-Douglas utility function
UTILITY..U=E=ALPHA*PROD(I, C(I)**BETA(I));
* FOC of utility max. (consumer demand)
DEMAND(I)..C(I)=E=BETA(I)*GDP/P(I);
* market clearing for open economy (X is net trade)
MAT_BAL(I)..X(I)=E=Q(I)-C(I);
* FOC for the profit maximization (supply)
PRODUCTION(I)..Q(I)=E=GAMMA(I)*SUM(J, DELTA(J,I)*F(J,I)**RHO(I))**(1/RHO(I));
* simple resource constraint
RESOURCE(J)..FBAR(J)=E=SUM(I, F(J,I));
* FOC for the factor demands (underlying is a CES production function)
FDEMAND(J,I)..R(J)=E=P(I)*Q(I)*SUM(JJ, DELTA(JJ,I)*F(JJ,I)**RHO(I))**(-1)*DELTA(J,I)*F(J,I)**(RHO(I)-1);
* budget constraint defined by optimal output (q)
INCOME..GDP=E=SUM(I, P(I)*Q(I));
*===================================================================================
* CALIBRATION TEST, i.e. solve the calibrated model on the benchmark data set
*===================================================================================
*
* --- Original formulation by Gilbert-Tower
* (the budget constraint of the consumer is linked to the optimal GDP of the profit max. problem)
*
* Strange mixture of direct optimization and first order conditions
* As we have the Marshallian demand function in (i.e. FOC of the utility max. problem),
* we don't actually need to maximize utility directly in the model.
* => We can drop the objective function and look for simply a feasible solution (e.g. with a dummy objective)
* The first order conditions guarantee the optimality anyway.
MODEL SMALL /utility, demand, mat_bal, production, resource, fdemand, income/;
* comment out the solprint statement if you want to check the listing
small.solprint = 0;
SOLVE SMALL USING NLP MAXIMIZING U;
*
* --- Let's drop the utility maximizing objective function
* and solve for a simple feasible solution
*
variable v_dummy "dummy objective value";
equation dummy "dummy objective functions";
dummy .. v_dummy =e= 1;
model dummy_model / dummy, demand, mat_bal, production, resource, fdemand, income/;
dummy_model.solprint = 0;
solve dummy_model using nlp minimizing v_dummy;
*
* --- GAMS makes it possible to solve square system of equations (as the one above)
* by formulating it as a CNS model.
* Naturally, we don't have to define an objective function...
*
model small_cns /demand, mat_bal, production, resource, fdemand, income/;
small_cns.solprint = 0;
solve small_cns using CNS;
*
* --- We could also opt for a direct utility maximizing model for the consumer.
* The profit maximization problem of the producer is still in the FOC form.
*
equation budget_constraint "budget constraint of the consumer";
budget_constraint .. gdp =e= sum(i, p(i) * c(i));
model direct_opt /utility, budget_constraint, mat_bal, production, resource, fdemand, income/;
direct_opt.solprint = 0;
solve direct_opt using nlp maximizing u;
*
* --- Alternatively, we can formulate the problem as profit maximization
* [still linked to the consumer's behavioural model through the income line]
*
variable profit "producer's profit";
equation
CES_prod_function(i) "CES production function"
profit_function "profit function"
;
CES_prod_function(i) .. q(i) =e= gamma(i) * sum(j, delta(j,i) * f(j,i) ** rho(i)) ** (1/rho(i));
profit_function .. profit =e= sum(i, p(i) * q(i));
MODEL profit_max /profit_function, CES_prod_function, demand, mat_bal, resource, fdemand, income/;
profit_max.solprint = 0;
solve profit_max using nlp maximizing profit;
*
* --- A more elegant solution is to build on the FOCs alone
* and formulate the model as a Mixed Complementarity Problem (MCP).
* By the way that's the usual formulation in modern CGE models
* where you can hardly find direct utility maximization...
*
*
* --- We can solve the optim. problem in one go, or in two subsequent steps
*
* IN ONE GO:
model one_go /
* "supply function"
production.q
* "factor demand"
fdemand.f
* "resource constraint --> factor prices"
resource.r
* "market clearing --> net trade"
mat_bal.x
* "demand equation (utility maximizing agent)"
demand.c
* "max. attainable GDP", links the profit max. problem to the consumers' decision problem
income.gdp
* "consumers' utility" (in the MCP formulation it becomes a reporting equation, i.e. could be left out of the model)
utility.u
/;
one_go.solprint = 0;
solve one_go using MCP
*
* --- SOLVE THE PROBLEM IN TWO SUBSEQUENT STEPS
*
model first_step /
production.q
fdemand.f
resource.r
income.gdp
/;
model second_step /
demand.c
mat_bal.x
utility.u
/;
first_step.solprint = 0;
solve first_step using mcp;
*
* --- fix variables to the optimum level of the profit max. problem
*
gdp.fx = gdp.l;
q.fx(i) = q.l(i);
second_step.solprint = 0;
solve second_step using mcp;
gdp.lo = 0;
gdp.up = +inf;
q.lo(i) = 0;
q.up(i) = +inf;
*===================================================================================
* Introduce Explicit trade balance
*===================================================================================
equation
trade_balance
demand_Hicksian(i)
;
trade_balance ..
sum(i, x(i) * p(i)) =e= 0;
demand_Hicksian(i) ..
c(i) =e= (U / alpha) * (beta(i) / p(i)) * prod(ii, (p(ii) / beta(ii)) ** beta(ii));
model tradebal_defines_u "let the explicit trade balance define the attainable utility" /
* "supply function"
production.q
* "factor demand"
fdemand.f
* "resource constraint --> factor prices"
resource.r
* "market clearing --> net trade"
mat_bal.x
* Hicksian demand
demand_Hicksian.c
* trade balance defines utility
trade_balance.u
/;
model tradebal_defines_gdp "let the explicit trade balance define max. profit" /
* "supply function"
production.q
* "factor demand"
fdemand.f
* "resource constraint --> factor prices"
resource.r
* "market clearing --> net trade"
mat_bal.x
* Hicksian demand
demand.c
* trade balance defines utility
trade_balance.gdp
utility.u
/;
*===================================================================================
* TEST SCENARIO
*===================================================================================
*
* --- Generate trade with price differences
*
p("1") = 1.2;
*
* --- solve with different formulations and compare results
*
parameter p_check_trade(i);
small.solprint = 1;
solve small using nlp maximizing u;
p_check_trade(i) = x.l(i);
one_go.solprint = 1;
solve one_go using mcp;
if(sum(i, abs(p_check_trade(i) - x.l(i))) gt 1.E-3, abort "different result obtained!");
tradebal_defines_u.solprint = 1;
solve tradebal_defines_u using mcp;
if(sum(i, abs(p_check_trade(i) - x.l(i))) gt 1.E-3, abort "different result obtained!");
tradebal_defines_gdp.solprint = 1;
solve tradebal_defines_gdp using mcp;
if(sum(i, abs(p_check_trade(i) - x.l(i))) gt 1.E-3, abort "different result obtained!");
*
* --- two-step optimization
*
first_step.solprint = 0;
solve first_step using mcp;
gdp.fx = gdp.l;
q.fx(i) = q.l(i);
second_step.solprint = 1;
solve second_step using mcp;
if(sum(i, abs(p_check_trade(i) - x.l(i))) gt 1.E-3, abort "different result obtained!");
$exit
Solution for net trade
---- VAR X Trade levels
LOWER LEVEL UPPER MARGINAL
1 -INF 24.8945 +INF .
2 -INF -29.8734 +INF .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment