$title	Illustrative MPSGE model based on a social accounting matrix

*	See sam.xls for the dataset (here is the Excel file)

set	i	SAM row and column indices /1*14/,
	s	Sectors	/	agr	Agriculture, 
				ind	Industry,
				ser	Services /,
	f	Primary factors / L Labor, N Land, K Capital/,
	h	Households	/ rural, urban/

*	Goods and sectors are identical:

alias (s,g,gg), (i,j);

set	mapa(i,s)	Mapping from SAM to activities	 /1.agr, 2.ind, 3.ser/,
	mapc(i,g)	Mapping from SAM to commodities /4.agr, 5.ind, 6.ser/,
	mapf(i,f)	Mapping from SAM to factors	 /7.L, 8.N, 9.K /,
	maph(i,h)	Mapping from SAM to households	/ 10.rural, 11.urban/, 
	mapg(i)		Identifies government in the SAM  /12/,
	mapi(i)		Identifies the investment row    /13/,
	mapx(i)		Identifies the rest of world	 /14/;

parameter	sam(i,j)	Base year social accounts;
$libinclude xlimport sam sam.xls sam

*	Scale the social accounting matrix so that the average entry
*	is roughly 1:

sam(i,j) = sam(i,j) / 1000;

display sam;

parameter	chksam(i)	Consistency check of social accounts;
chksam(i) = sum(j, sam(i,j)-sam(j,i));
display "Consistency check before balancing:", chksam;

variable	obj		Objective -- least squares deviation;

variable	es(i,j)		Estimate of SAM entries;

equations	objdef		Defines the deviation
		balance(i)	SAM balance condition;

scalar	penalty  /1e6/;

objdef..	obj =e=   sum((i,j), sqr(es(i,j)-sam(i,j))) 
			+ sum((i,j)$(not sam(i,j)), penalty * es(i,j));

balance(i)..	sum(j, es(i,j)) =e= sum(j, es(j,i));

es.l(i,j) = sam(i,j);

model sambal /all/;
solve sambal using nlp minimizing obj;

parameter	nz(i,j)	New nonzeros in the SAM;
nz(i,j) = es.l(i,j)$(not sam(i,j));
display nz;

sam(i,j) = es.l(i,j);
chksam(i) = sum(j, sam(i,j)-sam(j,i));
display "Consistency check after balancing:", chksam;

*	Extract submatrices from the social accounts:

parameter	id0(g,s)	Intermediate demand
		fd0(f,s)	Factor demand
		tm0(g)		Import tariff collection
		m0(g)		Imports (cif),
		c0(g,h)		Private consumption
		g0(g)		Government demand
		i0(g)		Investment demand
		x0(g)		Exports
		tx0		Export taxes (total)
		ti0		Investment taxes (total)
		fe0(f,h)	Factor endowments
		it0(h)		Income taxes,
		s0(h)		Private saving,
		gs0		Government saving
		fs0		Foreign savings
		tf0(f)		Factor taxes
		tr0(h)		Government transfers to households, 
		it0(h)		Income tax payments
		tx(g)		Export tax rate (assumed uniform)
		px0(g)		Reference price for exports
		d0(s)		Domestic supply
		a0(s)		Aggregate supply
		pm0(s)		Reference price of imports
		tm(s)		Import tariff rate
		ti		Investment tax rate
		inv0		Total investment
		depr0(f)	Deprecation
		xk0(f)		Foreign factor return;

loop((mapc(i,g), mapa(j,s)),	id0(g,s) = sam(i,j) );
loop((mapf(i,f), mapa(j,s)),	fd0(f,s) = sam(i,j) );
loop((mapc(j,g), mapg(i)), 	tm0(g) = sam(i,j) );
loop((mapc(j,g), mapx(i)), 	m0(g) = sam(i,j) );
loop((mapc(i,g), maph(j,h)), 	c0(g,h) = sam(i,j) );
loop((mapc(i,g), mapg(j)),	g0(g) = sam(i,j));
loop((mapc(i,g), mapi(j)),	i0(g) = sam(i,j));
loop((mapc(i,g), mapx(j)),	x0(g) = sam(i,j));
loop((mapg(i), mapx(j)),	tx0 = sam(i,j));
loop((mapg(i), mapi(j)),	ti0 = sam(i,j));
loop((maph(i,h),mapf(j,f)),	fe0(f,h) = sam(i,j));
loop((mapg(i), maph(j,h)),	it0(h) = sam(i,j));
loop((mapi(i), maph(j,h)),	s0(h) = sam(i,j));
loop((mapi(i), mapg(j)),	gs0 = sam(i,j));
loop((mapi(i), mapx(j)),	fs0 = sam(i,j));
loop((maph(i,h), mapg(j)),	tr0(h) = sam(i,j));
loop((mapg(i), mapf(j,f)),	tf0(f) = sam(i,j));
loop((mapx(i), mapf(j,f)),	xk0(f) = sam(i,j));
loop((mapi(i), mapf(j,f)),	depr0(f) = sam(i,j));

*	Assume a uniform export tax:

tx(g) = tx0 / (tx0 + sum(gg, x0(gg)));

px0(g) = 1 - tx(g);

*	Express x0(g) as a gross of tax value:

x0(g) = x0(g) / (1 - tx(g));

d0(s) = sum(g, id0(g,s)) + sum(f, fd0(f,s)) - x0(s)*px0(s);

tm(g) = tm0(g) / m0(g);
pm0(g) = 1 + tm(g);
a0(g) = d0(g) + m0(g) * pm0(g);

inv0 = ti0 + sum(g, i0(g));
ti = ti0/inv0;



	y(s)	! Sectoral output (domestic production)
	a(s)	! Aggregate supply (Armington aggregate)
	c(h)	! Household consumption
	invest	! Aggregate investment

	pd(s)	! Domestic output
	pa(s)	! Composite demand price
	pc(h)	! Household consumption price
	pf(f)	! Factor prices
	pinv	! Investment 
	pfx	! Price of foreign exchange

	ra(h)	! Private households
	govt	! Government

	tau	! Consumption tax rate (for trade tax experiment)

*	Production for domestic market and for export:

$prod:y(s) s:0 t:4 va:1
	o:pfx	q:x0(s)	p:px0(s) a:govt t:tx(s)
	o:pd(s)	q:d0(s)
	i:pa(g)	q:id0(g,s)
	i:pf(f)	q:fd0(f,s)	va:

	v:x(s)	o:pfx	prod:y(s)

*	Armington aggregation of domestic and imported goods:

$prod:a(s)  s:2
	o:pa(s)	q:a0(s)
	i:pd(s)	q:d0(s)
	i:pfx	q:m0(s)  p:pm0(s)  a:govt  t:tm(s)

	v:m(s)	i:pfx	prod:a(s)

*	Investment:

	o:pinv	q:inv0	a:govt t:ti
	i:pa(g)	q:i0(g)

*	Household consumption:

$prod:c(h)  s:1
	o:pc(h)	q:(sum(g, c0(g,h)))
	i:pa(g)	q:c0(g,h)		a:govt  n:tau

*	Household demand (with exogenously fixed investment,
*	taxes and transfers):

	e:pinv	q:(-s0(h))
	e:pc(h)	q:(tr0(h)-it0(h))
	e:pf(f)	q:fe0(f,h)

*	Government demand:

$demand:govt s:0

*	Government demand for goods appears here:

	d:pa(g)	q:g0(g)

*	Income tax revenue less transfers, fixed in real terms:

	e:pc(h)	q:(it0(h)-tr0(h))

*	Inestment demand -- this includes government savings, foreign
*	savings and depreciation:

	e:pinv	q:(-gs0-fs0-sum(f,depr0(f)))

*	Factor ownership includes lump-sum taxes on factor income,
*	depreciation and returns to foreign factor owners:

	e:pf(f)	q:(tf0(f)+depr0(f)+xk0(f))

*	Foreign savings appear as a credit, returns paid to foriegn
*	factor owners is a debit:

	e:pfx	q:(fs0-sum(f,xk0(f)))

	govt =e= sum(g, pa(g) * g0(g));

$sysinclude mpsgeset soe

*	Check the benchmark:

soe.iterlim = 0;
$include soe.gen
solve soe using mcp;

*	Replace export and import taxes by a consumption tax,
*	holding public expenditure constant:

soe.iterlim = 5000;

tm(s) = 0;
tx(s) = 0;

$include soe.gen
solve soe using mcp;

*	Generate a report:

parameter	EV	Hicksian equivalent variation in income (% change)
		trade	Trade levels (% change)
		ct0(h)	Total consumption;

ct0(h) = sum(g, c0(g,h));

ev(H,"%C") = 100 * (c.l(h)-1);
ev(H,"%GDP") = 100 * (c.l(h)-1) * (ct0(h) / (ct0(h)+s0(h)));
ev("total","%C") = 100 * (sum(h,c.l(h)*ct0(h))/sum(h,ct0(h))-1);
ev("total","%GDP") = 100 * (sum(h,c.l(h)*ct0(h))/sum(h,ct0(h))-1) * 
				sum(h, ct0(h)) / sum(f, sum(h,fe0(f,h))+tf0(f)+depr0(f));

trade(s,"export") = 100 * (x.l(s)/x0(s) - 1);
trade(s,"import") = 100 * (m.l(s)/m0(s) - 1);

display trade, EV, tau.l;

Display output:

----   1081 PARAMETER TRADE         Trade levels (% change)

         EXPORT      IMPORT

AGR      36.218      42.128
IND      68.401      33.859
SER      26.678      33.733

----   1081 PARAMETER EV            Hicksian equivalent variation in income (% change)

               %C        %GDP

RURAL       4.759       4.756
URBAN       1.229       1.218
TOTAL       3.121       2.974

----   1081 VARIABLE  TAU.L                =        0.156 Consumption tax rate (for trade tax experiment)