|
24 | 24 | "processor cores available." |
25 | 25 | ] |
26 | 26 | }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "## Prerequisites\n", |
| 32 | + "\n", |
| 33 | + "\n", |
| 34 | + "CmdStanPy displays progress bars during sampling via use of package [tqdm](https://github.com/tqdm/tqdm).\n", |
| 35 | + "In order for these to display properly, you must have the \n", |
| 36 | + "[ipywidgets](https://ipywidgets.readthedocs.io/en/latest/index.html) package installed,\n", |
| 37 | + "and depending on your version of Jupyter or JupyterLab, you must enable it via command:" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": null, |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [], |
| 45 | + "source": [ |
| 46 | + "!jupyter nbextension enable --py widgetsnbextension" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "For more information, see the the\n", |
| 54 | + "[installation instructions](https://ipywidgets.readthedocs.io/en/latest/user_install.html#), \n", |
| 55 | + "also [this tqdm GitHub issue](https://github.com/tqdm/tqdm/issues/394#issuecomment-384743637).\n", |
| 56 | + "\n", |
| 57 | + "\n", |
| 58 | + " " |
| 59 | + ] |
| 60 | + }, |
27 | 61 | { |
28 | 62 | "cell_type": "markdown", |
29 | 63 | "metadata": {}, |
|
45 | 79 | "outputs": [], |
46 | 80 | "source": [ |
47 | 81 | "import os\n", |
48 | | - "from cmdstanpy.model import CmdStanModel\n", |
49 | | - "from cmdstanpy.utils import cmdstan_path\n", |
| 82 | + "from cmdstanpy import CmdStanModel, cmdstan_path\n", |
50 | 83 | " \n", |
51 | 84 | "bernoulli_dir = os.path.join(cmdstan_path(), 'examples', 'bernoulli')\n", |
52 | | - "bernoulli_path = os.path.join(bernoulli_dir, 'bernoulli.stan')\n", |
53 | | - "bernoulli_data = os.path.join(bernoulli_dir, 'bernoulli.data.json')\n", |
| 85 | + "stan_file = os.path.join(bernoulli_dir, 'bernoulli.stan')\n", |
| 86 | + "data_file = os.path.join(bernoulli_dir, 'bernoulli.data.json')\n", |
54 | 87 | "\n", |
55 | 88 | "# instantiate, compile bernoulli model\n", |
56 | | - "bernoulli_model = CmdStanModel(stan_file=bernoulli_path)" |
| 89 | + "model = CmdStanModel(stan_file=stan_file)" |
57 | 90 | ] |
58 | 91 | }, |
59 | 92 | { |
|
72 | 105 | "outputs": [], |
73 | 106 | "source": [ |
74 | 107 | "# run CmdStan's sample method, returns object `CmdStanMCMC`\n", |
75 | | - "bern_fit = bernoulli_model.sample(data=bernoulli_data)" |
| 108 | + "fit = model.sample(data=data_file)" |
76 | 109 | ] |
77 | 110 | }, |
78 | 111 | { |
|
91 | 124 | "metadata": {}, |
92 | 125 | "outputs": [], |
93 | 126 | "source": [ |
94 | | - "print('sampler diagnostic variables:\\n{}'.format(bern_fit.metadata.method_vars_cols.keys()))\n", |
95 | | - "print('stan model variables:\\n{}'.format(bern_fit.metadata.stan_vars_cols.keys()))" |
| 127 | + "print('sampler diagnostic variables:\\n{}'.format(fit.metadata.method_vars_cols.keys()))\n", |
| 128 | + "print('stan model variables:\\n{}'.format(fit.metadata.stan_vars_cols.keys()))" |
96 | 129 | ] |
97 | 130 | }, |
98 | 131 | { |
|
101 | 134 | "metadata": {}, |
102 | 135 | "outputs": [], |
103 | 136 | "source": [ |
104 | | - "bern_fit.summary()" |
| 137 | + "fit.summary()" |
105 | 138 | ] |
106 | 139 | }, |
107 | 140 | { |
|
117 | 150 | "metadata": {}, |
118 | 151 | "outputs": [], |
119 | 152 | "source": [ |
120 | | - "print(bern_fit.draws().shape)\n", |
121 | | - "bern_fit.draws_pd().head()" |
| 153 | + "print(fit.draws().shape)\n", |
| 154 | + "fit.draws_pd().head()" |
122 | 155 | ] |
123 | 156 | }, |
124 | 157 | { |
|
134 | 167 | "metadata": {}, |
135 | 168 | "outputs": [], |
136 | 169 | "source": [ |
137 | | - "bern_fit.draws_xr()" |
| 170 | + "fit.draws_xr()" |
138 | 171 | ] |
139 | 172 | }, |
140 | 173 | { |
141 | 174 | "cell_type": "markdown", |
142 | 175 | "metadata": {}, |
143 | 176 | "source": [ |
144 | | - "The ``bern_fit`` object records the command, the return code,\n", |
| 177 | + "The ``fit`` object records the command, the return code,\n", |
145 | 178 | "and the paths to the sampler output csv and console files.\n", |
146 | 179 | "The string representation of this object displays the CmdStan commands and\n", |
147 | 180 | "the location of the output files.\n", |
|
160 | 193 | "metadata": {}, |
161 | 194 | "outputs": [], |
162 | 195 | "source": [ |
163 | | - "bern_fit" |
| 196 | + "fit" |
164 | 197 | ] |
165 | 198 | }, |
166 | 199 | { |
|
178 | 211 | "cell_type": "markdown", |
179 | 212 | "metadata": {}, |
180 | 213 | "source": [ |
181 | | - "## Running a data-generating model using `fixed_param=True`\n", |
| 214 | + "### Sampler Progress\n", |
| 215 | + "\n", |
| 216 | + "Your model make take a long time to fit. The `sample` method provides two arguments:\n", |
| 217 | + " \n", |
| 218 | + "- visual progress bar: `show_progress=True`\n", |
| 219 | + "- stream CmdStan ouput to the console - `show_console=True`\n", |
| 220 | + " \n", |
| 221 | + "To illustrate how progress bars work, we will run the bernoulli model. Since the progress bars are only visible while the sampler is running and the bernoulli model takes no time at all to fit, we run this model for 200K iterations, in order to see the progress bars in action." |
| 222 | + ] |
| 223 | + }, |
| 224 | + { |
| 225 | + "cell_type": "code", |
| 226 | + "execution_count": null, |
| 227 | + "metadata": {}, |
| 228 | + "outputs": [], |
| 229 | + "source": [ |
| 230 | + "fit = model.sample(data=data_file, iter_warmup=100000, iter_sampling=100000, show_progress=True)\n" |
| 231 | + ] |
| 232 | + }, |
| 233 | + { |
| 234 | + "cell_type": "markdown", |
| 235 | + "metadata": {}, |
| 236 | + "source": [ |
| 237 | + "The Stan language `print` statement can be use to monitor the Stan program state.\n", |
| 238 | + "In order to see this information as the sampler is running, use the `show_console=True` argument.\n", |
| 239 | + "This will stream all CmdStan messages to the terminal while the sampler is running.\n" |
| 240 | + ] |
| 241 | + }, |
| 242 | + { |
| 243 | + "cell_type": "code", |
| 244 | + "execution_count": null, |
| 245 | + "metadata": {}, |
| 246 | + "outputs": [], |
| 247 | + "source": [ |
| 248 | + "fit = model.sample(data=data_file, chains=2, parallel_chains=1, show_console=True)\n", |
| 249 | + "\n" |
| 250 | + ] |
| 251 | + }, |
| 252 | + { |
| 253 | + "cell_type": "markdown", |
| 254 | + "metadata": {}, |
| 255 | + "source": [ |
| 256 | + "## Running a data-generating model\n", |
182 | 257 | "\n", |
183 | 258 | "In this example we use the CmdStan example model\n", |
184 | | - "[bernoulli_datagen.stan](https://github.com/stan-dev/cmdstanpy/blob/master/docs/notebooks/bernoulli_datagen.stan)\n", |
| 259 | + "[data_filegen.stan](https://github.com/stan-dev/cmdstanpy/blob/master/docs/notebooks/data_filegen.stan)\n", |
185 | 260 | "to generate a simulated dataset given fixed data values." |
186 | 261 | ] |
187 | 262 | }, |
|
191 | 266 | "metadata": {}, |
192 | 267 | "outputs": [], |
193 | 268 | "source": [ |
194 | | - "datagen_model = CmdStanModel(stan_file='bernoulli_datagen.stan')\n", |
| 269 | + "model_datagen = CmdStanModel(stan_file='bernoulli_datagen.stan')\n", |
195 | 270 | "datagen_data = {'N':300, 'theta':0.3}\n", |
196 | | - "sim_data = datagen_model.sample(data=datagen_data, fixed_param=True)\n", |
197 | | - "sim_data.summary()" |
| 271 | + "fit_sim = model_datagen.sample(data=datagen_data, fixed_param=True)\n", |
| 272 | + "fit_sim.summary()" |
198 | 273 | ] |
199 | 274 | }, |
200 | 275 | { |
|
210 | 285 | "metadata": {}, |
211 | 286 | "outputs": [], |
212 | 287 | "source": [ |
213 | | - "drawset_pd = sim_data.draws_pd()\n", |
| 288 | + "drawset_pd = fit_sim.draws_pd()\n", |
214 | 289 | "drawset_pd.columns\n", |
215 | 290 | "\n", |
216 | 291 | "# restrict to columns over new outcomes of N Bernoulli trials\n", |
|
238 | 313 | "name": "python", |
239 | 314 | "nbconvert_exporter": "python", |
240 | 315 | "pygments_lexer": "ipython3", |
241 | | - "version": "3.9.5" |
| 316 | + "version": "3.8.5" |
242 | 317 | } |
243 | 318 | }, |
244 | 319 | "nbformat": 4, |
|
0 commit comments