Skip to content

Commit f99a805

Browse files
committed
better docs
1 parent 1715d90 commit f99a805

1 file changed

Lines changed: 62 additions & 56 deletions

File tree

docsrc/examples/MCMC Sampling.ipynb

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,58 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# MCMC Sampling\n",
7+
"# MCMC Sampling"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## Overview\n",
815
"\n",
9-
"The [CmdStanModel](https://mc-stan.org/cmdstanpy/api.html#cmdstanmodel) object's\n",
10-
"method [sample](https://mc-stan.org/cmdstanpy/api.html#cmdstanpy.CmdStanModel.sample)\n",
11-
"invokes Stan's adaptive HMC-NUTS sampler which uses the Hamiltonian Monte Carlo (HMC) algorithm\n",
12-
"and its adaptive variant the no-U-turn sampler (NUTS) to produce a set of\n",
13-
"draws from the posterior distribution of the model parameters conditioned on the data.\n",
16+
"Stan's MCMC sampler implements the Hamiltonian Monte Carlo (HMC) algorithm and its adaptive variant\n",
17+
"the no-U-turn sampler (NUTS).\n",
18+
"It creates a set of draws from the posterior distribution of the model conditioned on the data,\n",
19+
"allowing for exact Bayesian inference of the model parameters.\n",
20+
"Each draw consists of the values for all parameter, transformed parameter, and\n",
21+
"generated quantities variables, reported on the constrained scale.\n",
1422
"\n",
15-
"The `sample` method returns a [CmdStanMCMC](https://mc-stan.org/cmdstanpy/api.html#cmdstanmcmc) object.\n",
16-
"Underlyingly, the sampler run outputs are a set of per-chain Stan CSV files.\n",
17-
"The `CmdStanMCMC` object provide multiple accessor functions which allow the user\n",
18-
"to access the resulting sample in whatever data format is needed for further analysis.\n",
23+
"The [CmdStanModel sample](https://mc-stan.org/cmdstanpy/api.html#cmdstanpy.CmdStanModel.sample) method\n",
24+
"wraps the CmdStan [sample](https://mc-stan.org/docs/cmdstan-guide/mcmc-config.html) method.\n",
25+
"Underlyingly, the CmdStan outputs are a set of per-chain Stan CSV files.\n",
26+
"In addition to the resulting sample, reported as one row per draw,\n",
27+
"the Stan CSV files encode information about the inference engine configuration\n",
28+
"and the sampler state.\n",
29+
"The NUTS-HMC adaptive sampler algorithm also outputs the per-chain\n",
30+
"HMC tuning parameters `step_size` and `metric`.\n",
1931
"\n",
20-
"The sample can be extracted in tabular format, either as\n",
32+
"The `sample` method returns a [CmdStanMCMC](https://mc-stan.org/cmdstanpy/api.html#cmdstanmcmc) object,\n",
33+
"which provides access to the disparate information from the Stan CSV files.\n",
34+
"Accessor functions allow the user\n",
35+
"to access the sample in whatever data format is needed for further analysis.\n",
2136
"\n",
22-
"- an [numpy.ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)\n",
37+
"- The sample can be extracted in tabular format, either as\n",
2338
"\n",
24-
"- a [pandas.DataFrame](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html#pandas.DataFrame)\n",
39+
" + an [numpy.ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)\n",
2540
"\n",
26-
"The sample can be treated as a collection of named, structured variables, and extracted as\n",
41+
" + a [pandas.DataFrame](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html#pandas.DataFrame)\n",
2742
"\n",
28-
"- a Python `dict` mapping names to `numpy.ndarray` objects\n",
43+
"- The sample can be treated as a collection of named, structured variables, and extracted as\n",
2944
"\n",
30-
"- an [xarray.Dataset](https://docs.xarray.dev/en/stable/generated/xarray.Dataset.html)\n",
45+
" + a Python `dict` mapping names to `numpy.ndarray` objects\n",
3146
"\n",
32-
"The CmdStanMCMC object also provides access to the per-chain HMC tuning parameters `step_size` and `metric`\n",
33-
"and the [InferenceMetadata](https://mc-stan.org/cmdstanpy/internal_api.html#inferencemetadata)\n",
34-
"which consists of the CmdStan configuration, the layout of the CSV file data table,\n",
35-
"and the mapping between the table columns and the Stan program structured variables.\n",
47+
" + an [xarray.Dataset](https://docs.xarray.dev/en/stable/generated/xarray.Dataset.html)\n",
3648
"\n",
3749
"\n",
38-
"\n"
50+
"In addtion, the `CmdStanMCMC` object has accessor methods for\n",
51+
"\n",
52+
"- The per-chain HMC tuning parameters `step_size` and `metric` \n",
53+
"\n",
54+
"- The CmdStan run configuration and console outputs\n",
55+
"\n",
56+
"- The sampler algorithm diagnostics\n",
57+
"\n",
58+
"- The mapping between the Stan model variables and the corresponding CSV file columns."
3959
]
4060
},
4161
{
@@ -185,25 +205,7 @@
185205
"cell_type": "markdown",
186206
"metadata": {},
187207
"source": [
188-
"## Summarizing the sample"
189-
]
190-
},
191-
{
192-
"cell_type": "code",
193-
"execution_count": null,
194-
"metadata": {
195-
"scrolled": true
196-
},
197-
"outputs": [],
198-
"source": [
199-
"fit.summary()"
200-
]
201-
},
202-
{
203-
"cell_type": "markdown",
204-
"metadata": {},
205-
"source": [
206-
"## Analyzing the sample"
208+
"## Accessing the sampler outputs"
207209
]
208210
},
209211
{
@@ -322,27 +324,18 @@
322324
"cell_type": "markdown",
323325
"metadata": {},
324326
"source": [
325-
"### Saving the sampler output files"
326-
]
327-
},
328-
{
329-
"cell_type": "markdown",
330-
"metadata": {},
331-
"source": [
332-
"The sampler output files are written to a temporary directory which\n",
333-
"is deleted upon session exit unless the ``output_dir`` argument is specified.\n",
334-
"The ``save_csvfiles`` function moves the CmdStan CSV output files\n",
335-
"to a specified directory without having to re-run the sampler.\n",
336-
"The console output files are not saved. These files are treated as ephemeral; if the sample is valid, all relevant information is recorded in the CSV files."
327+
"## Summarizing the sample"
337328
]
338329
},
339330
{
340331
"cell_type": "code",
341332
"execution_count": null,
342-
"metadata": {},
333+
"metadata": {
334+
"scrolled": true
335+
},
343336
"outputs": [],
344337
"source": [
345-
"# fit.save_csvfiles(dir=\"some_dir\")"
338+
"fit.summary()"
346339
]
347340
},
348341
{
@@ -369,7 +362,7 @@
369362
"cell_type": "markdown",
370363
"metadata": {},
371364
"source": [
372-
"#### eight_schools.stan"
365+
"**eight_schools.stan**"
373366
]
374367
},
375368
{
@@ -386,7 +379,7 @@
386379
"cell_type": "markdown",
387380
"metadata": {},
388381
"source": [
389-
"#### eight_schools.data.json"
382+
"**eight_schools.data.json**"
390383
]
391384
},
392385
{
@@ -434,6 +427,19 @@
434427
"source": [
435428
"print(eight_schools_fit.diagnose())"
436429
]
430+
},
431+
{
432+
"cell_type": "markdown",
433+
"metadata": {},
434+
"source": [
435+
"## Saving the sampler output files\n",
436+
"\n",
437+
"The sampler output files are written to a temporary directory which\n",
438+
"is deleted upon session exit unless the ``output_dir`` argument is specified.\n",
439+
"The ``save_csvfiles`` function moves the CmdStan CSV output files\n",
440+
"to a specified directory without having to re-run the sampler.\n",
441+
"The console output files are not saved. These files are treated as ephemeral; if the sample is valid, all relevant information is recorded in the CSV files."
442+
]
437443
}
438444
],
439445
"metadata": {

0 commit comments

Comments
 (0)