Skip to content

Commit 3c478ec

Browse files
committed
edits to notebook
1 parent 77625e6 commit 3c478ec

1 file changed

Lines changed: 32 additions & 8 deletions

File tree

docsrc/examples/VI as Sampler Inits.ipynb

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@
88
"\n",
99
"In this example we show how to use the parameter estimates return by Stan's variational inference algorithm\n",
1010
"as the initial parameter values for Stan's NUTS-HMC sampler.\n",
11-
"The program and data are taken from the [posteriordb package](https://github.com/stan-dev/posteriordb).\n",
12-
"\n",
13-
"The experiments reported in the paper [Pathfinder: Parallel quasi-Newton variational inference](https://arxiv.org/abs/2108.03782) by Zhang et al. show that mean-field ADVI provides a better estimate of the posterior, as measured by the 1-Wasserstein distance to the reference posterior, than 75 iterations of the warmup Phase I algorithm used by the NUTS-HMC sampler, furthermore, ADVI is more computationally efficient, requiring fewer evaluations of the log density and gradient functions. Therefore, using the estimates from ADVI to initialize the parameter values for the NUTS-HMC sampler will allow the sampler to do a better job of adapting the stepsize and metric during warmup, resulting in better performance and estimation.\n",
14-
"\n",
11+
"By default, the sampler algorithm randomly initializes all model parameters in the range uniform\\[-2, 2\\]. When the true parameter value is outside of this range, starting from the ADVI estimates will speed up and improve adaptation.\n",
1512
"\n",
1613
"### Model and data\n",
1714
"\n",
15+
"The Stan model and data are taken from the [posteriordb package](https://github.com/stan-dev/posteriordb).\n",
16+
"\n",
1817
"We use the [blr model](https://github.com/stan-dev/posteriordb/blob/master/posterior_database/models/stan/blr.stan),\n",
1918
"a Bayesian standard linear regression model with noninformative priors,\n",
2019
"and its corresponding simulated dataset [sblri.json](https://github.com/stan-dev/posteriordb/blob/master/posterior_database/data/data/sblri.json.zip),\n",
2120
"which was simulated via script [sblr.R](https://github.com/stan-dev/posteriordb/blob/master/posterior_database/data/data-raw/sblr/sblr.R).\n",
22-
"For conveince, we have copied the posteriordb model and data to this directory, in files [`blr.stan`](blr.stan) and [`sblri.json`](sblri.json)."
21+
"For conveince, we have copied the posteriordb model and data to this directory, in files `blr.stan` and `sblri.json`."
2322
]
2423
},
2524
{
@@ -81,11 +80,11 @@
8180
"cell_type": "markdown",
8281
"metadata": {},
8382
"source": [
84-
"Posteriordb provides reference posteriors for all models. For the blr model, conditioned on the dataset `sblri.json`, the reference posteriors are in file [`sblri-blr.json`](https://github.com/stan-dev/posteriordb/blob/master/posterior_database/reference_posteriors/summary_statistics/mean/mean/sblri-blr.json)\n",
83+
"Posteriordb provides reference posteriors for all models. For the blr model, conditioned on the dataset `sblri.json`, the reference posteriors are in file [sblri-blr.json](https://github.com/stan-dev/posteriordb/blob/master/posterior_database/reference_posteriors/summary_statistics/mean/mean/sblri-blr.json)\n",
8584
"\n",
8685
"The reference posteriors for all elements of `beta` and `sigma` are all very close to $1.0$.\n",
8786
"\n",
88-
"By default, the sampler algorithm randomly initializes all model parameters in the range uniform[-2, 2]. The ADVI estimates will provide a better starting point, and will therefore allow us to shorten the number of warmup iterations."
87+
"The experiments reported in the paper [Pathfinder: Parallel quasi-Newton variational inference](https://arxiv.org/abs/2108.03782) by Zhang et al. show that mean-field ADVI provides a better estimate of the posterior, as measured by the 1-Wasserstein distance to the reference posterior, than 75 iterations of the warmup Phase I algorithm used by the NUTS-HMC sampler, furthermore, ADVI is more computationally efficient, requiring fewer evaluations of the log density and gradient functions. Therefore, using the estimates from ADVI to initialize the parameter values for the NUTS-HMC sampler will allow the sampler to do a better job of adapting the stepsize and metric during warmup, resulting in better performance and estimation.\n"
8988
]
9089
},
9190
{
@@ -113,7 +112,23 @@
113112
"cell_type": "markdown",
114113
"metadata": {},
115114
"source": [
116-
"The sampler estimates match the reference posterior. If we run the HMC sampler for only 75 warmup iterations with random inits, it fails to estimate `sigma`, and produces fewer effective samples per second (N_Eff/s)."
115+
"The sampler estimates match the reference posterior."
116+
]
117+
},
118+
{
119+
"cell_type": "code",
120+
"execution_count": null,
121+
"metadata": {},
122+
"outputs": [],
123+
"source": [
124+
"print(mcmc_vb_inits_fit.diagnose())"
125+
]
126+
},
127+
{
128+
"cell_type": "markdown",
129+
"metadata": {},
130+
"source": [
131+
"Using the default random parameter initializations, we need to run more warmup iteratons. If we only run 75 warmup iterations with random inits, the result fails to estimate `sigma` correctly. It is necessary to run the model with at least 150 warmup iterations to produce a good set of estimates."
117132
]
118133
},
119134
{
@@ -133,6 +148,15 @@
133148
"source": [
134149
"mcmc_random_inits_fit.summary()"
135150
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": null,
155+
"metadata": {},
156+
"outputs": [],
157+
"source": [
158+
"print(mcmc_random_inits_fit.diagnose())"
159+
]
136160
}
137161
],
138162
"metadata": {

0 commit comments

Comments
 (0)