I am doing a Gravity model in GAMS to estimate the trade between countries. That is, I want to do the following equation:
Where A_i^m and B_j^m are unknown. They are calculated in the following way:
That is, they should be solved iteratively. I am doing that in GAMS using loops.
Consider the following data:
set I "Iteration" / I01 * I100 /
C "Commodity" / C01 /
A "Export country" / USA,Japan,Germany /
D "Import country" / USA,Japan,Germany /
T "Time" / 2018,2019 /;
parameters
tc(A,D,C) "Transport distance in 1.000 km (intranational distance is set to 1000000)"
/ USA .USA .C01 1000000
USA .Japan .C01 10.14
USA .Germany .C01 7.85
Japan .Japan .C01 1000000
Japan .USA .C01 10.14
Japan .Germany .C01 9.04
Germany .Germany .C01 1000000
Germany .USA .C01 7.85
Germany .Japan .C01 10.14 /
x(A,C,T) "Export"
/ USA .C01 .2018 5000
USA .C01 .2019 6000
Japan .C01 .2018 2000
Japan .C01 .2019 2500
Germany .C01 .2018 3000
Germany .C01 .2019 3500 /
y(D,C,T) "Import"
/ USA .C01 .2018 4000
USA .C01 .2019 5000
Japan .C01 .2018 3000
Japan .C01 .2019 3500
Germany .C01 .2018 3000
Germany .C01 .2019 3500 /
fc(A,D,C) "Function of transport distance";
fc(A,D,C) = exp(tc(A,D,C)*(-1));
loop((A,D)$(ord(A)=ord(D)),
fc(A,D,C) = 0;
;)
Then I am solving A_i^m and B_j^m and calculating x{ij}^m_ (called “Trade” in the code below) in the following way:
parameters Av(A,C,I),Bv(D,C,I),AvT(A,C),BvT(D,C),trade(A,D,C,T);
loop(T,
loop(C,
Av(A,C,"I01") = 1;
loop(I$(I.pos>1 and I.pos<100),
Bv(D,C,I) = 1 / (sum(A,fc(A,D,C) * Av(A,C,I-1) * x(A,C,T)));
Av(A,C,I) = 1 / (sum(D,fc(A,D,C) * Bv(D,C,I) * y(D,C,T)));
AvT(A,C) = Av(A,C,I);
BvT(D,C) = Bv(D,C,I);
);
Av(A,C,"I01") = AvT(A,C);
Bv(D,C,"I01") = BvT(D,C);
);
trade(A,D,C,T) = Av(A,C,"I01")*Bv(D,C,"I01")*x(A,C,T)*y(D,C,T)*fc(A,D,C);
);
However, my problem it takes me very long time if I have do to for at lot of years, commoditity and countries. Therefore, can anyone tell me if there is a faster way of doing it compared to the method above. Can I somehow optimize my loop or something like that?