* -------------- ANAR1 asamg2 ------------------------------------------

$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 ASAMG2 CELL ENTRIES DO NOT EQUAL ASAMG2 ENTRIES

  The ratios of the values in ASAG2 to ASAMG1 must equal the nueraire -
typically 1. IF the numeraire is doubled then the ratios must ALL
equal 2 - See ASAMG2CHK. This is a critical test of the model.
$offtext

*#### TO CHECK FOR DATA CONSISTENCY, DISPLAY CALCULATED ASAMG2
* Checking that the model returns a balanced ASAMG

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

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

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

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

 ASAMG2("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)) ;

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

 ASAMG2("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)) ;

 ASAMG2("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)) ;
 ASAMG2("GOVTN","ACTIVITY",r)   = SUM(a,TX.L(a,r)*PX.L(a,r)*QX.L(a,r)) ;

 ASAMG2("GOVTN","VALUAD",r)     = SUM(f,TYF.L(f,r)*(1-deprec(f,r))*YF.L(f,r)) ;
 ASAMG2("GOVTN","HHOLDS",r)     = SUM(h,TYH.L(h,r)*YH.L(h,r)) ;
 ASAMG2("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)) ;
 ASAMG2("GOVTN","TETAX",r)      = SUM((c,w),TE.L(c,w,r)
                                       *PWE.L(c,w,r)*ER.L(r)*QER.L(c,w,r)) ;
 ASAMG2("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)) ;

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

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

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

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

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

*-------- Checking that a balanced and equal ASAMG is produced
PARAMETER
 ASAMG2CHK(ss,ss,r)  Ratio of ASAMG2 to ASAMG1 should equal numeraire
 CHECK2(r)           Differences in Row and Column totals of ASAMG2

 DIFFASAMG2(ss,ss,r) Differences between ASAMG1 and ASAMG2

 CNTASAMG2(r)        Count of non zero entries in DIFFASAMG2

 ;

 ASAMG2CHK(ss,ssp,r)$ASAMG1(ss,ssp,r) = ASAMG2(ss,ssp,r)/ASAMG1(ss,ssp,r) ;

 CHECK2(r) = SUM(ssn,((ASAMG2(ssn,"TOTALS",r)-ASAMG2("TOTALS",ssn,r)))) ;

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

 DIFFASAMG2(ss,ssp,r)  = ASAMG1(ss,ssp,r) - ASAMG2(ss,ssp,r) ;

Parameter
 DIFFASAMG02(ss,ss,r)  Differences between ASAMG0 and ASMAG2 ;
 DIFFASAMG02(ss,ssp,r)  = ASAMG0(ss,ssp,r) - ASAMG2(ss,ssp,r) ;
Display DIFFASAMG02 ;

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

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

 CNTASAMG2(r)  = SUM((ssn,ssnp)$DIFFASAMG2(ssn,ssnp,r), 1) ;

 OPTION decimals = 6 ;

 DISPLAY ASAMG2, ASAMG2CHK, CHECK2, DIFFASAMG2, CNTASAMG2 ;

 OPTION decimals = 3 ;

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



* -------------- end of anar1 asamg2 -----------------------------------
