|
192 | 192 | {
|
193 | 193 | "data": {
|
194 | 194 | "application/vnd.jupyter.widget-view+json": {
|
195 |
| - "model_id": "7e9cf2c6b77f443bbbca89fd732fd1da", |
| 195 | + "model_id": "47b5f627590f4f09a279e866818a785b", |
196 | 196 | "version_major": 2,
|
197 | 197 | "version_minor": 0
|
198 | 198 | },
|
|
673 | 673 | {
|
674 | 674 | "data": {
|
675 | 675 | "application/vnd.jupyter.widget-view+json": {
|
676 |
| - "model_id": "fcf669609bf243cc86618b004ccbf703", |
| 676 | + "model_id": "00ce56b9f1cf44baa87a6d127cffa01b", |
677 | 677 | "version_major": 2,
|
678 | 678 | "version_minor": 0
|
679 | 679 | },
|
|
1043 | 1043 | "\n",
|
1044 | 1044 | "To resolve this potential ambiguity we can adjust the step size, $\\epsilon$, of the Hamiltonian transition. The smaller the step size the more accurate the trajectory and the less likely it will be mislabeled as a divergence. In other words, if we have geometric ergodicity between the Hamiltonian transition and the target distribution then decreasing the step size will reduce and then ultimately remove the divergences entirely. If we do not have geometric ergodicity, however, then decreasing the step size will not completely remove the divergences.\n",
|
1045 | 1045 | "\n",
|
1046 |
| - "Like `Stan`, the step size in `PyMC` is tuned automatically during warm up, but we can coerce smaller step sizes by tweaking the configuration of `PyMC`'s adaptation routine. In particular, we can increase the `target_accept` parameter from its default value of 0.8 closer to its maximum value of 1." |
| 1046 | + "In `PyMC` we do not control the step size directly, but we can coerce smaller step sizes by tweaking the configuration of `PyMC`'s adaptation routine. In particular, we can increase the `target_accept` parameter from its default value of 0.8 closer to its maximum value of 1." |
1047 | 1047 | ]
|
1048 | 1048 | },
|
1049 | 1049 | {
|
1050 | 1050 | "cell_type": "markdown",
|
1051 | 1051 | "metadata": {},
|
1052 | 1052 | "source": [
|
1053 |
| - "### Adjusting Adaptation Routine" |
| 1053 | + "### Adjusting Adaptation Routine\n", |
| 1054 | + "\n", |
| 1055 | + "To evaluate the effect of decreasing step size (increasing `target_accept`) we can run the same model across a range of `target_accept` values." |
1054 | 1056 | ]
|
1055 | 1057 | },
|
1056 | 1058 | {
|
|
1066 | 1068 | "Initializing NUTS using jitter+adapt_diag...\n",
|
1067 | 1069 | "Multiprocess sampling (2 chains in 2 jobs)\n",
|
1068 | 1070 | "NUTS: [mu, tau, theta]\n",
|
1069 |
| - "Sampling 2 chains for 2_000 tune and 5_000 draw iterations (4_000 + 10_000 draws total) took 5 seconds.\n", |
| 1071 | + "Sampling 2 chains for 2_000 tune and 5_000 draw iterations (4_000 + 10_000 draws total) took 6 seconds.\n", |
1070 | 1072 | "There were 214 divergences after tuning. Increase `target_accept` or reparameterize.\n",
|
1071 | 1073 | "We recommend running at least 4 chains for robust computation of convergence diagnostics\n",
|
1072 | 1074 | "Auto-assigning NUTS sampler...\n",
|
1073 | 1075 | "Initializing NUTS using jitter+adapt_diag...\n",
|
1074 | 1076 | "Multiprocess sampling (2 chains in 2 jobs)\n",
|
1075 | 1077 | "NUTS: [mu, tau, theta]\n",
|
1076 |
| - "Sampling 2 chains for 2_000 tune and 5_000 draw iterations (4_000 + 10_000 draws total) took 8 seconds.\n", |
| 1078 | + "Sampling 2 chains for 2_000 tune and 5_000 draw iterations (4_000 + 10_000 draws total) took 9 seconds.\n", |
1077 | 1079 | "There were 197 divergences after tuning. Increase `target_accept` or reparameterize.\n",
|
1078 | 1080 | "We recommend running at least 4 chains for robust computation of convergence diagnostics\n",
|
1079 | 1081 | "Auto-assigning NUTS sampler...\n",
|
1080 | 1082 | "Initializing NUTS using jitter+adapt_diag...\n",
|
1081 | 1083 | "Multiprocess sampling (2 chains in 2 jobs)\n",
|
1082 | 1084 | "NUTS: [mu, tau, theta]\n",
|
1083 |
| - "Sampling 2 chains for 2_000 tune and 5_000 draw iterations (4_000 + 10_000 draws total) took 13 seconds.\n", |
| 1085 | + "Sampling 2 chains for 2_000 tune and 5_000 draw iterations (4_000 + 10_000 draws total) took 14 seconds.\n", |
1084 | 1086 | "There were 129 divergences after tuning. Increase `target_accept` or reparameterize.\n",
|
1085 | 1087 | "We recommend running at least 4 chains for robust computation of convergence diagnostics\n",
|
1086 | 1088 | "Auto-assigning NUTS sampler...\n",
|
1087 | 1089 | "Initializing NUTS using jitter+adapt_diag...\n",
|
1088 | 1090 | "Multiprocess sampling (2 chains in 2 jobs)\n",
|
1089 | 1091 | "NUTS: [mu, tau, theta]\n",
|
1090 |
| - "Sampling 2 chains for 2_000 tune and 5_000 draw iterations (4_000 + 10_000 draws total) took 25 seconds.\n", |
| 1092 | + "Sampling 2 chains for 2_000 tune and 5_000 draw iterations (4_000 + 10_000 draws total) took 27 seconds.\n", |
1091 | 1093 | "There were 18 divergences after tuning. Increase `target_accept` or reparameterize.\n",
|
1092 | 1094 | "We recommend running at least 4 chains for robust computation of convergence diagnostics\n"
|
1093 | 1095 | ]
|
|
1111 | 1113 | "cell_type": "code",
|
1112 | 1114 | "execution_count": 17,
|
1113 | 1115 | "metadata": {},
|
1114 |
| - "outputs": [ |
1115 |
| - { |
1116 |
| - "data": { |
1117 |
| - "text/plain": [ |
1118 |
| - "189" |
1119 |
| - ] |
1120 |
| - }, |
1121 |
| - "execution_count": 17, |
1122 |
| - "metadata": {}, |
1123 |
| - "output_type": "execute_result" |
1124 |
| - } |
1125 |
| - ], |
1126 |
| - "source": [ |
1127 |
| - "longer_trace.sample_stats[\"diverging\"].sum().item()" |
1128 |
| - ] |
1129 |
| - }, |
1130 |
| - { |
1131 |
| - "cell_type": "code", |
1132 |
| - "execution_count": 18, |
1133 |
| - "metadata": {}, |
1134 | 1116 | "outputs": [
|
1135 | 1117 | {
|
1136 | 1118 | "data": {
|
|
1202 | 1184 | "4 0.036504 18 .99"
|
1203 | 1185 | ]
|
1204 | 1186 | },
|
1205 |
| - "execution_count": 18, |
| 1187 | + "execution_count": 17, |
1206 | 1188 | "metadata": {},
|
1207 | 1189 | "output_type": "execute_result"
|
1208 | 1190 | }
|
|
1239 | 1221 | "\n",
|
1240 | 1222 | "This behavior also has a nice geometric intuition. The more we decrease the step size the more the Hamiltonian Markov chain can explore the neck of the funnel. Consequently, the marginal posterior distribution for $log (\\tau)$ stretches further and further towards negative values with the decreasing step size. \n",
|
1241 | 1223 | "\n",
|
1242 |
| - "Since in `PyMC` after tuning we have a smaller step size than `Stan`, the geometery is better explored.\n", |
1243 |
| - "\n", |
1244 |
| - "However, the Hamiltonian transition is still not geometrically ergodic with respect to the centered implementation of the Eight Schools model. Indeed, this is expected given the observed bias." |
| 1224 | + "The Hamiltonian transition is still not geometrically ergodic with respect to the centered implementation of the Eight Schools model, as evidenced by the observed bias." |
1245 | 1225 | ]
|
1246 | 1226 | },
|
1247 | 1227 | {
|
1248 | 1228 | "cell_type": "code",
|
1249 |
| - "execution_count": 19, |
| 1229 | + "execution_count": 18, |
1250 | 1230 | "metadata": {},
|
1251 | 1231 | "outputs": [
|
1252 | 1232 | {
|
|
1278 | 1258 | },
|
1279 | 1259 | {
|
1280 | 1260 | "cell_type": "code",
|
1281 |
| - "execution_count": 20, |
| 1261 | + "execution_count": 19, |
1282 | 1262 | "metadata": {},
|
1283 | 1263 | "outputs": [
|
1284 | 1264 | {
|
|
1333 | 1313 | "$$\\tilde{\\theta}_{n} \\sim \\mathcal{N}(0, 1)$$\n",
|
1334 | 1314 | "$$\\theta_{n} = \\mu + \\tau \\cdot \\tilde{\\theta}_{n}.$$\n",
|
1335 | 1315 | "\n",
|
1336 |
| - "Stan model:\n", |
| 1316 | + "In Stan, this is specified as:\n", |
1337 | 1317 | "\n",
|
1338 | 1318 | "```C\n",
|
1339 | 1319 | "data {\n",
|
|
1360 | 1340 | " theta_tilde ~ normal(0, 1);\n",
|
1361 | 1341 | " y ~ normal(theta, sigma);\n",
|
1362 | 1342 | "}\n",
|
1363 |
| - "```" |
| 1343 | + "```\n", |
| 1344 | + "\n", |
| 1345 | + "Here is the corresponding `PyMC` model:" |
1364 | 1346 | ]
|
1365 | 1347 | },
|
1366 | 1348 | {
|
1367 | 1349 | "cell_type": "code",
|
1368 |
| - "execution_count": 21, |
| 1350 | + "execution_count": 20, |
1369 | 1351 | "metadata": {},
|
1370 | 1352 | "outputs": [],
|
1371 | 1353 | "source": [
|
|
1381 | 1363 | },
|
1382 | 1364 | {
|
1383 | 1365 | "cell_type": "code",
|
1384 |
| - "execution_count": 22, |
| 1366 | + "execution_count": 21, |
1385 | 1367 | "metadata": {},
|
1386 | 1368 | "outputs": [
|
1387 | 1369 | {
|
|
1397 | 1379 | {
|
1398 | 1380 | "data": {
|
1399 | 1381 | "application/vnd.jupyter.widget-view+json": {
|
1400 |
| - "model_id": "25be11a0d9654db68c8587169e5e2ab4", |
| 1382 | + "model_id": "9b2f351550ff440eac0da8fcfeff8d05", |
1401 | 1383 | "version_major": 2,
|
1402 | 1384 | "version_minor": 0
|
1403 | 1385 | },
|
|
1435 | 1417 | },
|
1436 | 1418 | {
|
1437 | 1419 | "cell_type": "code",
|
1438 |
| - "execution_count": 23, |
| 1420 | + "execution_count": 22, |
1439 | 1421 | "metadata": {},
|
1440 | 1422 | "outputs": [
|
1441 | 1423 | {
|
|
1733 | 1715 | "theta_t[7] 7091.0 1.0 "
|
1734 | 1716 | ]
|
1735 | 1717 | },
|
1736 |
| - "execution_count": 23, |
| 1718 | + "execution_count": 22, |
1737 | 1719 | "metadata": {},
|
1738 | 1720 | "output_type": "execute_result"
|
1739 | 1721 | }
|
|
1746 | 1728 | "cell_type": "markdown",
|
1747 | 1729 | "metadata": {},
|
1748 | 1730 | "source": [
|
1749 |
| - "As shown above, the effective sample size per iteration has drastically improved, and the trace plots no longer show any \"stickyness\". However, we do still see the rare divergence. These infrequent divergences do not seem concentrate anywhere in parameter space, which is indicative of the divergences being false positives." |
| 1731 | + "Notice that the effective sample size per iteration has drastically improved, and the trace plots demonstrate relatively homogeneous exploration. However, we do still see the rare divergence. These infrequent divergences do not seem concentrate anywhere in parameter space, which is indicative of the divergences being false positives." |
1750 | 1732 | ]
|
1751 | 1733 | },
|
1752 | 1734 | {
|
1753 | 1735 | "cell_type": "code",
|
1754 |
| - "execution_count": 24, |
| 1736 | + "execution_count": 23, |
1755 | 1737 | "metadata": {},
|
1756 | 1738 | "outputs": [
|
1757 | 1739 | {
|
|
1816 | 1798 | "cell_type": "markdown",
|
1817 | 1799 | "metadata": {},
|
1818 | 1800 | "source": [
|
1819 |
| - "As expected of false positives, we can remove the divergences entirely by decreasing the step size." |
| 1801 | + "As expected of false positives, we can remove the divergences almost entirely by decreasing the step size." |
1820 | 1802 | ]
|
1821 | 1803 | },
|
1822 | 1804 | {
|
1823 | 1805 | "cell_type": "code",
|
1824 |
| - "execution_count": 25, |
| 1806 | + "execution_count": 24, |
1825 | 1807 | "metadata": {},
|
1826 | 1808 | "outputs": [
|
1827 | 1809 | {
|
|
1837 | 1819 | {
|
1838 | 1820 | "data": {
|
1839 | 1821 | "application/vnd.jupyter.widget-view+json": {
|
1840 |
| - "model_id": "9f2e99d6c14743a39e2c7645e94fbcac", |
| 1822 | + "model_id": "9ccf9f10055746508099d9aac64a8b7f", |
1841 | 1823 | "version_major": 2,
|
1842 | 1824 | "version_minor": 0
|
1843 | 1825 | },
|
|
1893 | 1875 | },
|
1894 | 1876 | {
|
1895 | 1877 | "cell_type": "code",
|
1896 |
| - "execution_count": 26, |
| 1878 | + "execution_count": 25, |
1897 | 1879 | "metadata": {},
|
1898 | 1880 | "outputs": [
|
1899 | 1881 | {
|
|
1926 | 1908 | },
|
1927 | 1909 | {
|
1928 | 1910 | "cell_type": "code",
|
1929 |
| - "execution_count": 27, |
| 1911 | + "execution_count": 26, |
1930 | 1912 | "metadata": {},
|
1931 | 1913 | "outputs": [
|
1932 | 1914 | {
|
|
1972 | 1954 | "* Updated by Agustina Arroyuelo in February 2018, ([pymc#2861](https://github.com/pymc-devs/pymc/pull/2861))\n",
|
1973 | 1955 | "* Updated by [@CloudChaoszero](https://github.com/CloudChaoszero) in January 2021, ([pymc-examples#25](https://github.com/pymc-devs/pymc-examples/pull/25))\n",
|
1974 | 1956 | "* Updated Markdown and styling by @reshamas in August 2022, ([pymc-examples#402](https://github.com/pymc-devs/pymc-examples/pull/402))\n",
|
1975 |
| - "* Updated by @fonnesbeck in August 2024\n" |
| 1957 | + "* Updated by @fonnesbeck in August 2024 ([pymc-examples#699](https://github.com/pymc-devs/pymc-examples/pull/699))\n" |
1976 | 1958 | ]
|
1977 | 1959 | },
|
1978 | 1960 | {
|
1979 | 1961 | "cell_type": "code",
|
1980 |
| - "execution_count": 28, |
| 1962 | + "execution_count": 27, |
1981 | 1963 | "metadata": {},
|
1982 | 1964 | "outputs": [
|
1983 | 1965 | {
|
|
1990 | 1972 | "Python version : 3.12.5\n",
|
1991 | 1973 | "IPython version : 8.27.0\n",
|
1992 | 1974 | "\n",
|
1993 |
| - "pymc : 5.16.2\n", |
1994 |
| - "matplotlib: 3.9.2\n", |
1995 | 1975 | "arviz : 0.19.0\n",
|
| 1976 | + "pymc : 5.16.2\n", |
1996 | 1977 | "pandas : 2.2.2\n",
|
1997 | 1978 | "numpy : 1.26.4\n",
|
| 1979 | + "matplotlib: 3.9.2\n", |
1998 | 1980 | "\n",
|
1999 | 1981 | "Watermark: 2.4.3\n",
|
2000 | 1982 | "\n"
|
|
2013 | 1995 | ":::{include} ../page_footer.md\n",
|
2014 | 1996 | ":::"
|
2015 | 1997 | ]
|
2016 |
| - }, |
2017 |
| - { |
2018 |
| - "cell_type": "code", |
2019 |
| - "execution_count": null, |
2020 |
| - "metadata": {}, |
2021 |
| - "outputs": [], |
2022 |
| - "source": [] |
2023 | 1998 | }
|
2024 | 1999 | ],
|
2025 | 2000 | "metadata": {
|
|
0 commit comments