* ---------- ANAR1 Calculating various indices ----------------------------

* ---- Terms of trade

 TOT(r,clos,simr) = (SUM((c,w),resPWE(c,w,r,clos,simr)*QER0(c,w,r))/
                          SUM((c,w), PWE0(c,w,r)*QER0(c,w,r)) ) /
                         (SUM((c,w),resPWM(w,c,r,clos,simr)*QMR0(w,c,r))/
                          SUM((c,w), PWM0(w,c,r)*QMR0(w,c,r)) )  ;

 TOTcagg("cagr",r,clos,simr)$(SUM((cagr,w), PWE0(cagr,w,r)*QER0(cagr,w,r))
                        AND SUM((cagr,w), PWM0(w,cagr,r)*QMR0(w,cagr,r)))
                  = (SUM((cagr,w),resPWE(cagr,w,r,clos,simr)*QER0(cagr,w,r))
                                / SUM((cagr,w), PWE0(cagr,w,r)*QER0(cagr,w,r)) )
                     /(SUM((cagr,w),resPWM(w,cagr,r,clos,simr)*QMR0(w,cagr,r))
                               / SUM((cagr,w), PWM0(w,cagr,r)*QMR0(w,cagr,r)) )  ;

 TOTcagg("cnat",r,clos,simr)$(SUM((cnat,w), PWE0(cnat,w,r)*QER0(cnat,w,r))
                        AND SUM((cnat,w), PWM0(w,cnat,r)*QMR0(w,cnat,r)))
                  = (SUM((cnat,w),resPWE(cnat,w,r,clos,simr)*QER0(cnat,w,r))
                                / SUM((cnat,w), PWE0(cnat,w,r)*QER0(cnat,w,r)) )
                     /(SUM((cnat,w),resPWM(w,cnat,r,clos,simr)*QMR0(w,cnat,r))
                               / SUM((cnat,w), PWM0(w,cnat,r)*QMR0(w,cnat,r)) )  ;

 TOTcagg("cfd",r,clos,simr)$(SUM((cfd,w), PWE0(cfd,w,r)*QER0(cfd,w,r))
                        AND SUM((cfd,w), PWM0(w,cfd,r)*QMR0(w,cfd,r)))
                  = (SUM((cfd,w),resPWE(cfd,w,r,clos,simr)*QER0(cfd,w,r))
                                / SUM((cfd,w), PWE0(cfd,w,r)*QER0(cfd,w,r)) )
                     /(SUM((cfd,w),resPWM(w,cfd,r,clos,simr)*QMR0(w,cfd,r))
                               / SUM((cfd,w), PWM0(w,cfd,r)*QMR0(w,cfd,r)) )  ;

 TOTcagg("cind",r,clos,simr)$(SUM((cind,w), PWE0(cind,w,r)*QER0(cind,w,r))
                        AND SUM((cind,w), PWM0(w,cind,r)*QMR0(w,cind,r)))
                  = (SUM((cind,w),resPWE(cind,w,r,clos,simr)*QER0(cind,w,r))
                                / SUM((cind,w), PWE0(cind,w,r)*QER0(cind,w,r)) )
                     /(SUM((cind,w),resPWM(w,cind,r,clos,simr)*QMR0(w,cind,r))
                               / SUM((cind,w), PWM0(w,cind,r)*QMR0(w,cind,r)) )  ;

 TOTcagg("cuti",r,clos,simr)$(SUM((cuti,w), PWE0(cuti,w,r)*QER0(cuti,w,r))
                        AND SUM((cuti,w), PWM0(w,cuti,r)*QMR0(w,cuti,r)))
                  = (SUM((cuti,w),resPWE(cuti,w,r,clos,simr)*QER0(cuti,w,r))
                                / SUM((cuti,w), PWE0(cuti,w,r)*QER0(cuti,w,r)) )
                     /(SUM((cuti,w),resPWM(w,cuti,r,clos,simr)*QMR0(w,cuti,r))
                               / SUM((cuti,w), PWM0(w,cuti,r)*QMR0(w,cuti,r)) )  ;

 TOTcagg("cser",r,clos,simr)$(SUM((cser,w), PWE0(cser,w,r)*QER0(cser,w,r))
                        AND SUM((cser,w), PWM0(w,cser,r)*QMR0(w,cser,r)))
                  = (SUM((cser,w),resPWE(cser,w,r,clos,simr)*QER0(cser,w,r))
                                / SUM((cser,w), PWE0(cser,w,r)*QER0(cser,w,r)) )
                     /(SUM((cser,w),resPWM(w,cser,r,clos,simr)*QMR0(w,cser,r))
                               / SUM((cser,w), PWM0(w,cser,r)*QMR0(w,cser,r)) )  ;

Display TOT, TOTcagg ;

* ---- Aggregate world export prices
$ontext
 Create agg world export price by region valued in world currency
 using PWE
 Step two: construct a weighted average where the weights are each regions's
 share in world exports
$offtext

 regxwts(c,w,r)$QER0(c,w,r)
                        = (PWE0(c,w,r)*QER0(c,w,r))/
                           SUM(wp, PWE0(c,wp,r)*QER0(c,wp,r)) ;

 PWEAGG(c,r,clos,simr)  = SUM(w, resPWE(c,w,r,clos,simr)* regxwts(c,w,r)) ;

* regxwtsCHK(c,r)        = SUM(w, regxwts(c,w,r)) ;

 totalx(c)              = SUM((wp,rp),PWE0(c,wp,rp)*QER0(c,wp,rp));

 regxwts2(c,r)$totalx(c)
                        = SUM(w, PWE0(c,w,r)*QER0(c,w,r))/
                           SUM((wp,rp), PWE0(c,wp,rp)*QER0(c,wp,rp));

 PWEAGG2(c,clos,simr)   = SUM(r, PWEAGG(c,r,clos,simr)*regxwts2(c,r)) ;

 pPWEAGG2(c,clos,simr)$PWEAGG2(c,clos,"base")
                    = (PWEAGG2(c,clos,simr)/PWEAGG2(c,clos,"base") -1)*100 ;

* regxwts2CHK(c)        = SUM(r, regxwts2(c,r)) ;

*Display  regxwtsCHK, regxwts2CHK ;

* ---- Aggregate world import prices
$ontext
 Create agg world import price by region valued in world currency
 using PWMFOB
 Step two: construct a weighted average where the weights are each regions's
 share in world exports
$offtext

 regmwts(w,c,r)$QMR0(w,c,r)
                        = (PWMFOB0(w,c,r)*QMR0(w,c,r))/
                           SUM(wp, PWMFOB0(wp,c,r)*QMR0(wp,c,r)) ;

 PWMAGG(c,r,clos,simr)   = SUM(w, resPWMFOB(w,c,r,clos,simr)* regmwts(w,c,r)) ;

* regmwtsCHK(c,r)        = SUM(w, regmwts(w,c,r)) ;

 totalm(c)              = SUM((wp,rp),PWMFOB0(wp,c,rp)*QMR0(wp,c,rp));

 regmwts2(c,r)$totalm(c)
                        = SUM(w, PWMFOB0(w,c,r)*QMR0(w,c,r))/
                           SUM((wp,rp), PWMFOB0(wp,c,rp)*QMR0(wp,c,rp));

 PWMAGG2(c,clos,simr)     = SUM(r,PWMAGG(c,r,clos,simr)*regmwts2(c,r)) ;

 pPWMAGG2(c,clos,simr)$PWMAGG2(c,clos,"base")
                    = (PWMAGG2(c,clos,simr)/PWMAGG2(c,clos,"base") -1)*100 ;

* regmwts2CHK(c)        = SUM(r, regmwts2(c,r)) ;

*Display  regmwtsCHK, regmwts2CHK ;

* ---- Government and Investment Price Indices

 gcomtotsh(c,r)$SUM(cp,SAMG(cp,"govt",r))
                      = SAMG(c,"govt",r)/(SUM(cp,SAMG(cp,"govt",r))) ;
 icomtotsh(c,r)$SUM(cp,SAMG(cp,"kap",r))
                      = SAMG(c,"kap",r)/(SUM(cp,SAMG(cp,"kap",r))) ;

 GPI(r,clos,simr)     = SUM(c,gcomtotsh(c,r)*resPQD(c,r,clos,simr)) ;
 IPI(r,clos,simr)     = SUM(c,icomtotsh(c,r)*resPQD(c,r,clos,simr)) ;


* ------- 9. Exporting Results to GDX ------------------------------------

* This writes the results data to GDX

Execute_Unload  'results/anar1__resindex.gdx',

* Writing variable results to GDX
* ---- Terms of trade
 TOT
 TOTcagg

* ---- Aggregate world export prices
 PWEAGG
 pPWEAGG
 PWEAGG2
 pPWEAGG2

* ---- Aggregate world import prices
 PWMAGG
 pPWMAGG
 PWMAGG2
 pPWMAGG2

* ---- Government and Investment Price Indices
 GPI
 IPI

   ;


* ---------- End of anar1 Calculating various indices ---------------------