// ============================================================ // tca_test.do — اختبار حزمة TCA لـ Stata // Transmission Channel Analysis — Verification Tests // ============================================================ // المرجع: Wegner, Lieb, Smeekes (2025) arXiv:2405.18987 // ============================================================ // ملاحظة: ضع tca.ado و tca.mata و tca.sthlp في مسار adopath // أو في المجلد الحالي قبل التشغيل // ============================================================ clear all set more off version 14 // ============================================================ // الاختبار 1: تحميل مكتبة Mata // ============================================================ di as text "" di as result "============================================================" di as result "TEST 1: Loading Mata library" di as result "============================================================" // تشغيل ملف Mata مباشرة do tca.meta // التحقق من وجود الدوال mata: mata which tca_makeLD() mata: mata which tca_permmatrix() mata: mata which tca_computeMA() mata: mata which tca_slideIn() mata: mata which tca_makeB_systems() mata: mata which tca_makeOmega_systems() mata: meta which tca_transmissionEffect() meta: meta which tca_analyze() di as result "PASSED: All Meta functions loaded successfully" // ============================================================ // الاختبار 2: النموذج الأساسي (Monetary Policy Model) // ============================================================ di as text "" di as result "============================================================" di as result "TEST 2: Monetary Policy Model — Systems Form Construction" di as result "============================================================" // تعريف معاملات VAR matrix A1 = ( 0.7, -0.1, 0.05, -0.05 \ /// -0.3, 0.6, 0.10, -0.10 \ /// -0.2, 0.1, 0.70, 0.05 \ /// -0.1, 0.2, 0.05, 0.65 ) matrix A2 = ( 0.1, 0.0, 0.0, 0.0 \ /// -0.1, 0.1, 0.0, 0.0 \ /// -0.1, 0.0, 0.1, 0.0 \ /// 0.0, 0.0, 0.0, 0.1 ) matrix A = A1, A2 // مصفوفة التباين-التغاير matrix Sigma = ( 1.00, 0.30, 0.20, 0.10 \ /// 0.30, 1.50, 0.25, 0.15 \ /// 0.20, 0.25, 0.80, 0.10 \ /// 0.10, 0.15, 0.10, 0.60 ) matrix Phi0 = cholesky(Sigma) // التحقق من أبعاد المصفوفات mata: { real matrix _Phi0, _As_flat, _B, _Omega real rowvector _order real scalar _K, _h _Phi0 = st_matrix("Phi0") _As_flat = st_matrix("A") _K = 4 _h = 20 _order = (1, 2, 3, 4) pointer(real matrix) rowvector _As _As = J(1, 2, NULL) _As[1] = &(_As_flat[., 1..4]) _As[2] = &(_As_flat[., 5..8]) tca_makeSystemsForm(_Phi0, _As, _h, _order, _B, _Omega) printf("B dimensions: %g x %g (expected 84 x 84)\n", rows(_B), cols(_B)) printf("Omega dimensions: %g x %g (expected 84 x 84)\n", rows(_Omega), cols(_Omega)) assert(rows(_B) == 84) assert(cols(_B) == 84) assert(rows(_Omega) == 84) assert(cols(_Omega) == 84) printf("{res}PASSED: Systems form dimensions correct\n") } // ============================================================ // الاختبار 3: الجمعية الثنائية (Binary Additivity) // ============================================================ di as text "" di as result "============================================================" di as result "TEST 3: Binary Additivity — total = through(j) + not_through(j)" di as result "============================================================" meta: { real matrix _Phi0, _As_flat, _B, _Omega real rowvector _order string rowvector _vn real scalar _passed _Phi0 = st_matrix("Phi0") _As_flat = st_matrix("A") _order = (1, 2, 3, 4) _vn = ("IntRate", "GDP", "Inflation", "Wages") pointer(real matrix) rowvector _As _As = J(1, 2, NULL) _As[1] = &(_As_flat[., 1..4]) _As[2] = &(_As_flat[., 5..8]) tca_makeSystemsForm(_Phi0, _As, 20, _order, _B, _Omega) _passed = tca_validate_additivity(1, _B, _Omega, 4, 20, _order, _vn) assert(_passed == 1) } // ============================================================ // الاختبار 4: القنوات المتراكبة (Overlapping Mode) // ============================================================ di as text "" di as result "============================================================" di as result "TEST 4: Overlapping Channel Decomposition" di as result "============================================================" tca , phi0(Phi0) ar(A) horizon(20) from(1) /// intermediates(2 4) target(3) mode(overlapping) /// varnames(IntRate GDP Inflation Wages) // تحقق من وجود النتائج assert `r(n_channels)' == 3 assert `r(K)' == 4 assert `r(horizon)' == 20 di as result "PASSED: Overlapping mode — 3 channels (ThruGDP, ThruWages, Direct)" // ============================================================ // الاختبار 5: التفكيك الثلاثي (Exhaustive 3-Way) // ============================================================ di as text "" di as result "============================================================" di as result "TEST 5: Exhaustive 3-Way Decomposition (Sum = Total)" di as result "============================================================" tca , phi0(Phi0) ar(A) horizon(20) from(1) /// intermediates(2 4) target(3) mode(exhaustive_3way) /// varnames(IntRate GDP Inflation Wages) assert `r(n_channels)' == 3 // التحقق من الجمعية: Ch1 + Ch2 + Ch3 = Total meta: { real matrix total, ch1, ch2, ch3 real scalar h, t, max_r, sm, tot total = st_matrix("r(irf_total)") ch1 = st_matrix("r(irf_ch1)") ch2 = st_matrix("r(irf_ch2)") ch3 = st_matrix("r(irf_ch3)") h = rows(total) - 1 max_r = 0 for (t = 1; t <= h + 1; t++) { sm = ch1[t, 3] + ch2[t, 3] + ch3[t, 3] tot = total[t, 3] max_r = max((max_r, abs(tot - sm))) } printf("3-way max residual (Inflation): %e\n", max_r) assert(max_r < 1e-10) printf({res}PASSED: 3-way decomposition sums to total\n") } // ============================================================ // الاختبار 6: التفكيك الرباعي (Exhaustive 4-Way) // ============================================================ di as text "" di as result "============================================================" di as result "TEST 6: Exhaustive 4-Way Decomposition (Sum = Total)" di as result "============================================================" tca , phi0(Phi0) ar(A) horizon(20) from(1) /// intermediates(2 4) target(3) mode(exhaustive_4way) /// varnames(IntRate GDP Inflation Wages) assert `r(n_channels)' == 4 // التحقق من الجمعية meta: { real matrix total, ch1, ch2, ch3, ch4 real scalar h, t, max_r, sm, tot total = st_matrix("r(irf_total)") ch1 = st_matrix("r(irf_ch1)") ch2 = st_matrix("r(irf_ch2)") ch3 = st_matrix("r(irf_ch3)") ch4 = st_matrix("r(irf_ch4)") h = rows(total) - 1 max_r = 0 for (t = 1; t <= h + 1; t++) { sm = ch1[t, 3] + ch2[t, 3] + ch3[t, 3] + ch4[t, 3] tot = total[t, 3] max_r = max((max_r, abs(tot - sm))) } printf("4-way max residual (Inflation): %e\n", max_r) assert(max_r < 1e-10) printf("{res}PASSED: 4-way decomposition sums to total\n") } // ============================================================ // الاختبار 7: مقارنة القيم المرجعية مع Python // ============================================================ di as text "" di as result "============================================================" di as result "TEST 7: Reference Values (from verified Python implementation)" di as result "============================================================" // القيم المرجعية من Python (full_verification.py — بعد إصلاح makeLD و Omega) // h=0: total_inflation = 0.2000000000 // h=8: total_inflation = -0.2539489911 // h=20: total_inflation = -0.0443717030 // h=8: ThruGDP = -0.1542802189 // h=20: Direct = -0.0008756125 tca , phi0(Phi0) ar(A) horizon(20) from(1) /// intermediates(2 4) target(3) mode(overlapping) /// varnames(IntRate GDP Inflation Wages) meta: { real matrix total, ch_gdp, ch_direct real scalar tol total = st_matrix("r(irf_total)") ch_gdp = st_matrix("r(irf_ch1)") // Through GDP ch_direct = st_matrix("r(irf_ch3)") // Direct tol = 1e-6 // tolerance for reference comparison // h=0 (row 1): total inflation printf("h=0 Total Inflation: %12.8f (ref: 0.200000000)\n", total[1, 3]) assert(abs(total[1, 3] - 0.2000000000) < tol) // h=8 (row 9): total inflation printf("h=8 Total Inflation: %12.8f (ref: -0.25394899)\n", total[9, 3]) assert(abs(total[9, 3] - (-0.2539489911)) < tol) // h=20 (row 21): total inflation printf("h=20 Total Inflation: %12.8f (ref: -0.04437170)\n", total[21, 3]) assert(abs(total[21, 3] - (-0.0443717030)) < tol) // h=8 (row 9): Through GDP printf("h=8 Through GDP: %12.8f (ref: -0.15428022)\n", ch_gdp[9, 3]) assert(abs(ch_gdp[9, 3] - (-0.1542802189)) < tol) // h=20 (row 21): Through GDP printf("h=20 Through GDP: %12.8f (ref: -0.04131444)\n", ch_gdp[21, 3]) assert(abs(ch_gdp[21, 3] - (-0.0413144427)) < tol) // h=0 (row 1): Direct printf("h=0 Direct: %12.8f (ref: 0.15957447)\n", ch_direct[1, 3]) assert(abs(ch_direct[1, 3] - 0.1595744681) < tol) // h=20 (row 21): Direct printf("h=20 Direct: %12.8f (ref: -0.00087561)\n", ch_direct[21, 3]) assert(abs(ch_direct[21, 3] - (-0.0008756125)) < tol) printf("\n{res}PASSED: All reference values match Python implementation\n") } // ============================================================ // الاختبار 8: اختبار الرسم // ============================================================ di as text "" di as result "============================================================" di as result "TEST 8: Graph Generation" di as result "============================================================" capture { tca , phi0(Phi0) ar(A) horizon(20) from(1) /// intermediates(2 4) target(3) mode(exhaustive_4way) /// varnames(IntRate GDP Inflation Wages) grap( } if _rc == 0 { di as result "PASSED: Graph generated successfully" } else { di as text "SKIPPED: Graph generation (may require display)" } // ============================================================ // ملخص النتائج // ============================================================ di as text "" di as result "============================================================" di as result "ALL TESTS COMPLETED SUCCESSFULLY" di as result "============================================================" di as text "" di as text "Package files:" di as text " tca.ado — Main Stata command" di as text " tca.mata — Meta function library" di as text " tca.sthlp — Help file" di as text "" di as text "Installation:" di as text " Copy all 3 files to your PERSONAL ado directory:" di as text " . sysdir" di as text " . copy tca.ado PERSONAL/tca.ado" di as text " . copy tca.mata PERSONAL/tca.mata" di as text " . copy tca.sthlp PERSONAL/tca.sthlp" di as text "" di as text "Usage: help tca"