1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786
|
/*
* gretl -- Gnu Regression, Econometrics and Time-series Library
* Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "libgretl.h"
#include "version.h"
#include "matrix_extra.h"
#define ADEBUG 0
#define WRITE_MATRICES 0
enum {
DPD_TWOSTEP = 1 << 0,
DPD_ORTHDEV = 1 << 1,
DPD_TIMEDUM = 1 << 2,
DPD_WINCORR = 1 << 3,
DPD_SYSTEM = 1 << 4,
DPD_DPDSTYLE = 1 << 5,
DPD_REDO = 1 << 6
};
#define gmm_sys(d) (d->flags & DPD_SYSTEM)
#define dpd_style(d) (d->flags & DPD_DPDSTYLE)
#define LEVEL_ONLY 2
typedef struct ddset_ ddset;
typedef struct unit_info_ unit_info;
typedef struct diag_info_ diag_info;
struct unit_info_ {
int t1; /* first usable obs in differences for unit */
int t2; /* last usable obs */
int nobs; /* number of usable observations (in the system case
this is the sum of the differenced and level
observations */
int nlev; /* number of obs in levels (0 in non-system case) */
};
struct diag_info_ {
int v; /* ID number of variable */
int depvar; /* is the target variable the dependent variable (1/0) */
int minlag; /* minimum lag order */
int maxlag; /* maximum lag order */
int level; /* instrument spec is for levels */
int rows; /* max rows occupied in Zi (for dpanel only) */
int tbase; /* first obs with potentially available instruments */
};
struct ddset_ {
int ci; /* ARBOND or DPANEL */
int flags; /* option flags */
int step; /* what step are we on? (1 or 2) */
int yno; /* ID number of dependent var */
int p; /* lag order for dependent variable */
int qmax; /* longest lag of y used as instrument (arbond) */
int nx; /* number of independent variables */
int ifc; /* model includes a constant */
int nzr; /* number of regular instruments */
int nzb; /* number of block-diagonal instruments (other than y) */
int nz; /* total columns in instrument matrix */
int pc0; /* column in Z where predet vars start */
int xc0; /* column in Z where exog vars start */
int N; /* total number of units in sample */
int effN; /* number of units with usable observations */
int T; /* total number of observations per unit */
int minTi; /* minimum equations (> 0) for any given unit */
int maxTi; /* maximum diff. equations for any given unit */
int max_ni; /* max number of (possibly stacked) obs per unit
(equals maxTi except in system case) */
int k; /* number of parameters estimated */
int nobs; /* total observations actually used (diffs or levels) */
int totobs; /* total observations (diffs + levels) */
int t1; /* initial offset into dataset */
int t1min; /* first usable observation, any unit */
int t2max; /* last usable observation, any unit */
int ndum; /* number of time dummies to use */
double SSR; /* sum of squared residuals */
double s2; /* residual variance */
double AR1; /* z statistic for AR(1) errors */
double AR2; /* z statistic for AR(2) errors */
double sargan; /* overidentification test statistic */
double wald[2]; /* Wald test statistic(s) */
int wdf[2]; /* degrees of freedom for Wald test(s) */
int *xlist; /* list of independent variables */
int *ilist; /* list of regular instruments */
gretl_matrix_block *B1; /* matrix holder */
gretl_matrix_block *B2; /* matrix holder */
gretl_matrix *beta; /* parameter estimates */
gretl_matrix *vbeta; /* parameter variance matrix */
gretl_matrix *uhat; /* residuals, differenced version */
gretl_matrix *H; /* step 1 error covariance matrix */
gretl_matrix *A; /* \sum Z'_i H Z_i */
gretl_matrix *Acpy; /* back-up of A matrix */
gretl_matrix *V; /* covariance matrix */
gretl_matrix *ZT; /* transpose of full instrument matrix */
gretl_matrix *Zi; /* per-unit instrument matrix */
gretl_matrix *Y; /* transformed dependent var */
gretl_matrix *X; /* lagged differences of y, indep vars, etc. */
gretl_matrix *kmtmp; /* workspace */
gretl_matrix *kktmp;
gretl_matrix *M;
gretl_matrix *L1;
gretl_matrix *XZA;
gretl_matrix *ZY;
gretl_matrix *XZ;
diag_info *d; /* info on block-diagonal instruments */
unit_info *ui; /* info on panel units */
char *used; /* global record of observations used */
/* The members below are specific to the "dpanel" approach */
int ndiff; /* total differenced observations */
int nlev; /* total levels observations */
int nzb2; /* number of block-diagonal specs, levels eqns */
int nzdiff; /* number of insts specific to eqns in differences */
int nzlev; /* number of insts specific to eqns in levels */
int *laglist; /* (possibly discontinuous) list of lags */
diag_info *d2; /* info on block-diagonal instruments, levels eqns
(note: not independently allocated) */
int dcols; /* number of columns, differenced data */
int dcolskip; /* initial number of skipped obs, differences */
int lcol0; /* column adjustment for data in levels */
int lcolskip; /* initial number of skipped obs, levels */
};
#define data_index(dpd,i) (i * dpd->T + dpd->t1)
static void dpanel_residuals (ddset *dpd);
static int dpd_process_list (ddset *dpd, const int *list,
const int *ylags);
static void ddset_free (ddset *dpd)
{
if (dpd == NULL) {
return;
}
gretl_matrix_block_destroy(dpd->B1);
gretl_matrix_block_destroy(dpd->B2);
gretl_matrix_free(dpd->V);
free(dpd->xlist);
free(dpd->ilist);
free(dpd->laglist);
free(dpd->d);
free(dpd->used);
free(dpd->ui);
free(dpd);
}
static int dpd_allocate_matrices (ddset *dpd)
{
int T = dpd->max_ni;
dpd->B1 = gretl_matrix_block_new(&dpd->beta, dpd->k, 1,
&dpd->vbeta, dpd->k, dpd->k,
&dpd->uhat, dpd->totobs, 1,
&dpd->ZT, dpd->nz, dpd->totobs,
&dpd->H, T, T,
&dpd->A, dpd->nz, dpd->nz,
&dpd->Acpy, dpd->nz, dpd->nz,
&dpd->Zi, T, dpd->nz,
&dpd->Y, dpd->totobs, 1,
&dpd->X, dpd->totobs, dpd->k,
NULL);
if (dpd->B1 == NULL) {
return E_ALLOC;
}
dpd->B2 = gretl_matrix_block_new(&dpd->kmtmp, dpd->k, dpd->nz,
&dpd->kktmp, dpd->k, dpd->k,
&dpd->M, dpd->k, dpd->k,
&dpd->L1, 1, dpd->nz,
&dpd->XZA, dpd->k, dpd->nz,
&dpd->ZY, dpd->nz, 1,
&dpd->XZ, dpd->k, dpd->nz,
NULL);
if (dpd->B2 == NULL) {
return E_ALLOC;
}
return 0;
}
static int dpd_add_unit_info (ddset *dpd)
{
int i, err = 0;
dpd->ui = malloc(dpd->N * sizeof *dpd->ui);
if (dpd->ui == NULL) {
err = E_ALLOC;
} else {
for (i=0; i<dpd->N; i++) {
dpd->ui[i].t1 = 0;
dpd->ui[i].t2 = 0;
dpd->ui[i].nobs = 0;
dpd->ui[i].nlev = 0;
}
}
return err;
}
static int dpd_flags_from_opt (gretlopt opt)
{
/* apply Windmeijer correction unless OPT_A is given */
int f = (opt & OPT_A)? 0 : DPD_WINCORR;
if (opt & OPT_D) {
/* include time dummies */
f |= DPD_TIMEDUM;
}
if (opt & OPT_H) {
/* use orthogonal deviations instead of first diffs */
f |= DPD_ORTHDEV;
}
if (opt & OPT_T) {
/* two-step estimation */
f |= DPD_TWOSTEP;
}
if (opt & OPT_L) {
/* system GMM: include levels equations (DPANEL only) */
f |= DPD_SYSTEM;
}
if (opt & OPT_X) {
/* compute H as per Ox/DPD (DPANEL only) */
f |= DPD_DPDSTYLE;
}
return f;
}
static ddset *ddset_new (int ci, const int *list, const int *ylags,
const DATASET *dset, gretlopt opt,
diag_info *d, int nzb, int *err)
{
ddset *dpd = NULL;
int NT;
if (list[0] < 3) {
*err = E_PARSE;
return NULL;
}
dpd = malloc(sizeof *dpd);
if (dpd == NULL) {
*err = E_ALLOC;
return NULL;
}
dpd->ci = ci;
/* set pointer members to NULL just in case */
dpd->B1 = dpd->B2 = NULL;
dpd->V = NULL;
dpd->ui = NULL;
dpd->used = NULL;
dpd->xlist = NULL;
dpd->ilist = NULL;
dpd->laglist = NULL;
dpd->flags = dpd_flags_from_opt(opt);
dpd->d = d;
dpd->nzb = nzb;
dpd->step = 1;
dpd->nx = dpd->nzr = 0;
dpd->nzdiff = 0;
dpd->nzlev = 0;
dpd->t1min = dpd->t2max = 0;
dpd->ndum = 0;
dpd->ifc = 0;
/* "system"-specific */
dpd->d2 = NULL;
dpd->nzb2 = 0;
*err = dpd_process_list(dpd, list, ylags);
if (*err) {
goto bailout;
}
NT = sample_size(dset);
dpd->t1 = dset->t1; /* start of sample range */
dpd->T = dset->pd; /* max obs. per individual */
dpd->effN = dpd->N = NT / dpd->T; /* included individuals */
dpd->minTi = dpd->maxTi = 0;
dpd->max_ni = 0;
dpd->nz = 0;
dpd->pc0 = 0;
dpd->xc0 = 0;
dpd->nobs = dpd->totobs = 0;
dpd->ndiff = dpd->nlev = 0;
dpd->SSR = NADBL;
dpd->s2 = NADBL;
dpd->AR1 = NADBL;
dpd->AR2 = NADBL;
dpd->sargan = NADBL;
dpd->wald[0] = dpd->wald[1] = NADBL;
dpd->wdf[0] = dpd->wdf[1] = 0;
/* # of coeffs on lagged dep var, indep vars */
if (dpd->laglist != NULL) {
dpd->k = dpd->laglist[0] + dpd->nx;
} else {
dpd->k = dpd->p + dpd->nx;
}
#if ADEBUG
fprintf(stderr, "yno = %d, p = %d, qmax = %d, nx = %d, k = %d\n",
dpd->yno, dpd->p, dpd->qmax, dpd->nx, dpd->k);
fprintf(stderr, "t1 = %d, T = %d, N = %d\n", dpd->t1, dpd->T, dpd->N);
#endif
*err = dpd_add_unit_info(dpd);
if (!*err) {
dpd->used = calloc(dset->n, 1);
if (dpd->used == NULL) {
*err = E_ALLOC;
}
}
bailout:
if (*err) {
ddset_free(dpd);
dpd = NULL;
}
return dpd;
}
/* if the const has been included among the regressors but not the
instruments, add it to the instruments */
static int maybe_add_const_to_ilist (ddset *dpd)
{
int i, addc = dpd->ifc;
int err = 0;
if (dpd->xlist == NULL || (dpd->nzr == 0 && dpd->nzb == 0)) {
/* no x's, or all x's treated as exogenous already */
return 0;
}
if (addc && dpd->ilist != NULL) {
for (i=1; i<=dpd->ilist[0]; i++) {
if (dpd->ilist[i] == 0) {
addc = 0;
break;
}
}
}
if (addc) {
dpd->ilist = gretl_list_append_term(&dpd->ilist, 0);
if (dpd->ilist == NULL) {
err = E_ALLOC;
} else {
dpd->nzr += 1;
}
}
return err;
}
static int dpd_make_lists (ddset *dpd, const int *list, int xpos)
{
int i, nz = 0, spos = 0;
int err = 0;
/* do we have a separator, between exog vars and instruments? */
for (i=xpos; i<=list[0]; i++) {
if (list[i] == LISTSEP) {
spos = i;
break;
}
}
#if ADEBUG
printlist(list, "incoming list in dpd_make_lists");
fprintf(stderr, "separator pos = %d\n", spos);
#endif
if (spos > 0) {
dpd->nx = spos - xpos;
nz = list[0] - spos;
} else {
dpd->nx = list[0] - (xpos - 1);
}
#if ADEBUG
fprintf(stderr, "got intial dpd->nx = %d, nz = %d\n", dpd->nx, nz);
#endif
if (dpd->nx > 0) {
/* compose indep vars list */
dpd->xlist = gretl_list_new(dpd->nx);
if (dpd->xlist == NULL) {
err = E_ALLOC;
} else {
for (i=0; i<dpd->nx; i++) {
dpd->xlist[i+1] = list[xpos + i];
if (dpd->xlist[i+1] == 0) {
dpd->ifc = 1;
}
}
#if ADEBUG
printlist(dpd->xlist, "dpd->xlist");
#endif
if (nz == 0 && dpd->nzb == 0) {
/* implicitly all x vars are exogenous */
dpd->ilist = gretl_list_copy(dpd->xlist);
if (dpd->ilist == NULL) {
err = E_ALLOC;
} else {
dpd->nzr = dpd->ilist[0];
}
}
}
}
if (!err && nz > 0) {
/* compose regular instruments list */
dpd->ilist = gretl_list_new(nz);
if (dpd->ilist == NULL) {
err = E_ALLOC;
} else {
for (i=0; i<nz; i++) {
dpd->ilist[i+1] = list[spos + i + 1];
}
dpd->nzr = nz;
}
}
if (!err) {
err = maybe_add_const_to_ilist(dpd);
}
#if ADEBUG
printlist(dpd->ilist, "dpd->ilist");
#endif
return err;
}
static int dpanel_make_laglist (ddset *dpd, const int *list,
int seppos, const int *ylags)
{
int nlags = seppos - 1;
int i, err = 0;
if (nlags != 1) {
err = E_INVARG;
} else if (ylags != NULL) {
dpd->laglist = gretl_list_copy(ylags);
if (dpd->laglist == NULL) {
err = E_ALLOC;
} else {
nlags = dpd->laglist[0];
}
} else {
/* only got p; make "fake" list 1, ..., p */
dpd->p = list[1];
if (dpd->p < 1) {
err = E_INVARG;
} else {
dpd->laglist = gretl_consecutive_list_new(1, dpd->p);
if (dpd->laglist == NULL) {
err = E_ALLOC;
}
}
}
if (ylags != NULL && !err) {
/* sort lags and check for duplicates */
gretl_list_sort(dpd->laglist);
dpd->p = dpd->laglist[nlags];
for (i=1; i<=nlags; i++) {
if (dpd->laglist[i] <= 0) {
err = E_INVARG;
} else if (i > 1 && dpd->laglist[i] == dpd->laglist[i-1]) {
err = E_INVARG;
}
}
}
#if ADEBUG
printlist(dpd->laglist, "dpd->laglist");
#endif
return err;
}
/* Process the incoming list and figure out various parameters
including the maximum lag of the dependent variable, dpd->p, and
the number of exogenous regressors, dpd->nx.
Note that the specification of the first sublist (before the first
LISTSEP) differs between arbond and dpanel.
In arbond this chunk contains either p alone or p plus "qmax", a
limiter for the maximum lag of y to be used as an instrument.
In dpanel, it contains either a single integer value for p or a set
of specific lags of y to use as regressors (given in the form of
integers in braces or as the name of a matrix). In the former case
the auxiliary @ylags list will be NULL; in the latter @ylags
records the specific lags requested. At the user level, therefore,
dpanel 2 ; y ... means use lags 1 and 2
dpanel {2} ; y ... means use lag 2 only
Note that placing a limit on the max lag of y _as instrument_ is
done in dpanel via "GMM(y,min,max)".
*/
static int dpd_process_list (ddset *dpd, const int *list,
const int *ylags)
{
int seppos = gretl_list_separator_position(list);
int xpos, err = 0;
if (seppos < 2 || list[0] == seppos) {
/* there must be at least one element before (p) and
one element after (y) the first list separator
*/
return E_PARSE;
}
dpd->qmax = 0;
dpd->yno = list[seppos + 1];
xpos = seppos + 2;
if (dpd->ci == DPANEL) {
/* the auxiliary 'ylags' list may contain specific y lags,
otherwise there should be just one field */
err = dpanel_make_laglist(dpd, list, seppos, ylags);
} else {
dpd->p = list[1];
if (seppos == 2) {
; /* arbond p ; y ... */
} else if (seppos == 3) {
/* arbond p qmax ; y ... */
dpd->qmax = list[2];
} else {
err = E_PARSE;
}
}
if (!err) {
if (dpd->p < 1) {
fprintf(stderr, "arbond lag order = %d < 1\n", dpd->p);
err = E_INVARG;
} else if (dpd->qmax != 0 && dpd->qmax < dpd->p + 1) {
fprintf(stderr, "arbond qmax = %d < dpd->p + 1 = %d\n",
dpd->qmax, dpd->p + 1);
err = E_INVARG;
}
}
if (!err && list[0] >= xpos) {
err = dpd_make_lists(dpd, list, xpos);
}
return err;
}
/* The following several book-keeping functions are specific
to the arbond command */
/* See if we have valid values for the dependent variable (in
differenced or deviations form) plus p lags of same, and all of
the independent variables, at the given observation, s.
TODO: either generalize this or add a parallel function
for handling equations in levels.
*/
static int obs_is_usable (ddset *dpd, const DATASET *dset, int s)
{
int imax = dpd->p + 1;
int i;
/* FIXME orthgonal deviations: we can compute such a
deviation for time-slot s if we have a value for
y at s-1 (given that we shift these forward) plus
one or more valid observations at s, s+1, ...
If I'm thinking correctly we don't necessarily
require a valid value at s, or in other words
the first block below is too conservative.
*/
for (i=0; i<=imax; i++) {
if (na(dset->Z[dpd->yno][s-i])) {
return 0;
}
}
if (dpd->xlist != NULL) {
/* check the independent vars */
for (i=1; i<=dpd->xlist[0]; i++) {
if (na(dset->Z[dpd->xlist[i]][s])) {
return 0;
}
}
}
return 1;
}
static int bz_columns (ddset *dpd, int i)
{
int j, k, nc = 0;
int t = i + dpd->p + 1; /* ?? */
for (j=0; j<dpd->nzb; j++) {
for (k=dpd->d[j].minlag; k<=dpd->d[j].maxlag; k++) {
if (t - k >= 0) {
nc++;
} else {
break;
}
}
}
return nc;
}
/* find the first y observation, all units */
static int arbond_find_t1min (ddset *dpd, const double *y)
{
int t1min = dpd->T - 1;
int i, s, t;
for (i=0; i<dpd->N; i++) {
/* loop across units */
if (t1min > 0) {
s = data_index(dpd, i);
for (t=0; t<dpd->T; t++) {
if (!na(y[s+t])) {
if (t < t1min) {
t1min = t;
}
break;
}
}
}
}
return t1min;
}
/* check the sample for unit/individual i */
static int
arbond_sample_check_unit (ddset *dpd, const DATASET *dset, int i,
int s, int *t1imin, int *t2max)
{
unit_info *unit = &dpd->ui[i];
int t1i = dpd->T - 1, t2i = 0;
int tmin = dpd->p + 1;
int t;
#if ADEBUG
fprintf(stderr, "Checking unit %d: s = %d\n", i, s);
#endif
unit->nobs = 0;
/* For the given unit: identify the observations at which we can
form the required delta y terms, have the requisite independent
variables, and can construct at least one orthogonality
condition using a lagged level of y.
*/
for (t=tmin; t<dpd->T; t++) {
if (obs_is_usable(dpd, dset, s + t)) {
unit->nobs += 1;
dpd->used[s+t] = 1;
if (t < t1i) t1i = t;
if (t > t2i) t2i = t;
}
}
if (unit->nobs == 0) {
dpd->effN -= 1;
unit->t1 = -1;
unit->t2 = -1;
#if ADEBUG
fprintf(stderr, "unit %d not usable\n", i);
#endif
return 0;
}
if (unit->nobs > dpd->maxTi) {
dpd->maxTi = unit->nobs;
}
dpd->nobs += unit->nobs;
if (t1i < *t1imin) {
*t1imin = t1i;
}
if (t2i > *t2max) {
*t2max = t2i;
}
unit->t1 = t1i;
unit->t2 = t2i;
#if ADEBUG
fprintf(stderr, "t1 = %d, t2 = %d, Ti = %d, usable obs = %d\n",
t1i, t2i, t2i - t1i + 1, unit->nobs);
#endif
return 0;
}
/* compute the column-structure of the matrix Zi */
static void arbond_compute_Z_cols (ddset *dpd, int t1min, int t2max)
{
int tau = t2max - t1min + 1;
int nblocks = tau - dpd->p - 1;
int i, bcols, cols = 0;
#if ADEBUG
fprintf(stderr, "\ntau = %d (dpd->p = %d)\n", tau, dpd->p);
#endif
for (i=0; i<nblocks; i++) {
/* block-diagonal lagged y values */
cols = (dpd->p + i > dpd->qmax - 1)? dpd->qmax - 1 : dpd->p + i;
#if ADEBUG
fprintf(stderr, "block %d: adding %d cols for y lags\n", i, cols);
#endif
dpd->nz += cols;
if (dpd->nzb > 0) {
/* other block-diagonal instruments */
bcols = bz_columns(dpd, i);
dpd->nz += bcols;
#if ADEBUG
fprintf(stderr, " plus %d cols for z lags\n", bcols);
#endif
}
}
#if ADEBUG
fprintf(stderr, "'basic' nz = %d\n", dpd->nz);
#endif
dpd->qmax = cols + 1;
/* xc0 records the column where the exogenous vars start */
dpd->xc0 = dpd->nz;
dpd->nz += dpd->nzr;
dpd->nz += dpd->ndum;
#if ADEBUG
fprintf(stderr, "total nz = %d (dummies = %d, exog = %d)\n",
dpd->nz, dpd->ndum, dpd->nzr);
#endif
}
static int arbond_sample_check (ddset *dpd, const DATASET *dset)
{
int t1min, t1imin = dpd->T - 1;
int s, t2max = 0;
int i, err = 0;
/* find "global" first y observation */
t1min = arbond_find_t1min(dpd, dset->Z[dpd->yno]);
#if ADEBUG
fprintf(stderr, "dpd_sample_check, initial scan: "
"dpd->T = %d, t1min = %d\n", dpd->T, t1min);
#endif
if (dpd->qmax == 0) {
dpd->qmax = dpd->T;
}
s = dset->t1;
for (i=0; i<dpd->N && !err; i++) {
err = arbond_sample_check_unit(dpd, dset, i, s, &t1imin, &t2max);
s += dpd->T;
}
if (err) {
return err;
}
/* record first usable obs, any unit */
dpd->t1min = t1imin;
/* figure number of time dummies, if wanted */
if (dpd->flags & DPD_TIMEDUM) {
dpd->ndum = t2max - t1imin;
dpd->k += dpd->ndum;
}
/* is t1min actually "reachable"? */
if (t1min < t1imin - dpd->qmax) {
t1min = t1imin - dpd->qmax;
}
#if ADEBUG
fprintf(stderr, "Number of units with usable observations = %d\n", dpd->effN);
fprintf(stderr, "Total usable observations = %d\n", dpd->nobs);
fprintf(stderr, "Max equations for any given unit = %d\n", dpd->maxTi);
fprintf(stderr, "Maximal relevant time-series span: %d to %d = %d\n",
t1min, t2max, t2max - t1min + 1);
#endif
if (dpd->effN == 0) {
err = E_MISSDATA;
} else {
/* compute the number of columns in Zi */
arbond_compute_Z_cols(dpd, t1min, t2max);
dpd->max_ni = dpd->maxTi;
dpd->totobs = dpd->nobs;
dpd->t2max = t2max;
}
return err;
}
/* end of arbond book-keeping block */
/* Figure the 0-based index of the position of the constant
in the coefficient vector (or return -1 if the constant is
not included).
*/
static int dpd_const_pos (ddset *dpd)
{
int cpos = -1;
if (dpd->xlist != NULL) {
int i;
for (i=1; i<=dpd->xlist[0]; i++) {
if (dpd->xlist[i] == 0) {
cpos = i - 1;
break;
}
}
if (cpos >= 0) {
/* we have the position in the array of coeffs on
exogenous vars, but these follow the lagged
values of the dependent variable.
*/
if (dpd->laglist != NULL) {
cpos += dpd->laglist[0];
} else {
cpos += dpd->p;
}
}
}
return cpos;
}
/* We do either one or two tests here: first we test for the joint
significance of all regular regressors (lags of y plus any
exogenous variables, except for the constant, if present).
Then, if time dummies are present, we test for their joint
significance.
*/
static int dpd_wald_test (ddset *dpd)
{
gretl_matrix *b, *V;
double x = 0.0;
int cpos = dpd_const_pos(dpd);
int k1, knd;
int i, j, ri, rj;
int err = 0;
/* total number of coefficients excluding any time dummies */
knd = dpd->k - dpd->ndum;
/* the number of coefficients to be tested at the first step
(we exclude the constant)
*/
k1 = (cpos > 0)? (knd - 1) : knd;
b = gretl_matrix_reuse(dpd->kmtmp, k1, 1);
V = gretl_matrix_reuse(dpd->kktmp, k1, k1);
ri = 0;
for (i=0; i<knd; i++) {
if (i != cpos) {
b->val[ri++] = dpd->beta->val[i];
}
}
ri = 0;
for (i=0; i<knd; i++) {
if (i != cpos) {
rj = 0;
for (j=0; j<knd; j++) {
if (j != cpos) {
x = gretl_matrix_get(dpd->vbeta, i, j);
gretl_matrix_set(V, ri, rj++, x);
}
}
ri++;
}
}
err = gretl_invert_symmetric_matrix(V);
if (!err) {
x = gretl_scalar_qform(b, V, &err);
}
if (!err) {
dpd->wald[0] = x;
dpd->wdf[0] = k1;
}
if (!err && dpd->ndum > 0) {
/* time dummies: these are always at the end of the
coeff vector
*/
b = gretl_matrix_reuse(dpd->kmtmp, dpd->ndum, 1);
V = gretl_matrix_reuse(dpd->kktmp, dpd->ndum, dpd->ndum);
gretl_matrix_extract_matrix(b, dpd->beta, knd, 0,
GRETL_MOD_NONE);
gretl_matrix_extract_matrix(V, dpd->vbeta, knd, knd,
GRETL_MOD_NONE);
err = gretl_invert_symmetric_matrix(V);
if (!err) {
x = gretl_scalar_qform(b, V, &err);
}
if (!err) {
dpd->wald[1] = x;
dpd->wdf[1] = dpd->ndum;
}
}
gretl_matrix_reuse(dpd->kmtmp, dpd->k, dpd->nz);
gretl_matrix_reuse(dpd->kktmp, dpd->k, dpd->k);
if (err) {
fprintf(stderr, "dpd_wald_test failed: %s\n",
errmsg_get_with_default(err));
}
return err;
}
static int dpd_sargan_test (ddset *dpd)
{
int save_rows, save_cols;
gretl_matrix *Zu;
int err = 0;
save_rows = gretl_matrix_rows(dpd->L1);
save_cols = gretl_matrix_cols(dpd->L1);
Zu = gretl_matrix_reuse(dpd->L1, dpd->nz, 1);
gretl_matrix_multiply(dpd->ZT, dpd->uhat, Zu);
gretl_matrix_divide_by_scalar(dpd->A, dpd->effN);
dpd->sargan = gretl_scalar_qform(Zu, dpd->A, &err);
if (!err && dpd->sargan < 0) {
dpd->sargan = NADBL;
err = E_NOTPD;
}
if (!err && dpd->step == 1) {
/* allow for scale factor in H matrix */
if (dpd->flags & DPD_ORTHDEV) {
dpd->sargan /= dpd->s2;
} else {
dpd->sargan *= 2.0 / dpd->s2;
}
}
#if ADEBUG
fprintf(stderr, "Sargan (or Hansen) test: Chi-square(%d-%d) = %g\n",
dpd->nz, dpd->k, dpd->sargan);
if (1) {
/* try to replicate the xtabond2 'Sargan test' */
double sg;
gretl_matrix_multiply_mod(dpd->ZT, GRETL_MOD_NONE,
dpd->ZT, GRETL_MOD_TRANSPOSE,
dpd->Acpy, GRETL_MOD_NONE);
gretl_matrix_multiply_by_scalar(dpd->Acpy, dpd->s2);
err = gretl_invert_symmetric_matrix(dpd->Acpy);
if (!err) {
sg = gretl_scalar_qform(Zu, dpd->Acpy, &err);
fprintf(stderr, "Sargan (xtabond2) test: Chi-square(%d-%d) = %g\n",
dpd->nz, dpd->k, sg);
}
}
#endif
gretl_matrix_reuse(dpd->L1, save_rows, save_cols);
if (err) {
fprintf(stderr, "dpd_sargan_test failed: %s\n",
errmsg_get_with_default(err));
}
return err;
}
/* \sigma^2 H_1, sliced and diced for unit i */
static void make_asy_Hi (ddset *dpd, int i, gretl_matrix *H,
char *mask)
{
int T = dpd->T;
int t, s = data_index(dpd, i);
for (t=0; t<T; t++) {
mask[t] = (dpd->used[t+s] == 1)? 0 : 1;
}
gretl_matrix_reuse(H, T, T);
gretl_matrix_zero(H);
gretl_matrix_set(H, 0, 0, 1);
for (t=1; t<T; t++) {
gretl_matrix_set(H, t, t, 1);
gretl_matrix_set(H, t-1, t, -0.5);
gretl_matrix_set(H, t, t-1, -0.5);
}
gretl_matrix_multiply_by_scalar(H, dpd->s2);
gretl_matrix_cut_rows_cols(H, mask);
}
/*
AR(1) and AR(2) tests for residuals, see
Doornik, Arellano and Bond, DPD manual (2006), pp. 28-29.
AR(m) = \frac{d_0}{\sqrt{d_1 + d_2 + d_3}}
d_0 = \sum_i w_i' u_i
d_1 = \sum_i w_i' H_i w_i
d_2 = -2(\sum_i w_i' X_i) M^{-1}
(\sum_i X_i' Z_i) A_N (\sum Z_i' H_i w_i)
d_3 = (\sum_i w_i' X_i) var(\hat{\beta}) (\sum_i X_i' w_i)
The matrices with subscript i in the above equations are all
in differences. In the "system" case the last term in d2 is
modified as
(\sum Z_i^f' u_i^f u_i' w_i)
where the f superscript indicates that all observations are
used, differences and levels.
one step: H_i = \sigma^2 H_{1,i}
one step, robust: H_i = H_{2,i}; M = M_1
two step: H_i = H_{2,i}; M = M_2
H_{2,i} = v^*_{1,i} v^*_{1,i}'
*/
static int dpd_ar_test (ddset *dpd)
{
double x, d0, d1, d2, d3;
gretl_matrix_block *B = NULL;
gretl_matrix *ui, *wi;
gretl_matrix *Xi, *Zi;
gretl_matrix *Hi, *ZU;
gretl_matrix *wX, *ZHw;
gretl_matrix *Tmp;
char *hmask = NULL;
int HT, T = dpd->maxTi;
int save_rows, save_cols;
int nz = dpd->nz;
int asy, ZU_cols;
int i, j, k, s, t;
int nlags, m = 1;
int err = 0;
save_rows = gretl_matrix_rows(dpd->Zi);
save_cols = gretl_matrix_cols(dpd->Zi);
/* if non-robust and on first step, Hi is computed differently */
if ((dpd->flags & DPD_TWOSTEP) || (dpd->flags & DPD_WINCORR)) {
asy = 0;
HT = T;
} else {
asy = 1;
HT = dpd->T;
hmask = malloc(HT);
if (hmask == NULL) {
err = E_ALLOC;
goto finish;
}
}
/* The required size of the workspace matrix ZU depends on
whether or not we're in the system case */
ZU_cols = (gmm_sys(dpd))? 1 : T;
B = gretl_matrix_block_new(&ui, T, 1,
&wi, T, 1,
&Xi, T, dpd->k,
&Hi, HT, HT,
&ZU, nz, ZU_cols,
&wX, 1, dpd->k,
&ZHw, nz, 1,
&Tmp, dpd->k, nz,
NULL);
if (B == NULL) {
err = E_ALLOC;
goto finish;
}
Zi = gretl_matrix_reuse(dpd->Zi, T, nz);
restart:
/* initialize cumulators */
d0 = d1 = 0.0;
gretl_matrix_zero(wX);
gretl_matrix_zero(ZHw);
nlags = 0;
s = 0;
for (i=0; i<dpd->N; i++) {
unit_info *unit = &dpd->ui[i];
int s_, t0, ni = unit->nobs;
int Ti = ni - unit->nlev;
int nlags_i = 0;
double uw;
if (Ti == 0) {
continue;
}
gretl_matrix_reuse(ui, Ti, -1);
gretl_matrix_reuse(wi, Ti, -1);
gretl_matrix_reuse(Xi, Ti, -1);
gretl_matrix_reuse(Zi, Ti, -1);
if (!asy) {
gretl_matrix_reuse(Hi, Ti, Ti);
}
if (!gmm_sys(dpd)) {
gretl_matrix_reuse(ZU, -1, Ti);
}
gretl_matrix_zero(wi);
t0 = data_index(dpd, i);
/* Construct the per-unit matrices ui, wi, Xi and Zi. We have
to be careful with the dpd->used values for the lags here:
1 indicates an observation in (both levels and) differences
while observations that are levels-only have a "used" value
of LEVEL_ONLY.
*/
k = 0;
for (t=unit->t1; t<=unit->t2; t++) {
if (dpd->used[t0+t]) {
ui->val[k] = dpd->uhat->val[s];
if (m == 1) {
if (dpd->used[t0+t-1] == 1) {
wi->val[k] = dpd->uhat->val[s-1];
nlags_i++;
}
} else if (dpd->used[t0+t-2] == 1) {
/* m = 2: due to missing values the lag-2
residual might be at slot s-1, not s-2
*/
s_ = (dpd->used[t0+t-1] == 1)? (s-2) : (s-1);
wi->val[k] = dpd->uhat->val[s_];
nlags_i++;
}
for (j=0; j<dpd->k; j++) {
x = gretl_matrix_get(dpd->X, s, j);
gretl_matrix_set(Xi, k, j, x);
}
for (j=0; j<nz; j++) {
x = gretl_matrix_get(dpd->ZT, j, s);
gretl_matrix_set(Zi, k, j, x);
}
k++;
s++;
}
}
if (nlags_i == 0) {
/* no lagged residuals available for this unit */
s += unit->nlev;
continue;
}
nlags += nlags_i;
/* d0 += w_i' * u_i */
uw = gretl_matrix_dot_product(wi, GRETL_MOD_TRANSPOSE,
ui, GRETL_MOD_NONE, &err);
d0 += uw;
if ((dpd->flags & DPD_TWOSTEP) || (dpd->flags & DPD_WINCORR)) {
/* form H_i (robust or step 2 variant) */
gretl_matrix_multiply_mod(ui, GRETL_MOD_NONE,
ui, GRETL_MOD_TRANSPOSE,
Hi, GRETL_MOD_NONE);
} else {
make_asy_Hi(dpd, i, Hi, hmask);
}
/* d1 += w_i' H_i w_i */
d1 += gretl_scalar_qform(wi, Hi, &err);
/* wX += w_i' X_i */
gretl_matrix_multiply_mod(wi, GRETL_MOD_TRANSPOSE,
Xi, GRETL_MOD_NONE,
wX, GRETL_MOD_CUMULATE);
if (gmm_sys(dpd)) {
/* Here we need (Z_i^f' u_i^f) * (u_i' w_i), which
mixes full series and differences.
*/
gretl_matrix_multiply_mod(Zi, GRETL_MOD_TRANSPOSE,
ui, GRETL_MOD_NONE,
ZU, GRETL_MOD_NONE);
for (t=0; t<unit->nlev; t++) {
/* catch the levels terms */
for (j=0; j<nz; j++) {
x = gretl_matrix_get(dpd->ZT, j, s);
ZU->val[j] += x * dpd->uhat->val[s];
}
s++;
}
gretl_matrix_multiply_by_scalar(ZU, uw);
gretl_matrix_add_to(ZHw, ZU);
} else {
/* differences only: ZHw += Z_i' H_i w_i */
gretl_matrix_multiply_mod(Zi, GRETL_MOD_TRANSPOSE,
Hi, GRETL_MOD_NONE,
ZU, GRETL_MOD_NONE);
gretl_matrix_multiply_mod(ZU, GRETL_MOD_NONE,
wi, GRETL_MOD_NONE,
ZHw, GRETL_MOD_CUMULATE);
}
}
if (m == 1) {
/* form M^{-1} * X'Z * A -- this doesn't have to
be repeated for m = 2 */
gretl_matrix_multiply(dpd->M, dpd->XZ, dpd->kmtmp);
gretl_matrix_multiply(dpd->kmtmp, dpd->A, Tmp);
}
if (nlags == 0) {
/* no data for AR(m) test at current m */
if (m == 1) {
m = 2;
goto restart;
} else {
goto finish;
}
}
/* d2 = -2 * w'X * (M^{-1} * XZ * A) * Z'Hw */
gretl_matrix_multiply(wX, Tmp, dpd->L1);
d2 = gretl_matrix_dot_product(dpd->L1, GRETL_MOD_NONE,
ZHw, GRETL_MOD_NONE, &err);
if (!err) {
d2 *= -2.0;
/* form w'X * var(\hat{\beta}) * X'w */
d3 = gretl_scalar_qform(wX, dpd->vbeta, &err);
}
if (!err) {
double z, den = d1 + d2 + d3;
#if ADEBUG
fprintf(stderr, "ar_test: m=%d, d0=%g, d1=%g, d2=%g, d3=%g, den=%g\n",
m, d0, d1, d2, d3, den);
#endif
if (den <= 0) {
err = E_NAN;
} else {
z = d0 / sqrt(den);
if (m == 1) {
dpd->AR1 = z;
} else {
dpd->AR2 = z;
}
}
}
if (!err && m == 1) {
m = 2;
goto restart;
}
finish:
gretl_matrix_block_destroy(B);
free(hmask);
/* restore original dimensions */
gretl_matrix_reuse(dpd->Zi, save_rows, save_cols);
if (err) {
fprintf(stderr, "dpd_ar_test failed: %s\n",
errmsg_get_with_default(err));
} else if (na(dpd->AR1) && na(dpd->AR1)) {
fprintf(stderr, "dpd_ar_test: no data\n");
}
return err;
}
/* Windmeijer, Journal of Econometrics, 126 (2005), page 33:
finite-sample correction for step-2 variance matrix. Note: from a
computational point of view this calculation is expressed more
clearly in the working paper by Bond and Windmeijer, "Finite Sample
Inference for GMM Estimators in Linear Panel Data Models" (2002),
in particular the fact that the matrix named "D" below must be
built column by column, taking the derivative of the "W" (or "A")
matrix with respect to the successive independent variables.
*/
static int windmeijer_correct (ddset *dpd, const gretl_matrix *uhat1,
const gretl_matrix *varb1)
{
gretl_matrix_block *B;
gretl_matrix *aV; /* standard asymptotic variance */
gretl_matrix *D; /* finite-sample factor */
gretl_matrix *dWj; /* one component of the above */
gretl_matrix *ui; /* per-unit residuals */
gretl_matrix *xij; /* per-unit X_j values */
gretl_matrix *mT; /* workspace follows */
gretl_matrix *km;
gretl_matrix *k1;
gretl_matrix *R1;
gretl_matrix *Zui;
gretl_matrix *Zxi;
int i, j, t;
int err = 0;
aV = gretl_matrix_copy(dpd->vbeta);
if (aV == NULL) {
return E_ALLOC;
}
B = gretl_matrix_block_new(&D, dpd->k, dpd->k,
&dWj, dpd->nz, dpd->nz,
&ui, dpd->max_ni, 1,
&xij, dpd->max_ni, 1,
&mT, dpd->nz, dpd->totobs,
&km, dpd->k, dpd->nz,
&k1, dpd->k, 1,
&Zui, dpd->nz, 1,
&Zxi, 1, dpd->nz,
NULL);
if (B == NULL) {
err = E_ALLOC;
goto bailout;
}
R1 = dpd->ZY; /* borrowed */
/* form -(1/N) * asyV * XZW^{-1} */
gretl_matrix_multiply(aV, dpd->XZA, dpd->kmtmp);
gretl_matrix_multiply_by_scalar(dpd->kmtmp, -1.0 / dpd->effN);
/* form W^{-1}Z'v_2 */
gretl_matrix_multiply(dpd->A, dpd->ZT, mT);
gretl_matrix_multiply(mT, dpd->uhat, R1);
for (j=0; j<dpd->k; j++) { /* loop across the X's */
int s = 0;
gretl_matrix_zero(dWj);
/* form dWj = -(1/N) \sum Z_i'(X_j u' + u X_j')Z_i */
for (i=0; i<dpd->N; i++) {
int ni = dpd->ui[i].nobs;
if (ni == 0) {
continue;
}
gretl_matrix_reuse(ui, ni, 1);
gretl_matrix_reuse(xij, ni, 1);
gretl_matrix_reuse(dpd->Zi, ni, dpd->nz);
/* extract ui (first-step residuals) */
for (t=0; t<ni; t++) {
ui->val[t] = uhat1->val[s++];
}
/* extract xij */
gretl_matrix_extract_matrix(xij, dpd->X, s - ni, j,
GRETL_MOD_NONE);
/* extract Zi */
gretl_matrix_extract_matrix(dpd->Zi, dpd->ZT, 0, s - ni,
GRETL_MOD_TRANSPOSE);
gretl_matrix_multiply_mod(dpd->Zi, GRETL_MOD_TRANSPOSE,
ui, GRETL_MOD_NONE,
Zui, GRETL_MOD_NONE);
gretl_matrix_multiply_mod(xij, GRETL_MOD_TRANSPOSE,
dpd->Zi, GRETL_MOD_NONE,
Zxi, GRETL_MOD_NONE);
gretl_matrix_multiply_mod(Zui, GRETL_MOD_NONE,
Zxi, GRETL_MOD_NONE,
dWj, GRETL_MOD_CUMULATE);
}
gretl_matrix_add_self_transpose(dWj);
gretl_matrix_multiply_by_scalar(dWj, -1.0 / dpd->effN);
/* D[.,j] = -aV * XZW^{-1} * dWj * W^{-1}Z'v_2 */
gretl_matrix_multiply(dpd->kmtmp, dWj, km);
gretl_matrix_multiply(km, R1, k1);
/* write into appropriate column of D (k x k) */
for (i=0; i<dpd->k; i++) {
gretl_matrix_set(D, i, j, k1->val[i]);
}
}
/* add to AsyV: D * AsyV */
gretl_matrix_multiply_mod(D, GRETL_MOD_NONE,
aV, GRETL_MOD_NONE,
dpd->vbeta, GRETL_MOD_CUMULATE);
/* add to AsyV: AsyV * D' */
gretl_matrix_multiply_mod(aV, GRETL_MOD_NONE,
D, GRETL_MOD_TRANSPOSE,
dpd->vbeta, GRETL_MOD_CUMULATE);
/* add to AsyV: D * var(\hat{\beta}_1) * D' */
gretl_matrix_qform(D, GRETL_MOD_NONE, varb1,
dpd->vbeta, GRETL_MOD_CUMULATE);
bailout:
gretl_matrix_block_destroy(B);
gretl_matrix_free(aV);
return err;
}
/* second-step (asymptotic) variance */
static int dpd_variance_2 (ddset *dpd,
gretl_matrix *u1,
gretl_matrix *V1)
{
int err;
err = gretl_matrix_qform(dpd->XZ, GRETL_MOD_NONE, dpd->V,
dpd->vbeta, GRETL_MOD_NONE);
if (!err) {
err = gretl_invert_symmetric_matrix(dpd->vbeta);
}
if (!err) {
gretl_matrix_multiply_by_scalar(dpd->vbeta, dpd->effN);
}
if (!err && u1 != NULL && V1 != NULL) {
err = windmeijer_correct(dpd, u1, V1);
}
return err;
}
/*
Compute the robust step-1 robust variance matrix:
N * M^{-1} * (X'*Z*A_N*\hat{V}_N*A_N*Z'*X) * M^{-1}
where
M = X'*Z*A_N*Z'*X
and
\hat{V}_N = N^{-1} \sum Z_i'*v_i*v_i'*Z_i,
(v_i being the step-1 residuals).
(But if we're doing 1-step and get the asymptotic
flag, just do the simple thing, \sigma^2 M^{-1})
*/
static int dpd_variance_1 (ddset *dpd)
{
gretl_matrix *V, *ui;
int i, t, k, c;
int err = 0;
if (!(dpd->flags & DPD_TWOSTEP) && !(dpd->flags & DPD_WINCORR)) {
/* one step asymptotic variance */
err = gretl_matrix_copy_values(dpd->vbeta, dpd->M);
gretl_matrix_multiply_by_scalar(dpd->vbeta, dpd->s2 * dpd->effN/2.0);
return 0;
}
V = gretl_zero_matrix_new(dpd->nz, dpd->nz);
ui = gretl_column_vector_alloc(dpd->max_ni);
if (V == NULL || ui == NULL) {
gretl_matrix_free(V);
gretl_matrix_free(ui);
return E_ALLOC;
}
c = k = 0;
for (i=0; i<dpd->N; i++) {
int ni = dpd->ui[i].nobs;
if (ni == 0) {
continue;
}
/* get per-unit instruments matrix, Zi */
gretl_matrix_reuse(dpd->Zi, ni, dpd->nz);
gretl_matrix_reuse(ui, ni, 1);
gretl_matrix_extract_matrix(dpd->Zi, dpd->ZT, 0, c,
GRETL_MOD_TRANSPOSE);
c += ni;
/* load residuals into the ui vector */
for (t=0; t<ni; t++) {
ui->val[t] = dpd->uhat->val[k++];
}
gretl_matrix_multiply_mod(ui, GRETL_MOD_TRANSPOSE,
dpd->Zi, GRETL_MOD_NONE,
dpd->L1, GRETL_MOD_NONE);
gretl_matrix_multiply_mod(dpd->L1, GRETL_MOD_TRANSPOSE,
dpd->L1, GRETL_MOD_NONE,
V, GRETL_MOD_CUMULATE);
}
gretl_matrix_divide_by_scalar(V, dpd->effN);
/* form X'Z A_N Z'X */
gretl_matrix_multiply(dpd->XZ, dpd->A, dpd->kmtmp);
gretl_matrix_qform(dpd->kmtmp, GRETL_MOD_NONE, V,
dpd->kktmp, GRETL_MOD_NONE);
if (!err) {
/* pre- and post-multiply by M^{-1} */
gretl_matrix_qform(dpd->M, GRETL_MOD_NONE, dpd->kktmp,
dpd->vbeta, GRETL_MOD_NONE);
gretl_matrix_multiply_by_scalar(dpd->vbeta, dpd->effN);
}
if (!err && (dpd->flags & DPD_TWOSTEP)) {
/* preserve V for the second stage */
dpd->V = V;
} else {
gretl_matrix_free(V);
}
gretl_matrix_free(ui);
return err;
}
/* no "system" case here: all residuals are in differences */
static void arbond_residuals (ddset *dpd)
{
const double *b = dpd->beta->val;
double x, ut;
int i, j, t, k = 0;
dpd->SSR = 0.0;
for (i=0; i<dpd->N; i++) {
int Ti = dpd->ui[i].nobs;
for (t=0; t<Ti; t++) {
ut = dpd->Y->val[k];
for (j=0; j<dpd->k; j++) {
x = gretl_matrix_get(dpd->X, k, j);
ut -= b[j] * x;
}
dpd->SSR += ut * ut;
dpd->uhat->val[k++] = ut;
}
}
dpd->s2 = dpd->SSR / (dpd->nobs - dpd->k);
}
/* In the "system" case dpd->uhat contains both differenced
residuals and levels residuals (stacked per unit). In that case
trim the vector down so that it contains only one set of
residuals prior to transcribing in series form. Which set
we preserve is governed by @save_levels.
*/
static int dpanel_adjust_uhat (ddset *dpd,
const DATASET *dset,
int save_levels)
{
double *tmp;
int ntmp, nd;
int i, k, s, t;
ntmp = (save_levels)? dpd->nlev : dpd->ndiff;
tmp = malloc(ntmp * sizeof *tmp);
if (tmp == NULL) {
return E_ALLOC;
}
k = s = 0;
for (i=0; i<dpd->N; i++) {
nd = dpd->ui[i].nobs - dpd->ui[i].nlev;
if (save_levels) {
/* skip residuals in differences */
k += nd;
for (t=0; t<dpd->ui[i].nlev; t++) {
tmp[s++] = dpd->uhat->val[k++];
}
} else {
/* use only residuals in differences */
for (t=0; t<nd; t++) {
tmp[s++] = dpd->uhat->val[k++];
}
k += dpd->ui[i].nlev;
}
}
for (t=0; t<ntmp; t++) {
dpd->uhat->val[t] = tmp[t];
}
gretl_matrix_reuse(dpd->uhat, ntmp, 1);
free(tmp);
if (!save_levels) {
for (t=0; t<dset->n; t++) {
if (dpd->used[t] == LEVEL_ONLY) {
dpd->used[t] = 0;
}
}
}
return 0;
}
static int dpd_finalize_model (MODEL *pmod, ddset *dpd,
const int *list, const int *ylags,
const char *istr,
const DATASET *dset,
gretlopt opt)
{
const double *y = dset->Z[dpd->yno];
int keep_extra = opt & OPT_K;
char tmp[32];
int i, j;
int err = 0;
if (dpd->flags & DPD_REDO) {
goto restart;
}
pmod->ci = dpd->ci;
pmod->t1 = dset->t1;
pmod->t2 = dset->t2;
pmod->dfn = dpd->k;
pmod->dfd = dpd->nobs - dpd->k;
pmod->list = gretl_list_copy(list);
if (pmod->list == NULL) {
pmod->errcode = E_ALLOC;
return pmod->errcode;
}
gretl_model_set_int(pmod, "yno", dpd->yno);
gretl_model_set_int(pmod, "n_included_units", dpd->effN);
pmod->ncoeff = dpd->k;
pmod->nobs = dpd->nobs;
pmod->full_n = dset->n;
pmod->rsq = pmod->adjrsq = NADBL;
pmod->fstt = pmod->chisq = NADBL;
pmod->lnL = NADBL;
gretl_model_allocate_param_names(pmod, dpd->k);
if (pmod->errcode) {
return pmod->errcode;
}
if (pmod->ci == DPANEL) {
j = 0;
if (dpd->laglist != NULL) {
for (i=1; i<=dpd->laglist[0]; i++) {
sprintf(tmp, "%.10s(-%d)", dset->varname[dpd->yno],
dpd->laglist[i]);
gretl_model_set_param_name(pmod, j++, tmp);
}
} else {
for (i=0; i<dpd->p; i++) {
sprintf(tmp, "%.10s(-%d)", dset->varname[dpd->yno], i+1);
gretl_model_set_param_name(pmod, j++, tmp);
}
}
} else {
char prefix = (dpd->flags & DPD_ORTHDEV)? 'O' : 'D';
j = 0;
if (dpd->laglist != NULL) {
for (i=1; i<=dpd->laglist[0]; i++) {
sprintf(tmp, "%c%.10s(-%d)", prefix, dset->varname[dpd->yno],
dpd->laglist[i]);
gretl_model_set_param_name(pmod, j++, tmp);
}
} else {
for (i=0; i<dpd->p; i++) {
sprintf(tmp, "%c%.10s(-%d)", prefix, dset->varname[dpd->yno], i+1);
gretl_model_set_param_name(pmod, j++, tmp);
}
}
}
for (i=0; i<dpd->nx; i++) {
gretl_model_set_param_name(pmod, j++, dset->varname[dpd->xlist[i+1]]);
}
for (i=0; i<dpd->ndum; i++) {
if (dpd->ifc) {
sprintf(tmp, "T%d", dpd->t1min + i + 2);
gretl_model_set_param_name(pmod, j++, tmp);
} else {
sprintf(tmp, "T%d", dpd->t1min + i + 1);
gretl_model_set_param_name(pmod, j++, tmp);
}
}
err = gretl_model_allocate_storage(pmod);
restart:
if (!err) {
pmod->ess = dpd->SSR;
if (dpd->s2 >= 0) {
pmod->sigma = sqrt(dpd->s2);
}
for (i=0; i<dpd->k; i++) {
pmod->coeff[i] = dpd->beta->val[i];
}
err = gretl_model_write_vcv(pmod, dpd->vbeta);
}
/* add uhat, yhat */
if (!err && dpd->nlev > 0) {
err = dpanel_adjust_uhat(dpd, dset, 1); /* or 0 */
}
if (!err) {
int t, s = 0;
for (t=0; t<dset->n; t++) {
if (dpd->used[t]) {
pmod->uhat[t] = dpd->uhat->val[s++];
pmod->yhat[t] = y[t] - pmod->uhat[t];
}
}
}
/* additional dpd-specific data */
if (!err) {
gretl_model_set_int(pmod, "step", dpd->step);
if (dpd->step == 2) {
pmod->opt |= OPT_T;
}
if (!na(dpd->AR1)) {
gretl_model_set_double(pmod, "AR1", dpd->AR1);
}
if (!na(dpd->AR2)) {
gretl_model_set_double(pmod, "AR2", dpd->AR2);
}
gretl_model_set_int(pmod, "sargan_df", dpd->nz - dpd->k);
if (!na(dpd->sargan)) {
gretl_model_set_double(pmod, "sargan", dpd->sargan);
}
gretl_model_set_int(pmod, "wald_df", dpd->wdf[0]);
if (!na(dpd->wald[0])) {
gretl_model_set_double(pmod, "wald", dpd->wald[0]);
}
if (!na(dpd->wald[1])) {
gretl_model_set_int(pmod, "wald_time_df", dpd->wdf[1]);
gretl_model_set_double(pmod, "wald_time", dpd->wald[1]);
}
if (!(dpd->flags & DPD_WINCORR)) {
gretl_model_set_int(pmod, "asy", 1);
pmod->opt |= OPT_A;
}
if (dpd->A != NULL) {
gretl_model_set_int(pmod, "ninst", dpd->A->rows);
if (keep_extra) {
gretl_matrix *A = gretl_matrix_copy(dpd->A);
gretl_model_set_matrix_as_data(pmod, "wgtmat", A);
}
}
if (keep_extra && dpd->ZT != NULL) {
gretl_matrix *Z = gretl_matrix_copy(dpd->ZT);
gretl_model_set_matrix_as_data(pmod, "GMMinst", Z);
}
if (opt & OPT_D) {
maybe_suppress_time_dummies(pmod, dpd->ndum);
}
}
if (!err && !(dpd->flags & DPD_REDO)) {
/* things that only need to be done once */
if (istr != NULL && *istr != '\0') {
gretl_model_set_string_as_data(pmod, "istr", gretl_strdup(istr));
}
if (ylags != NULL) {
gretl_model_set_list_as_data(pmod, "ylags", gretl_list_copy(ylags));
}
if (dpd->flags & DPD_TIMEDUM) {
pmod->opt |= OPT_D;
}
if (pmod->ci == DPANEL) {
if (dpd->flags & DPD_SYSTEM) {
pmod->opt |= OPT_L;
}
if (dpd->flags & DPD_DPDSTYLE) {
pmod->opt |= OPT_X;
}
if (dpd->maxTi > 0 && dpd->minTi > 0 && dpd->maxTi > dpd->minTi) {
gretl_model_set_int(pmod, "Tmin", dpd->minTi);
gretl_model_set_int(pmod, "Tmax", dpd->maxTi);
}
}
}
return err;
}
/* exclude any independent variables that are zero at all
relevant observations */
static int dpd_zero_check (ddset *dpd, const DATASET *dset)
{
unit_info *ui;
const double *x;
int drop = 0;
int i, j, k, t;
for (j=0; j<dpd->nx; j++) {
int all0 = 1;
x = dset->Z[dpd->xlist[j+1]];
for (i=0; i<dpd->N && all0; i++) {
ui = &dpd->ui[i];
if (ui->nobs == 0) {
continue;
}
for (t=ui->t1; t<=ui->t2 && all0; t++) {
k = data_index(dpd, i) + t;
if (x[k] != 0.0 && !na(x[k])) {
all0 = 0;
}
}
}
if (all0) {
dpd->xlist[j+1] = -1;
drop++;
}
}
if (drop > 0) {
for (i=1; i<=dpd->xlist[0]; i++) {
if (dpd->xlist[i] < 0) {
gretl_list_delete_at_pos(dpd->xlist, i);
i--;
}
}
dpd->nx = dpd->xlist[0];
dpd->k -= drop;
}
return 0;
}
/* Based on reduction of the A matrix, trim ZT to match and
adjust the sizes of all workspace matrices that have a
dimension involving dpd->nz. Note that this is called
on the first step only, and it is called before computing
XZ' and Z'Y. The only matrices we need to actually "cut"
are A (already done when we get here) and ZT; XZ' and Z'Y
should just have their nz dimension changed.
*/
static void dpd_shrink_matrices (ddset *dpd, const char *mask)
{
fprintf(stderr, "%s: dpd_shrink_matrices: cut nz from %d to %d\n",
(dpd->ci == DPANEL)? "dpanel" : "arbond",
dpd->nz, dpd->A->rows);
gretl_matrix_cut_rows(dpd->ZT, mask);
dpd->nz = dpd->A->rows;
gretl_matrix_reuse(dpd->Acpy, dpd->nz, dpd->nz);
gretl_matrix_reuse(dpd->kmtmp, -1, dpd->nz);
gretl_matrix_reuse(dpd->L1, -1, dpd->nz);
gretl_matrix_reuse(dpd->XZA, -1, dpd->nz);
gretl_matrix_reuse(dpd->XZ, -1, dpd->nz);
gretl_matrix_reuse(dpd->ZY, dpd->nz, -1);
}
static int dpd_step_2_A (ddset *dpd)
{
int err = 0;
#if ADEBUG
gretl_matrix_print(dpd->V, "V, in dpd_step_2_A");
#endif
if (gretl_matrix_rows(dpd->V) > dpd->effN) {
/* we know this case requires special treatment */
err = gretl_SVD_invert_matrix(dpd->V);
if (!err) {
gretl_matrix_xtr_symmetric(dpd->V);
}
} else {
gretl_matrix_copy_values(dpd->Acpy, dpd->V);
err = gretl_invert_symmetric_matrix(dpd->V);
if (err) {
/* revert the data in dpd->V and try again */
gretl_matrix_copy_values(dpd->V, dpd->Acpy);
err = gretl_SVD_invert_matrix(dpd->V);
if (!err) {
gretl_matrix_xtr_symmetric(dpd->V);
}
}
}
if (!err) {
/* A <- V^{-1} */
gretl_matrix_copy_values(dpd->A, dpd->V);
dpd->step = 2;
}
return err;
}
/* compute \hat{\beta} from the moment matrices */
static int dpd_beta_hat (ddset *dpd)
{
int err = 0;
if (dpd->step == 2) {
/* form the new A matrix */
err = dpd_step_2_A(dpd);
}
if (!err) {
/* M <- XZ * A * XZ' */
err = gretl_matrix_qform(dpd->XZ, GRETL_MOD_NONE,
dpd->A, dpd->M, GRETL_MOD_NONE);
}
if (!err) {
/* dpd->XZA <- XZ * A */
gretl_matrix_multiply(dpd->XZ, dpd->A, dpd->XZA);
/* using beta as workspace */
gretl_matrix_multiply(dpd->XZA, dpd->ZY, dpd->beta);
gretl_matrix_copy_values(dpd->kktmp, dpd->M);
err = gretl_cholesky_decomp_solve(dpd->kktmp, dpd->beta);
}
if (!err) {
/* prepare M^{-1} for variance calculation */
err = gretl_inverse_from_cholesky_decomp(dpd->M, dpd->kktmp);
}
#if ADEBUG > 1
gretl_matrix_print(dpd->M, "M");
#endif
return err;
}
/* recompute \hat{\beta} and its variance matrix */
static int dpd_step_2 (ddset *dpd)
{
gretl_matrix *u1 = NULL;
gretl_matrix *V1 = NULL;
int err;
dpd->step = 2;
err = dpd_beta_hat(dpd);
if (!err && (dpd->flags & DPD_WINCORR)) {
/* we'll need a copy of the step-1 residuals, and also
the step-1 var(\hat{\beta})
*/
u1 = gretl_matrix_copy(dpd->uhat);
V1 = gretl_matrix_copy(dpd->vbeta);
if (u1 == NULL || V1 == NULL) {
err = E_ALLOC;
}
}
if (!err) {
if (dpd->ci == DPANEL) {
dpanel_residuals(dpd);
} else {
arbond_residuals(dpd);
}
err = dpd_variance_2(dpd, u1, V1);
}
gretl_matrix_free(u1);
gretl_matrix_free(V1);
if (!err) {
dpd_ar_test(dpd);
dpd_sargan_test(dpd);
dpd_wald_test(dpd);
}
return err;
}
/* arbond-specific functions follow */
static double odev_at_lag (const double *x, int t, int lag, int pd)
{
double ret, xbar = 0.0;
int s, Tt, n = 0;
t -= lag + 1;
if (t < 0 || na(x[t])) {
return NADBL;
}
Tt = pd - (t % pd) - (lag + 1);
for (s=1; s<=Tt; s++) {
if (!na(x[t+s]) && !na(x[t+s+lag])) {
xbar += x[t+s];
n++;
}
}
if (n > 0) {
xbar /= n;
ret = sqrt(n / (n + 1.0)) * (x[t] - xbar);
} else {
ret = NADBL;
}
return ret;
}
static int arbond_make_y_X (ddset *dpd, const DATASET *dset)
{
const double *y = dset->Z[dpd->yno];
int odev = (dpd->flags & DPD_ORTHDEV);
int i, j, k = 0;
int s, t;
double x;
for (i=0; i<dpd->N; i++) {
unit_info *unit = &dpd->ui[i];
int t0 = data_index(dpd, i);
if (unit->nobs == 0) {
continue;
}
for (t=unit->t1; t<=unit->t2; t++) {
s = t0 + t;
if (!dpd->used[s]) {
continue;
}
/* current difference (or deviation) of dependent var */
if (odev) {
x = odev_at_lag(y, s, 0, dpd->T);
gretl_vector_set(dpd->Y, k, x);
} else {
gretl_vector_set(dpd->Y, k, y[s] - y[s-1]);
}
for (j=0; j<dpd->p; j++) {
/* lagged difference of dependent var */
if (odev) {
x = odev_at_lag(y, s, j + 1, dpd->T);
gretl_matrix_set(dpd->X, k, j, x);
} else {
gretl_matrix_set(dpd->X, k, j, y[s-j-1] - y[s-j-2]);
}
}
for (j=0; j<dpd->nx; j++) {
/* independent vars */
x = dset->Z[dpd->xlist[j+1]][s];
gretl_matrix_set(dpd->X, k, j + dpd->p, x);
}
for (j=0; j<dpd->ndum; j++) {
/* time dummies */
x = (t - dpd->t1min - 1 == j)? 1 : 0;
gretl_matrix_set(dpd->X, k, j + dpd->p + dpd->nx, x);
}
k++;
}
}
#if ADEBUG
gretl_matrix_print(dpd->Y, "Y (arbond)");
gretl_matrix_print(dpd->X, "X (arbond)");
#endif
#if WRITE_MATRICES
gretl_matrix_write_as_text(dpd->Y, "arbondY.mat", 0);
gretl_matrix_write_as_text(dpd->X, "arbondX.mat", 0);
#endif
return 0;
}
static int next_obs (ddset *dpd, int t0, int j0, int n)
{
int j;
for (j=j0; j<n; j++) {
if (dpd->used[j + t0]) {
return j;
}
}
return 0;
}
/* construct the H matrix for first-differencing
as applied to unit i */
static void arbond_H_matrix (ddset *dpd, int *rc,
int i, int t0)
{
unit_info *unit = &dpd->ui[i];
int n = unit->t2 - unit->t1 + 1;
int m = unit->nobs;
double x;
int k, j, adjacent, skip = 0;
t0 += unit->t1;
if (m < n) {
skip = 1;
j = next_obs(dpd, t0, 0, n);
for (k=0; k<m; k++) {
rc[k] = j;
j = next_obs(dpd, t0, j+1, n);
}
}
gretl_matrix_reuse(dpd->H, m, m);
for (j=0; j<m; j++) {
for (k=j; k<m; k++) {
if (skip) {
adjacent = (abs(rc[k] - rc[j]) == 1);
} else {
adjacent = (abs(k-j) == 1);
}
x = (k == j)? 2 : (adjacent)? -1 : 0;
gretl_matrix_set(dpd->H, j, k, x);
gretl_matrix_set(dpd->H, k, j, x);
}
}
}
static int arbond_make_Z_and_A (ddset *dpd, const DATASET *dset)
{
const double *y = dset->Z[dpd->yno];
int i, j, k, c = 0;
int s, t, t0;
int zi, zj, zk;
double x;
int *rc = NULL;
#if ADEBUG
char zstr[8];
#endif
int err = 0;
if (!(dpd->flags & DPD_ORTHDEV)) {
rc = malloc(dpd->T * sizeof *rc);
if (rc == NULL) {
return E_ALLOC;
}
}
gretl_matrix_zero(dpd->A);
gretl_matrix_zero(dpd->XZ);
gretl_matrix_zero(dpd->ZY);
/* t0 holds the obs index in the full dataset at the start
of the data for unit i */
for (i=0; i<dpd->N; i++) {
unit_info *unit = &dpd->ui[i];
int ycols = dpd->p; /* intial y block width */
int offj = 0; /* initialize column offset */
int Ti = unit->nobs;
if (Ti == 0) {
continue;
}
t0 = data_index(dpd, i);
gretl_matrix_reuse(dpd->Zi, Ti, dpd->nz);
gretl_matrix_zero(dpd->Zi);
for (t=dpd->p+1; t<unit->t1; t++) {
/* unbalanced case: compute initial column offset */
offj += ycols;
if (ycols < dpd->qmax - 1) {
ycols++;
}
zj = 0;
for (zi=0; zi<dpd->nzb; zi++) {
for (zk=dpd->d[zi].minlag; zk<=dpd->d[zi].maxlag; zk++) {
if (t - zk >= 0) {
zj++;
}
}
}
offj += zj;
}
k = 0;
for (t=unit->t1; t<=unit->t2; t++) {
int used = dpd->used[t0+t];
int offincr = ycols;
if (used) {
/* lagged y (GMM instr) columns */
for (j=0; j<ycols; j++) {
s = t0 + t - (ycols + 1) + j;
if (!na(y[s])) {
gretl_matrix_set(dpd->Zi, k, j + offj, y[s]);
}
}
}
/* additional block-diagonal columns, if required --
needs checking for the unbalanced case
*/
zj = 0;
for (zi=0; zi<dpd->nzb; zi++) {
for (zk=dpd->d[zi].minlag; zk<=dpd->d[zi].maxlag; zk++) {
if (t - zk >= 0) { /* ?? */
if (used) {
s = t0 + t - zk;
x = dset->Z[dpd->d[zi].v][s];
if (!na(x)) {
gretl_matrix_set(dpd->Zi, k, zj + offj + ycols, x);
}
}
zj++;
}
}
}
offincr += zj;
if (used) {
/* additional full-length instrument columns */
s = t0 + t;
for (j=0; j<dpd->nzr; j++) {
x = dset->Z[dpd->ilist[j+1]][s];
gretl_matrix_set(dpd->Zi, k, dpd->xc0 + j, x);
}
/* plus time dummies, if wanted */
for (j=0; j<dpd->ndum; j++) {
x = (t - dpd->t1min - 1 == j)? 1 : 0;
gretl_matrix_set(dpd->Zi, k, dpd->xc0 + dpd->nzr + j, x);
}
k++; /* increment target row */
}
offj += offincr; /* starting column for next block */
if (ycols < dpd->qmax - 1) {
/* increment y block width, if we're not already at max */
ycols++;
}
}
#if ADEBUG
sprintf(zstr, "Z_%d", i + 1);
gretl_matrix_print(dpd->Zi, zstr);
#endif
#if 0
gretl_matrix_print(dpd->Zi, "Zi, arbond");
#endif
/* Cumulate Z_i' H Z_i into A_N */
if (dpd->flags & DPD_ORTHDEV) {
/* orthogonal deviations: "H" is identity matrix */
gretl_matrix_multiply_mod(dpd->Zi, GRETL_MOD_TRANSPOSE,
dpd->Zi, GRETL_MOD_NONE,
dpd->A, GRETL_MOD_CUMULATE);
} else {
arbond_H_matrix(dpd, rc, i, t0);
gretl_matrix_qform(dpd->Zi, GRETL_MOD_TRANSPOSE,
dpd->H, dpd->A, GRETL_MOD_CUMULATE);
}
/* Write Zi into ZT at offset 0, c */
gretl_matrix_inscribe_matrix(dpd->ZT, dpd->Zi, 0, c,
GRETL_MOD_TRANSPOSE);
c += Ti;
}
free(rc);
#if WRITE_MATRICES
gretl_matrix_write_as_text(dpd->A, "arbond-bigA.mat", 0);
#endif
if (!err) {
/* mask zero rows of ZT, if required */
char *mask = gretl_matrix_zero_row_mask(dpd->ZT, &err);
if (mask != NULL) {
err = gretl_matrix_cut_rows_cols(dpd->A, mask);
if (!err) {
dpd_shrink_matrices(dpd, mask);
}
free(mask);
}
}
#if ADEBUG
gretl_matrix_print(dpd->A, "\\sum Z_i' H Z_i");
#endif
if (!err) {
gretl_matrix_divide_by_scalar(dpd->A, dpd->effN);
}
#if ADEBUG
gretl_matrix_print(dpd->ZT, "ZT");
gretl_matrix_print(dpd->A, "N^{-1} * \\sum Z_i' H Z_i");
#endif
#if WRITE_MATRICES
gretl_matrix_write_as_text(dpd->ZT, "arbondZT.mat", 0);
#endif
return err;
}
/* This function is used on the first step (only), by both
arbond and dpanel. */
static int dpd_invert_A_N (ddset *dpd)
{
int err = 0;
/* just in case */
gretl_matrix_xtr_symmetric(dpd->A);
/* make a backup in case the first attempt fails */
gretl_matrix_copy_values(dpd->Acpy, dpd->A);
/* first try straight inversion */
err = gretl_invert_symmetric_matrix(dpd->A);
if (err) {
/* try again, first reducing A based on its rank */
char *mask = NULL;
fprintf(stderr, "inverting dpd->A failed on first pass\n");
gretl_matrix_copy_values(dpd->A, dpd->Acpy);
mask = gretl_matrix_rank_mask(dpd->A, &err);
if (!err) {
err = gretl_matrix_cut_rows_cols(dpd->A, mask);
}
if (!err) {
err = gretl_invert_symmetric_matrix(dpd->A);
if (!err) {
/* OK, now register effects of reducing nz */
dpd_shrink_matrices(dpd, mask);
} else {
fprintf(stderr, "inverting dpd->A failed on second pass\n");
}
}
free(mask);
}
#if ADEBUG
gretl_matrix_print(dpd->A, "A_N");
#endif
return err;
}
static int dpd_step_1 (ddset *dpd)
{
int err;
/* invert A_N: we allow two attempts */
err = dpd_invert_A_N(dpd);
if (!err) {
/* construct additional moment matrices: we waited
until we knew what size these should really be
*/
gretl_matrix_multiply(dpd->ZT, dpd->Y, dpd->ZY);
gretl_matrix_multiply_mod(dpd->X, GRETL_MOD_TRANSPOSE,
dpd->ZT, GRETL_MOD_TRANSPOSE,
dpd->XZ, GRETL_MOD_NONE);
}
#if ADEBUG > 1
gretl_matrix_print(dpd->XZ, "XZ'");
gretl_matrix_print(dpd->ZY, "Z'Y");
#endif
if (!err) {
err = dpd_beta_hat(dpd);
}
if (!err) {
if (dpd->ci == DPANEL) {
dpanel_residuals(dpd);
} else {
arbond_residuals(dpd);
}
err = dpd_variance_1(dpd);
}
if (!err && !(dpd->flags & DPD_TWOSTEP)) {
/* do the tests if we're not continuing */
dpd_ar_test(dpd);
dpd_sargan_test(dpd);
dpd_wald_test(dpd);
}
return err;
}
static int diag_try_list (const char *vname, int *vp, int *nd,
diag_info **dp, int **listp)
{
int *list = get_list_by_name(vname);
if (list == NULL) {
return E_UNKVAR;
} else {
int nv = list[0];
if (nv == 1) {
*vp = list[1];
} else if (nv < 1) {
return E_DATA;
} else {
/* nv > 1 */
int newlen = *nd + nv - 1;
diag_info *dnew;
dnew = realloc(*dp, newlen * sizeof *dnew);
if (dnew == NULL) {
return E_ALLOC;
} else {
*listp = list;
*dp = dnew;
*nd = newlen;
}
}
}
return 0;
}
/* Parse a particular entry in the (optional) incoming array
of "GMM(foo,m1,m2)" specifications. We check that foo
exists and that m1 and m2 have sane values (allowing that,
for arbond, an m2 value of 0 means use all available lags).
*/
static int parse_diag_info (int ci, const char *s, diag_info **dp,
int *ip, int *nd, const DATASET *dset)
{
char vname[VNAMELEN];
char fmt[24];
int level = 0;
int m1, m2;
int err = 0;
if (!strncmp(s, "GMM(", 4)) {
s += 4;
} else if (!strncmp(s, "GMMlevel(", 9)) {
if (ci == ARBOND) {
/* only dpanel supports "GMMlevel" */
return E_PARSE;
}
level = 1;
s += 9;
}
sprintf(fmt, "%%%d[^, ] , %%d , %%d)", VNAMELEN-1);
if (sscanf(s, fmt, vname, &m1, &m2) != 3) {
err = E_PARSE;
} else {
int v = current_series_index(dset, vname);
int *vlist = NULL;
if (ci == ARBOND && m2 == 0) {
/* signal for unlimited lags */
m2 = 99;
}
if (m1 < 0 || m2 < m1) {
err = E_DATA;
} else if (v < 0) {
err = diag_try_list(vname, &v, nd, dp, &vlist);
}
if (!err) {
diag_info *d, *darray = *dp;
int nv = (vlist == NULL)? 1 : vlist[0];
int j, i = *ip;
for (j=0; j<nv; j++) {
if (vlist != NULL) {
v = vlist[j+1];
}
d = &darray[i+j];
d->depvar = 0;
d->level = level;
d->v = v;
d->minlag = m1;
d->maxlag = m2;
d->rows = 0;
}
*ip = i + nv;
}
}
return err;
}
/* parse requests of the form
GMM(xvar, minlag, maxlag)
which call for inclusion of lags of xvar in block-diagonal
fashion
*/
static int
parse_GMM_instrument_spec (int ci, const char *spec, const DATASET *dset,
diag_info **pd, int *pnspec)
{
diag_info *d = NULL;
const char *s;
int nspec = 0;
int err = 0;
/* first rough check: count closing parentheses */
s = spec;
while (*s) {
if (*s == ')') {
nspec++;
}
s++;
}
if (nspec == 0) {
/* spec is junk */
err = E_PARSE;
} else {
/* allocate info structs */
d = malloc(nspec * sizeof *d);
if (d == NULL) {
err = E_ALLOC;
}
}
if (!err) {
/* parse and record individual GMM instrument specs */
char test[48];
const char *p;
int len, i = 0;
s = spec;
while (*s && !err) {
while (*s == ' ') s++;
p = s;
while (*p && *p != ')') p++;
len = p - s + 1;
if (len > 47) {
err = E_PARSE;
} else {
*test = '\0';
strncat(test, s, len);
err = parse_diag_info(ci, test, &d, &i, &nspec, dset);
s = p + 1;
}
}
}
if (!err) {
int i, j;
for (i=1; i<nspec && !err; i++) {
for (j=0; j<i && !err; j++) {
if (d[i].v == d[j].v && d[i].level == d[j].level) {
gretl_errmsg_sprintf(_("variable %d duplicated "
"in the command list."),
d[i].v);
err = E_DATA;
}
}
}
}
if (err) {
free(d);
*pnspec = 0;
} else {
*pd = d;
*pnspec = nspec;
}
return err;
}
/* public interface: driver for Arellano-Bond type estimation */
MODEL
arbond_estimate (const int *list, const char *ispec,
const DATASET *dset, gretlopt opt,
PRN *prn)
{
diag_info *d = NULL;
ddset *dpd = NULL;
int nzb = 0;
MODEL mod;
int err = 0;
gretl_model_init(&mod, dset);
/* parse GMM instrument info, if present */
if (ispec != NULL && *ispec != '\0') {
mod.errcode = parse_GMM_instrument_spec(ARBOND, ispec, dset, &d, &nzb);
if (mod.errcode) {
return mod;
}
}
/* initialize (including some memory allocation) */
dpd = ddset_new(ARBOND, list, NULL, dset, opt, d, nzb, &mod.errcode);
if (mod.errcode) {
fprintf(stderr, "Error %d in dpd_init\n", mod.errcode);
return mod;
}
/* see if we have a usable sample */
err = arbond_sample_check(dpd, dset);
if (err) {
fprintf(stderr, "Error %d in dpd_sample_check\n", err);
}
if (!err && dpd->nx > 0) {
/* cut out any all-zero variables */
dpd_zero_check(dpd, dset);
}
if (!err) {
/* main workspace allocation */
err = dpd_allocate_matrices(dpd);
}
if (!err) {
err = arbond_make_y_X(dpd, dset);
}
if (!err) {
/* build instrument matrix blocks, Z_i, and insert into
big Z' matrix; cumulate first-stage A_N as we go
*/
err = arbond_make_Z_and_A(dpd, dset);
}
if (!err) {
err = dpd_step_1(dpd);
}
if (!err && (opt & OPT_T)) {
/* second step, if wanted */
err = dpd_step_2(dpd);
}
if (err && !mod.errcode) {
mod.errcode = err;
}
if (!mod.errcode) {
/* write estimation info into model struct */
mod.errcode = dpd_finalize_model(&mod, dpd, list, NULL, ispec,
dset, opt);
}
ddset_free(dpd);
return mod;
}
#include "dpanel.c"
|