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
|
################################################################################
# Copyright (C) 2011-2014 Jaakko Luttinen
#
# This file is licensed under the MIT License.
################################################################################
"""
Module for the Gaussian distribution and similar distributions.
"""
import numpy as np
from scipy import special
import truncnorm
from bayespy.utils import (random,
misc,
linalg)
from bayespy.utils.linalg import dot, mvdot
from .expfamily import (ExponentialFamily,
ExponentialFamilyDistribution,
useconstructor)
from .wishart import (WishartMoments,
WishartPriorMoments)
from .gamma import (GammaMoments,
GammaDistribution,
GammaPriorMoments)
from .deterministic import Deterministic
from .node import (Moments,
ensureparents)
#
# MOMENTS
#
class GaussianMoments(Moments):
r"""
Class for the moments of Gaussian variables.
"""
def __init__(self, shape):
self.shape = shape
self.ndim = len(shape)
self.dims = (shape, 2*shape)
super().__init__()
def compute_fixed_moments(self, x):
r"""
Compute the moments for a fixed value
"""
x = np.asanyarray(x)
x = misc.atleast_nd(x, self.ndim)
return [x, linalg.outer(x, x, ndim=self.ndim)]
@classmethod
def from_values(cls, x, ndim):
r"""
Return the shape of the moments for a fixed value.
"""
if ndim == 0:
return cls(())
else:
return cls(np.shape(x)[-ndim:])
def get_instance_conversion_kwargs(self):
return dict(ndim=self.ndim)
def get_instance_converter(self, ndim):
if ndim == self.ndim or ndim is None:
return None
return GaussianToGaussian(self, ndim)
class GaussianToGaussian():
def __init__(self, moments_from, ndim_to):
if not isinstance(moments_from, GaussianMoments):
raise ValueError()
if ndim_to < 0:
return ValueError("ndim_to must be non-negative")
self.shape_from = moments_from.shape
self.ndim_from = moments_from.ndim
self.ndim_to = ndim_to
if self.ndim_to > self.ndim_from:
raise ValueError()
if self.ndim_to == 0:
self.moments = GaussianMoments(())
else:
self.moments = GaussianMoments(self.shape_from[-self.ndim_to:])
return
def compute_moments(self, u):
if self.ndim_to == self.ndim_from:
return u
u0 = u[0]
u1 = misc.get_diag(u[1], ndim=self.ndim_from, ndim_to=self.ndim_to)
return [u0, u1]
def compute_message_to_parent(self, m, u_parent):
# Handle broadcasting in m_child
m0 = m[0] * np.ones(self.shape_from)
m1 = (
misc.make_diag(m[1], ndim=self.ndim_from, ndim_from=self.ndim_to)
* misc.identity(*self.shape_from)
)
return [m0, m1]
def compute_weights_to_parent(self, weights):
diff = self.ndim_from - self.ndim_to
if diff == 0:
return weights
return np.sum(
weights * np.ones(self.shape_from[:diff]),
#misc.atleast_nd(weights, diff),
axis=tuple(range(-diff, 0))
)
def plates_multiplier_from_parent(self, plates_multiplier):
diff = self.ndim_from - self.ndim_to
return plates_multiplier + diff * (1,)
def plates_from_parent(self, plates):
diff = self.ndim_from - self.ndim_to
if diff == 0:
return plates
return plates + self.shape_from[:diff]
def plates_to_parent(self, plates):
diff = self.ndim_from - self.ndim_to
if diff == 0:
return plates
return plates[:-diff]
class GaussianGammaMoments(Moments):
r"""
Class for the moments of Gaussian-gamma-ISO variables.
"""
def __init__(self, shape):
r"""
Create moments object for Gaussian-gamma isotropic variables
ndim=0: scalar
ndim=1: vector
ndim=2: matrix
...
"""
self.shape = shape
self.ndim = len(shape)
self.dims = (shape, 2*shape, (), ())
super().__init__()
def compute_fixed_moments(self, x_alpha):
r"""
Compute the moments for a fixed value
`x` is a mean vector.
`alpha` is a precision scale
"""
(x, alpha) = x_alpha
x = np.asanyarray(x)
alpha = np.asanyarray(alpha)
u0 = x * misc.add_trailing_axes(alpha, self.ndim)
u1 = (linalg.outer(x, x, ndim=self.ndim)
* misc.add_trailing_axes(alpha, 2*self.ndim))
u2 = np.copy(alpha)
u3 = np.log(alpha)
u = [u0, u1, u2, u3]
return u
@classmethod
def from_values(cls, x_alpha, ndim):
r"""
Return the shape of the moments for a fixed value.
"""
(x, alpha) = x_alpha
if ndim == 0:
shape = ( (), (), (), () )
else:
shape = np.shape(x)[-ndim:]
return cls(shape)
def get_instance_conversion_kwargs(self):
return dict(ndim=self.ndim)
def get_instance_converter(self, ndim):
# FIXME/TODO: IMPLEMENT THIS CORRECTLY!
if ndim != self.ndim:
raise NotImplementedError(
"Conversion to different ndim in GaussianMoments not yet "
"implemented."
)
return None
class GaussianWishartMoments(Moments):
r"""
Class for the moments of Gaussian-Wishart variables.
"""
def __init__(self, shape):
self.shape = shape
self.ndim = len(shape)
self.dims = ( shape, (), 2*shape, () )
super().__init__()
def compute_fixed_moments(self, x, Lambda):
r"""
Compute the moments for a fixed value
`x` is a vector.
`Lambda` is a precision matrix
"""
x = np.asanyarray(x)
Lambda = np.asanyarray(Lambda)
u0 = linalg.mvdot(Lambda, x, ndim=self.ndim)
u1 = np.einsum(
'...i,...ij,...j->...',
misc.flatten_axes(x, self.ndim),
misc.flatten_axes(Lambda, self.ndim, self.ndim),
misc.flatten_axes(x, self.ndim)
)
u2 = np.copy(Lambda)
u3 = linalg.logdet_cov(Lambda, ndim=self.ndim)
return [u0, u1, u2, u3]
@classmethod
def from_values(self, x, Lambda, ndim):
r"""
Return the shape of the moments for a fixed value.
"""
if ndim == 0:
return cls(())
else:
if np.ndim(x) < ndim:
raise ValueError("Mean must be a vector")
shape = np.shape(x)[-ndim:]
if np.shape(Lambda)[-2*ndim:] != shape + shape:
raise ValueError("Shapes inconsistent")
return cls(shape)
#
# DISTRIBUTIONS
#
class GaussianDistribution(ExponentialFamilyDistribution):
r"""
Class for the VMP formulas of Gaussian variables.
Currently, supports only vector variables.
Notes
-----
Message passing equations:
.. math::
\mathbf{x} &\sim \mathcal{N}(\boldsymbol{\mu}, \mathbf{\Lambda}),
.. math::
\mathbf{x},\boldsymbol{\mu} \in \mathbb{R}^{D},
\quad \mathbf{\Lambda} \in \mathbb{R}^{D \times D},
\quad \mathbf{\Lambda} \text{ symmetric positive definite}
.. math::
\log\mathcal{N}( \mathbf{x} | \boldsymbol{\mu}, \mathbf{\Lambda} )
&=
- \frac{1}{2} \mathbf{x}^{\mathrm{T}} \mathbf{\Lambda} \mathbf{x}
+ \mathbf{x}^{\mathrm{T}} \mathbf{\Lambda} \boldsymbol{\mu}
- \frac{1}{2} \boldsymbol{\mu}^{\mathrm{T}} \mathbf{\Lambda}
\boldsymbol{\mu}
+ \frac{1}{2} \log |\mathbf{\Lambda}|
- \frac{D}{2} \log (2\pi)
"""
def __init__(self, shape):
self.shape = shape
self.ndim = len(shape)
self.set_limits(None, None)
super().__init__()
def set_limits(self, minimum=None, maximum=None):
self.minimum = minimum
self.maximum = maximum
self.has_limits = minimum is not None or maximum is not None
return
def compute_message_to_parent(self, parent, index, u, u_mu_Lambda):
r"""
Compute the message to a parent node.
.. math::
\boldsymbol{\phi}_{\boldsymbol{\mu}} (\mathbf{x}, \mathbf{\Lambda})
&=
\left[ \begin{matrix}
\mathbf{\Lambda} \mathbf{x}
\\
- \frac{1}{2} \mathbf{\Lambda}
\end{matrix} \right]
\\
\boldsymbol{\phi}_{\mathbf{\Lambda}} (\mathbf{x}, \boldsymbol{\mu})
&=
\left[ \begin{matrix}
- \frac{1}{2} \mathbf{xx}^{\mathrm{T}}
+ \frac{1}{2} \mathbf{x}\boldsymbol{\mu}^{\mathrm{T}}
+ \frac{1}{2} \boldsymbol{\mu}\mathbf{x}^{\mathrm{T}}
- \frac{1}{2} \boldsymbol{\mu\mu}^{\mathrm{T}}
\\
\frac{1}{2}
\end{matrix} \right]
"""
if index == 0:
x = u[0]
xx = u[1]
m0 = x
m1 = -0.5
m2 = -0.5*xx
m3 = 0.5
return [m0, m1, m2, m3]
else:
raise ValueError("Index out of bounds")
def compute_phi_from_parents(self, u_mu_Lambda, mask=True):
r"""
Compute the natural parameter vector given parent moments.
.. math::
\boldsymbol{\phi} (\boldsymbol{\mu}, \mathbf{\Lambda})
&=
\left[ \begin{matrix}
\mathbf{\Lambda} \boldsymbol{\mu}
\\
- \frac{1}{2} \mathbf{\Lambda}
\end{matrix} \right]
"""
Lambda_mu = u_mu_Lambda[0]
Lambda = u_mu_Lambda[2]
return [Lambda_mu,
-0.5 * Lambda]
def compute_moments_and_cgf(self, phi, mask=True):
r"""
Compute the moments and :math:`g(\phi)`.
.. math::
\overline{\mathbf{u}} (\boldsymbol{\phi})
&=
\left[ \begin{matrix}
- \frac{1}{2} \boldsymbol{\phi}^{-1}_2 \boldsymbol{\phi}_1
\\
\frac{1}{4} \boldsymbol{\phi}^{-1}_2 \boldsymbol{\phi}_1
\boldsymbol{\phi}^{\mathrm{T}}_1 \boldsymbol{\phi}^{-1}_2
- \frac{1}{2} \boldsymbol{\phi}^{-1}_2
\end{matrix} \right]
\\
g_{\boldsymbol{\phi}} (\boldsymbol{\phi})
&=
\frac{1}{4} \boldsymbol{\phi}^{\mathrm{T}}_1 \boldsymbol{\phi}^{-1}_2
\boldsymbol{\phi}_1
+ \frac{1}{2} \log | -2 \boldsymbol{\phi}_2 |
"""
# TODO: Compute -2*phi[1] and simplify the formulas
L = linalg.chol(-2*phi[1], ndim=self.ndim)
k = np.shape(phi[0])[-1]
Cov = linalg.chol_inv(L, ndim=self.ndim)
mu = linalg.chol_solve(L, phi[0], ndim=self.ndim)
# G
g = (-0.5 * linalg.inner(mu, phi[0], ndim=self.ndim)
+ 0.5 * linalg.chol_logdet(L, ndim=self.ndim))
if self.has_limits:
if self.ndim != 1:
raise NotImplementedError("Limits for ndim!=1 not yet supported")
(p, u0, u1)= truncnorm.moments(
mu,
Cov,
self.minimum,
self.maximum,
2,
)
logp = np.log(p)
else:
u0 = mu
u1 = Cov + linalg.outer(u0, u0, ndim=self.ndim)
logp = 0
u = [u0, u1]
return (u, g - logp)
def compute_cgf_from_parents(self, u_mu_Lambda):
r"""
Compute :math:`\mathrm{E}_{q(p)}[g(p)]`
.. math::
g (\boldsymbol{\mu}, \mathbf{\Lambda})
&=
- \frac{1}{2} \operatorname{tr}(\boldsymbol{\mu\mu}^{\mathrm{T}}
\mathbf{\Lambda} )
+ \frac{1}{2} \log |\mathbf{\Lambda}|
"""
mu_Lambda_mu = u_mu_Lambda[1]
logdet_Lambda = u_mu_Lambda[3]
g = -0.5*mu_Lambda_mu + 0.5*logdet_Lambda
return g
def compute_fixed_moments_and_f(self, x, mask=True):
r"""
Compute the moments and :math:`f(x)` for a fixed value.
.. math::
\mathbf{u} (\mathbf{x})
&=
\left[ \begin{matrix}
\mathbf{x}
\\
\mathbf{xx}^{\mathrm{T}}
\end{matrix} \right]
\\
f(\mathbf{x})
&= - \frac{D}{2} \log(2\pi)
"""
k = np.shape(x)[-1]
u = [x, linalg.outer(x, x, ndim=self.ndim)]
f = -k/2*np.log(2*np.pi)
return (u, f)
def compute_gradient(self, g, u, phi):
r"""
Compute the standard gradient with respect to the natural parameters.
Gradient of the moments:
.. math::
\mathrm{d}\overline{\mathbf{u}} &=
\begin{bmatrix}
\frac{1}{2} \phi_2^{-1} \mathrm{d}\phi_2 \phi_2^{-1} \phi_1
- \frac{1}{2} \phi_2^{-1} \mathrm{d}\phi_1
\\
- \frac{1}{4} \phi_2^{-1} \mathrm{d}\phi_2 \phi_2^{-1} \phi_1 \phi_1^{\mathrm{T}} \phi_2^{-1}
- \frac{1}{4} \phi_2^{-1} \phi_1 \phi_1^{\mathrm{T}} \phi_2^{-1} \mathrm{d}\phi_2 \phi_2^{-1}
+ \frac{1}{2} \phi_2^{-1} \mathrm{d}\phi_2 \phi_2^{-1}
+ \frac{1}{4} \phi_2^{-1} \mathrm{d}\phi_1 \phi_1^{\mathrm{T}} \phi_2^{-1}
+ \frac{1}{4} \phi_2^{-1} \phi_1 \mathrm{d}\phi_1^{\mathrm{T}} \phi_2^{-1}
\end{bmatrix}
\\
&=
\begin{bmatrix}
2 (\overline{u}_2 - \overline{u}_1 \overline{u}_1^{\mathrm{T}}) \mathrm{d}\phi_2 \overline{u}_1
+ (\overline{u}_2 - \overline{u}_1 \overline{u}_1^{\mathrm{T}}) \mathrm{d}\phi_1
\\
u_2 d\phi_2 u_2 - 2 u_1 u_1^T d\phi_2 u_1 u_1^T
+ 2 (u_2 - u_1 u_1^T) d\phi_1 u_1^T
\end{bmatrix}
Standard gradient given the gradient with respect to the moments, that
is, given the Riemannian gradient :math:`\tilde{\nabla}`:
.. math::
\nabla =
\begin{bmatrix}
(\overline{u}_2 - \overline{u}_1 \overline{u}_1^{\mathrm{T}}) \tilde{\nabla}_1
+ 2 (u_2 - u_1 u_1^T) \tilde{\nabla}_2 u_1
\\
(u_2 - u_1 u_1^T) \tilde{\nabla}_1 u_1^T
+ u_1 \tilde{\nabla}_1^T (u_2 - u_1 u_1^T)
+ 2 u_2 \tilde{\nabla}_2 u_2
- 2 u_1 u_1^T \tilde{\nabla}_2 u_1 u_1^T
\end{bmatrix}
"""
ndim = 1
x = u[0]
xx = u[1]
# Some helpful variables
x_x = linalg.outer(x, x, ndim=self.ndim)
Cov = xx - x_x
cov_g0 = linalg.mvdot(Cov, g[0], ndim=self.ndim)
cov_g0_x = linalg.outer(cov_g0, x, ndim=self.ndim)
g1_x = linalg.mvdot(g[1], x, ndim=self.ndim)
# Compute gradient terms
d0 = cov_g0 + 2 * linalg.mvdot(Cov, g1_x, ndim=self.ndim)
d1 = (cov_g0_x + linalg.transpose(cov_g0_x, ndim=self.ndim)
+ 2 * linalg.mmdot(xx,
linalg.mmdot(g[1], xx, ndim=self.ndim),
ndim=self.ndim)
- 2 * x_x * misc.add_trailing_axes(linalg.inner(g1_x,
x,
ndim=self.ndim),
2*self.ndim))
return [d0, d1]
def random(self, *phi, plates=None):
r"""
Draw a random sample from the distribution.
"""
# TODO/FIXME: You shouldn't draw random values for
# observed/fixed elements!
# Note that phi[1] is -0.5*inv(Cov)
U = linalg.chol(-2*phi[1], ndim=self.ndim)
mu = linalg.chol_solve(U, phi[0], ndim=self.ndim)
shape = plates + self.shape
z = np.random.randn(*shape)
# Denote Lambda = -2*phi[1]
# Then, Cov = inv(Lambda) = inv(U'*U) = inv(U) * inv(U')
# Thus, compute mu + U\z
z = linalg.solve_triangular(U, z, trans='N', lower=False, ndim=self.ndim)
return mu + z
class GaussianARDDistribution(ExponentialFamilyDistribution):
r"""
...
Log probability density function:
.. math::
\log p(x|\mu, \alpha) = -\frac{1}{2} x^T \mathrm{diag}(\alpha) x + x^T
\mathrm{diag}(\alpha) \mu - \frac{1}{2} \mu^T \mathrm{diag}(\alpha) \mu
+ \frac{1}{2} \sum_i \log \alpha_i - \frac{D}{2} \log(2\pi)
Parent has moments:
.. math::
\begin{bmatrix}
\alpha \circ \mu
\\
\alpha \circ \mu \circ \mu
\\
\alpha
\\
\log(\alpha)
\end{bmatrix}
"""
def __init__(self, shape):
self.shape = shape
self.ndim = len(shape)
super().__init__()
def compute_message_to_parent(self, parent, index, u, u_mu_alpha):
r"""
...
.. math::
m =
\begin{bmatrix}
x
\\
[-\frac{1}{2}, \ldots, -\frac{1}{2}]
\\
-\frac{1}{2} \mathrm{diag}(xx^T)
\\
[\frac{1}{2}, \ldots, \frac{1}{2}]
\end{bmatrix}
"""
if index == 0:
x = u[0]
x2 = misc.get_diag(u[1], ndim=self.ndim)
m0 = x
m1 = -0.5 * np.ones(self.shape)
m2 = -0.5 * x2
m3 = 0.5 * np.ones(self.shape)
return [m0, m1, m2, m3]
else:
raise ValueError("Invalid parent index")
def compute_weights_to_parent(self, index, weights):
r"""
Maps the mask to the plates of a parent.
"""
if index != 0:
raise IndexError()
return misc.add_trailing_axes(weights, self.ndim)
def compute_phi_from_parents(self, u_mu_alpha, mask=True):
alpha_mu = u_mu_alpha[0]
alpha = u_mu_alpha[2]
#mu = u_mu[0]
#alpha = u_alpha[0]
## if np.ndim(mu) < self.ndim_mu:
## raise ValueError("Moment of mu does not have enough dimensions")
## mu = misc.add_axes(mu,
## axis=np.ndim(mu)-self.ndim_mu,
## num=self.ndim-self.ndim_mu)
phi0 = alpha_mu
phi1 = -0.5 * alpha
if self.ndim > 0:
# Ensure that phi is not using broadcasting for variable
# dimension axes
ones = np.ones(self.shape)
phi0 = ones * phi0
phi1 = ones * phi1
# Make a diagonal matrix
phi1 = misc.diag(phi1, ndim=self.ndim)
return [phi0, phi1]
def compute_moments_and_cgf(self, phi, mask=True):
if self.ndim == 0:
# Use scalar equations
u0 = -phi[0] / (2*phi[1])
u1 = u0**2 - 1 / (2*phi[1])
u = [u0, u1]
g = (-0.5 * u[0] * phi[0] + 0.5 * np.log(-2*phi[1]))
# TODO/FIXME: You could use these equations if phi is a scalar
# in practice although ndim>0 (because the shape can be, e.g.,
# (1,1,1,1) for ndim=4).
else:
# Reshape to standard vector and matrix
D = np.prod(self.shape)
phi0 = np.reshape(phi[0], phi[0].shape[:-self.ndim] + (D,))
phi1 = np.reshape(phi[1], phi[1].shape[:-2*self.ndim] + (D,D))
# Compute the moments
L = linalg.chol(-2*phi1)
Cov = linalg.chol_inv(L)
u0 = linalg.chol_solve(L, phi0)
u1 = linalg.outer(u0, u0) + Cov
# Compute CGF
g = (- 0.5 * np.einsum('...i,...i', u0, phi0)
+ 0.5 * linalg.chol_logdet(L))
# Reshape to arrays
u0 = np.reshape(u0, u0.shape[:-1] + self.shape)
u1 = np.reshape(u1, u1.shape[:-2] + self.shape + self.shape)
u = [u0, u1]
return (u, g)
def compute_cgf_from_parents(self, u_mu_alpha):
r"""
Compute the value of the cumulant generating function.
"""
# Compute sum(mu^2 * alpha) correctly for broadcasted shapes
alpha_mu2 = u_mu_alpha[1]
logdet_alpha = u_mu_alpha[3]
axes = tuple(range(-self.ndim, 0))
# TODO/FIXME: You could use plate multiplier type of correction instead
# of explicitly broadcasting with ones.
if self.ndim > 0:
alpha_mu2 = misc.sum_multiply(alpha_mu2, np.ones(self.shape),
axis=axes)
if self.ndim > 0:
logdet_alpha = misc.sum_multiply(logdet_alpha, np.ones(self.shape),
axis=axes)
# Compute g
g = -0.5*alpha_mu2 + 0.5*logdet_alpha
return g
def compute_fixed_moments_and_f(self, x, mask=True):
r""" Compute u(x) and f(x) for given x. """
if self.ndim > 0 and np.shape(x)[-self.ndim:] != self.shape:
raise ValueError("Invalid shape")
k = np.prod(self.shape)
u = [x, linalg.outer(x, x, ndim=self.ndim)]
f = -k/2*np.log(2*np.pi)
return (u, f)
def plates_to_parent(self, index, plates):
r"""
Resolves the plate mapping to a parent.
Given the plates of the node's moments, this method returns the plates
that the message to a parent has for the parent's distribution.
"""
if index != 0:
raise IndexError()
return plates + self.shape
def plates_from_parent(self, index, plates):
r"""
Resolve the plate mapping from a parent.
Given the plates of a parent's moments, this method returns the plates
that the moments has for this distribution.
"""
if index != 0:
raise IndexError()
if self.ndim == 0:
return plates
else:
return plates[:-self.ndim]
def random(self, *phi, plates=None):
r"""
Draw a random sample from the Gaussian distribution.
"""
# TODO/FIXME: You shouldn't draw random values for
# observed/fixed elements!
D = self.ndim
if D == 0:
dims = ()
else:
dims = np.shape(phi[0])[-D:]
if np.prod(dims) == 1.0:
# Scalar Gaussian
phi1 = phi[1]
if D > 0:
# Because the covariance matrix has shape (1,1,...,1,1),
# that is 2*D number of ones, remove the extra half of the
# shape
phi1 = np.reshape(phi1, np.shape(phi1)[:-2*D] + D*(1,))
var = -0.5 / phi1
std = np.sqrt(var)
mu = var * phi[0]
shape = plates + dims
z = np.random.randn(*shape)
x = mu + std * z
else:
N = np.prod(dims)
dims_cov = dims + dims
# Reshape precision matrix
plates_cov = np.shape(phi[1])[:-2*D]
V = -2 * np.reshape(phi[1], plates_cov + (N,N))
# Compute Cholesky
U = linalg.chol(V)
# Reshape mean vector
plates_phi0 = np.shape(phi[0])[:-D]
phi0 = np.reshape(phi[0], plates_phi0 + (N,))
mu = linalg.chol_solve(U, phi0)
# Compute mu + U\z
shape = plates + (N,)
z = np.random.randn(*shape)
# Denote Lambda = -2*phi[1]
# Then, Cov = inv(Lambda) = inv(U'*U) = inv(U) * inv(U')
# Thus, compute mu + U\z
x = mu + linalg.solve_triangular(U, z,
trans='N',
lower=False)
x = np.reshape(x, plates + dims)
return x
def compute_gradient(self, g, u, phi):
r"""
Compute the standard gradient with respect to the natural parameters.
Gradient of the moments:
.. math::
\mathrm{d}\overline{\mathbf{u}} &=
\begin{bmatrix}
\frac{1}{2} \phi_2^{-1} \mathrm{d}\phi_2 \phi_2^{-1} \phi_1
- \frac{1}{2} \phi_2^{-1} \mathrm{d}\phi_1
\\
- \frac{1}{4} \phi_2^{-1} \mathrm{d}\phi_2 \phi_2^{-1} \phi_1 \phi_1^{\mathrm{T}} \phi_2^{-1}
- \frac{1}{4} \phi_2^{-1} \phi_1 \phi_1^{\mathrm{T}} \phi_2^{-1} \mathrm{d}\phi_2 \phi_2^{-1}
+ \frac{1}{2} \phi_2^{-1} \mathrm{d}\phi_2 \phi_2^{-1}
+ \frac{1}{4} \phi_2^{-1} \mathrm{d}\phi_1 \phi_1^{\mathrm{T}} \phi_2^{-1}
+ \frac{1}{4} \phi_2^{-1} \phi_1 \mathrm{d}\phi_1^{\mathrm{T}} \phi_2^{-1}
\end{bmatrix}
\\
&=
\begin{bmatrix}
2 (\overline{u}_2 - \overline{u}_1 \overline{u}_1^{\mathrm{T}}) \mathrm{d}\phi_2 \overline{u}_1
+ (\overline{u}_2 - \overline{u}_1 \overline{u}_1^{\mathrm{T}}) \mathrm{d}\phi_1
\\
u_2 d\phi_2 u_2 - 2 u_1 u_1^T d\phi_2 u_1 u_1^T
+ 2 (u_2 - u_1 u_1^T) d\phi_1 u_1^T
\end{bmatrix}
Standard gradient given the gradient with respect to the moments, that
is, given the Riemannian gradient :math:`\tilde{\nabla}`:
.. math::
\nabla =
\begin{bmatrix}
(\overline{u}_2 - \overline{u}_1 \overline{u}_1^{\mathrm{T}}) \tilde{\nabla}_1
+ 2 (u_2 - u_1 u_1^T) \tilde{\nabla}_2 u_1
\\
(u_2 - u_1 u_1^T) \tilde{\nabla}_1 u_1^T
+ u_1 \tilde{\nabla}_1^T (u_2 - u_1 u_1^T)
+ 2 u_2 \tilde{\nabla}_2 u_2
- 2 u_1 u_1^T \tilde{\nabla}_2 u_1 u_1^T
\end{bmatrix}
"""
ndim = self.ndim
x = u[0]
xx = u[1]
# Some helpful variables
x_x = linalg.outer(x, x, ndim=ndim)
Cov = xx - x_x
cov_g0 = linalg.mvdot(Cov, g[0], ndim=ndim)
cov_g0_x = linalg.outer(cov_g0, x, ndim=ndim)
g1_x = linalg.mvdot(g[1], x, ndim=ndim)
# Compute gradient terms
d0 = cov_g0 + 2 * linalg.mvdot(Cov, g1_x, ndim=ndim)
d1 = (cov_g0_x + linalg.transpose(cov_g0_x, ndim=ndim)
+ 2 * linalg.mmdot(xx,
linalg.mmdot(g[1], xx, ndim=ndim),
ndim=ndim)
- 2 * x_x * misc.add_trailing_axes(linalg.inner(g1_x,
x,
ndim=ndim),
2*ndim))
return [d0, d1]
class GaussianGammaDistribution(ExponentialFamilyDistribution):
r"""
Class for the VMP formulas of Gaussian-Gamma-ISO variables.
Currently, supports only vector variables.
Log pdf of the prior:
.. math::
\log p(\mathbf{x}, \tau | \boldsymbol{\mu}, \mathbf{\Lambda}, a, b) =&
- \frac{1}{2} \tau \mathbf{x}^T \mathbf{\Lambda} \mathbf{x}
+ \frac{1}{2} \tau \mathbf{x}^T \mathbf{\Lambda} \boldsymbol{\mu}
+ \frac{1}{2} \tau \boldsymbol{\mu}^T \mathbf{\Lambda} \mathbf{x}
- \frac{1}{2} \tau \boldsymbol{\mu}^T \mathbf{\Lambda} \boldsymbol{\mu}
+ \frac{1}{2} \log|\mathbf{\Lambda}|
+ \frac{D}{2} \log\tau
- \frac{D}{2} \log(2\pi)
\\ &
- b \tau
+ a \log\tau
- \log\tau
+ a \log b
- \log \Gamma(a)
Log pdf of the posterior approximation:
.. math::
\log q(\mathbf{x}, \tau) =&
\tau \mathbf{x}^T \boldsymbol{\phi}_1
+ \tau \mathbf{x}^T \mathbf{\Phi}_2 \mathbf{x}
+ \tau \phi_3
+ \log\tau \phi_4
+ g(\boldsymbol{\phi}_1, \mathbf{\Phi}_2, \phi_3, \phi_4)
+ f(x, \tau)
"""
def __init__(self, shape):
self.shape = shape
self.ndim = len(shape)
super().__init__()
def compute_message_to_parent(self, parent, index, u, u_mu_Lambda, u_a, u_b):
r"""
Compute the message to a parent node.
- Parent :math:`(\boldsymbol{\mu}, \mathbf{\Lambda})`
Moments:
.. math::
\begin{bmatrix}
\mathbf{\Lambda}\boldsymbol{\mu}
\\
\boldsymbol{\mu}^T\mathbf{\Lambda}\boldsymbol{\mu}
\\
\mathbf{\Lambda}
\\
\log|\mathbf{\Lambda}|
\end{bmatrix}
Message:
.. math::
\begin{bmatrix}
\langle \tau \mathbf{x} \rangle
\\
- \frac{1}{2} \langle \tau \rangle
\\
- \frac{1}{2} \langle \tau \mathbf{xx}^T \rangle
\\
\frac{1}{2}
\end{bmatrix}
- Parent :math:`a`:
Moments:
.. math::
\begin{bmatrix}
a
\\
\log \Gamma(a)
\end{bmatrix}
Message:
.. math::
\begin{bmatrix}
\langle \log\tau \rangle + \langle \log b \rangle
\\
-1
\end{bmatrix}
- Parent :math:`b`:
Moments:
.. math::
\begin{bmatrix}
b
\\
\log b
\end{bmatrix}
Message:
.. math::
\begin{bmatrix}
- \langle \tau \rangle
\\
\langle a \rangle
\end{bmatrix}
"""
x_tau = u[0]
xx_tau = u[1]
tau = u[2]
logtau = u[3]
if index == 0:
m0 = x_tau
m1 = -0.5 * tau
m2 = -0.5 * xx_tau
m3 = 0.5
return [m0, m1, m2, m3]
elif index == 1:
logb = u_b[1]
m0 = logtau + logb
m1 = -1
return [m0, m1]
elif index == 2:
a = u_a[0]
m0 = -tau
m1 = a
return [m0, m1]
else:
raise ValueError("Index out of bounds")
def compute_phi_from_parents(self, u_mu_Lambda, u_a, u_b, mask=True):
r"""
Compute the natural parameter vector given parent moments.
"""
Lambda_mu = u_mu_Lambda[0]
mu_Lambda_mu = u_mu_Lambda[1]
Lambda = u_mu_Lambda[2]
a = u_a[0]
b = u_b[0]
phi = [Lambda_mu,
-0.5*Lambda,
-0.5*mu_Lambda_mu - b,
a]
return phi
def compute_moments_and_cgf(self, phi, mask=True):
r"""
Compute the moments and :math:`g(\phi)`.
"""
# Compute helpful variables
V = -2*phi[1]
L_V = linalg.chol(V, ndim=self.ndim)
logdet_V = linalg.chol_logdet(L_V, ndim=self.ndim)
mu = linalg.chol_solve(L_V, phi[0], ndim=self.ndim)
Cov = linalg.chol_inv(L_V, ndim=self.ndim)
a = phi[3]
b = -phi[2] - 0.5 * linalg.inner(mu, phi[0], ndim=self.ndim)
log_b = np.log(b)
# Compute moments
u2 = a / b
u3 = -log_b + special.psi(a)
u0 = mu * misc.add_trailing_axes(u2, self.ndim)
u1 = Cov + (
linalg.outer(mu, mu, ndim=self.ndim)
* misc.add_trailing_axes(u2, 2 * self.ndim)
)
u = [u0, u1, u2, u3]
# Compute g
g = 0.5*logdet_V + a*log_b - special.gammaln(a)
return (u, g)
def compute_cgf_from_parents(self, u_mu_Lambda, u_a, u_b):
r"""
Compute :math:`\mathrm{E}_{q(p)}[g(p)]`
"""
logdet_Lambda = u_mu_Lambda[3]
a = u_a[0]
gammaln_a = u_a[1]
log_b = u_b[1]
g = 0.5*logdet_Lambda + a*log_b - gammaln_a
return g
def compute_fixed_moments_and_f(self, x_alpha, mask=True):
r"""
Compute the moments and :math:`f(x)` for a fixed value.
"""
(x, alpha) = x_alpha
logalpha = np.log(alpha)
u0 = x * misc.add_trailing_axes(alpha, self.ndim)
u1 = linalg.outer(x, x, ndim=self.ndim) * misc.add_trailing_axes(alpha, 2*self.ndim)
u2 = alpha
u3 = logalpha
u = [u0, u1, u2, u3]
if self.ndim > 0:
D = np.prod(np.shape(x)[-self.ndim:])
else:
D = 1
f = (D/2 - 1) * logalpha - D/2 * np.log(2*np.pi)
return (u, f)
def random(self, *phi, plates=None):
r"""
Draw a random sample from the distribution.
"""
# TODO/FIXME: This is incorrect, I think. Gamma distribution parameters
# aren't directly those, because phi has some parts from the Gaussian
# distribution.
alpha = GammaDistribution().random(
phi[2],
phi[3],
plates=plates
)
mu = GaussianARDDistribution(self.shape).random(
misc.add_trailing_axes(alpha, self.ndim) * phi[0],
misc.add_trailing_axes(alpha, 2*self.ndim) * phi[1],
plates=plates
)
return (mu, alpha)
class GaussianWishartDistribution(ExponentialFamilyDistribution):
r"""
Class for the VMP formulas of Gaussian-Wishart variables.
Currently, supports only vector variables.
.. math::
\log p(\mathbf{x}, \mathbf{\Lambda} | \boldsymbol{\mu},
\alpha, n, \mathbf{V})
=&
- \frac{1}{2} \alpha \mathbf{x}^T \mathbf{\Lambda} \mathbf{x}
+ \frac{1}{2} \alpha \mathbf{x}^T \mathbf{\Lambda} \boldsymbol{\mu}
+ \frac{1}{2} \alpha \boldsymbol{\mu}^T \mathbf{\Lambda} \mathbf{x}
- \frac{1}{2} \alpha \boldsymbol{\mu}^T \mathbf{\Lambda} \boldsymbol{\mu}
+ \frac{1}{2} \log|\mathbf{\Lambda}|
+ \frac{D}{2} \log\alpha
- \frac{D}{2} \log(2\pi)
\\ &
- \frac{1}{2} \mathrm{tr}(\mathbf{V}\mathbf{\Lambda})
+ \frac{n-d-1}{2} \log|\mathbf{\Lambda}|
- \frac{nd}{2}\log 2
- \frac{n}{2} \log|\mathbf{V}|
- \log\Gamma_d(\frac{n}{2})
Posterior approximation:
.. math::
\log q(\mathbf{x}, \mathbf{\Lambda})
=&
\mathbf{x}^T \mathbf{\Lambda} \boldsymbol{\phi}_1
+ \phi_2 \mathbf{x}^T \mathbf{\Lambda} \mathbf{x}
+ \mathrm{tr}(\mathbf{\Lambda} \mathbf{\Phi}_3)
+ \phi_4 \log|\mathbf{\Lambda}|
+ g(\boldsymbol{\phi}_1, \phi_2, \mathbf{\Phi}_3, \phi_4)
+ f(\mathbf{x}, \mathbf{\Lambda})
"""
def compute_message_to_parent(self, parent, index, u, u_mu_alpha, u_n, u_V):
r"""
Compute the message to a parent node.
For parent :math:`q(\boldsymbol{\mu}, \alpha)`:
.. math::
\alpha \boldsymbol{\mu}^T \mathbf{m}_1
\Rightarrow &
\mathbf{m}_1 = \langle \mathbf{\Lambda x} \rangle
\\
\alpha \boldsymbol{\mu}^T \mathbf{M}_2 \boldsymbol{\mu}
\Rightarrow &
\mathbf{M}_2 = - \frac{1}{2} \langle \mathbf{\Lambda} \rangle
\\
\alpha m_3
\Rightarrow &
m_3 = - \frac{1}{2} \langle \mathbf{x}^T \mathbf{\Lambda} \mathbf{x} \rangle
\\
m_4 \log \alpha
\Rightarrow &
m_4 = \frac{d}{2}
For parent :math:`q(\mathbf{V})`:
.. math::
\mathbf{M}_1 &= \frac{\partial \langle \log p \rangle}{\partial
\langle \mathbf{V} \rangle} = -\frac{1}{2} \langle \mathbf{\Lambda} \rangle
\\
\mathbf{M}_2 &= \frac{\partial \langle \log p \rangle}{\partial \langle \log|\mathbf{V}| \rangle}
= ...
"""
if index == 0:
m0
m1
m2
m3
raise NotImplementedError()
elif index == 1:
raise NotImplementedError()
elif index == 2:
raise NotImplementedError()
else:
raise ValueError("Index out of bounds")
def compute_phi_from_parents(self, u_mu_alpha, u_n, u_V, mask=True):
r"""
Compute the natural parameter vector given parent moments.
"""
alpha_mu = u_mu_alpha[0]
alpha_mumu = u_mu_alpha[1]
alpha = u_mu_alpha[2]
V = u_V[0]
n = u_n[0]
phi0 = alpha_mu
phi1 = -0.5 * alpha
phi2 = -0.5 * (V + alpha_mumu)
phi3 = 0.5 * n
return [phi0, phi1, phi2, phi3]
def compute_moments_and_cgf(self, phi, mask=True):
r"""
Compute the moments and :math:`g(\phi)`.
"""
# TODO/FIXME: This isn't probably correct. Phi[2:] has terms that are
# related to the Gaussian also, not only Wishart.
u_Lambda = WishartDistribution((D,)).compute_moments_and_cgf(phi[2:])
raise NotImplementedError()
return (u, g)
def compute_cgf_from_parents(self, u_mu_alpha, u_n, u_V):
r"""
Compute :math:`\mathrm{E}_{q(p)}[g(p)]`
"""
raise NotImplementedError()
return g
def compute_fixed_moments_and_f(self, x, Lambda, mask=True):
r"""
Compute the moments and :math:`f(x)` for a fixed value.
"""
raise NotImplementedError()
return (u, f)
def random(self, *params, plates=None):
r"""
Draw a random sample from the distribution.
"""
raise NotImplementedError()
#
# NODES
#
class _GaussianTemplate(ExponentialFamily):
def translate(self, b, debug=False):
"""
Transforms the current posterior by adding a bias to the mean
Parameters
----------
b : array
Constant to add
"""
ndim = len(self.dims[0])
if ndim > 0 and np.shape(b)[-ndim:] != self.dims[0]:
raise ValueError("Bias has incorrect shape")
x = self.u[0]
xb = linalg.outer(x, b, ndim=ndim)
bx = linalg.transpose(xb, ndim=ndim)
bb = linalg.outer(b, b, ndim=ndim)
uh = [
self.u[0] + b,
self.u[1] + xb + bx + bb
]
Lambda = -2 * self.phi[1]
Lambda_b = linalg.mvdot(Lambda, b, ndim=ndim)
dg = -0.5 * (
linalg.inner(b, Lambda_b, ndim=ndim)
+ 2 * linalg.inner(x, Lambda_b, ndim=ndim)
)
phih = [
self.phi[0] + Lambda_b,
self.phi[1]
]
self._check_shape(uh)
self._check_shape(phih)
self.u = uh
self.phi = phih
self.g = self.g + dg
# TODO: This is all just debugging stuff and can be removed
if debug:
uh = [ui.copy() for ui in uh]
gh = self.g.copy()
self._update_moments_and_cgf()
if any(not np.allclose(uih, ui, atol=1e-6) for (uih, ui) in zip(uh, self.u)):
raise RuntimeError("BUG")
if not np.allclose(self.g, gh, atol=1e-6):
raise RuntimeError("BUG")
return
class Gaussian(_GaussianTemplate):
r"""
Node for Gaussian variables.
The node represents a :math:`D`-dimensional vector from the Gaussian
distribution:
.. math::
\mathbf{x} &\sim \mathcal{N}(\boldsymbol{\mu}, \mathbf{\Lambda}),
where :math:`\boldsymbol{\mu}` is the mean vector and
:math:`\mathbf{\Lambda}` is the precision matrix (i.e., inverse of the
covariance matrix).
.. math::
\mathbf{x},\boldsymbol{\mu} \in \mathbb{R}^{D},
\quad \mathbf{\Lambda} \in \mathbb{R}^{D \times D},
\quad \mathbf{\Lambda} \text{ symmetric positive definite}
Parameters
----------
mu : Gaussian-like node or GaussianGamma-like node or GaussianWishart-like node or array
Mean vector
Lambda : Wishart-like node or array
Precision matrix
See also
--------
Wishart, GaussianARD, GaussianWishart, GaussianGamma
"""
def __init__(self, mu, Lambda, **kwargs):
r"""
Create Gaussian node
"""
super().__init__(mu, Lambda, **kwargs)
@classmethod
def _constructor(cls, mu, Lambda, ndim=1, **kwargs):
r"""
Constructs distribution and moments objects.
"""
mu_Lambda = WrapToGaussianWishart(mu, Lambda, ndim=ndim)
shape = mu_Lambda._moments.shape
moments = GaussianMoments(shape)
parent_moments = (mu_Lambda._moments,)
if mu_Lambda.dims != ( shape, (), shape+shape, () ):
raise Exception("Parents have wrong dimensionality")
distribution = GaussianDistribution(shape)
parents = [mu_Lambda]
return (parents,
kwargs,
moments.dims,
cls._total_plates(kwargs.get('plates'),
distribution.plates_from_parent(0, mu_Lambda.plates)),
distribution,
moments,
parent_moments)
def initialize_from_parameters(self, mu, Lambda):
u = self._parent_moments[0].compute_fixed_moments(mu, Lambda)
self._initialize_from_parent_moments(u)
def observe_limits(self, minimum=-np.inf, maximum=np.inf):
self._distribution.set_limits(minimum, maximum)
self._update_mask()
return
def _set_mask(self, mask):
self.mask = np.logical_or(
mask,
np.logical_or(
self.observed,
self._distribution.has_limits,
),
)
def __str__(self):
ndim = len(self.dims[0])
mu = self.u[0]
Cov = self.u[1] - linalg.outer(mu, mu, ndim=ndim)
return ("%s ~ Gaussian(mu, Cov)\n"
" mu = \n"
"%s\n"
" Cov = \n"
"%s\n"
% (self.name, mu, Cov))
def rotate(self, R, inv=None, logdet=None, Q=None):
# TODO/FIXME: Combine and refactor all these rotation transformations
# into _GaussianTemplate
if self._moments.ndim != 1:
raise NotImplementedError("Not implemented for ndim!=1 yet")
if inv is not None:
invR = inv
else:
invR = np.linalg.inv(R)
if logdet is not None:
logdetR = logdet
else:
logdetR = np.linalg.slogdet(R)[1]
# It would be more efficient and simpler, if you just rotated the
# moments and didn't touch phi. However, then you would need to call
# update() before lower_bound_contribution. This is more error-safe.
# Rotate plates, if plate rotation matrix is given. Assume that there's
# only one plate-axis
if Q is not None:
# Rotate moments using Q
self.u[0] = np.einsum('ik,kj->ij', Q, self.u[0])
sumQ = np.sum(Q, axis=0)
# Rotate natural parameters using Q
self.phi[1] = np.einsum('d,dij->dij', sumQ**(-2), self.phi[1])
self.phi[0] = np.einsum('dij,dj->di', -2*self.phi[1], self.u[0])
# Transform parameters using R
self.phi[0] = mvdot(invR.T, self.phi[0])
self.phi[1] = dot(invR.T, self.phi[1], invR)
if Q is not None:
self._update_moments_and_cgf()
else:
# Transform moments and g using R
self.u[0] = mvdot(R, self.u[0])
self.u[1] = dot(R, self.u[1], R.T)
self.g -= logdetR
def rotate_matrix(self, R1, R2, inv1=None, logdet1=None, inv2=None, logdet2=None, Q=None):
r"""
The vector is reshaped into a matrix by stacking the row vectors.
Computes R1*X*R2', which is identical to kron(R1,R2)*x (??)
Note that this is slightly different from the standard Kronecker product
definition because Numpy stacks row vectors instead of column vectors.
Parameters
----------
R1 : ndarray
A matrix from the left
R2 : ndarray
A matrix from the right
"""
if self._moments.ndim != 1:
raise NotImplementedError("Not implemented for ndim!=1 yet")
if Q is not None:
# Rotate moments using Q
self.u[0] = np.einsum('ik,kj->ij', Q, self.u[0])
sumQ = np.sum(Q, axis=0)
# Rotate natural parameters using Q
self.phi[1] = np.einsum('d,dij->dij', sumQ**(-2), self.phi[1])
self.phi[0] = np.einsum('dij,dj->di', -2*self.phi[1], self.u[0])
if inv1 is None:
inv1 = np.linalg.inv(R1)
if logdet1 is None:
logdet1 = np.linalg.slogdet(R1)[1]
if inv2 is None:
inv2 = np.linalg.inv(R2)
if logdet2 is None:
logdet2 = np.linalg.slogdet(R2)[1]
D1 = np.shape(R1)[0]
D2 = np.shape(R2)[0]
# Reshape into matrices
sh0 = np.shape(self.phi[0])[:-1] + (D1,D2)
sh1 = np.shape(self.phi[1])[:-2] + (D1,D2,D1,D2)
phi0 = np.reshape(self.phi[0], sh0)
phi1 = np.reshape(self.phi[1], sh1)
# Apply rotations to phi
#phi0 = dot(inv1, phi0, inv2.T)
phi0 = dot(inv1.T, phi0, inv2)
phi1 = np.einsum('...ia,...abcd->...ibcd', inv1.T, phi1)
phi1 = np.einsum('...ic,...abcd->...abid', inv1.T, phi1)
phi1 = np.einsum('...ib,...abcd->...aicd', inv2.T, phi1)
phi1 = np.einsum('...id,...abcd->...abci', inv2.T, phi1)
# Reshape back into vectors
self.phi[0] = np.reshape(phi0, self.phi[0].shape)
self.phi[1] = np.reshape(phi1, self.phi[1].shape)
# It'd be better to rotate the moments too..
self._update_moments_and_cgf()
class GaussianARD(_GaussianTemplate):
r"""
Node for Gaussian variables with ARD prior.
The node represents a :math:`D`-dimensional vector from the Gaussian
distribution:
.. math::
\mathbf{x} &\sim \mathcal{N}(\boldsymbol{\mu}, \mathrm{diag}(\boldsymbol{\alpha})),
where :math:`\boldsymbol{\mu}` is the mean vector and
:math:`\mathrm{diag}(\boldsymbol{\alpha})` is the diagonal precision matrix
(i.e., inverse of the covariance matrix).
.. math::
\mathbf{x},\boldsymbol{\mu} \in \mathbb{R}^{D}, \quad \alpha_d > 0 \text{
for } d=0,\ldots,D-1
*Note:* The form of the posterior approximation is a Gaussian distribution with full
covariance matrix instead of a diagonal matrix.
Parameters
----------
mu : Gaussian-like node or GaussianGamma-like node or array Mean vector
alpha : gamma-like node or array
Diagonal elements of the precision matrix
See also
--------
Gamma, Gaussian, GaussianGamma, GaussianWishart
"""
def __init__(self, mu, alpha, ndim=None, shape=None, **kwargs):
r"""
Create GaussianARD node.
"""
super().__init__(mu, alpha, ndim=ndim, shape=shape, **kwargs)
@classmethod
def _constructor(cls, mu, alpha, ndim=None, shape=None, **kwargs):
r"""
Constructs distribution and moments objects.
If __init__ uses useconstructor decorator, this method is called to
construct distribution and moments objects.
The method is given the same inputs as __init__. For some nodes, some of
these can't be "static" class attributes, then the node class must
overwrite this method to construct the objects manually.
The point of distribution class is to move general distribution but
not-node specific code. The point of moments class is to define the
messaging protocols.
"""
mu_alpha = WrapToGaussianGamma(mu, alpha, ndim=0)
if ndim is None:
if shape is not None:
ndim = len(shape)
else:
shape = ()
ndim = 0
else:
if shape is not None:
if ndim != len(shape):
raise ValueError("Given shape and ndim inconsistent")
else:
if ndim == 0:
shape = ()
else:
if ndim > len(mu_alpha.plates):
raise ValueError(
"Cannot determine shape for ndim={0} because parent "
"full shape has ndim={1}."
.format(ndim, len(mu_alpha.plates))
)
shape = mu_alpha.plates[-ndim:]
moments = GaussianMoments(shape)
parent_moments = [GaussianGammaMoments(())]
distribution = GaussianARDDistribution(shape)
plates = cls._total_plates(kwargs.get('plates'),
distribution.plates_from_parent(0, mu_alpha.plates))
parents = [mu_alpha]
return (parents,
kwargs,
moments.dims,
plates,
distribution,
moments,
parent_moments)
def initialize_from_parameters(self, mu, alpha):
# Explicit broadcasting so the shapes match
mu = mu * np.ones(np.shape(alpha))
alpha = alpha * np.ones(np.shape(mu))
# Compute parent moments
u = self._parent_moments[0].compute_fixed_moments([mu, alpha])
# Initialize distribution
self._initialize_from_parent_moments(u)
def initialize_from_mean_and_covariance(self, mu, Cov):
ndim = len(self._distribution.shape)
u = [mu, Cov + linalg.outer(mu, mu, ndim=ndim)]
mask = np.logical_not(self.observed)
# TODO: You could compute the CGF but it requires Cholesky of
# Cov. Do it later.
self._set_moments_and_cgf(u, np.nan, mask=mask)
return
def __str__(self):
mu = self.u[0]
Cov = self.u[1] - linalg.outer(mu, mu)
return ("%s ~ Gaussian(mu, Cov)\n"
" mu = \n"
"%s\n"
" Cov = \n"
"%s\n"
% (self.name, mu, Cov))
def rotate(self, R, inv=None, logdet=None, axis=-1, Q=None, subset=None, debug=False):
if Q is not None:
raise NotImplementedError()
if subset is not None:
raise NotImplementedError()
# TODO/FIXME: Combine and refactor all these rotation transformations
# into _GaussianTemplate
ndim = len(self._distribution.shape)
if inv is not None:
invR = inv
else:
invR = np.linalg.inv(R)
if logdet is not None:
logdetR = logdet
else:
logdetR = np.linalg.slogdet(R)[1]
self.phi[0] = rotate_mean(self.phi[0], invR.T,
axis=axis,
ndim=ndim)
self.phi[1] = rotate_covariance(self.phi[1], invR.T,
axis=axis,
ndim=ndim)
self.u[0] = rotate_mean(self.u[0], R,
axis=axis,
ndim=ndim)
self.u[1] = rotate_covariance(self.u[1], R,
axis=axis,
ndim=ndim)
s = list(self.dims[0])
s.pop(axis)
self.g -= logdetR * np.prod(s)
# TODO: This is all just debugging stuff and can be removed
if debug:
uh = [ui.copy() for ui in self.u]
gh = self.g.copy()
self._update_moments_and_cgf()
if any(not np.allclose(uih, ui, atol=1e-6) for (uih, ui) in zip(uh, self.u)):
raise RuntimeError("BUG")
if not np.allclose(self.g, gh, atol=1e-6):
raise RuntimeError("BUG")
return
def rotate_plates(self, Q, plate_axis=-1):
r"""
Approximate rotation of a plate axis.
Mean is rotated exactly but covariance/precision matrix is rotated
approximately.
"""
ndim = len(self._distribution.shape)
# Rotate moments using Q
if not isinstance(plate_axis, int):
raise ValueError("Plate axis must be integer")
if plate_axis >= 0:
plate_axis -= len(self.plates)
if plate_axis < -len(self.plates) or plate_axis >= 0:
raise ValueError("Axis out of bounds")
u0 = rotate_mean(self.u[0], Q,
ndim=ndim+(-plate_axis),
axis=0)
sumQ = misc.add_trailing_axes(np.sum(Q, axis=0),
2*ndim-plate_axis-1)
phi1 = sumQ**(-2) * self.phi[1]
phi0 = -2 * matrix_dot_vector(phi1, u0, ndim=ndim)
self.phi[0] = phi0
self.phi[1] = phi1
self._update_moments_and_cgf()
return
class GaussianGamma(ExponentialFamily):
r"""
Node for Gaussian-gamma (isotropic) random variables.
The prior:
.. math::
p(x, \alpha| \mu, \Lambda, a, b)
p(x|\alpha, \mu, \Lambda) = \mathcal{N}(x | \mu, \alpha Lambda)
p(\alpha|a, b) = \mathcal{G}(\alpha | a, b)
The posterior approximation :math:`q(x, \alpha)` has the same Gaussian-gamma
form.
Currently, supports only vector variables.
"""
@classmethod
def _constructor(cls, mu, Lambda, a, b, ndim=1, **kwargs):
r"""
Constructs distribution and moments objects.
This method is called if useconstructor decorator is used for __init__.
`mu` is the mean/location vector
`alpha` is the scale
`V` is the scale matrix
`n` is the degrees of freedom
"""
# Convert parent nodes
mu_Lambda = WrapToGaussianWishart(mu, Lambda, ndim=ndim)
a = cls._ensure_moments(a, GammaPriorMoments)
b = cls._ensure_moments(b, GammaMoments)
shape = mu_Lambda.dims[0]
distribution = GaussianGammaDistribution(shape)
moments = GaussianGammaMoments(shape)
parent_moments = (
mu_Lambda._moments,
a._moments,
b._moments,
)
# Check shapes
if mu_Lambda.dims != ( shape, (), 2*shape, () ):
raise ValueError("mu and Lambda have wrong shape")
if a.dims != ( (), () ):
raise ValueError("a has wrong shape")
if b.dims != ( (), () ):
raise ValueError("b has wrong shape")
# List of parent nodes
parents = [mu_Lambda, a, b]
return (parents,
kwargs,
moments.dims,
cls._total_plates(kwargs.get('plates'),
distribution.plates_from_parent(0, mu_Lambda.plates),
distribution.plates_from_parent(1, a.plates),
distribution.plates_from_parent(2, b.plates)),
distribution,
moments,
parent_moments)
def translate(self, b, debug=False):
if self._moments.ndim != 1:
raise NotImplementedError("Only ndim=1 supported at the moment")
tau = self.u[2]
x = self.u[0] / tau[...,None]
xb = linalg.outer(x, b, ndim=1)
bx = linalg.transpose(xb, ndim=1)
bb = linalg.outer(b, b, ndim=1)
uh = [
self.u[0] + tau[...,None] * b,
self.u[1] + tau[...,None,None] * (xb + bx + bb),
self.u[2],
self.u[3]
]
Lambda = -2 * self.phi[1]
dtau = -0.5 * (
np.einsum('...ij,...i,...j->...', Lambda, b, b)
+ 2 * np.einsum('...ij,...i,...j->...', Lambda, b, x)
)
phih = [
self.phi[0] + np.einsum('...ij,...j->...i', Lambda, b),
self.phi[1],
self.phi[2] + dtau,
self.phi[3]
]
self._check_shape(uh)
self._check_shape(phih)
self.phi = phih
self.u = uh
# TODO: This is all just debugging stuff and can be removed
if debug:
uh = [ui.copy() for ui in uh]
gh = self.g.copy()
self._update_moments_and_cgf()
if any(not np.allclose(uih, ui, atol=1e-6) for (uih, ui) in zip(uh, self.u)):
raise RuntimeError("BUG")
if not np.allclose(self.g, gh, atol=1e-6):
raise RuntimeError("BUG")
return
def rotate(self, R, inv=None, logdet=None, debug=False):
if self._moments.ndim != 1:
raise NotImplementedError("Only ndim=1 supported at the moment")
if inv is None:
inv = np.linalg.inv(R)
if logdet is None:
logdet = np.linalg.slogdet(R)[1]
uh = [
rotate_mean(self.u[0], R),
rotate_covariance(self.u[1], R),
self.u[2],
self.u[3]
]
phih = [
rotate_mean(self.phi[0], inv.T),
rotate_covariance(self.phi[1], inv.T),
self.phi[2],
self.phi[3]
]
self._check_shape(uh)
self._check_shape(phih)
self.phi = phih
self.u = uh
self.g = self.g - logdet
# TODO: This is all just debugging stuff and can be removed
if debug:
uh = [ui.copy() for ui in uh]
gh = self.g.copy()
self._update_moments_and_cgf()
if any(not np.allclose(uih, ui, atol=1e-6) for (uih, ui) in zip(uh, self.u)):
raise RuntimeError("BUG")
if not np.allclose(self.g, gh, atol=1e-6):
raise RuntimeError("BUG")
return
def plotmatrix(self):
r"""
Creates a matrix of marginal plots.
On diagonal, are marginal plots of each variable. Off-diagonal plot
(i,j) shows the joint marginal density of x_i and x_j.
"""
import bayespy.plot as bpplt
if self.ndim != 1:
raise NotImplementedError("Only ndim=1 supported at the moment")
if np.prod(self.plates) != 1:
raise ValueError("Currently, does not support plates in the node.")
if len(self.dims[0]) != 1:
raise ValueError("Currently, supports only vector variables")
# Dimensionality of the Gaussian
D = self.dims[0][0]
# Compute standard parameters
tau = self.u[2]
mu = self.u[0]
mu = mu / misc.add_trailing_axes(tau, 1)
Cov = self.u[1] - linalg.outer(self.u[0], mu, ndim=1)
Cov = Cov / misc.add_trailing_axes(tau, 2)
a = self.phi[3]
b = -self.phi[2] - 0.5*linalg.inner(self.phi[0], mu, ndim=1)
# Create subplots
(fig, axes) = bpplt.pyplot.subplots(D+1, D+1)
# Plot marginal Student t distributions
for i in range(D):
for j in range(i+1):
if i == j:
bpplt._pdf_t(*(random.gaussian_gamma_to_t(mu[i],
Cov[i,i],
a,
b,
ndim=0)),
axes=axes[i,i])
else:
S = Cov[np.ix_([i,j],[i,j])]
(m, S, nu) = random.gaussian_gamma_to_t(mu[[i,j]],
S,
a,
b)
bpplt._contour_t(m, S, nu, axes=axes[i,j])
bpplt._contour_t(m, S, nu, axes=axes[j,i], transpose=True)
# Plot Gaussian-gamma marginal distributions
for k in range(D):
bpplt._contour_gaussian_gamma(mu[k], Cov[k,k], a, b,
axes=axes[D,k])
bpplt._contour_gaussian_gamma(mu[k], Cov[k,k], a, b,
axes=axes[k,D],
transpose=True)
# Plot gamma marginal distribution
bpplt._pdf_gamma(a, b, axes=axes[D,D])
return axes
def get_gaussian_location(self):
r"""
Return the mean and variance of the distribution
"""
if self._moments.ndim != 1:
raise NotImplementedError("Only ndim=1 supported at the moment")
tau = self.u[2]
tau_mu = self.u[0]
return tau_mu / tau[...,None]
def get_gaussian_mean_and_variance(self):
r"""
Return the mean and variance of the distribution
"""
if self.ndim != 1:
raise NotImplementedError("Only ndim=1 supported at the moment")
a = self.phi[3]
nu = 2*a
if np.any(nu <= 1):
raise ValueError("Mean not defined for degrees of freedom <= 1")
if np.any(nu <= 2):
raise ValueError("Variance not defined if degrees of freedom <= 2")
tau = self.u[2]
tau_mu = self.u[0]
mu = tau_mu / misc.add_trailing_axes(tau, 1)
var = misc.get_diag(self.u[1], ndim=1) - tau_mu*mu
var = var / misc.add_trailing_axes(tau, 1)
var = nu / (nu-2) * var
return (mu, var)
def get_marginal_logpdf(self, gaussian=None, gamma=None):
r"""
Get the (marginal) log pdf of a subset of the variables
Parameters
----------
gaussian : list or None
Indices of the Gaussian variables to keep or None
gamma : bool or None
True if keep the gamma variable, otherwise False or None
Returns
-------
function
A function which computes log-pdf
"""
if self.ndim != 1:
raise NotImplementedError("Only ndim=1 supported at the moment")
if gaussian is None and not gamma:
raise ValueError("Must give some variables")
# Compute standard parameters
tau = self.u[2]
mu = self.u[0]
mu = mu / misc.add_trailing_axes(tau, 1)
Cov = np.linalg.inv(-2*self.phi[1])
if not np.allclose(Cov,
self.u[1] - linalg.outer(self.u[0], mu, ndim=1)):
raise Exception("WAAAT")
#Cov = Cov / misc.add_trailing_axes(tau, 2)
a = self.phi[3]
b = -self.phi[2] - 0.5*linalg.inner(self.phi[0], mu, ndim=1)
if not gamma:
# Student t distributions
inds = list(gaussian)
mu = mu[inds]
Cov = Cov[np.ix_(inds, inds)]
(mu, Cov, nu) = random.gaussian_gamma_to_t(mu,
Cov,
a,
b,
ndim=1)
L = linalg.chol(Cov)
logdet_Cov = linalg.chol_logdet(L)
D = len(inds)
def logpdf(x):
y = x - mu
v = linalg.chol_solve(L, y)
z2 = linalg.inner(y, v, ndim=1)
return random.t_logpdf(z2, logdet_Cov, nu, D)
return logpdf
elif gaussian is None:
# Gamma distribution
def logpdf(x):
logx = np.log(x)
return random.gamma_logpdf(b*x,
logx,
a*logx,
a*np.log(b),
special.gammaln(a))
return logpdf
else:
# Gaussian-gamma distribution
inds = list(gaussian)
mu = mu[inds]
Cov = Cov[np.ix_(inds, inds)]
D = len(inds)
L = linalg.chol(Cov)
logdet_Cov = linalg.chol_logdet(L)
def logpdf(x):
tau = x[...,-1]
logtau = np.log(tau)
x = x[...,:-1]
y = x - mu
v = linalg.chol_solve(L, y) * tau[...,None]
z2 = linalg.inner(y, v, ndim=1)
return (random.gaussian_logpdf(z2,
0,
0,
logdet_Cov + D*logtau,
D) +
random.gamma_logpdf(b*tau,
logtau,
a*logtau,
a*np.log(b),
special.gammaln(a)))
return logpdf
class GaussianWishart(ExponentialFamily):
r"""
Node for Gaussian-Wishart random variables.
The prior:
.. math::
p(x, \Lambda| \mu, \alpha, V, n)
p(x|\Lambda, \mu, \alpha) = \mathcal(N)(x | \mu, \alpha^{-1} Lambda^{-1})
p(\Lambda|V, n) = \mathcal(W)(\Lambda | n, V)
The posterior approximation :math:`q(x, \Lambda)` has the same Gaussian-Wishart form.
Currently, supports only vector variables.
"""
_distribution = GaussianWishartDistribution()
@classmethod
def _constructor(cls, mu, alpha, n, V, **kwargs):
r"""
Constructs distribution and moments objects.
This method is called if useconstructor decorator is used for __init__.
`mu` is the mean/location vector
`alpha` is the scale
`n` is the degrees of freedom
`V` is the scale matrix
"""
# Convert parent nodes
mu_alpha = WrapToGaussianGamma(mu, alpha, ndim=1)
D = mu_alpha.dims[0][0]
shape = mu_alpha._moments.shape
moments = GaussianWishartMoments(shape)
n = cls._ensure_moments(n, WishartPriorMoments, d=D)
V = cls._ensure_moments(V, WishartMoments, ndim=1)
parent_moments = (
mu_alpha._moments,
n._moments,
V._moments
)
# Check shapes
if mu_alpha.dims != ( (D,), (D,D), (), () ):
raise ValueError("mu and alpha have wrong shape")
if V.dims != ( (D,D), () ):
raise ValueError("Precision matrix has wrong shape")
if n.dims != ( (), () ):
raise ValueError("Degrees of freedom has wrong shape")
parents = [mu_alpha, n, V]
return (parents,
kwargs,
moments.dims,
cls._total_plates(kwargs.get('plates'),
cls._distribution.plates_from_parent(0, mu_alpha.plates),
cls._distribution.plates_from_parent(1, n.plates),
cls._distribution.plates_from_parent(2, V.plates)),
cls._distribution,
moments,
parent_moments)
#
# CONVERTERS
#
class GaussianToGaussianGamma(Deterministic):
r"""
Converter for Gaussian moments to Gaussian-gamma isotropic moments
Combines the Gaussian moments with gamma moments for a fixed value 1.
"""
def __init__(self, X, **kwargs):
r"""
"""
if not isinstance(X._moments, GaussianMoments):
raise ValueError("Wrong moments, should be Gaussian")
shape = X._moments.shape
self.ndim = X._moments.ndim
self._moments = GaussianGammaMoments(shape)
self._parent_moments = [GaussianMoments(shape)]
shape = X.dims[0]
dims = ( shape, 2*shape, (), () )
super().__init__(X, dims=dims, **kwargs)
def _compute_moments(self, u_X):
r"""
"""
x = u_X[0]
xx = u_X[1]
u = [x, xx, 1, 0]
return u
def _compute_message_to_parent(self, index, m_child, u_X):
r"""
"""
if index == 0:
m = m_child[:2]
return m
else:
raise ValueError("Invalid parent index")
def _compute_function(self, x):
return (x, 1)
GaussianMoments.add_converter(GaussianGammaMoments,
GaussianToGaussianGamma)
class GaussianGammaToGaussianWishart(Deterministic):
r"""
"""
def __init__(self, X_alpha, **kwargs):
raise NotImplementedError()
GaussianGammaMoments.add_converter(GaussianWishartMoments,
GaussianGammaToGaussianWishart)
#
# WRAPPERS
#
# These wrappers form a single node from two nodes for messaging purposes.
#
class WrapToGaussianGamma(Deterministic):
r"""
"""
def __init__(self, X, alpha, ndim=None, **kwargs):
r"""
"""
# In case X is a numerical array, convert it to Gaussian first
try:
X = self._ensure_moments(X, GaussianMoments, ndim=ndim)
except Moments.NoConverterError:
pass
try:
ndim = X._moments.ndim
except AttributeError as err:
raise TypeError("ndim needs to be given explicitly") from err
X = self._ensure_moments(X, GaussianGammaMoments, ndim=ndim)
if len(X.dims[0]) != ndim:
raise RuntimeError("Conversion failed ndim.")
shape = X.dims[0]
dims = ( shape, 2 * shape, (), () )
self.shape = shape
self.ndim = len(shape)
self._moments = GaussianGammaMoments(shape)
self._parent_moments = [
GaussianGammaMoments(shape),
GammaMoments()
]
super().__init__(X, alpha, dims=dims, **kwargs)
def _compute_moments(self, u_X, u_alpha):
r"""
"""
(tau_x, tau_xx, tau, logtau) = u_X
(alpha, logalpha) = u_alpha
u0 = tau_x * misc.add_trailing_axes(alpha, self.ndim)
u1 = tau_xx * misc.add_trailing_axes(alpha, 2 * self.ndim)
u2 = tau * alpha
u3 = logtau + logalpha
return [u0, u1, u2, u3]
def _compute_message_to_parent(self, index, m_child, u_X, u_alpha):
r"""
"""
if index == 0:
alpha = u_alpha[0]
m0 = m_child[0] * misc.add_trailing_axes(alpha, self.ndim)
m1 = m_child[1] * misc.add_trailing_axes(alpha, 2 * self.ndim)
m2 = m_child[2] * alpha
m3 = m_child[3]
return [m0, m1, m2, m3]
elif index == 1:
(tau_x, tau_xx, tau, logtau) = u_X
m0 = (
linalg.inner(m_child[0], tau_x, ndim=self.ndim)
+ linalg.inner(m_child[1], tau_xx, ndim=2*self.ndim)
+ m_child[2] * tau
)
m1 = m_child[3]
return [m0, m1]
else:
raise ValueError("Invalid parent index")
class WrapToGaussianWishart(Deterministic):
r"""
Wraps Gaussian and Wishart nodes into a Gaussian-Wishart node.
The following node combinations can be wrapped:
* Gaussian and Wishart
* Gaussian-gamma and Wishart
* Gaussian-Wishart and gamma
"""
def __init__(self, X, Lambda, ndim=1, **kwargs):
r"""
"""
# Just in case X is an array, convert it to a Gaussian node first.
try:
X = self._ensure_moments(X, GaussianMoments, ndim=ndim)
except Moments.NoConverterError:
pass
try:
# Try combo Gaussian-Gamma and Wishart
X = self._ensure_moments(X, GaussianGammaMoments, ndim=ndim)
except Moments.NoConverterError:
# Have to use Gaussian-Wishart and Gamma
X = self._ensure_moments(X, GaussianWishartMoments, ndim=ndim)
Lambda = self._ensure_moments(Lambda, GammaMoments, ndim=ndim)
shape = X.dims[0]
if Lambda.dims != ((), ()):
raise ValueError(
"Mean and precision have inconsistent shapes: {0} and {1}"
.format(
X.dims,
Lambda.dims
)
)
self.wishart = False
else:
# Gaussian-Gamma and Wishart
shape = X.dims[0]
Lambda = self._ensure_moments(Lambda, WishartMoments, ndim=ndim)
if Lambda.dims != (2 * shape, ()):
raise ValueError(
"Mean and precision have inconsistent shapes: {0} and {1}"
.format(
X.dims,
Lambda.dims
)
)
self.wishart = True
self.ndim = len(shape)
self._parent_moments = (
X._moments,
Lambda._moments,
)
self._moments = GaussianWishartMoments(shape)
super().__init__(X, Lambda, dims=self._moments.dims, **kwargs)
def _compute_moments(self, u_X_alpha, u_Lambda):
r"""
"""
if self.wishart:
alpha_x = u_X_alpha[0]
alpha_xx = u_X_alpha[1]
alpha = u_X_alpha[2]
log_alpha = u_X_alpha[3]
Lambda = u_Lambda[0]
logdet_Lambda = u_Lambda[1]
D = np.prod(self.dims[0])
u0 = linalg.mvdot(Lambda, alpha_x, ndim=self.ndim)
u1 = linalg.inner(Lambda, alpha_xx, ndim=2*self.ndim)
u2 = Lambda * misc.add_trailing_axes(alpha, 2*self.ndim)
u3 = logdet_Lambda + D * log_alpha
u = [u0, u1, u2, u3]
return u
else:
raise NotImplementedError()
def _compute_message_to_parent(self, index, m_child, u_X_alpha, u_Lambda):
r"""
...
Message from the child is :math:`[m_0, m_1, m_2, m_3]`:
.. math::
\alpha m_0^T \Lambda x + m_1 \alpha x^T \Lambda x
+ \mathrm{tr}(\alpha m_2 \Lambda) + m_3 (\log | \alpha \Lambda |)
In case of Gaussian-gamma and Wishart parents:
Message to the first parent (x, alpha):
.. math::
\tilde{m_0} &= \Lambda m_0
\\
\tilde{m_1} &= m_1 \Lambda
\\
\tilde{m_2} &= \mathrm{tr}(m_2 \Lambda)
\\
\tilde{m_3} &= m_3 \cdot D
Message to the second parent (Lambda):
.. math::
\tilde{m_0} &= \alpha (\frac{1}{2} m_0 x^T + \frac{1}{2} x m_0^T +
m_1 xx^T + m_2)
\\
\tilde{m_1} &= m_3
"""
if index == 0:
if self.wishart:
# Message to Gaussian-gamma (isotropic)
Lambda = u_Lambda[0]
D = np.prod(self.dims[0])
m0 = linalg.mvdot(Lambda, m_child[0], ndim=self.ndim)
m1 = Lambda * misc.add_trailing_axes(m_child[1], 2*self.ndim)
m2 = linalg.inner(Lambda, m_child[2], ndim=2*self.ndim)
m3 = D * m_child[3]
m = [m0, m1, m2, m3]
return m
else:
# Message to Gaussian-Wishart
raise NotImplementedError()
elif index == 1:
if self.wishart:
# Message to Wishart
alpha_x = u_X_alpha[0]
alpha_xx = u_X_alpha[1]
alpha = u_X_alpha[2]
m0 = (0.5*linalg.outer(alpha_x, m_child[0], ndim=self.ndim) +
0.5*linalg.outer(m_child[0], alpha_x, ndim=self.ndim) +
alpha_xx * misc.add_trailing_axes(m_child[1], 2*self.ndim) +
misc.add_trailing_axes(alpha, 2*self.ndim) * m_child[2])
m1 = m_child[3]
m = [m0, m1]
return m
else:
# Message to gamma (isotropic)
raise NotImplementedError()
else:
raise ValueError("Invalid parent index")
def reshape_gaussian_array(dims_from, dims_to, x0, x1):
r"""
Reshape the moments Gaussian array variable.
The plates remain unaffected.
"""
num_dims_from = len(dims_from)
num_dims_to = len(dims_to)
# Reshape the first moment / mean
num_plates_from = np.ndim(x0) - num_dims_from
plates_from = np.shape(x0)[:num_plates_from]
shape = (
plates_from
+ (1,)*(num_dims_to-num_dims_from) + dims_from
)
x0 = np.ones(dims_to) * np.reshape(x0, shape)
# Reshape the second moment / covariance / precision
num_plates_from = np.ndim(x1) - 2*num_dims_from
plates_from = np.shape(x1)[:num_plates_from]
shape = (
plates_from
+ (1,)*(num_dims_to-num_dims_from) + dims_from
+ (1,)*(num_dims_to-num_dims_from) + dims_from
)
x1 = np.ones(dims_to+dims_to) * np.reshape(x1, shape)
return (x0, x1)
def transpose_covariance(Cov, ndim=1):
r"""
Transpose the covariance array of Gaussian array variable.
That is, swap the last ndim axes with the ndim axes before them. This makes
transposing easy for array variables when the covariance is not a matrix but
a multidimensional array.
"""
axes_in = [Ellipsis] + list(range(2*ndim,0,-1))
axes_out = [Ellipsis] + list(range(ndim,0,-1)) + list(range(2*ndim,ndim,-1))
return np.einsum(Cov, axes_in, axes_out)
def left_rotate_covariance(Cov, R, axis=-1, ndim=1):
r"""
Rotate the covariance array of Gaussian array variable.
ndim is the number of axes for the Gaussian variable.
For vector variable, ndim=1 and covariance is a matrix.
"""
if not isinstance(axis, int):
raise ValueError("Axis must be an integer")
if axis < -ndim or axis >= ndim:
raise ValueError("Axis out of range")
# Force negative axis
if axis >= 0:
axis -= ndim
# Rotation from left
axes_R = [Ellipsis, ndim+abs(axis)+1, ndim+abs(axis)]
axes_Cov = [Ellipsis] + list(range(ndim+abs(axis),
0,
-1))
axes_out = [Ellipsis, ndim+abs(axis)+1] + list(range(ndim+abs(axis)-1,
0,
-1))
Cov = np.einsum(R, axes_R, Cov, axes_Cov, axes_out)
return Cov
def right_rotate_covariance(Cov, R, axis=-1, ndim=1):
r"""
Rotate the covariance array of Gaussian array variable.
ndim is the number of axes for the Gaussian variable.
For vector variable, ndim=1 and covariance is a matrix.
"""
if not isinstance(axis, int):
raise ValueError("Axis must be an integer")
if axis < -ndim or axis >= ndim:
raise ValueError("Axis out of range")
# Force negative axis
if axis >= 0:
axis -= ndim
# Rotation from right
axes_R = [Ellipsis, abs(axis)+1, abs(axis)]
axes_Cov = [Ellipsis] + list(range(abs(axis),
0,
-1))
axes_out = [Ellipsis, abs(axis)+1] + list(range(abs(axis)-1,
0,
-1))
Cov = np.einsum(R, axes_R, Cov, axes_Cov, axes_out)
return Cov
def rotate_covariance(Cov, R, axis=-1, ndim=1):
r"""
Rotate the covariance array of Gaussian array variable.
ndim is the number of axes for the Gaussian variable.
For vector variable, ndim=1 and covariance is a matrix.
"""
# Rotate from left and right
Cov = left_rotate_covariance(Cov, R, ndim=ndim, axis=axis)
Cov = right_rotate_covariance(Cov, R, ndim=ndim, axis=axis)
return Cov
def rotate_mean(mu, R, axis=-1, ndim=1):
r"""
Rotate the mean array of Gaussian array variable.
ndim is the number of axes for the Gaussian variable.
For vector variable, ndim=1 and mu is a vector.
"""
if not isinstance(axis, int):
raise ValueError("Axis must be an integer")
if axis < -ndim or axis >= ndim:
raise ValueError("Axis out of range")
# Force negative axis
if axis >= 0:
axis -= ndim
# Rotation from right
axes_R = [Ellipsis, abs(axis)+1, abs(axis)]
axes_mu = [Ellipsis] + list(range(abs(axis),
0,
-1))
axes_out = [Ellipsis, abs(axis)+1] + list(range(abs(axis)-1,
0,
-1))
mu = np.einsum(R, axes_R, mu, axes_mu, axes_out)
return mu
def array_to_vector(x, ndim=1):
if ndim == 0:
return x
shape_x = np.shape(x)
D = np.prod(shape_x[-ndim:])
return np.reshape(x, shape_x[:-ndim] + (D,))
def array_to_matrix(A, ndim=1):
if ndim == 0:
return A
shape_A = np.shape(A)
D = np.prod(shape_A[-ndim:])
return np.reshape(A, shape_A[:-2*ndim] + (D,D))
def vector_to_array(x, shape):
shape_x = np.shape(x)
return np.reshape(x, np.shape(x)[:-1] + tuple(shape))
def matrix_dot_vector(A, x, ndim=1):
if ndim < 0:
raise ValueError("ndim must be non-negative integer")
if ndim == 0:
return A*x
dims_x = np.shape(x)[-ndim:]
A = array_to_matrix(A, ndim=ndim)
x = array_to_vector(x, ndim=ndim)
y = np.einsum('...ik,...k->...i', A, x)
return vector_to_array(y, dims_x)
|