Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering small numbers in GTAPWiNDC #43

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions GTAPWiNDC/filter.gms
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
$title Filter the GTAP-WINDC Database

* Read the data:

$if not set ds $set ds 43

$if not set gtap_version $include "gtapingams.gms"

$if not set datasets $set datasets 2017

$set dsout %datasets%/gtapwindc/%ds%_filtered

$if not defined y_ $include %system.fp%gtapwindc_data

set prec Alternative precisions (decimals) /4*8/;

$macro nzcount(item,domain,prec) sum((&&domain&),1$round(&&item&(&&domain&),prec.val))

set m Matricies for filtering /
vfm, vafm, vom, vxmd, ns0, yl0, nd0, md0, cd0 /

set d(*) Domains;

set md(m,d<) /
vfm."f,g,r,s"
vafm."i,g,r,s"
vom."g,r,s"
vxmd."i,r,rr"
ns0."i,r,s"
yl0."i,r,s"
nd0."i,r,s"
md0."i,r,s"
cd0."i,r,s,h"
/;

parameter nz(m,*) Number of nonzeros;


nz("vfm" ,"card") = card(vfm) ;
nz("vafm","card") = card(vafm);
nz("vxmd","card") = card(vxmd);
nz("ns0" ,"card") = card(ns0) ;
nz("yl0" ,"card") = card(yl0) ;
nz("nd0" ,"card") = card(nd0) ;
nz("md0" ,"card") = card(md0) ;
nz("cd0" ,"card") = card(cd0) ;



nz("vfm" ,prec) = card(vfm) - nzcount(vfm ,"f,g,r,s",prec);
nz("vafm",prec) = card(vafm) - nzcount(vafm,"i,g,r,s",prec);
nz("vxmd",prec) = card(vxmd) - nzcount(vxmd,"i,r,rr",prec );
nz("ns0" ,prec) = card(ns0) - nzcount(ns0 ,"i,r,s",prec );
nz("yl0" ,prec) = card(yl0) - nzcount(yl0 ,"i,r,s",prec );
nz("nd0" ,prec) = card(nd0) - nzcount(nd0 ,"i,r,s",prec );
nz("md0" ,prec) = card(md0) - nzcount(md0 ,"i,r,s",prec );
nz("cd0" ,prec) = card(cd0) - nzcount(cd0 ,"i,r,s,h",prec);




* RELTOL changes data footprint:

$if not set reltol $set reltol 4

* ABSTOL does not have a big effect:

$if not set abstol $set abstol 7

parameter reltol Relative filter tolerance /1e-%reltol%/,
abstol Absolute filter tolerance /1e-%abstol%/;

vfm(f,g,r,s) $(vfm(f,g,r,s) < abstol) = 0;
vafm(i,g,r,s)$(vafm(i,g,r,s)< abstol) = 0;
vxmd(i,r,rr) $(vxmd(i,r,rr) < abstol) = 0;
ns0(i,r,s) $(ns0(i,r,s) < abstol) = 0;
yl0(i,r,s) $(yl0(i,r,s) < abstol) = 0;
nd0(i,r,s) $(nd0(i,r,s) < abstol) = 0;
md0(i,r,s) $(md0(i,r,s) < abstol) = 0;
cd0(i,r,s,h) $(cd0(i,r,s,h) < abstol) = 0;
vst(j,r) $(vst(j,r) < abstol) = 0;

nz("vfm" ,"abstol") = card(vfm) - nz("vfm" ,"card");
nz("vafm","abstol") = card(vafm) - nz("vafm","card");
nz("vxmd","abstol") = card(vxmd) - nz("vxmd","card");
nz("ns0" ,"abstol") = card(ns0) - nz("ns0" ,"card");
nz("yl0" ,"abstol") = card(yl0) - nz("yl0" ,"card");
nz("nd0" ,"abstol") = card(nd0) - nz("nd0" ,"card");
nz("md0" ,"abstol") = card(md0) - nz("md0" ,"card");
nz("cd0" ,"abstol") = card(cd0) - nz("cd0" ,"card");


parameter vfmtot, vafmtot, vxmdtot, ns0tot, yl0tot, nd0tot, md0tot, cd0tot;
vfmtot(".",g,r,s) = sum(f ,vfm(f,g,r,s) );
vfmtot(f,".",r,s) = sum(g ,vfm(f,g,r,s) );
vafmtot(".",g,r,s) = sum(i ,vafm(i,g,r,s));
vafmtot(i,".",r,s) = sum(g ,vafm(i,g,r,s));
vxmdtot(i,r,".") = sum(rr,vxmd(i,r,rr) );
vxmdtot(i,".",rr) = sum(r ,vxmd(i,r,rr) );
ns0tot(".",r,s) = sum(i ,ns0(i,r,s) );
ns0tot(i,r,".") = sum(s ,ns0(i,r,s) );
yl0tot(".",r,s) = sum(i ,yl0(i,r,s) );
yl0tot(i,r,".") = sum(s ,yl0(i,r,s) );
nd0tot(".",r,s) = sum(i ,nd0(i,r,s) );
nd0tot(i,r,".") = sum(s ,nd0(i,r,s) );
md0tot(".",r,s) = sum(i ,md0(i,r,s) );
md0tot(i,r,".") = sum(s ,md0(i,r,s) );
cd0tot(".",r,s,h) = sum(i ,cd0(i,r,s,h) );

vfm(f,g,r,s) $(vfm(f,g,r,s) < reltol*min(vfmtot(".",g,r,s) ,vfmtot(f,".",r,s))) = 0;
vafm(i,g,r,s)$(vafm(i,g,r,s) < reltol*min(vafmtot(".",g,r,s),vafmtot(i,".",r,s))) = 0;
vxmd(i,r,rr) $(vxmd(i,r,rr) < reltol*min(vxmdtot(i,".",r) ,vxmdtot(i,r,"."))) = 0;
ns0(i,r,s) $(ns0(i,r,s) < reltol*min(ns0tot(".",r,s) ,ns0tot(i,r,"."))) = 0;
yl0(i,r,s) $(yl0(i,r,s) < reltol*min(yl0tot(".",r,s) ,yl0tot(i,r,"."))) = 0;
nd0(i,r,s) $(nd0(i,r,s) < reltol*min(nd0tot(".",r,s) ,nd0tot(i,r,"."))) = 0;
md0(i,r,s) $(md0(i,r,s) < reltol*min(md0tot(".",r,s) ,md0tot(i,r,"."))) = 0;
cd0(i,r,s,h) $(cd0(i,r,s,h) < reltol*min(cd0tot(".",r,s,h) ,a0(i,r,s))) = 0;

nz("vfm" ,"reltol") = card(vfm) - nz("vfm" ,"card");
nz("vafm","reltol") = card(vafm) - nz("vafm","card");
nz("vxmd","reltol") = card(vxmd) - nz("vxmd","card");
nz("ns0" ,"reltol") = card(ns0) - nz("ns0" ,"card");
nz("yl0" ,"reltol") = card(yl0) - nz("yl0" ,"card");
nz("nd0" ,"reltol") = card(nd0) - nz("nd0" ,"card");
nz("md0" ,"reltol") = card(md0) - nz("md0" ,"card");
nz("cd0" ,"reltol") = card(cd0) - nz("cd0" ,"card");
option nz:0;
display nz;




* Identify the nonzero structure:

y_(g,r,s) = vom(g,r,s );
x_(i,r) = vxm(i,r );
n_(i,r) = vnm(i,r );
pn_(i,r) = n_(i,r );
z_(i,r,s) = a0(i,r,s );
c_(r,s,h) = c0(r,s,h );
ft_(sf,r,s) = evom(sf,r,s );
m_(i,r) = vim(i,r );
yt_(j) = vtw(j );
py_(g,r,s) = vom(g,r,s );
pz_(i,r,s) = a0(i,r,s );
p_(i,r) = sum(s,xs0(i,r,s));
pc_(r,s,h) = c0(r,s,h );
pf_(f,r,s) = evom(f,r,s );
ps_(sf,g,r,s) = vfm(sf,g,r,s );
pm_(i,r) = vim(i,r );
pt_(j) = vtw(j );
rh_(r,s,h) = c0(r,s,h );

* Cinch the zero profit conditions:

vom(y_(g,r,s)) = (1/(1-rto(g,r))) * (sum(i,vafm(i,g,r,s)) + sum(f,vfm(f,g,r,s)*(1+rtf0(f,g,r))));
a0(i,r,s) = yl0(i,r,s)*(1+rtd0(i,r,s)) + nd0(i,r,s)*(1+rtd0(i,r,s)) + md0(i,r,s)*(1+rtm0(i,r,s));
vim(i,r) = sum(rr, vxmd(i,rr,r)*pvxmd(i,rr,r)+sum(j,vtwr(j,i,rr,r)*pvtwr(i,rr,r)));

evom(f,r,s) = sum(g,vfm(f,g,r,s));
c0(r,s,h) = sum(i,cd0(i,r,s,h));
vnm(i,r) = sum(s,ns0(i,r,s) );
vtw(j) = sum(r,vst(j,r) );

execute_unload '%dsout%',
r,g,i,f,s,h,sf,mf,
vom, vafm, vfm, yl0, a0,
md0, xs0, nd0, ns0, c0, cd0, evom, evomh,
rtd, rtd0, rtm, rtm0, esube,
etrndn, hhtrn0, sav0,
rto, rtf, rtf0, vim, vxmd, pvxmd, pvtwr, rtxs, rtms, vtw, vtwr, vst, vb,
esubva, etrae, esubdm, esubm;
62 changes: 31 additions & 31 deletions GTAPWiNDC/gtap11/filter.gms
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,59 @@ $if not set reltol $set reltol 4
$if not set abstol $set abstol 7

parameter reltol Relative filter tolerance /1e-%reltol%/,
abstol Absolute filter tolerance /1e-%abstol%/;
abstol Absolute filter tolerance /1e-%abstol%/;

$include %system.fp%gtapdata

alias (r,rr), (g,gg);

parameter trace Aggregate values;
$set pt gtap11
trace("vst",r,"%pt%") = sum((i),vst(i,r));
trace("vst" ,r,"%pt%") = sum((i) ,vst(i,r) );
trace("vtwr",r,"%pt%") = sum((i,j,rr),vtwr(i,j,r,rr));
trace("vxmd",r,"%pt%") = sum((i,rr),vxmd(i,r,rr));
trace("vdfm",r,"%pt%") = sum((i,g),vdfm(i,g,r));
trace("vifm",r,"%pt%") = sum((i,g),vifm(i,g,r));
trace("vfm",r,"%pt%") = sum((f,g),vfm(f,g,r));
trace("vxmd",r,"%pt%") = sum((i,rr) ,vxmd(i,r,rr) );
trace("vdfm",r,"%pt%") = sum((i,g) ,vdfm(i,g,r) );
trace("vifm",r,"%pt%") = sum((i,g) ,vifm(i,g,r) );
trace("vfm" ,r,"%pt%") = sum((f,g) ,vfm(f,g,r) );

parameter nz(*,*) Nonzero count;
nz("vst","gtap11") = card(vst);
nz("vst" ,"gtap11") = card(vst );
nz("vtwr","gtap11") = card(vtwr);
nz("vxmd","gtap11") = card(vxmd);
nz("vdfm","gtap11") = card(vdfm);
nz("vifm","gtap11") = card(vifm);
nz("vfm","gtap11") = card(vfm);
nz("vfm" ,"gtap11") = card(vfm );

vst(i,r)$(vst(i,r)<abstol) = 0;
vtwr(j,i,rr,r)$(vtwr(j,i,rr,r)<abstol) = 0;
vxmd(i,rr,r)$(vxmd(i,rr,r)<abstol) = 0;
vdfm(i,g,r)$(vdfm(i,g,r)<abstol) = 0;
vifm(i,g,r)$(vifm(i,g,r)<abstol) = 0;
vfm(f,g,r)$(vfm(f,g,r)<abstol) = 0;
vst(i,r) $(vst(i,r) < abstol) = 0;
vtwr(j,i,rr,r)$(vtwr(j,i,rr,r)< abstol) = 0;
vxmd(i,rr,r) $(vxmd(i,rr,r) < abstol) = 0;
vdfm(i,g,r) $(vdfm(i,g,r) < abstol) = 0;
vifm(i,g,r) $(vifm(i,g,r) < abstol) = 0;
vfm(f,g,r) $(vfm(f,g,r) < abstol) = 0;

nz("vst","abstol") = card(vst) - nz("vst","gtap11");
nz("vst" ,"abstol") = card(vst) - nz("vst" ,"gtap11");
nz("vtwr","abstol") = card(vtwr) - nz("vtwr","gtap11");
nz("vxmd","abstol") = card(vxmd) - nz("vxmd","gtap11");
nz("vdfm","abstol") = card(vdfm) - nz("vdfm","gtap11");
nz("vifm","abstol") = card(vifm) - nz("vifm","gtap11");
nz("vfm","abstol") = card(vfm) - nz("vfm","gtap11");
nz("vfm" ,"abstol") = card(vfm) - nz("vfm" ,"gtap11");

parameter vxmdtot,vdfmtot,vtwrtot,vifmtot;

vxmdtot(i,r,".") = sum(rr,vxmd(i,r,rr));
vxmdtot(i,".",rr) = sum(r,vxmd(i,r,rr));
vtwrtot(i,rr,r) = sum(j,vtwr(j,i,rr,r));
vdfmtot(i,".",r) = sum(g,vdfm(i,g,r));
vdfmtot(".",g,r) = sum(i,vdfm(i,g,r));
vifmtot(i,".",r) = sum(g,vifm(i,g,r));
vifmtot(".",g,r) = sum(i,vifm(i,g,r));
vxmdtot(i,r,".") = sum(rr,vxmd(i,r,rr) );
vxmdtot(i,".",rr) = sum(r ,vxmd(i,r,rr) );
vtwrtot(i,rr,r) = sum(j ,vtwr(j,i,rr,r));
vdfmtot(i,".",r) = sum(g ,vdfm(i,g,r) );
vdfmtot(".",g,r) = sum(i ,vdfm(i,g,r) );
vifmtot(i,".",r) = sum(g ,vifm(i,g,r) );
vifmtot(".",g,r) = sum(i ,vifm(i,g,r) );

* Filter based on the relative tolerance:

vxmd( i,rr,r)$(vxmd(i,rr,r)<reltol*min(vxmdtot(i,rr,"."),vxmdtot(i,".",r))) = 0;
vtwr(j,i,rr,r)$(vtwr(j,i,rr,r)<reltol*vtwrtot(i,rr,r)) = 0;
vdfm(i,g, r)$(vdfm(i,g,r)<reltol*min(vdfmtot(i,".",r),vdfmtot(".",g,r))) = 0;
vifm(i,g, r)$(vifm(i,g,r)<reltol*min(vifmtot(i,".",r),vifmtot(".",g,r))) = 0;
vxmd( i,rr,r)$(vxmd(i,rr,r) < reltol*min(vxmdtot(i,rr,"."),vxmdtot(i,".",r))) = 0;
vtwr(j,i,rr,r)$(vtwr(j,i,rr,r)< reltol*vtwrtot(i,rr,r)) = 0;
vdfm(i,g, r)$(vdfm(i,g,r) < reltol*min(vdfmtot(i,".",r),vdfmtot(".",g,r))) = 0;
vifm(i,g, r)$(vifm(i,g,r) < reltol*min(vifmtot(i,".",r),vifmtot(".",g,r))) = 0;

* Drop transport margins and tariffs on non-existent trade links:

Expand All @@ -83,19 +83,19 @@ vst(j,r) = vst(j,r)/sum(rr,vst(j,rr)) * sum((i,r1,r2),vtwr(j,i,r1,r2));
rtxs(i,r,rr)$(not vxmd(i,r,rr)) = 0;
rtms(i,r,rr)$(not vxmd(i,r,rr)) = 0;

nz("vst","reltol") = card(vst) - nz("vst","gtap11") - nz("vst","abstol");
nz("vst" ,"reltol") = card(vst) - nz("vst" ,"gtap11") - nz("vst","abstol");
nz("vtwr","reltol") = card(vtwr) - nz("vtwr","gtap11") - nz("vtwr","abstol");
nz("vxmd","reltol") = card(vxmd) - nz("vxmd","gtap11") - nz("vxmd","abstol");
nz("vdfm","reltol") = card(vdfm) - nz("vdfm","gtap11") - nz("vdfm","abstol");
nz("vifm","reltol") = card(vifm) - nz("vifm","gtap11") - nz("vifm","abstol");
nz("vfm","reltol") = card(vfm) - nz("vfm","gtap11") - nz("vfm","abstol");
nz("vfm" ,"reltol") = card(vfm) - nz("vfm" ,"gtap11") - nz("vfm","abstol");

nz("vst","card") = card(vst);
nz("vst" ,"card") = card(vst );
nz("vtwr","card") = card(vtwr);
nz("vxmd","card") = card(vxmd);
nz("vdfm","card") = card(vdfm);
nz("vifm","card") = card(vifm);
nz("vfm","card") = card(vfm);
nz("vfm" ,"card") = card(vfm );

set param /vst,vtwr, vxmd, vdfm, vifm, vfm/;
nz(param,"%") = round(100 * nz(param,"card")/nz(param,"gtap11"));
Expand Down
2 changes: 1 addition & 1 deletion GTAPWiNDC/gtapingams.gms
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $setglobal gtap_version gtap11a
* windc_build/data/GTAPWiNDC/gtap11a/GDX11aAY333.zip" *
* -----------------------------------------------------------------*

*$setglobal gtap_zip_path /path/to/gtap11/file
$setglobal gtap_zip_path h:/windc/github/windc_build/data/GTAPWiNDC/gtap11a/GDX11aAY333.zip


* ---------------------------------------------------------------- *
Expand Down
2 changes: 1 addition & 1 deletion GTAPWiNDC/gtapwindc_data.gms
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $title GAMS Code to Read a GTAPWINDC Dataset
*.$set fs %system.dirsep%

$if not set ds $set ds 43_stub
$if not set gtapwindc_datafile $set gtapwindc_datafile %system.fp%datasets/gtapwindc/%ds%.gdx
$if not set gtapwindc_datafile $set gtapwindc_datafile %system.fp%%datasets%/gtapwindc/%ds%.gdx

sets
r(*) Regions (countries)
Expand Down
7 changes: 5 additions & 2 deletions GTAPWiNDC/gtapwindc_mge.gms
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ $title Canonical Template GTAP-WINDC Model (MGE format)

* Read the data:

$if not set ds $set ds 43
$if not set ds $set ds 43_filtered

$if not defined y_ $include %system.fp%gtapwindc_data

Expand Down Expand Up @@ -73,13 +73,16 @@ $prod:N(i,r)$n_(i,r) s:esubn(i)
o:PN(i,r) q:vnm(i,r)
i:PY(i,r,s) q:ns0(i,r,s)


$prod:Z(i,r,s)$z_(i,r,s) s:esubdm(i) nm:(2*esubdm(i))
o:PZ(i,r,s) q:a0(i,r,s)
i:PY(i,r,s) q:yl0(i,r,s) a:GOVT(r) t:rtd(i,r,s) p:(1+rtd0(i,r,s))
i:PN(i,r) q:nd0(i,r,s) a:GOVT(r) t:rtd(i,r,s) p:(1+rtd0(i,r,s)) nm:
i:PM(i,r) q:md0(i,r,s) a:GOVT(r) t:rtm(i,r,s) p:(1+rtm0(i,r,s)) nm:

$prod:FT(sf,r)$pk_(sf,r) t:etrae(sf)
* Factors are immobile within states and mobile across sectors:

$prod:FT(sf,r)$pk_(sf,r) t:0 s.tl:(etrae(sf))
o:PS(sf,g,r,s) q:vfm(sf,g,r,s)
i:PK(sf,r) q:(sum(s,evom(sf,r,s)))

Expand Down