* -------------- ANAR1 asamg1 ---------------------------------------------

$ontext
  This section checks that the model is calibrated with a balanced SAM,
by using a Macro SAM.

  The programme aborts IF
           - ROW AND COLUMN SUMS FOR ASAMG1 NOT EQUAL
           - ALL ASAMG1 CELL ENTRIES DO NOT EQUAL ASAMG0 ENTRIES

$offtext

*#### TO CHECK FOR DATA CONSISTENCY, DISPLAY CALCULATED ASAMG

 ASAMG1("COMMDTY","ACTIVITY",r) = SUM(c,QINTD.L(c,r)*PQD.L(c,r)) ;
 ASAMG1("COMMDTY","HHOLDS",r)   = SUM((c,h),QCD.L(c,h,r)*PQD.L(c,r)
                                      *(1+TV.L(c,r))) ;
 ASAMG1("COMMDTY","GOVTN",r)    = SUM(c,QGD.L(c,r)*PQD.L(c,r)) ;
 ASAMG1("COMMDTY","KAPITAL",r)  = SUM(c,QINVD.L(c,r)*PQD.L(c,r)) ;
 ASAMG1("COMMDTY","WORLD",r)    = SUM((c,w),PWE.L(c,w,r)
                                     *QER.L(c,w,r)*ER.L(r)) ;

 ASAMG1("ACTIVITY","COMMDTY",r) = SUM(a,PX.L(a,r)*QX.L(a,r)) ;

 ASAMG1("VALUAD","ACTIVITY",r)  = SUM((f,a),WF.L(f,r)*WFDIST.L(f,a,r)
                                      *FD.L(f,a,r)) ;

 ASAMG1("HHOLDS","VALUAD",r)    = SUM((h,f),hvash(h,f,r)*YFDIST.L(f,r)) ;

 ASAMG1("TMTAX","COMMDTY",r)    = SUM((c,w),TM.L(w,c,r)
                                       *PWM.L(w,c,r)*QMR.L(w,c,r)*ER.L(r))
                                   + SUM((c,w),TMS.L(w,c,r)*QMR.L(w,c,r)) ;

 ASAMG1("TETAX","COMMDTY",r)    = SUM((c,w),TE.L(c,w,r)
                                       *PWE.L(c,w,r)*ER.L(r)*QER.L(c,w,r)) ;

 ASAMG1("TFTAX","ACTIVITY",r)   = SUM((f,a), TF.L(f,a,r)
                                    *WF.L(f,r)*WFDIST.L(f,a,r)*FD.L(f,a,r)) ;

 ASAMG1("GOVTN","COMMDTY",r)    = SUM(c,TS.L(c,r)*PQS.L(c,r)*QQ.L(c,r))
                                     + SUM((c,h),TV.L(c,r)*PQD.L(c,r)
                                      *QCD.L(c,h,r)) ;
 ASAMG1("GOVTN","ACTIVITY",r)   = SUM(a,TX.L(a,r)*PX.L(a,r)*QX.L(a,r)) ;

 ASAMG1("GOVTN","VALUAD",r)     = SUM(f,TYF.L(f,r)*(1-deprec(f,r))*YF.L(f,r)) ;
 ASAMG1("GOVTN","HHOLDS",r)     = SUM(h,TYH.L(h,r)*YH.L(h,r)) ;
 ASAMG1("GOVTN","TMTAX",r)      = SUM((c,w),TM.L(w,c,r)
                                       *PWM.L(w,c,r)*QMR.L(w,c,r)*ER.L(r))
                                   + SUM((c,w),TMS.L(w,c,r)*QMR.L(w,c,r)) ; ;
 ASAMG1("GOVTN","TETAX",r)      = SUM((c,w),TE.L(c,w,r)
                                       *PWE.L(c,w,r)*ER.L(r)*QER.L(c,w,r)) ;
 ASAMG1("GOVTN","TFTAX",r)      = SUM((f,a), TF.L(f,a,r)
                                      *WF.L(f,r)*WFDIST.L(f,a,r)*FD.L(f,a,r)) ;

 ASAMG1("KAPITAL","VALUAD",r)   = SUM(f,deprec(f,r)*YF.L(f,r)) ;
 ASAMG1("KAPITAL","HHOLDS",r)   = SUM(h,(YH.L(h,r)*(1-TYH.L(h,r)))
                                        *(SHH.L(h,r))) ;
 ASAMG1("KAPITAL","GOVTN",r)    = KAPGOV.L(r) ;
 ASAMG1("KAPITAL","WORLD",r)    = (KAPWOR.L(r)*ER.L(r)) ;

 ASAMG1("MARGS","COMMDTY",r)    = SUM((c,w),PT.L(c,r)*QT.L(w,c,r)*ER.L(r)) ;

 ASAMG1("WORLD","COMMDTY",r)    = SUM((c,w),PWMFOB.L(w,c,r)
                                      *QMR.L(w,c,r)*ER.L(r)) ;

 ASAMG1("WORLD","MARGS",r)      = SUM((c,w),PT.L(c,r)*QT.L(w,c,r)*ER.L(r)) ;

 ASAMG1("TOTALS",ssnp,r)        = SUM(ssn,ASAMG1(ssn,ssnp,r)) ;
 ASAMG1(ssn,"TOTALS",r)         = SUM(ssnp,ASAMG1(ssn,ssnp,r)) ;


Execute_Unload  'ASAMG1_chk.gdx', ASAMG1 ;

*-------- Checking that a balanced and equal ASAMG is produced
PARAMETER
 ASAMG1CHK(ss,ss,r)  Ratio of ASAMG1 to ASAMG0 should be 1
 CHKASAMG1(r)        Differences in Row and Column totals of ASAMG1

 DIFFASAMG1(ss,ss,r) Differences between SAMG and SAMG1

 CNTASAMG1(r)        Count of non zero entries in DIFFASAMG1

 ;

 ASAMG1CHK(ss,ssp,r)$ASAMG0(ss,ssp,r) = ASAMG1(ss,ssp,r)/ASAMG0(ss,ssp,r) ;

 CHKASAMG1(r) = SUM(ssn,((ASAMG1(ssn,"TOTALS",r)-ASAMG1("TOTALS",ssn,r)))) ;

 ABORT $(SUM(r,CHKASAMG1(r) GT 0.000005))
         "ASAMG1: ROW AND COLUMN SUMS FOR ASAMG1 NOT EQUAL" ;

 DIFFASAMG1(ss,ssp,r)  = ASAMG0(ss,ssp,r) - ASAMG1(ss,ssp,r) ;

 DIFFASAMG1(ss,ssp,r)$(ABS(DIFFASAMG1(ss,ssp,r)
                  lt (10*mod_cont("toldiffsam")))) = 0.0 ;

 DIFFASAMG1("totals","totals",r)   = 0.0 ;

 CNTASAMG1(r)  = SUM((ssn,ssnp)$DIFFASAMG1(ssn,ssnp,r), 1) ;

 OPTION decimals = 6 ;

 DISPLAY ASAMG1, ASAMG1CHK, CHKASAMG1, DIFFASAMG1, CNTASAMG1 ;

 OPTION decimals = 3 ;

 ABORT$(SUM(r,CNTASAMG1(r) GT 0.0))
         "ALL ASAMG1 CELL ENTRIES DO NOT EQUAL ASAMG0 ENTRIES" ;

* -------------- end of anar1 asamg1 --------------------------------------
