|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 |
| - "# Quickstart" |
| 7 | + "# LittleMCMC Quickstart\n", |
| 8 | + "\n", |
| 9 | + "LittleMCMC is a lightweight and performant implementation of HMC and NUTS in Python, spun out of the PyMC project. In this quickstart tutorial, we will introduce LittleMCMC\n", |
| 10 | + "\n", |
| 11 | + "## Table of Contents\n", |
| 12 | + "\n", |
| 13 | + "- [Who should use LittleMCMC?](#Who-should-use-LittleMCMC?)\n", |
| 14 | + "- [Sampling](#Sampling)\n", |
| 15 | + " - [Inspecting the Output of `lmc.sample`](#Inspecting-the-Output-of-lmc.sample)\n", |
| 16 | + "- [Other Modules](#Other-Modules)\n", |
| 17 | + "\n", |
| 18 | + "## Who should use LittleMCMC?\n", |
| 19 | + "\n", |
| 20 | + "<div class=\"alert alert-block alert-info\">\n", |
| 21 | + "LittleMCMC is a fairly bare bones library with a very niche use case. Most users will probably find that [PyMC3](https://github.com/pymc-devs/pymc3) will satisfy their needs, with better strength of support and quality of documentation.\n", |
| 22 | + "</div>\n", |
| 23 | + "\n", |
| 24 | + "If you:\n", |
| 25 | + "\n", |
| 26 | + "1. Have model with only continuous parameters,\n", |
| 27 | + "1. Are willing to manually \"unconstrain\" all of your model's parameters (if necessary),\n", |
| 28 | + "1. Have methods to compute the log probability of the model and its derivative, exposed via a Python callable,\n", |
| 29 | + "1. And all you need is an implementation of HMC/NUTS (preferably in Python) to sample from your model\n", |
| 30 | + "\n", |
| 31 | + "then you should consider using LittleMCMC!\n", |
| 32 | + "\n", |
| 33 | + "## Sampling" |
8 | 34 | ]
|
9 | 35 | },
|
10 | 36 | {
|
|
38 | 64 | },
|
39 | 65 | {
|
40 | 66 | "cell_type": "code",
|
41 |
| - "execution_count": null, |
| 67 | + "execution_count": 3, |
42 | 68 | "metadata": {},
|
43 |
| - "outputs": [], |
44 |
| - "source": [] |
| 69 | + "outputs": [ |
| 70 | + { |
| 71 | + "name": "stderr", |
| 72 | + "output_type": "stream", |
| 73 | + "text": [ |
| 74 | + "/Users/george/miniconda3/lib/python3.6/site-packages/ipykernel_launcher.py:2: RuntimeWarning: divide by zero encountered in log\n", |
| 75 | + " \n", |
| 76 | + "/Users/george/miniconda3/lib/python3.6/site-packages/ipykernel_launcher.py:2: RuntimeWarning: divide by zero encountered in log\n", |
| 77 | + " \n" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "name": "stdout", |
| 82 | + "output_type": "stream", |
| 83 | + "text": [ |
| 84 | + "\n" |
| 85 | + ] |
| 86 | + } |
| 87 | + ], |
| 88 | + "source": [ |
| 89 | + "trace, stats, results = lmc.sample(\n", |
| 90 | + " logp_dlogp_func=logp_dlogp_func,\n", |
| 91 | + " size=1,\n", |
| 92 | + " draws=1000,\n", |
| 93 | + " tune=500,\n", |
| 94 | + " step=lmc.NUTS(logp_dlogp_func=logp_dlogp_func, size=1),\n", |
| 95 | + " chains=4,\n", |
| 96 | + " cores=4,\n", |
| 97 | + " progressbar=\"notebook\"\n", |
| 98 | + ")" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "markdown", |
| 103 | + "metadata": {}, |
| 104 | + "source": [ |
| 105 | + "### Inspecting the Output of `lmc.sample`" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "code", |
| 110 | + "execution_count": 4, |
| 111 | + "metadata": {}, |
| 112 | + "outputs": [ |
| 113 | + { |
| 114 | + "data": { |
| 115 | + "text/plain": [ |
| 116 | + "array([-0.38331274, -1.76994233, -0.67234733, ..., 0.27817656,\n", |
| 117 | + " 0.29250676, 0.42966184])" |
| 118 | + ] |
| 119 | + }, |
| 120 | + "execution_count": 4, |
| 121 | + "metadata": {}, |
| 122 | + "output_type": "execute_result" |
| 123 | + } |
| 124 | + ], |
| 125 | + "source": [ |
| 126 | + "trace" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "code", |
| 131 | + "execution_count": 5, |
| 132 | + "metadata": {}, |
| 133 | + "outputs": [ |
| 134 | + { |
| 135 | + "data": { |
| 136 | + "text/plain": [ |
| 137 | + "(4000,)" |
| 138 | + ] |
| 139 | + }, |
| 140 | + "execution_count": 5, |
| 141 | + "metadata": {}, |
| 142 | + "output_type": "execute_result" |
| 143 | + } |
| 144 | + ], |
| 145 | + "source": [ |
| 146 | + "trace.shape" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "code", |
| 151 | + "execution_count": 6, |
| 152 | + "metadata": {}, |
| 153 | + "outputs": [ |
| 154 | + { |
| 155 | + "data": { |
| 156 | + "text/plain": [ |
| 157 | + "{'depth': array([1, 1, 1, ..., 1, 2, 1]),\n", |
| 158 | + " 'step_size': array([0.94586326, 0.94586326, 0.94586326, ..., 2.16938615, 2.16938615,\n", |
| 159 | + " 2.16938615]),\n", |
| 160 | + " 'tune': array([False, False, False, ..., False, False, False]),\n", |
| 161 | + " 'mean_tree_accept': array([1. , 0.43665689, 1. , ..., 0.98765583, 0.72296808,\n", |
| 162 | + " 0.97965297]),\n", |
| 163 | + " 'step_size_bar': array([1.20597596, 1.20597596, 1.20597596, ..., 1.28614833, 1.28614833,\n", |
| 164 | + " 1.28614833]),\n", |
| 165 | + " 'tree_size': array([1., 1., 1., ..., 1., 3., 1.]),\n", |
| 166 | + " 'diverging': array([False, False, False, ..., False, False, False]),\n", |
| 167 | + " 'energy_error': array([-0.25675836, 0.82860753, -0.74393026, ..., 0.01242099,\n", |
| 168 | + " 0.00169732, 0.02055688]),\n", |
| 169 | + " 'energy': array([1.25393394, 2.56056236, 1.91071276, ..., 0.95981431, 1.76229677,\n", |
| 170 | + " 1.02575724]),\n", |
| 171 | + " 'max_energy_error': array([-0.25675836, 0.82860753, -0.74393026, ..., 0.01242099,\n", |
| 172 | + " 0.56981615, 0.02055688]),\n", |
| 173 | + " 'model_logp': array([-0.99240286, -2.48528646, -1.144964 , ..., -0.95762963,\n", |
| 174 | + " -0.96171864, -1.01124318])}" |
| 175 | + ] |
| 176 | + }, |
| 177 | + "execution_count": 6, |
| 178 | + "metadata": {}, |
| 179 | + "output_type": "execute_result" |
| 180 | + } |
| 181 | + ], |
| 182 | + "source": [ |
| 183 | + "stats" |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "code", |
| 188 | + "execution_count": 7, |
| 189 | + "metadata": {}, |
| 190 | + "outputs": [ |
| 191 | + { |
| 192 | + "data": { |
| 193 | + "text/plain": [ |
| 194 | + "(4000,)" |
| 195 | + ] |
| 196 | + }, |
| 197 | + "execution_count": 7, |
| 198 | + "metadata": {}, |
| 199 | + "output_type": "execute_result" |
| 200 | + } |
| 201 | + ], |
| 202 | + "source": [ |
| 203 | + "stats[\"diverging\"].shape" |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "markdown", |
| 208 | + "metadata": {}, |
| 209 | + "source": [ |
| 210 | + "## Other Modules\n", |
| 211 | + "\n", |
| 212 | + "LittleMCMC exposes:\n", |
| 213 | + "\n", |
| 214 | + "1. Two step methods: Hamiltonian Monte Carlo (HMC) and the No-U-Turn Sampler (NUTS)\n", |
| 215 | + "1. Quadpotentials (a.k.a. mass matrices or inverse metrics)\n", |
| 216 | + "1. Dual-averaging step size adaptation\n", |
| 217 | + "1. Leapfrog integration\n", |
| 218 | + "\n", |
| 219 | + "Refer to the [API Reference](https://littlemcmc.readthedocs.io/en/latest/api.html) for more information." |
| 220 | + ] |
45 | 221 | }
|
46 | 222 | ],
|
47 | 223 | "metadata": {
|
|
0 commit comments