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 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048
|
/*
* gretl -- Gnu Regression, Econometrics and Time-series Library
* Copyright (C) 2017 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/>.
*
*/
/* Code for regularized least squares. Includes these methods:
ADMM: Based on Boyd et al, "Distributed Optimization and
Statistical Learning via the Alternating Direction Method of
Multipliers", Foundations and Trends in Machine Learning, Vol. 3,
No. 1 (2010) 1-122.
CCD (Cyclical Coordinate Descent): Based on the Fortran code
employed by R's glmnet for the Gaussian case and the "covariance"
algorithm.
SVD: for Ridge.
*/
#include "libgretl.h"
#include "matrix_extra.h"
#include "version.h"
#ifdef HAVE_MPI
# include "gretl_mpi.h"
# include "gretl_foreign.h"
#endif
#if defined(USE_AVX)
# define USE_SIMD
# if defined(HAVE_IMMINTRIN_H)
# include <immintrin.h>
# else
# include <mmintrin.h>
# include <xmmintrin.h>
# include <emmintrin.h>
# endif
#endif
#define ADMM_MAX_ITER 20000
#define ADMM_RELTOL_DEFAULT 1.0e-4
#define ADMM_ABSTOL_DEFAULT 1.0e-6
double admm_reltol;
double admm_abstol;
#define CCD_MAX_ITER 100000
#define CCD_TOLER_DEFAULT 1.0e-7
#define BIG_LAMBDA 9.9e35
#define RHO_DEBUG 0
#define LAMBDA_DEBUG 0
#define RIDGE_DEBUG 0
double ccd_toler;
enum {
LAMSCALE_NONE,
LAMSCALE_GLMNET,
LAMSCALE_FROB
};
typedef struct regls_info_ {
gretl_bundle *b;
gretl_matrix *X;
gretl_matrix *y;
gretl_matrix *lfrac;
gretl_matrix *Xty;
gretl_matrix *R2;
gretl_matrix *crit;
gretl_matrix *BIC;
gretl_matrix *edf;
double rho;
double infnorm;
double alpha;
int nlam;
int n;
int k;
int nf;
gint8 ccd;
gint8 ridge;
gint8 stdize;
gint8 xvalid;
gint8 verbose;
gint8 lamscale;
gint8 randfolds;
gint8 use_1se;
PRN *prn;
} regls_info;
typedef struct ccd_info_ {
gretl_matrix_block *MB;
gretl_matrix *Xty;
gretl_matrix *xv;
gretl_matrix *B;
gretl_matrix *lam;
double lmax;
} ccd_info;
#ifdef HAVE_MPI
static int mpi_parent_action (regls_info *ri);
#endif
static void prepare_ccd_param (regls_info *ri)
{
double tol;
tol = gretl_bundle_get_scalar(ri->b, "ccd_toler", NULL);
if (!na(tol) && tol > 0.0 && tol < 1.0) {
ccd_toler = tol;
} else {
ccd_toler = CCD_TOLER_DEFAULT;
}
}
static void maybe_set_lambda_scale (regls_info *ri)
{
if (gretl_bundle_has_key(ri->b, "lambda_scale")) {
ri->lamscale = gretl_bundle_get_int(ri->b, "lambda_scale", NULL);
}
}
static void prepare_admm_params (regls_info *ri)
{
gretl_matrix *ctrl;
int len;
/* set defaults */
admm_reltol = ADMM_RELTOL_DEFAULT;
admm_abstol = ADMM_ABSTOL_DEFAULT;
ctrl = gretl_bundle_get_matrix(ri->b, "admmctrl", NULL);
len = gretl_vector_get_length(ctrl);
if (len > 0 && ctrl->val[0] > 0) {
ri->rho = ctrl->val[0];
}
if (len > 1 && ctrl->val[1] > 0) {
admm_reltol = ctrl->val[1];
}
if (len > 2 && ctrl->val[2] > 0) {
admm_abstol = ctrl->val[2];
}
/* scale the absolute tolerance */
admm_abstol *= sqrt(ri->X->cols);
}
static int get_xvalidation_details (regls_info *ri)
{
int err = 0;
ri->nf = gretl_bundle_get_int(ri->b, "nfolds", &err);
ri->randfolds = gretl_bundle_get_bool(ri->b, "randfolds", 0);
if (!err && ri->nf < 2) {
err = E_INVARG;
}
return err;
}
static regls_info *regls_info_new (gretl_matrix *X,
gretl_matrix *y,
gretl_bundle *b,
PRN *prn, int *err)
{
regls_info *ri = calloc(1, sizeof *ri);
if (ri == NULL) {
*err = E_ALLOC;
} else {
ri->lfrac = gretl_bundle_get_matrix(b, "lfrac", err);
}
if (!*err) {
ri->b = b;
ri->X = X;
ri->y = y;
ri->stdize = gretl_bundle_get_int(b, "stdize", err);
ri->xvalid = gretl_bundle_get_int(b, "xvalidate", err);
ri->verbose = gretl_bundle_get_bool(b, "verbosity", 1);
if (gretl_bundle_has_key(b, "alpha")) {
ri->alpha = gretl_bundle_get_scalar(b, "alpha", NULL);
if (ri->alpha == 0) {
ri->ridge = 1;
}
} else {
ri->ridge = gretl_bundle_get_bool(b, "ridge", 0);
ri->alpha = ri->ridge ? 0 : 1;
}
if (ri->alpha > 0 && ri->alpha < 1) {
ri->ccd = 1;
} else {
ri->ccd = gretl_bundle_get_bool(b, "ccd", 0);
}
}
if (*err) {
free(ri);
ri = NULL;
} else {
ri->prn = prn;
ri->R2 = ri->crit = ri->BIC = ri->edf = NULL;
ri->n = ri->X->rows;
ri->k = ri->X->cols;
ri->nlam = gretl_vector_get_length(ri->lfrac);
ri->rho = 8.0;
ri->infnorm = 0.0;
ri->lamscale = LAMSCALE_GLMNET;
ri->Xty = NULL;
if (ri->ccd) {
prepare_ccd_param(ri);
} else if (!ri->ridge && !ri->ccd) {
prepare_admm_params(ri);
}
if (ri->alpha < 1) {
maybe_set_lambda_scale(ri);
ri->edf = gretl_matrix_alloc(ri->nlam, 1);
if (ri->edf == NULL) {
*err = E_ALLOC;
}
}
if (!*err && ri->xvalid) {
*err = get_xvalidation_details(ri);
} else if (!*err) {
ri->nf = ri->randfolds = ri->use_1se;
ri->crit = gretl_matrix_alloc(ri->nlam, 1);
ri->R2 = gretl_matrix_alloc(ri->nlam, 1);
ri->BIC = gretl_matrix_alloc(ri->nlam, 1);
if (ri->R2 == NULL || ri->crit == NULL || ri->BIC == NULL) {
*err = E_ALLOC;
}
}
}
return ri;
}
static void regls_info_free (regls_info *ri)
{
if (ri != NULL) {
gretl_matrix_free(ri->Xty);
gretl_matrix_free(ri->R2);
gretl_matrix_free(ri->crit);
gretl_matrix_free(ri->BIC);
free(ri);
}
}
/* For when we're not doing cross validation: push various
statistics into the output bundle
*/
static void regls_set_crit_data (regls_info *ri)
{
if (ri->nlam > 1) {
gretl_bundle_donate_data(ri->b, "crit", ri->crit, GRETL_TYPE_MATRIX, 0);
if (ri->BIC != NULL) {
gretl_bundle_donate_data(ri->b, "BIC", ri->BIC, GRETL_TYPE_MATRIX, 0);
}
if (ri->R2 != NULL) {
gretl_bundle_donate_data(ri->b, "R2", ri->R2, GRETL_TYPE_MATRIX, 0);
}
if (ri->edf != NULL) {
gretl_bundle_donate_data(ri->b, "edf", ri->edf, GRETL_TYPE_MATRIX, 0);
}
ri->crit = ri->BIC = ri->R2 = ri->edf = NULL;
} else {
gretl_bundle_set_scalar(ri->b, "crit", ri->crit->val[0]);
if (ri->BIC != NULL) {
gretl_bundle_set_scalar(ri->b, "BIC", ri->BIC->val[0]);
}
if (ri->R2 != NULL) {
gretl_bundle_set_scalar(ri->b, "R2", ri->R2->val[0]);
}
if (ri->edf != NULL) {
gretl_bundle_set_scalar(ri->b, "edf", ri->edf->val[0]);
}
}
}
static double vector_infnorm (const gretl_vector *z)
{
const int n = gretl_vector_get_length(z);
double azi, ret = 0;
int i;
for (i=0; i<n; i++) {
azi = fabs(z->val[i]);
if (azi > ret) ret = azi;
}
return ret;
}
/* compute X'y and its infinity-norm for all training data */
static int regls_set_Xty (regls_info *ri)
{
int err = 0;
ri->Xty = gretl_matrix_alloc(ri->X->cols, 1);
if (ri->Xty == NULL) {
err = E_ALLOC;
} else {
gretl_matrix_multiply_mod(ri->X, GRETL_MOD_TRANSPOSE,
ri->y, GRETL_MOD_NONE,
ri->Xty, GRETL_MOD_NONE);
ri->infnorm = vector_infnorm(ri->Xty);
if (ri->ccd || ri->ridge) {
ri->infnorm /= ri->n;
}
#if LAMBDA_DEBUG
fprintf(stderr, "regls_set_Xty: infnorm = %g\n", ri->infnorm);
#endif
}
return err;
}
static int randomize_rows (gretl_matrix *X, gretl_matrix *y)
{
gretl_vector *vp;
double x, tmp;
int i, j, src;
vp = gretl_matrix_alloc(X->rows, 1);
if (vp == NULL) {
return E_ALLOC;
}
fill_permutation_vector(vp, X->rows);
for (i=0; i<X->rows; i++) {
src = vp->val[i] - 1;
if (src == i) {
continue;
}
for (j=0; j<X->cols; j++) {
tmp = gretl_matrix_get(X, i, j);
x = gretl_matrix_get(X, src, j);
gretl_matrix_set(X, i, j, x);
gretl_matrix_set(X, src, j, tmp);
}
tmp = y->val[i];
y->val[i] = y->val[src];
y->val[src] = tmp;
}
gretl_matrix_free(vp);
return 0;
}
static void vector_copy_values (gretl_vector *targ,
const gretl_vector *src,
int n)
{
memcpy(targ->val, src->val, n * sizeof *targ->val);
}
static inline double max (double x, double y)
{
return x >= y ? x : y;
}
#if defined(USE_SIMD)
static inline double hsum_double_avx (__m256d v)
{
__m128d vlow = _mm256_castpd256_pd128(v);
__m128d vhigh = _mm256_extractf128_pd(v, 1);
__m128d high64;
vlow = _mm_add_pd(vlow, vhigh);
high64 = _mm_unpackhi_pd(vlow, vlow);
return _mm_cvtsd_f64(_mm_add_sd(vlow, high64));
}
static void vector_add_into (const gretl_vector *a,
const gretl_vector *b,
gretl_vector *c, int n)
{
const double *ax = a->val;
const double *bx = b->val;
double *cx = c->val;
int imax = n / 4;
int rem = n % 4;
int i;
__m256d a256, b256, c256;
for (i=0; i<imax; i++) {
a256 = _mm256_loadu_pd(ax);
b256 = _mm256_loadu_pd(bx);
c256 = _mm256_add_pd(a256, b256);
_mm256_storeu_pd(cx, c256);
ax += 4;
bx += 4;
cx += 4;
}
for (i=0; i<rem; i++) {
cx[i] = ax[i] + bx[i];
}
}
static void vector_add_to (gretl_vector *a,
const gretl_vector *b,
int n)
{
double *ax = a->val;
const double *bx = b->val;
int imax = n / 4;
int rem = n % 4;
int i;
__m256d a256, b256, sum;
for (i=0; i<imax; i++) {
a256 = _mm256_loadu_pd(ax);
b256 = _mm256_loadu_pd(bx);
sum = _mm256_add_pd(a256, b256);
_mm256_storeu_pd(ax, sum);
ax += 4;
bx += 4;
}
for (i=0; i<rem; i++) {
ax[i] += bx[i];
}
}
/* a = a - b */
static void vector_subtract_from (gretl_vector *a,
const gretl_vector *b,
int n)
{
double *ax = a->val;
const double *bx = b->val;
int imax = n / 4;
int rem = n % 4;
int i;
__m256d a256, b256, dif;
for (i=0; i<imax; i++) {
a256 = _mm256_loadu_pd(ax);
b256 = _mm256_loadu_pd(bx);
dif = _mm256_sub_pd(a256, b256);
_mm256_storeu_pd(ax, dif);
ax += 4;
bx += 4;
}
for (i=0; i<rem; i++) {
ax[i] -= bx[i];
}
}
/* c = a - b */
static void vector_subtract_into (const gretl_vector *a,
const gretl_vector *b,
gretl_vector *c, int n,
int cumulate)
{
const double *ax = a->val;
const double *bx = b->val;
double *cx = c->val;
int imax = n / 4;
int rem = n % 4;
int i;
__m256d a256, b256, c256;
for (i=0; i<imax; i++) {
a256 = _mm256_loadu_pd(ax);
b256 = _mm256_loadu_pd(bx);
if (cumulate) {
__m256d d256 = _mm256_sub_pd(a256, b256);
c256 = _mm256_loadu_pd(cx);
d256 = _mm256_add_pd(c256, d256);
_mm256_storeu_pd(cx, d256);
} else {
c256 = _mm256_sub_pd(a256, b256);
_mm256_storeu_pd(cx, c256);
}
ax += 4;
bx += 4;
cx += 4;
}
for (i=0; i<rem; i++) {
if (cumulate) {
cx[i] += ax[i] - bx[i];
} else {
cx[i] = ax[i] - bx[i];
}
}
}
/* compute q = rho * (b - u) + X'y */
static inline void compute_q (gretl_vector *q,
const gretl_vector *b,
const gretl_vector *u,
const gretl_vector *a, /* X'y */
double rho, int n)
{
__m256d b256, u256, a256;
__m256d r256, tmp;
const double *bx = b->val;
const double *ux = u->val;
const double *ax = a->val;
double *qx = q->val;
const int mul = rho != 1.0;
int imax = n / 4;
int rem = n % 4;
int i;
if (mul) {
/* broadcast rho */
r256 = _mm256_broadcast_sd(&rho);
}
for (i=0; i<imax; i++) {
b256 = _mm256_loadu_pd(bx);
u256 = _mm256_loadu_pd(ux);
a256 = _mm256_loadu_pd(ax);
/* subtract u from b */
tmp = _mm256_sub_pd(b256, u256);
if (mul) {
/* multiply by rho */
tmp = _mm256_mul_pd(tmp, r256);
}
/* add a */
tmp = _mm256_add_pd(tmp, a256);
/* write result into q */
_mm256_storeu_pd(qx, tmp);
bx += 4;
ux += 4;
ax += 4;
qx += 4;
}
for (i=0; i<rem; i++) {
if (mul) {
qx[i] = rho * (bx[i] - ux[i]) + ax[i];
} else {
qx[i] = bx[i] - ux[i] + ax[i];
}
}
}
static double dot_product (const double *x, const double *y, int n)
{
double ret = 0.0;
int i, imax = n / 4;
int rem = n % 4;
__m256d x256, y256, tmp;
for (i=0; i<imax; i++) {
x256 = _mm256_loadu_pd(x);
y256 = _mm256_loadu_pd(y);
tmp = _mm256_mul_pd(x256, y256);
ret += hsum_double_avx(tmp);
x += 4;
y += 4;
}
for (i=0; i<rem; i++) {
ret += x[i] * y[i];
}
return ret;
}
#else
static void vector_add_into (const gretl_vector *a,
const gretl_vector *b,
gretl_vector *c, int n)
{
int i;
for (i=0; i<n; i++) {
c->val[i] = a->val[i] + b->val[i];
}
}
static void vector_add_to (gretl_vector *a,
const gretl_vector *b,
int n)
{
int i;
for (i=0; i<n; i++) {
a->val[i] += b->val[i];
}
}
static void vector_subtract_from (gretl_vector *a,
const gretl_vector *b,
int n)
{
int i;
for (i=0; i<n; i++) {
a->val[i] -= b->val[i];
}
}
static void vector_subtract_into (const gretl_vector *a,
const gretl_vector *b,
gretl_vector *c, int n,
int cumulate)
{
int i;
for (i=0; i<n; i++) {
if (cumulate) {
c->val[i] += a->val[i] - b->val[i];
} else {
c->val[i] = a->val[i] - b->val[i];
}
}
}
static double dot_product (const double *x, const double *y, int n)
{
double ret = 0.0;
int i;
for (i=0; i<n; i++) {
ret += x[i] * y[i];
}
return ret;
}
static inline void compute_q (gretl_vector *q,
const gretl_vector *b,
const gretl_vector *u,
const gretl_vector *Xty,
double rho, int n)
{
const int mul = rho != 1.0;
int i;
for (i=0; i<n; i++) {
if (mul) {
q->val[i] = rho * (b->val[i] - u->val[i]) + Xty->val[i];
} else {
q->val[i] = b->val[i] - u->val[i] + Xty->val[i];
}
}
}
#endif /* AVX or not */
static double own_dot_product (const gretl_vector *x)
{
int n = gretl_vector_get_length(x);
return dot_product(x->val, x->val, n);
}
/* fortran: dot_product(X(:,j), X(:,k)) for @X with @n rows */
static double dot_prod_jk (const gretl_matrix *X, int j, int k, int n)
{
const double *xj = X->val + n * j;
const double *xk = X->val + n * k;
return dot_product(xj, xk, n);
}
/* fortran: dot_product(v(1:n), m(j,1:n)) */
static double dot_prod_vm (const double *v,
const gretl_matrix *m,
int j, int n)
{
double ret = 0;
int i;
for (i=0; i<n; i++) {
ret += v[i] * gretl_matrix_get(m, j, i);
}
return ret;
}
/* implement these fortran lines:
x(1:n) = y(idx(1:n)) - x(1:n) !! sub = 1
x(1:n) = y(idx(1:n)) !! sub = 0
*/
static void range_set_sub (double *x, const double *y,
const int *idx, int n, int sub)
{
int i;
if (sub) {
for (i=0; i<n; i++) {
x[i] = y[idx[i]] - x[i];
}
} else {
for (i=0; i<n; i++) {
x[i] = y[idx[i]];
}
}
}
/* fortran: B(1:n,j) = a(idx(1:n)) */
static void fill_coeff_column (gretl_matrix *B, int nx, int j,
const double *a, const int *idx,
int n)
{
int i, offset = B->rows > nx;
double *b = B->val + j * B->rows;
for (i=0; i<n; i++) {
b[i+offset] = a[idx[i]];
}
}
/* sign(x,y): gives "the value of x with the sign of y",
but in context @x will always be positive.
*/
static inline double sign (double x, double y)
{
return y >= 0 ? x : -x;
}
static double abs_sum (const gretl_vector *z)
{
const int n = gretl_vector_get_length(z);
double ret = 0;
int i;
for (i=0; i<n; i++) {
ret += fabs(z->val[i]);
}
return ret;
}
/* Cyclical Coordinate Descent (CCD) auxiliary functions */
static int ccd_scale (gretl_matrix *x, double *y,
double *xty, double *xv)
{
int i, j, n = x->rows;
double *xj, v = sqrt(1.0/n);
for (i=0; i<n; i++) {
y[i] *= v;
}
for (j=0; j<x->cols; j++) {
xj = x->val + j * n;
for (i=0; i<n; i++) {
xj[i] *= v;
}
if (xv != NULL) {
xv[j] = dot_product(xj, xj, n);
}
if (xty != NULL) {
xty[j] = dot_product(y, xj, n);
}
}
return 0;
}
static void finalize_ccd_coeffs (gretl_matrix *B,
double *a, int nx,
int *ia)
{
int offset = B->rows > nx;
size_t asize = nx * sizeof *a;
double *bj;
int i, j;
for (j=0; j<B->cols; j++) {
bj = B->val + j*B->rows + offset;
memcpy(a, bj, asize);
for (i=0; i<nx; i++) {
bj[i] = 0.0;
}
for (i=0; i<nx; i++) {
if (a[i] != 0) {
bj[ia[i]] = a[i];
}
}
}
}
static int ccd_iteration (double alpha, const gretl_matrix *X, double *g,
int nlam, const double *ulam, double thr,
int maxit, const double *xv, int *lmu,
gretl_matrix *B, int *ia, int *kin,
double *Rsq, int *pnlp)
{
gretl_matrix *C;
double alm, u, v, rsq = 0;
double ak, del, dlx, cij;
double omb, dem, ab;
double *a, *da;
int *mm, nin, jz, iz = 0;
int j, k, l, m, nlp = 0;
int nx = X->cols;
int bad_R2 = 0;
int err = 0;
C = gretl_matrix_alloc(nx, nx);
a = malloc(nx * sizeof *a);
da = malloc(nx * sizeof *da);
mm = malloc(nx * sizeof *mm);
if (C == NULL || a == NULL || da == NULL || mm == NULL) {
return E_ALLOC;
}
/* "zero" @a and @mm */
for (j=0; j<nx; j++) {
a[j] = 0.0;
mm[j] = -1;
}
nin = nlp = *pnlp = 0;
omb = 1.0 - alpha; /* = 0 for lasso */
for (m=0; m<nlam; m++) {
alm = ulam[m];
dem = alm*omb;
ab = alm*alpha;
jz = 1;
maybe_restart:
if (iz * jz == 0) {
nlp++;
dlx = 0.0;
for (k=0; k<nx; k++) {
ak = a[k];
u = g[k] + ak*xv[k];
v = fabs(u) - ab;
a[k] = v > 0.0 ? sign(v,u) / (xv[k]+dem) : 0.0;
if (a[k] != ak) {
if (mm[k] < 0) {
if (nin >= nx) goto check_conv;
for (j=0; j<nx; j++) {
if (mm[j] >= 0) {
cij = gretl_matrix_get(C, k, mm[j]);
gretl_matrix_set(C, j, nin, cij);
} else if (j != k) {
cij = dot_prod_jk(X, j, k, X->rows);
gretl_matrix_set(C, j, nin, cij);
} else {
gretl_matrix_set(C, j, nin, xv[j]);
}
}
mm[k] = nin;
ia[nin] = k;
nin++;
}
del = a[k] - ak;
rsq += del * (2*g[k] - del*xv[k]);
dlx = max(xv[k]*del*del, dlx);
for (j=0; j<nx; j++) {
cij = gretl_matrix_get(C, j, mm[k]);
g[j] -= cij*del;
}
}
}
check_conv:
if (dlx < thr || nin > nx) {
goto m_finish;
} else if (nlp > maxit) {
fprintf(stderr, "ccd: max iters reached\n");
err = E_NOCONV;
goto getout;
}
}
iz = 1;
range_set_sub(da, a, ia, nin, 0);
nlp_plus:
nlp++;
dlx = 0.0;
for (l=0; l<nin; l++) {
k = ia[l];
ak = a[k];
u = g[k] + ak*xv[k];
v = fabs(u) - ab;
a[k] = v > 0.0 ? sign(v,u) / (xv[k]+dem) : 0.0;
if (a[k] != ak) {
del = a[k] - ak;
rsq += del * (2*g[k] - del*xv[k]);
dlx = max(xv[k]*del*del, dlx);
for (j=0; j<nin; j++) {
cij = gretl_matrix_get(C, ia[j], mm[k]);
g[ia[j]] -= cij*del;
}
}
}
if (dlx < thr) {
range_set_sub(da, a, ia, nin, 1);
for (j=0; j<nx; j++) {
if (mm[j] < 0) {
g[j] -= dot_prod_vm(da, C, j, nin);
}
}
jz = 0;
goto maybe_restart;
} else if (nlp <= maxit) {
/* try another iteration */
goto nlp_plus;
} else {
/* reached max iterations */
err = E_NOCONV;
goto getout;
}
m_finish:
if (nin <= nx) {
if (nin > 0) {
fill_coeff_column(B, nx, m, a, ia, nin);
}
kin[m] = nin;
if (Rsq != NULL) {
if (rsq > 1) {
bad_R2 = 1;
}
Rsq[m] = rsq;
}
*lmu = m + 1;
} else {
err = E_NOCONV;
fprintf(stderr, "ccd: error at foot of loop\n");
goto getout;
}
} /* end loop over lambda values */
getout:
if (!err) {
finalize_ccd_coeffs(B, a, nx, ia);
if (bad_R2) {
Rsq[0] = NADBL;
}
}
*pnlp = nlp;
free(a);
free(mm);
free(da);
gretl_matrix_free(C);
return err;
}
static int ccd_get_crit (const gretl_matrix *B,
const gretl_matrix *lam,
regls_info *ri)
{
double *bj, l1, l2, SSR;
double ll, llc, edf = 0;
double lambda, nulldev = 1.0;
double alpha = ri->alpha;
double penalty;
int imin = B->rows > ri->k;
int dfj, n = ri->n;
int i, j;
if (!ri->stdize) {
/* in case @y is in fact non-standard */
const double *y = ri->y->val;
nulldev = 0.0;
for (i=0; i<n; i++) {
nulldev += y[i] * y[i];
}
}
llc = -0.5 * n * (1 + LN_2_PI - log(n));
for (j=0; j<B->cols; j++) {
lambda = lam->val[j];
l1 = l2 = 0;
dfj = 0;
bj = B->val + j*B->rows;
for (i=imin; i<B->rows; i++) {
if (alpha == 1) {
/* lasso */
l1 += fabs(bj[i]);
dfj += bj[i] != 0;
} else if (alpha == 0) {
/* ridge */
l2 += bj[i] * bj[i];
} else {
l1 += alpha * fabs(bj[i]);
l2 += bj[i] * bj[i];
dfj += bj[i] != 0;
}
}
SSR = nulldev * (1.0 - ri->R2->val[j]);
/* with CCD, y and X are scaled by 1/sqrt(n) */
ll = llc - 0.5 * n * log(n*SSR);
if (alpha == 1) {
/* lasso */
gretl_vector_set(ri->crit, j, 0.5 * SSR + lambda * l1);
gretl_vector_set(ri->BIC, j, -2 * ll + dfj * log(n));
} else if (alpha == 0) {
/* ridge */
edf = ri->edf->val[j];
gretl_vector_set(ri->crit, j, SSR + lambda * l2);
gretl_vector_set(ri->BIC, j, -2 * ll + edf * log(n));
} else {
/* elnet */
edf = ri->edf->val[j];
penalty = 0.5 * (1 - alpha) * l2 + alpha * l1;
gretl_vector_set(ri->crit, j, 0.5 * SSR + lambda * penalty);
gretl_vector_set(ri->BIC, j, -2 * ll + edf * log(n));
}
}
return 0;
}
/* We call ridge_effective_df() only if we're doing ridge
via CCD -- otherwise the effective df gets computed as
part of the larger SVD calculation.
*/
static int ridge_effective_df (const gretl_matrix *lam,
regls_info *ri)
{
gretl_matrix *s = NULL;
int err;
err = gretl_matrix_SVD(ri->X, NULL, &s, NULL, 0);
if (!err) {
int i, j, k = gretl_vector_get_length(s);
double sv2, edfj;
for (i=0; i<k; i++) {
sv2 = s->val[i] * s->val[i];
s->val[i] = sv2;
}
for (j=0; j<ri->nlam; j++) {
edfj = 0;
for (i=0; i<k; i++) {
edfj += s->val[i] / (s->val[i] + lam->val[j]);
}
ri->edf->val[j] = edfj;
}
gretl_matrix_free(s);
}
return err;
}
static int elnet_effective_df (const gretl_matrix *lam,
const gretl_matrix *B,
regls_info *ri)
{
gretl_matrix *XTX = NULL;
gretl_matrix *X = NULL;
gretl_matrix *xi = NULL;
double *dest, *src;
double xii, dfj;
size_t csize;
int i, j, t;
int err = 0;
X = gretl_matrix_copy(ri->X);
XTX = gretl_matrix_alloc(ri->k, ri->k);
xi = gretl_matrix_alloc(1, ri->k);
if (X == NULL || XTX == NULL || xi == NULL) {
return E_ALLOC;
}
csize = ri->n * sizeof(double);
for (j=0; j<ri->nlam; j++) {
double lam2 = lam->val[j] * (1 - ri->alpha)/2;
int inv_err, kj = 0;
dest = X->val;
dfj = 0;
for (i=0; i<ri->k; i++) {
if (gretl_matrix_get(B, i, j) != 0) {
src = ri->X->val + i * ri->n;
memcpy(dest, src, csize);
dest += ri->n;
kj++;
}
}
if (kj == 0) {
ri->edf->val[j] = 0;
continue;
}
gretl_matrix_reuse(X, -1, kj);
gretl_matrix_reuse(XTX, kj, kj);
gretl_matrix_reuse(xi, 1, kj);
gretl_matrix_multiply_mod(X, GRETL_MOD_TRANSPOSE,
X, GRETL_MOD_NONE,
XTX, GRETL_MOD_NONE);
for (i=0; i<kj; i++) {
xii = gretl_matrix_get(XTX, i, i);
gretl_matrix_set(XTX, i, i, xii + lam2);
}
inv_err = gretl_invert_symmetric_matrix(XTX);
if (inv_err) {
fprintf(stderr, "elnet df: inversion failed for j=%d\n", j);
ri->edf->val[j] = NADBL;
} else {
for (t=0; t<ri->n; t++) {
for (i=0; i<kj; i++) {
xi->val[i] = gretl_matrix_get(X, t, i);
}
dfj += gretl_scalar_qform(xi, XTX, &err);
}
ri->edf->val[j] = dfj;
}
}
gretl_matrix_free(X);
gretl_matrix_free(XTX);
gretl_matrix_free(xi);
return err;
}
static gchar *crit_print_format (const gretl_matrix *crit,
int ridge)
{
gchar *fmt = NULL;
if (ridge) {
fmt = g_strdup_printf("%%12f %%6.2f %%.4f %%#g\n");
} else {
fmt = g_strdup_printf("%%12f %%5d %%f %%.4f %%#g\n");
}
return fmt;
}
static void lambda_sequence_header (PRN *prn)
{
pputc(prn, '\n');
pputs(prn, " lambda/n df criterion R^2 BIC\n");
}
static void ccd_print (const gretl_matrix *B,
const gretl_matrix *lam,
regls_info *ri)
{
gchar *cfmt = NULL;
double *bj;
int k = B->rows;
int nlam = B->cols;
int i, j, dfj;
if (ri->crit != NULL) {
/* header for output showing penalized criterion */
lambda_sequence_header(ri->prn);
} else {
/* as per R, more or less */
pputc(ri->prn, '\n');
pputs(ri->prn, " df R^2 lambda BIC\n");
}
cfmt = crit_print_format(ri->crit, 0);
for (j=0; j<nlam; j++) {
bj = B->val + j*k;
dfj = 0;
for (i=0; i<k; i++) {
dfj += fabs(bj[i]) > 0;
}
if (ri->crit != NULL) {
pprintf(ri->prn, cfmt, lam->val[j], dfj, ri->crit->val[j],
ri->R2->val[j], ri->BIC->val[j]);
} else {
pprintf(ri->prn, "%-2d %2d %.4f %.4f %#g\n", j+1, dfj,
ri->R2->val[j], lam->val[j], ri->BIC->val[j]);
}
}
g_free(cfmt);
}
/* This also serves for printing elastic net results */
static void ridge_print (const gretl_matrix *lam,
regls_info *ri)
{
gchar *cfmt = NULL;
int j;
pprintf(ri->prn, "\n %s\n\n", _("df = effective number of free parameters"));
pputs(ri->prn, " lambda df R^2 BIC\n");
cfmt = crit_print_format(ri->crit, 1);
for (j=0; j<ri->nlam; j++) {
pprintf(ri->prn, cfmt, lam->val[j], ri->edf->val[j],
ri->R2->val[j], ri->BIC->val[j]);
}
g_free(cfmt);
}
static void xv_ridge_print (const gretl_matrix *lam,
regls_info *ri)
{
int j;
pputc(ri->prn, '\n');
pputs(ri->prn, " lambda df\n");
for (j=0; j<ri->nlam; j++) {
pprintf(ri->prn, "%12f %.3f\n", lam->val[j], ri->edf->val[j]);
}
}
/* end functions specific to CCD */
/* calculate the lasso objective function */
static double lasso_objective (const gretl_matrix *X,
const gretl_vector *y,
const gretl_vector *b,
double lambda,
gretl_vector *u,
double *pSSR,
double *pR2)
{
double TSS, SSR, obj;
TSS = own_dot_product(y);
gretl_matrix_multiply(X, b, u);
vector_subtract_from(u, y, y->rows);
SSR = own_dot_product(u);
obj = 0.5 * SSR + lambda * abs_sum(b);
*pR2 = 1.0 - SSR/TSS;
if (pSSR != NULL) {
*pSSR = SSR;
}
return obj / y->rows;
}
/* calculate the cross validation criterion */
static double xv_score (const gretl_matrix *X,
const gretl_vector *y,
const gretl_vector *b,
gretl_vector *Xb)
{
double sum = 0;
/* get fitted values */
gretl_matrix_multiply(X, b, Xb);
/* compute and process residuals */
vector_subtract_from(Xb, y, X->rows);
sum = own_dot_product(Xb);
return sum / X->rows;
}
static void soft_threshold (gretl_vector *v, double lambda,
double rho)
{
double vi, k;
int i;
k = rho == 1.0 ? lambda : lambda / rho;
for (i=0; i<v->rows; i++) {
vi = v->val[i];
if (vi > k) { v->val[i] = vi - k; }
else if (vi < -k) { v->val[i] = vi + k; }
else { v->val[i] = 0; }
}
}
static int get_cholesky_factor (const gretl_matrix *X,
gretl_matrix *L,
double rho)
{
double d;
int i;
if (X->rows >= X->cols) {
/* "skinny": L = chol(X'X + rho*I) */
gretl_matrix_multiply_mod(X, GRETL_MOD_TRANSPOSE,
X, GRETL_MOD_NONE,
L, GRETL_MOD_NONE);
for (i=0; i<X->cols; i++) {
d = gretl_matrix_get(L, i, i);
gretl_matrix_set(L, i, i, d + rho);
}
} else {
/* "fat": L = chol(I + 1/rho*XX') */
gretl_matrix_multiply_mod(X, GRETL_MOD_NONE,
X, GRETL_MOD_TRANSPOSE,
L, GRETL_MOD_NONE);
if (rho != 1.0) {
gretl_matrix_multiply_by_scalar(L, 1/rho);
}
for (i=0; i<X->rows; i++) {
d = gretl_matrix_get(L, i, i);
gretl_matrix_set(L, i, i, d + 1.0);
}
}
return gretl_matrix_cholesky_decomp(L);
}
static int admm_iteration (const gretl_matrix *X,
const gretl_vector *Xty,
gretl_matrix *L,
gretl_vector *v, gretl_vector *b,
gretl_vector *u, gretl_vector *q,
gretl_vector *p, gretl_vector *r,
gretl_vector *bprev, gretl_vector *bdiff,
double lambda, double *prho,
int tune_rho, int *iters)
{
double nxstack, nystack;
double prires, dualres;
double eps_pri, eps_dual;
double rho = *prho;
double nrm2, rho2 = rho*rho;
int itermin = 1;
int n = X->cols;
int iter = 0;
int err = 0;
#if RHO_DEBUG
fprintf(stderr, "*** admm: lambda %g, rho %g ***\n", lambda, rho);
#endif
while (iter < ADMM_MAX_ITER && !err) {
/* u-update: u = u + r */
vector_add_to(u, r, n);
/* v-update: v = (X^T X + rho I) \ (X^T y + rho b - u) */
compute_q(q, b, u, Xty, rho, n);
if (X->rows >= X->cols) {
/* v = U \ (L \ q) */
gretl_cholesky_solve(L, q);
vector_copy_values(v, q, n);
} else {
/* v = q/rho - 1/rho^2 * X^T * (U \ (L \ (X*q))) */
gretl_matrix_multiply(X, q, p);
err = gretl_cholesky_solve(L, p);
gretl_matrix_multiply_mod(X, GRETL_MOD_TRANSPOSE,
p, GRETL_MOD_NONE,
v, GRETL_MOD_NONE);
gretl_matrix_multiply_by_scalar(v, -1/rho2);
gretl_matrix_multiply_by_scalar(q, 1/rho);
vector_add_to(v, q, n);
}
/* sqrt(sum ||r_i||_2^2) */
prires = sqrt(own_dot_product(r));
/* sqrt(sum ||v_i||_2^2) */
nxstack = sqrt(own_dot_product(v));
/* sqrt(sum ||u_i||_2^2) */
nystack = own_dot_product(u) / rho2;
nystack = sqrt(nystack);
vector_copy_values(bprev, b, n);
vector_add_into(v, u, b, n);
soft_threshold(b, lambda, rho);
/* Termination checks */
/* dual residual */
vector_subtract_into(b, bprev, bdiff, n, 0); /* bdiff = b - bprev */
/* ||s^k||_2^2 = N rho^2 ||b - bprev||_2^2 */
nrm2 = sqrt(own_dot_product(bdiff));
dualres = rho * nrm2;
/* compute primal and dual feasibility tolerances */
nrm2 = sqrt(own_dot_product(b));
eps_pri = admm_abstol + admm_reltol * fmax(nxstack, nrm2);
eps_dual = admm_abstol + admm_reltol * nystack;
if (iter >= itermin && prires <= eps_pri && dualres <= eps_dual) {
/* converged */
break;
}
/* Compute residual: r = v - b */
vector_subtract_into(v, b, r, n, 0);
if (tune_rho && iter > 0 && (iter == 32 || iter % 200 == 0)) {
double mult = 10;
double adj = 0.0;
if (prires > mult * dualres) {
adj = 2.0;
} else if (dualres > mult * prires) {
adj = 0.5;
}
if (adj > 0) {
rho *= adj;
# if RHO_DEBUG
fprintf(stderr, " iter %d: rho *= %g (now %g)\n",
iter, adj, rho);
# endif
rho2 = rho * rho;
gretl_matrix_multiply_by_scalar(u, 1.0/adj);
gretl_matrix_multiply_by_scalar(r, 1.0/adj);
get_cholesky_factor(X, L, rho);
/* ensure a fair number of subsequent iterations */
itermin = iter + 100;
}
}
iter++;
}
*iters = iter;
*prho = rho;
return err;
}
static gretl_matrix *make_coeff_matrix (regls_info *ri,
int *jmin,
int *jmax)
{
gretl_matrix *B = NULL;
int xv_single_b = 0;
int rows;
if (ri->xvalid) {
/* do we want just the "best" coeff vector? */
xv_single_b = gretl_bundle_get_bool(ri->b, "single_b", 0);
}
rows = ri->k + ri->stdize;
if (xv_single_b) {
int use_1se = gretl_bundle_get_bool(ri->b, "use_1se", 0);
const char *ikey = use_1se ? "idx1se" : "idxmin";
int idx;
idx = gretl_bundle_get_int(ri->b, ikey, NULL);
B = gretl_zero_matrix_new(rows, 1);
*jmin = idx - 1; /* zero-based */
*jmax = *jmin + 1;
} else {
B = gretl_zero_matrix_new(rows, ri->nlam);
*jmin = 0;
*jmax = ri->nlam;
}
if (B != NULL) {
gretl_bundle_donate_data(ri->b, "B", B, GRETL_TYPE_MATRIX, 0);
}
return B;
}
static void ccd_make_lambda (regls_info *ri,
gretl_matrix *lam,
double *lmax)
{
int i;
#if LAMBDA_DEBUG
fprintf(stderr, "ccd_make_lambda: lmax = %g\n", *lmax);
#endif
gretl_matrix_copy_values(lam, ri->lfrac);
if (ri->lamscale == LAMSCALE_NONE) {
for (i=0; i<ri->nlam; i++) {
lam->val[i] /= ri->n;
}
return;
}
if (ri->alpha < 1.0) {
*lmax /= max(ri->alpha, 1.0e-3);
#if LAMBDA_DEBUG
fprintf(stderr, "revised lmax = %g\n", *lmax);
#endif
}
for (i=0; i<ri->nlam; i++) {
lam->val[i] *= *lmax;
}
if (ri->alpha < 1.0 && ri->nlam > 1) {
lam->val[0] = BIG_LAMBDA;
}
}
static void lasso_lambda_report (regls_info *ri)
{
pprintf(ri->prn, "lambda-max = %g\n", ri->infnorm);
#if 0 /* the following repeats what's shown via hansl */
if (ri->nlam > 1) {
pprintf(ri->prn, "lambda-fraction sequence of length %d, starting at %g\n",
ri->nlam, ri->lfrac->val[0]);
} else {
pprintf(ri->prn, "single lambda-fraction %g\n", ri->lfrac->val[0]);
}
#endif
}
/* Remedial R^2 calculation for CCD: it seems that we end up
coming here only when standardization is turned off but
the dependent variable is substantially non-standard,
in which case ccd_iteration() can produce R^2 > 1.
*/
static int ccd_alt_R2 (regls_info *ri, gretl_matrix *B)
{
gretl_matrix *bj, *yh;
int n = ri->y->rows;
int k = ri->X->cols;
int err = 0;
bj = gretl_matrix_alloc(k, 1);
yh = gretl_matrix_alloc(n, 1);
if (bj == NULL || yh == NULL) {
err = E_ALLOC;
} else {
const double *y = ri->y->val;
size_t sz = k * sizeof(double);
double ui, SSR, TSS = 0;
int i, j;
for (i=0; i<n; i++) {
TSS += y[i] * y[i];
}
for (j=0; j<ri->nlam; j++) {
memcpy(bj->val, B->val + j*B->rows, sz);
gretl_matrix_multiply(ri->X, bj, yh);
SSR = 0;
for (i=0; i<n; i++) {
ui = y[i] - yh->val[i];
SSR += ui * ui;
}
ri->R2->val[j] = 1.0 - SSR/TSS;
}
}
gretl_matrix_free(yh);
gretl_matrix_free(bj);
return err;
}
static int ccd_prep (regls_info *ri, ccd_info *ci)
{
int nlam = ri->nlam;
int k = ri->k;
ci->MB = gretl_matrix_block_new(&ci->xv, k, 1,
&ci->Xty, k, 1,
&ci->lam, nlam, 1,
NULL);
ci->B = gretl_zero_matrix_new(k + ri->stdize, nlam);
if (ci->MB == NULL || ci->B == NULL) {
return E_ALLOC;
}
/* scale data by sqrt(1/n) */
ccd_scale(ri->X, ri->y->val, ci->Xty->val, ci->xv->val);
/* and compute lambda sequence */
ci->lmax = vector_infnorm(ci->Xty);
ccd_make_lambda(ri, ci->lam, &ci->lmax);
return 0;
}
/* Cyclical Coordinate Descent driver: we come here either
to get coefficient estimates right away, or after
cross validation. Handles both LASSO and Ridge.
*/
static int ccd_regls (regls_info *ri)
{
ccd_info ci = {0};
double *Rsq = NULL;
int maxit = CCD_MAX_ITER;
int *ia, *nnz;
int nlp = 0, lmu = 0;
int nlam = ri->nlam;
int k = ri->k;
int err;
err = ccd_prep(ri, &ci);
if (err) {
return err;
}
/* integer workspace */
ia = malloc((k + nlam) * sizeof *ia);
if (ia == NULL) {
err = E_ALLOC;
goto bailout;
}
nnz = ia + k;
if (!ri->xvalid) {
Rsq = ri->R2->val;
}
if (ri->edf != NULL && ri->alpha == 0) {
/* we'll want this but it's not calculated by CCD */
err = ridge_effective_df(ci.lam, ri);
}
if (ri->alpha == 1 && !ri->xvalid && ri->verbose) {
lasso_lambda_report(ri);
}
#if LAMBDA_DEBUG
fprintf(stderr, "ccd_regls: ci.lmax = %g\n", ci.lmax);
gretl_matrix_print(ci.lam, "lam in ccd_regls");
#endif
err = ccd_iteration(ri->alpha, ri->X, ci.Xty->val, nlam, ci.lam->val,
ccd_toler, maxit, ci.xv->val, &lmu, ci.B, ia,
nnz, Rsq, &nlp);
if (err) {
goto bailout;
}
if (ri->edf != NULL && ri->alpha > 0 && ri->alpha < 1) {
/* elastic net */
elnet_effective_df(ci.lam, ci.B, ri);
}
if (Rsq != NULL && na(Rsq[0])) {
/* remedy spurious R^2 > 1 */
err = ccd_alt_R2(ri, ci.B);
if (err) {
goto bailout;
}
}
if (ri->lamscale == LAMSCALE_NONE) {
gretl_matrix_multiply_by_scalar(ri->y, sqrt(ri->n));
gretl_matrix_copy_values(ci.lam, ri->lfrac);
} else if (ri->alpha < 1.0) {
/* not entirely truthful! */
ci.lam->val[0] = ri->lfrac->val[0] * ci.lmax;
}
if (ri->xvalid && ri->verbose > 1 && ri->ridge && nlam > 1) {
xv_ridge_print(ci.lam, ri);
}
if (!ri->xvalid) {
ccd_get_crit(ci.B, ci.lam, ri);
if (ri->verbose) {
if (ri->alpha < 1) {
ridge_print(ci.lam, ri);
} else {
ccd_print(ci.B, ci.lam, ri);
}
}
if (nlam > 1) {
double BICmin = 1e200;
int j, idxmin = 0;
for (j=0; j<nlam; j++) {
if (ri->BIC->val[j] < BICmin) {
BICmin = ri->BIC->val[j];
idxmin = j;
}
}
gretl_bundle_set_scalar(ri->b, "idxmin", idxmin + 1);
gretl_bundle_set_scalar(ri->b, "lfmin", ri->lfrac->val[idxmin]);
}
regls_set_crit_data(ri);
}
if (!err) {
gretl_bundle_donate_data(ri->b, "B", ci.B, GRETL_TYPE_MATRIX, 0);
ci.B = NULL;
if (ri->lamscale != LAMSCALE_NONE) {
gretl_bundle_set_scalar(ri->b, "lmax", ci.lmax * ri->n);
}
if (nlam == 1) {
double lambda = ri->lfrac->val[0];
if (ri->lamscale != LAMSCALE_NONE) {
/* show a value comparable with ADMM (??) */
lambda *= ci.lmax * ri->n;
}
gretl_bundle_set_scalar(ri->b, "lambda", lambda);
}
}
bailout:
gretl_matrix_free(ci.B);
gretl_matrix_block_destroy(ci.MB);
free(ia);
return err;
}
/* Variant of SVD ridge that computes the covariance
matrix as well as the parameter vector */
static int svd_ridge_vcv (regls_info *ri,
double lam,
gretl_matrix *B,
gretl_matrix **pV)
{
gretl_matrix_block *MB = NULL;
gretl_matrix *V = NULL;
gretl_matrix *Vt = NULL;
gretl_matrix *sv = NULL;
gretl_matrix *sve = NULL;
gretl_matrix *RI = NULL;
gretl_matrix *Tmp = NULL;
gretl_matrix *Ve = NULL;
gretl_matrix *u = NULL;
gretl_matrix *b = NULL;
double vij, SSR, s2;
int n = ri->X->rows;
int k = ri->X->cols;
int offset = 0;
int i, j, err = 0;
#if RIDGE_DEBUG
fprintf(stderr, "*** svd_ridge_vcv, lam = %g ***\n", lam);
#endif
err = gretl_matrix_SVD(ri->X, NULL, &sv, &Vt, 0);
if (!err) {
MB = gretl_matrix_block_new(&sve, 1, k,
&u, n, 1,
&RI, k, k,
&Ve, k, k,
&Tmp, k, k,
&b, k, 1, NULL);
if (MB == NULL) {
err = E_ALLOC;
goto bailout;
}
}
if (!err) {
V = gretl_matrix_alloc(k, k);
if (V == NULL) {
err = E_ALLOC;
goto bailout;
}
}
if (ri->edf != NULL) {
ri->edf->val[0] = 0.0;
}
/* sve = 1 / (sv.^2 + lambda) */
for (i=0; i<k; i++) {
sve->val[i] = 1.0 / (sv->val[i] * sv->val[i] + lam);
if (ri->edf != NULL) {
ri->edf->val[0] += sv->val[i] * sv->val[i] * sve->val[i];
}
}
/* Ve = Vt' .* sve */
for (j=0; j<k; j++) {
for (i=0; i<k; i++) {
vij = gretl_matrix_get(Vt, j, i);
gretl_matrix_set(Ve, i, j, vij * sve->val[j]);
}
}
/* RI = Ve * Vt */
gretl_matrix_multiply(Ve, Vt, RI);
/* b = RI * Xty */
gretl_matrix_multiply(RI, ri->Xty, b);
/* transcribe b to @B */
offset = B->rows > k ? 1 : 0;
memcpy(B->val + offset, b->val, k * sizeof *b->val);
/* u = X*b - y */
gretl_matrix_multiply(ri->X, b, u);
gretl_matrix_subtract_from(u, ri->y);
/* residual variance */
SSR = own_dot_product(u);
s2 = SSR / n;
/* V = s2 * ridgeI * X'X * ridgeI */
gretl_matrix_multiply_mod(ri->X, GRETL_MOD_TRANSPOSE,
ri->X, GRETL_MOD_NONE,
Ve, GRETL_MOD_NONE);
gretl_matrix_multiply(RI, Ve, Tmp);
gretl_matrix_multiply(Tmp, RI, V);
gretl_matrix_multiply_by_scalar(V, s2);
if (ri->R2 != NULL) {
double TSS = own_dot_product(ri->y);
ri->R2->val[0] = 1.0 - SSR/TSS;
}
bailout:
if (!err) {
*pV = V;
} else {
gretl_matrix_free(V);
}
gretl_matrix_block_destroy(MB);
return err;
}
/* Variant of SVD ridge that just computes the
parameter vector */
static int svd_ridge_bhat (double *lam, int nlam, gretl_matrix *X,
gretl_matrix *y, gretl_matrix *B,
gretl_matrix *R2, gretl_matrix *edf)
{
gretl_matrix_block *MB = NULL;
gretl_matrix *U = NULL;
gretl_matrix *Vt = NULL;
gretl_matrix *sv = NULL;
gretl_matrix *sve = NULL;
gretl_matrix *Uty = NULL;
gretl_matrix *L = NULL;
gretl_matrix *yh = NULL;
gretl_matrix *b = NULL;
double vij, ui, SSR;
double edfl, TSS = 0;
double *targ;
int offset = 0;
int n = X->rows;
int k = X->cols;
int i, j, l;
int err;
#if RIDGE_DEBUG
fprintf(stderr, "*** svd_ridge_bhat ***\n");
#endif
err = gretl_matrix_SVD(X, &U, &sv, &Vt, 0);
if (!err) {
MB = gretl_matrix_block_new(&sve, 1, sv->cols,
&Uty, U->cols, 1,
&L, Vt->cols, Vt->rows,
&b, k, 1,
&yh, n, 1, NULL);
if (MB == NULL) {
err = E_ALLOC;
}
}
if (err) {
goto bailout;
}
if (R2 != NULL) {
for (i=0; i<n; i++) {
TSS += y->val[i] * y->val[i];
}
}
offset = B->rows > k ? 1 : 0;
gretl_matrix_multiply_mod(U, GRETL_MOD_TRANSPOSE,
y, GRETL_MOD_NONE,
Uty, GRETL_MOD_NONE);
for (l=0; l<nlam; l++) {
edfl = 0;
for (j=0; j<sv->cols; j++) {
sve->val[j] = sv->val[j] / (sv->val[j] * sv->val[j] + lam[l]);
if (edf != NULL) {
edfl += sv->val[j] * sve->val[j];
}
}
if (edf != NULL) {
edf->val[l] = edfl;
}
/* L = Vt' .* sve */
for (j=0; j<L->cols; j++) {
for (i=0; i<L->rows; i++) {
vij = gretl_matrix_get(Vt, j, i);
gretl_matrix_set(L, i, j, vij * sve->val[j]);
}
}
gretl_matrix_multiply(L, Uty, b);
gretl_matrix_multiply(X, b, yh);
if (R2 != NULL) {
SSR = 0.0;
for (i=0; i<n; i++) {
ui = y->val[i] - yh->val[i];
SSR += ui * ui;
}
R2->val[l] = 1.0 - SSR/TSS;
}
targ = B->val + l * B->rows + offset;
memcpy(targ, b->val, k * sizeof *targ);
}
bailout:
gretl_matrix_block_destroy(MB);
gretl_matrix_free(U);
gretl_matrix_free(sv);
gretl_matrix_free(Vt);
return err;
}
/* called only from svd_ridge() */
static double ridge_scale (regls_info *ri,
gretl_matrix *lam)
{
double lmax = NADBL;
int i;
#if RIDGE_DEBUG
fprintf(stderr, "*** ridge_scale, lamscale = %d ***\n",
ri->lamscale);
#endif
if (ri->lamscale == LAMSCALE_GLMNET) {
gretl_matrix *Xty = gretl_matrix_alloc(ri->X->cols, 1);
if (Xty == NULL) {
return lmax;
} else if (ri->nlam == 1) {
gretl_matrix_multiply_mod(ri->X, GRETL_MOD_TRANSPOSE,
ri->y, GRETL_MOD_NONE,
Xty, GRETL_MOD_NONE);
lmax = 1000 * vector_infnorm(Xty);
} else {
/* as per glmnet, scale data by sqrt(1/n) */
ccd_scale(ri->X, ri->y->val, Xty->val, NULL);
lmax = 1000 * vector_infnorm(Xty);
for (i=0; i<ri->nlam; i++) {
lam->val[i] *= lmax;
}
if (ri->nlam > 1) {
lam->val[0] = BIG_LAMBDA;
}
gretl_matrix_free(Xty);
}
} else {
/* max = squared Frobenius norm = X->cols */
lmax = ri->X->cols;
for (i=0; i<ri->nlam; i++) {
lam->val[i] *= lmax;
}
}
return lmax;
}
static int svd_ridge (regls_info *ri)
{
gretl_matrix *B = NULL;
gretl_matrix *lam = NULL;
gretl_matrix *V = NULL;
double lmax = 1.0;
double lam0 = 0.0;
int err = 0;
#if RIDGE_DEBUG
fprintf(stderr, "\n*** svd_ridge ***\n");
#endif
lam = gretl_matrix_copy(ri->lfrac);
B = gretl_zero_matrix_new(ri->k + ri->stdize, ri->nlam);
if (lam == NULL || B == NULL) {
return E_ALLOC;
}
if (ri->lamscale != LAMSCALE_NONE) {
lmax = ridge_scale(ri, lam);
}
#if RIDGE_DEBUG
fprintf(stderr, "lfrac[0] = %g, lmax = %g\n", ri->lfrac->val[0], lmax);
#endif
if (ri->nlam == 1) {
/* calculate the covariance matrix */
lam0 = ri->lfrac->val[0] * lmax;
err = svd_ridge_vcv(ri, lam0, B, &V);
} else {
/* just calculate the parameters */
err = svd_ridge_bhat(lam->val, ri->nlam, ri->X, ri->y, B,
ri->R2, ri->edf);
}
if (err) {
goto bailout;
}
if (ri->lamscale == LAMSCALE_GLMNET) {
/* not entirely truthful! */
lam->val[0] = ri->lfrac->val[0] * lmax;
if (ri->nlam == 1) {
lam->val[0] /= ri->n;
}
}
if (!ri->xvalid) {
ccd_get_crit(B, lam, ri);
if (ri->verbose) {
ridge_print(lam, ri);
}
if (ri->nlam > 1) {
double BICmin = 1e200;
int j, idxmin = 0;
for (j=0; j<ri->nlam; j++) {
if (ri->BIC->val[j] < BICmin) {
BICmin = ri->BIC->val[j];
idxmin = j;
}
}
gretl_bundle_set_scalar(ri->b, "idxmin", idxmin + 1);
gretl_bundle_set_scalar(ri->b, "lfmin", ri->lfrac->val[idxmin]);
}
regls_set_crit_data(ri);
}
if (!err) {
gretl_bundle_donate_data(ri->b, "B", B, GRETL_TYPE_MATRIX, 0);
B = NULL;
if (ri->lamscale != LAMSCALE_NONE) {
gretl_bundle_set_scalar(ri->b, "lmax", lmax * ri->n);
}
if (ri->nlam == 1) {
gretl_bundle_set_scalar(ri->b, "lambda", lam0);
// ri->lam->val[0] /= ri->n;
if (V != NULL) {
gretl_bundle_donate_data(ri->b, "vcv", V,
GRETL_TYPE_MATRIX, 0);
}
}
}
bailout:
gretl_matrix_free(B);
gretl_matrix_free(lam);
return err;
}
/* This function is executed when we want to obtain a set
of coefficients using the full training data, with either
a single value of lambda or a vector of lambdas. We come
here straight away if the user has not requested cross
validation; we also come here after cross validation.
*/
static int admm_lasso (regls_info *ri)
{
gretl_matrix_block *MB;
double BICmin = 1e200;
gretl_matrix *B = NULL;
double lmax, rho = ri->rho;
double llc = 0;
int k = ri->k;
int n = ri->n;
int i, j, ldim;
int jmin, jmax;
int idxmin = 0;
int err = 0;
gretl_vector *v, *u, *b, *r, *bprev, *bdiff;
gretl_vector *q, *n1;
gretl_matrix *L;
ldim = n >= k ? k : n;
MB = gretl_matrix_block_new(&v, k, 1, &u, k, 1,
&b, k, 1, &r, k, 1,
&bprev, k, 1, &bdiff, k, 1,
&q, k, 1, &n1, n, 1,
&L, ldim, ldim, NULL);
if (MB == NULL) {
return E_ALLOC;
}
gretl_matrix_block_zero(MB);
lmax = ri->infnorm;
if (!ri->xvalid && ri->verbose > 0) {
lasso_lambda_report(ri);
}
if (!err) {
get_cholesky_factor(ri->X, L, rho);
B = make_coeff_matrix(ri, &jmin, &jmax);
if (B == NULL) {
err = E_ALLOC;
}
}
if (err) {
gretl_matrix_block_destroy(MB);
return err;
}
if (!ri->xvalid && ri->verbose > 0) {
lambda_sequence_header(ri->prn);
llc = -0.5 * n * (1 + LN_2_PI - log(n));
}
for (j=jmin; j<jmax && !err; j++) {
/* loop across lambda values */
double critj, lambda = ri->lfrac->val[j] * lmax;
int tune_rho = 1;
int iters = 0;
int nnz = 0;
err = admm_iteration(ri->X, ri->Xty, L, v, b, u, q, n1, r,
bprev, bdiff, lambda, &rho, tune_rho,
&iters);
if (!err) {
for (i=0; i<k; i++) {
if (b->val[i] != 0.0) {
nnz++;
}
if (B->cols == 1) {
gretl_matrix_set(B, i + ri->stdize, 0, b->val[i]);
} else {
gretl_matrix_set(B, i + ri->stdize, j, b->val[i]);
}
}
if (!ri->xvalid) {
double R2, SSR, ll;
critj = lasso_objective(ri->X, ri->y, b, lambda, n1, &SSR, &R2);
ll = llc - 0.5 * n * log(SSR);
ri->BIC->val[j] = -2 * ll + nnz * log(n);
if (ri->verbose > 0) {
pprintf(ri->prn, "%12f %5d %f %.4f %#g\n",
lambda/n, nnz, critj, R2, ri->BIC->val[j]);
}
if (ri->BIC->val[j] < BICmin) {
BICmin = ri->BIC->val[j];
idxmin = j;
}
ri->crit->val[j] = critj;
}
}
}
gretl_bundle_set_scalar(ri->b, "lmax", lmax);
if (!ri->xvalid) {
if (ri->nlam > 1) {
gretl_bundle_set_scalar(ri->b, "idxmin", idxmin + 1);
gretl_bundle_set_scalar(ri->b, "lfmin", ri->lfrac->val[idxmin]);
}
regls_set_crit_data(ri);
}
if (ri->nlam == 1) {
gretl_bundle_set_scalar(ri->b, "lambda", ri->lfrac->val[0] * lmax);
}
gretl_matrix_block_destroy(MB);
return err;
}
static int admm_do_fold (const gretl_matrix *X,
const gretl_matrix *y,
const gretl_matrix *X_out,
const gretl_matrix *y_out,
const gretl_matrix *lfrac,
gretl_matrix *XVC,
double lmax, double rho0,
int fold)
{
static gretl_vector *v, *u, *b;
static gretl_vector *r, *bprev, *bdiff;
static gretl_vector *q, *Xty, *n1, *L;
static gretl_matrix_block *MB;
double rho = rho0;
int ldim, nlam;
int n, k, j;
int err = 0;
if (X == NULL) {
/* cleanup signal */
gretl_matrix_block_destroy(MB);
MB = NULL;
return 0;
}
nlam = gretl_vector_get_length(lfrac);
n = X->rows;
k = X->cols;
ldim = n >= k ? k : n;
if (MB == NULL) {
MB = gretl_matrix_block_new(&v, k, 1, &u, k, 1,
&b, k, 1, &r, k, 1,
&bprev, k, 1, &bdiff, k, 1,
&q, k, 1, &n1, n, 1,
&Xty, k, 1, &L, ldim, ldim,
NULL);
if (MB == NULL) {
return E_ALLOC;
}
gretl_matrix_block_zero(MB);
}
/* compute X'y for the estimation sample */
gretl_matrix_multiply_mod(X, GRETL_MOD_TRANSPOSE,
y, GRETL_MOD_NONE,
Xty, GRETL_MOD_NONE);
get_cholesky_factor(X, L, rho);
for (j=0; j<nlam && !err; j++) {
/* loop across lambda values */
double score, lambda = lfrac->val[j] * lmax;
int tune_rho = 1;
int iters = 0;
err = admm_iteration(X, Xty, L, v, b, u, q, n1, r, bprev, bdiff,
lambda, &rho, tune_rho, &iters);
if (!err) {
/* record out-of-sample criterion */
gretl_matrix_reuse(n1, X_out->rows, 1);
score = xv_score(X_out, y_out, b, n1);
gretl_matrix_reuse(n1, n, 1);
gretl_matrix_set(XVC, j, fold, score);
}
}
return err;
}
static int ccd_do_fold (gretl_matrix *X,
gretl_matrix *y,
gretl_matrix *X_out,
gretl_matrix *y_out,
const gretl_matrix *lam,
gretl_matrix *XVC,
int fold,
double alpha)
{
static gretl_matrix_block *MB;
static gretl_matrix *Xty, *xv;
static gretl_matrix *B;
static gretl_matrix *u;
static gretl_matrix *b;
static int *ia, *nnz;
int maxit = CCD_MAX_ITER;
int nlp = 0, lmu = 0;
int nlam, nout;
int k, j;
int err = 0;
if (X == NULL) {
/* cleanup signal */
gretl_matrix_block_destroy(MB);
MB = NULL;
free(ia);
ia = NULL;
return 0;
}
/* dimensions */
nlam = gretl_vector_get_length(lam);
nout = X_out->rows;
k = X->cols;
if (MB == NULL) {
MB = gretl_matrix_block_new(&xv, k, 1, &Xty, k, 1,
&B, k, nlam, &u, nout, 1,
&b, k, 1, NULL);
ia = malloc((k + nlam) * sizeof *ia);
if (MB == NULL || ia == NULL) {
return E_ALLOC;
}
nnz = ia + k;
}
gretl_matrix_zero(B);
#if LAMBDA_DEBUG
gretl_matrix_print(lam, "lam, in ccd_do_fold");
#endif
/* scale the estimation subset by sqrt(1/n) */
ccd_scale(X, y->val, Xty->val, xv->val);
err = ccd_iteration(alpha, X, Xty->val, nlam, lam->val,
ccd_toler, maxit, xv->val, &lmu, B,
ia, nnz, NULL, &nlp);
if (err) {
fprintf(stderr, "ccd_do_fold: ccd_iteration returned %d\n", err);
} else {
/* record out-of-sample criteria */
size_t bsize = k * sizeof(double);
double score;
for (j=0; j<nlam; j++) {
memcpy(b->val, B->val + j*k, bsize);
score = xv_score(X_out, y_out, b, u);
gretl_matrix_set(XVC, j, fold, score);
}
}
return err;
}
static int svd_do_fold (gretl_matrix *X,
gretl_matrix *y,
gretl_matrix *X_out,
gretl_matrix *y_out,
const gretl_matrix *lam,
gretl_matrix *XVC,
int fold,
gint8 lamscale)
{
static gretl_matrix_block *MB;
static gretl_matrix *B;
static gretl_matrix *u;
static gretl_matrix *b;
int nlam, nout;
int k, j;
int err = 0;
if (X == NULL) {
/* cleanup signal */
gretl_matrix_block_destroy(MB);
MB = NULL;
return 0;
}
nlam = gretl_vector_get_length(lam);
nout = X_out->rows;
k = X->cols;
if (MB == NULL) {
MB = gretl_matrix_block_new(&B, k, nlam, &u, nout, 1,
&b, k, 1, NULL);
if (MB == NULL) {
return E_ALLOC;
}
}
gretl_matrix_zero(B);
if (lamscale == LAMSCALE_GLMNET) {
/* scale the estimation sample by sqrt(1/n) */
ccd_scale(X, y->val, NULL, NULL);
}
err = svd_ridge_bhat(lam->val, nlam, X, y, B, NULL, NULL);
#if 0
fprintf(stderr, "svd: err=%d, nlp=%d, lmu=%d\n", err, nlp, lmu);
#endif
if (!err) {
/* record out-of-sample criteria */
size_t bsize = k * sizeof(double);
double score;
for (j=0; j<nlam; j++) {
memcpy(b->val, B->val + j*k, bsize);
score = xv_score(X_out, y_out, b, u);
gretl_matrix_set(XVC, j, fold, score);
}
}
return err;
}
/* Note: @X and @y are the full data matrices. @Xe and @ye will hold
the estimation sample, and @Xf and @yf will hold the data for
which prediction is to be performed. We need to be careful not to
write values out of bounds in case the two disjoint sub-samples do
not exhaust the full data (i.e. the full number of observations is
not divisible by the fold size without remainder).
*/
static void prepare_xv_data (const gretl_matrix *X,
const gretl_matrix *y,
gretl_matrix *Xe,
gretl_matrix *ye,
gretl_matrix *Xf,
gretl_matrix *yf,
int f)
{
int i, j, re, rf;
double xij;
for (j=0; j<X->cols; j++) {
re = rf = 0;
for (i=0; i<X->rows; i++) {
xij = gretl_matrix_get(X, i, j);
if (i/Xf->rows == f) {
/* "out of sample" range */
if (rf < Xf->rows) {
gretl_matrix_set(Xf, rf, j, xij);
if (j == 0) {
yf->val[rf] = y->val[i];
}
}
rf++;
} else {
/* estimation sample */
if (re < Xe->rows) {
gretl_matrix_set(Xe, re, j, xij);
if (j == 0) {
ye->val[re] = y->val[i];
}
}
re++;
}
}
}
}
/* Given @XVC holding criterion values per lambda (rows) and
per fold (columns), compose a matrix holding the means,
plus standard errors if wanted.
*/
static gretl_matrix *process_xv_criterion (gretl_matrix *XVC,
gretl_matrix *lfrac,
int *imin, int *i1se,
PRN *prn)
{
gretl_matrix *metrics;
double avg, d, v, se, se1, avgmin = 1e200;
int mcols = 2;
int nf = XVC->cols;
int i, j;
metrics = gretl_zero_matrix_new(XVC->rows, mcols);
if (metrics == NULL) {
return NULL;
}
*imin = 0;
for (i=0; i<XVC->rows; i++) {
v = avg = 0;
for (j=0; j<nf; j++) {
avg += gretl_matrix_get(XVC, i, j);
}
avg /= nf;
if (i == 0) {
avgmin = avg;
} else if (avg < avgmin) {
avgmin = avg;
*imin = i;
}
gretl_matrix_set(metrics, i, 0, avg);
for (j=0; j<nf; j++) {
d = gretl_matrix_get(XVC, i, j) - avg;
v += d * d;
}
v /= (nf - 1);
se = sqrt(v/nf);
gretl_matrix_set(metrics, i, 1, se);
}
*i1se = *imin;
/* estd. standard error of minimum average XVC */
se1 = gretl_matrix_get(metrics, *imin, 1);
/* Find the index of the largest lamba that gives
an average XVC within one standard error of the
minimum (glmnet's "$lambda.1se").
*/
for (i=*imin-1; i>=0; i--) {
avg = gretl_matrix_get(metrics, i, 0);
if (avg - avgmin < se1) {
*i1se = i;
} else {
break;
}
}
if (prn != NULL) {
int common = (*i1se == *imin);
pprintf(prn, " s %s se\n", "MSE");
for (i=0; i<XVC->rows; i++) {
avg = gretl_matrix_get(metrics, i, 0);
se = gretl_matrix_get(metrics, i, 1);
pprintf(prn, "%11f %10f %10f", lfrac->val[i], avg, se);
if (i == *imin && common) {
pputs(prn, " *+");
} else if (i == *imin) {
pputs(prn, " *");
} else if (i == *i1se) {
pputs(prn, " +");
}
pputc(prn, '\n');
}
}
return metrics;
}
/* Analyse and record results after cross-validation */
static int post_xvalidation_task (regls_info *ri,
gretl_matrix *XVC,
PRN *prn)
{
gretl_matrix *metrics;
int imin = 0, i1se = 0;
metrics = process_xv_criterion(XVC, ri->lfrac, &imin, &i1se, prn);
if (metrics == NULL) {
return E_ALLOC;
}
if (prn != NULL) {
pputs(prn, "\nNote: s = lambda/lambda-max\n");
pprintf(prn, "Average out-of-sample %s minimized at %#g for s=%#g (\"*\")\n",
"MSE", gretl_matrix_get(metrics, imin, 0), ri->lfrac->val[imin]);
pprintf(prn, "Largest s within one s.e. of best criterion: %#g (\"+\")\n",
ri->lfrac->val[i1se]);
}
gretl_bundle_donate_data(ri->b, "XVC", metrics, GRETL_TYPE_MATRIX, 0);
gretl_bundle_set_int(ri->b, "idxmin", imin + 1);
gretl_bundle_set_int(ri->b, "idx1se", i1se + 1);
gretl_bundle_set_scalar(ri->b, "lfmin", ri->lfrac->val[imin]);
gretl_bundle_set_scalar(ri->b, "lf1se", ri->lfrac->val[i1se]);
return 0;
}
/* called by the cross-validation driver functions, regls_xv()
and real_regls_xv_mpi(), for all algorithms: ADMM, CCD,
SVD.
*/
static double get_xvalidation_lmax (regls_info *ri, int esize)
{
double lmax = ri->infnorm;
#if LAMBDA_DEBUG
fprintf(stderr, "get_xvalidation_lmax: ri->infnorm = %g\n", lmax);
#endif
if (ri->ccd) {
if (ri->alpha < 1.0) {
lmax /= max(ri->alpha, 1.0e-3);
#if LAMBDA_DEBUG
fprintf(stderr, "revised lmax = %g\n", lmax);
#endif
}
} else if (ri->ridge && ri->lamscale == LAMSCALE_GLMNET) {
if (ri->alpha < 1.0) {
lmax /= max(ri->alpha, 1.0e-3);
}
} else if (ri->ridge && ri->lamscale == LAMSCALE_FROB) {
lmax = ri->X->cols; /* ?? */
}
return lmax;
}
static void xv_cleanup (regls_info *ri)
{
if (ri->ccd) {
ccd_do_fold(NULL, NULL, NULL, NULL, NULL, NULL, 0, 0);
} else if (ri->ridge) {
svd_do_fold(NULL, NULL, NULL, NULL, NULL, NULL, 0, 0);
} else {
admm_do_fold(NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
}
}
static gretl_matrix *make_xv_lambda (regls_info *ri,
double lmax,
int *err)
{
gretl_matrix *lam;
int i;
lam = gretl_matrix_copy(ri->lfrac);
if (lam == NULL) {
*err = E_ALLOC;
} else if (ri->lamscale != LAMSCALE_NONE) {
for (i=0; i<ri->nlam; i++) {
lam->val[i] *= lmax;
}
if (ri->alpha < 1 && ri->lamscale == LAMSCALE_GLMNET) {
lam->val[0] = BIG_LAMBDA;
}
}
return lam;
}
/* unified cross validation function, employed when we're
not doing MPI
*/
static int regls_xv (regls_info *ri)
{
PRN *prn = ri->prn;
gretl_matrix_block *XY;
gretl_matrix *Xe, *Xf;
gretl_matrix *ye, *yf;
gretl_matrix *lam = NULL;
gretl_matrix *XVC = NULL;
double lmax;
int f, fsize, esize;
int err = 0;
fsize = ri->n / ri->nf;
esize = (ri->nf - 1) * fsize;
if (ri->verbose) {
pprintf(prn, "regls_xv: nf=%d, fsize=%d, randfolds=%d, "
"ridge=%d, ccd=%d\n", ri->nf, fsize, ri->randfolds,
ri->ridge, ri->ccd);
gretl_flush(prn);
}
XY = gretl_matrix_block_new(&Xe, esize, ri->k,
&Xf, fsize, ri->k,
&ye, esize, 1,
&yf, fsize, 1, NULL);
if (XY == NULL) {
return E_ALLOC;
}
lmax = get_xvalidation_lmax(ri, esize);
if (ri->verbose) {
pprintf(prn, "cross-validation lmax = %g\n\n", lmax);
gretl_flush(prn);
}
if (ri->ccd || ri->ridge) {
lam = make_xv_lambda(ri, lmax, &err);
}
if (!err && ri->randfolds) {
/* scramble the row order of X and y */
randomize_rows(ri->X, ri->y);
}
if (!err) {
XVC = gretl_zero_matrix_new(ri->nlam, ri->nf);
if (XVC == NULL) {
err = E_ALLOC;
}
}
for (f=0; f<ri->nf && !err; f++) {
prepare_xv_data(ri->X, ri->y, Xe, ye, Xf, yf, f);
if (ri->ccd) {
err = ccd_do_fold(Xe, ye, Xf, yf, lam, XVC, f, ri->alpha);
} else if (ri->ridge) {
err = svd_do_fold(Xe, ye, Xf, yf, lam, XVC, f,
ri->lamscale);
} else {
err = admm_do_fold(Xe, ye, Xf, yf, ri->lfrac, XVC,
lmax, ri->rho, f);
}
}
/* send deallocation signal */
xv_cleanup(ri);
if (!err) {
PRN *myprn = ri->verbose ? prn : NULL;
err = post_xvalidation_task(ri, XVC, myprn);
if (!err) {
/* determine coefficient vector(s) on full training set */
if (ri->ccd) {
err = ccd_regls(ri);
} else if (ri->ridge) {
err = svd_ridge(ri);
} else {
err = admm_lasso(ri);
}
}
}
gretl_matrix_free(lam);
gretl_matrix_free(XVC);
gretl_matrix_block_destroy(XY);
return err;
}
#ifdef HAVE_MPI
static int real_regls_xv_mpi (regls_info *ri)
{
gretl_matrix_block *XY = NULL;
gretl_matrix *XVC = NULL;
gretl_matrix *Xe = NULL;
gretl_matrix *Xf = NULL;
gretl_matrix *ye = NULL;
gretl_matrix *yf = NULL;
gretl_matrix *lam = NULL;
double lmax;
int fsize, esize;
int folds_per;
int folds_rem;
int rank;
int np, rankmax = 0;
int f, r;
int my_f = 0;
int err = 0;
PRN *prn = ri->prn;
rank = gretl_mpi_rank();
np = gretl_mpi_n_processes();
rankmax = np - 1;
fsize = ri->n / ri->nf;
esize = (ri->nf - 1) * fsize;
folds_per = ri->nf / np;
folds_rem = ri->nf % np;
/* matrix-space for per-fold data */
XY = gretl_matrix_block_new(&Xe, esize, ri->k,
&Xf, fsize, ri->k,
&ye, esize, 1,
&yf, fsize, 1, NULL);
if (XY == NULL) {
return E_ALLOC;
}
if (rank == 0) {
lmax = get_xvalidation_lmax(ri, esize);
}
gretl_mpi_bcast(&lmax, GRETL_TYPE_DOUBLE, 0);
if (ri->randfolds) {
/* generate the same random folds in all processes */
unsigned seed;
if (rank == 0) {
if (gretl_bundle_has_key(ri->b, "seed")) {
seed = gretl_bundle_get_unsigned(ri->b, "seed", NULL);
} else {
seed = gretl_rand_get_seed();
}
}
gretl_mpi_bcast(&seed, GRETL_TYPE_UNSIGNED, 0);
gretl_rand_set_seed(seed);
randomize_rows(ri->X, ri->y);
}
if (rank < folds_rem) {
XVC = gretl_zero_matrix_new(ri->nlam, folds_per + 1);
} else {
XVC = gretl_zero_matrix_new(ri->nlam, folds_per);
}
if (XVC == NULL) {
err = E_ALLOC;
}
if (ri->ccd || ri->ridge) {
lam = make_xv_lambda(ri, lmax, &err);
}
if (rank == 0) {
if (ri->verbose) {
pprintf(prn, "regls_xv_mpi: nf=%d, fsize=%d, randfolds=%d\n\n",
ri->nf, fsize, ri->randfolds);
gretl_flush(prn);
}
}
/* process all folds */
r = 0;
for (f=0; f<ri->nf && !err; f++) {
if (rank == r) {
prepare_xv_data(ri->X, ri->y, Xe, ye, Xf, yf, f);
if (ri->verbose > 1) {
pprintf(ri->prn, "rank %d: taking fold %d\n", rank, f+1);
}
if (ri->ccd) {
err = ccd_do_fold(Xe, ye, Xf, yf, lam, XVC, my_f++,
ri->alpha);
} else if (ri->ridge) {
err = svd_do_fold(Xe, ye, Xf, yf, lam, XVC, my_f++,
ri->lamscale);
} else {
err = admm_do_fold(Xe, ye, Xf, yf, ri->lfrac, XVC, lmax,
ri->rho, my_f++);
}
}
if (r == rankmax) {
r = 0;
} else {
r++;
}
}
/* reduce @XVC to root by column concatenation */
gretl_matrix_mpi_reduce(XVC, &XVC, GRETL_MPI_HCAT, 0, OPT_NONE);
/* send deallocation signal, all processes */
xv_cleanup(ri);
if (rank == 0 && !err) {
PRN *myprn = ri->verbose ? prn : NULL;
err = post_xvalidation_task(ri, XVC, myprn);
if (!err) {
/* determine coefficient vector on full training set */
if (ri->ccd) {
err = ccd_regls(ri);
} else if (ri->ridge) {
err = svd_ridge(ri);
} else {
err = admm_lasso(ri);
}
}
}
gretl_matrix_free(lam);
gretl_matrix_free(XVC);
gretl_matrix_block_destroy(XY);
return err;
}
static int xv_use_mpi (regls_info *ri)
{
int no_mpi = gretl_bundle_get_bool(ri->b, "no_mpi", 0);
int ret = (no_mpi == 0);
/* It's not yet clear whether MPI is useful for the ridge case, or
for ccd in general. This may depend on the size of the data;
experimentation is needed!
*/
if (ret && (ri->ccd || ri->ridge)) {
ret = 0;
}
return ret;
}
#endif /* HAVE_MPI or not */
int gretl_regls (gretl_matrix *X,
gretl_matrix *y,
gretl_bundle *bun,
PRN *prn)
{
int (*regfunc) (regls_info *) = NULL;
regls_info *ri;
int err = 0;
ri = regls_info_new(X, y, bun, prn, &err);
if (err) {
fprintf(stderr, "err %d from regls_info_new\n", err);
return err;
}
if (ri->xvalid) {
#ifdef HAVE_MPI
if (xv_use_mpi(ri)) {
if (gretl_mpi_n_processes() > 1) {
regfunc = real_regls_xv_mpi;
} else if (auto_mpi_ok()) {
regfunc = mpi_parent_action;
}
}
#endif
if (regfunc == NULL) {
regfunc = regls_xv;
}
} else if (ri->ccd) {
regfunc = ccd_regls;
} else if (ri->ridge) {
regfunc = svd_ridge;
} else {
regfunc = admm_lasso;
}
#ifdef HAVE_MPI
if (regfunc != mpi_parent_action) {
err = regls_set_Xty(ri);
}
#else
err = regls_set_Xty(ri);
#endif
if (!err) {
err = regfunc(ri);
}
regls_info_free(ri);
return err;
}
#ifdef HAVE_MPI
/* We come here if a parent process has called our
automatic local MPI routine for cross validation:
this function will be executed by all gretlmpi
instances.
*/
int regls_xv_mpi (PRN *prn)
{
regls_info *ri = NULL;
gretl_bundle *bun = NULL;
gretl_matrix *X;
gretl_matrix *y;
int err = 0;
/* read matrices deposited by parent process */
X = gretl_matrix_read_from_file("regls_X.bin", 1, &err);
y = gretl_matrix_read_from_file("regls_y.bin", 1, &err);
if (!err) {
bun = gretl_bundle_read_from_file("regls_bun.xml", 1, &err);
}
if (!err) {
ri = regls_info_new(X, y, bun, prn, &err);
}
if (!err) {
err = regls_set_Xty(ri);
}
if (!err) {
err = real_regls_xv_mpi(ri);
if (!err && gretl_mpi_rank() == 0) {
/* write results, to be picked up by parent */
gretl_bundle_write_to_file(bun, "regls_XV_result.xml", 1);
}
}
gretl_matrix_free(X);
gretl_matrix_free(y);
gretl_bundle_destroy(bun);
regls_info_free(ri);
return err;
}
static int mpi_parent_action (regls_info *ri)
{
int err;
err = gretl_matrix_write_to_file(ri->X, "regls_X.bin", 1);
if (!err) {
err = gretl_matrix_write_to_file(ri->y, "regls_y.bin", 1);
}
if (!err) {
err = gretl_bundle_write_to_file(ri->b, "regls_bun.xml", 1);
}
if (!err) {
/* compose and execute MPI script */
err = foreign_start(MPI, NULL, OPT_NONE, ri->prn);
if (!err) {
int np = gretl_bundle_get_int(ri->b, "mpi_np", NULL);
int mpi_local = gretl_bundle_get_int(ri->b, "mpi_local", NULL);
gretlopt mpi_opt = OPT_S | OPT_Q;
if (np > 0) {
/* user-specified number of processes */
mpi_opt |= OPT_N;
set_optval_int(MPI, OPT_N, np);
}
if (mpi_local) {
/* local machine only */
mpi_opt |= OPT_L;
}
if (ri->verbose) {
pputs(ri->prn, "Invoking MPI...\n\n");
gretl_flush(ri->prn);
} else {
fprintf(stderr, "doing MPI\n");
}
foreign_append("_regls()", MPI);
err = foreign_execute(NULL, mpi_opt, ri->prn);
if (err) {
fprintf(stderr, "mpi_parent: foreign exec error %d\n", err);
}
}
}
if (!err) {
/* retrieve results bundle written by gretlmpi */
gretl_bundle *res;
res = gretl_bundle_read_from_file("regls_XV_result.xml", 1, &err);
if (!err) {
gretl_bundles_swap_content(ri->b, res);
gretl_bundle_destroy(res);
}
}
return err;
}
#endif /* HAVE_MPI */
|