Skip to content

Commit 47e2356

Browse files
committed
updating tutorials
1 parent 6accd75 commit 47e2356

21 files changed

Lines changed: 123 additions & 144 deletions

docs/tutorials/autocorr.rst

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Autocorrelation analysis & convergence
1212

1313
In this tutorial, we will discuss a method for convincing yourself that
1414
your chains are sufficiently converged. This can be a difficult subject
15-
to discuss because it isnt formally possible to guarantee convergence
15+
to discuss because it isn't formally possible to guarantee convergence
1616
for any but the simplest models, and therefore any argument that you
1717
make will be circular and heuristic. However, some discussion of
1818
autocorrelation analysis is (or should be!) a necessary part of any
@@ -72,13 +72,13 @@ error is actually given by
7272
where :math:`\tau_f` is the *integrated autocorrelation time* for the
7373
chain :math:`f(\theta^{(n)})`. In other words, :math:`N/\tau_f` is the
7474
effective number of samples and :math:`\tau_f` is the number of steps
75-
that are needed before the chain forgets where it started. This means
75+
that are needed before the chain "forgets" where it started. This means
7676
that, if you can estimate :math:`\tau_f`, then you can estimate the
7777
number of samples that you need to generate to reduce the relative error
7878
on your target integral to (say) a few percent.
7979

8080
**Note:** It is important to remember that :math:`\tau_f` depends on the
81-
specific function :math:`f(\theta)`. This means that there isnt just
81+
specific function :math:`f(\theta)`. This means that there isn't just
8282
*one* integrated autocorrelation time for a given Markov chain. Instead,
8383
you must compute a different :math:`\tau_f` for any integral you
8484
estimate using the samples.
@@ -90,7 +90,7 @@ There is a great discussion of methods for autocorrelation estimation in
9090
`a set of lecture notes by Alan
9191
Sokal <https://pdfs.semanticscholar.org/0bfe/9e3db30605fe2d4d26e1a288a5e2997e7225.pdf>`__
9292
and the interested reader should take a look at that for a more formal
93-
discussion, but Ill include a summary of some of the relevant points
93+
discussion, but I'll include a summary of some of the relevant points
9494
here. The integrated autocorrelation time is defined as
9595

9696
.. math::
@@ -134,7 +134,7 @@ estimator for :math:`\rho_f(\tau)` as
134134
135135
\hat{\tau}_f \stackrel{?}{=} \sum_{\tau=-N}^{N} \hat{\rho}_f(\tau) = 1 + 2\,\sum_{\tau=1}^N \hat{\rho}_f(\tau)
136136
137-
but this isnt actually a very good idea. At longer lags,
137+
but this isn't actually a very good idea. At longer lags,
138138
:math:`\hat{\rho}_f(\tau)` starts to contain more noise than signal and
139139
summing all the way out to :math:`N` will result in a very noisy
140140
estimate of :math:`\tau_f`. Instead, we want to estimate :math:`\tau_f`
@@ -152,15 +152,15 @@ smallest value of :math:`M` where :math:`M \ge C\,\hat{\tau}_f (M)` for
152152
a constant :math:`C \sim 5`. Sokal says that he finds this procedure to
153153
work well for chains longer than :math:`1000\,\tau_f`, but the situation
154154
is a bit better with emcee because we can use the parallel chains to
155-
reduce the variance and weve found that chains longer than about
155+
reduce the variance and we've found that chains longer than about
156156
:math:`50\,\tau` are often sufficient.
157157

158158
A toy problem
159159
-------------
160160

161-
To demonstrate this method, well start by generating a set of chains
161+
To demonstrate this method, we'll start by generating a set of "chains"
162162
from a process with known autocorrelation structure. To generate a large
163-
enough dataset, well use `celerite <http://celerite.readthedocs.io>`__:
163+
enough dataset, we'll use `celerite <http://celerite.readthedocs.io>`__:
164164

165165
.. code:: python
166166
@@ -198,7 +198,7 @@ enough dataset, we’ll use `celerite <http://celerite.readthedocs.io>`__:
198198
.. image:: autocorr_files/autocorr_5_0.png
199199

200200

201-
Now well estimate the empirical autocorrelation function for each of
201+
Now we'll estimate the empirical autocorrelation function for each of
202202
these parallel chains and compare this to the true function.
203203

204204
.. code:: python
@@ -258,7 +258,7 @@ the empricial estimator is shown as a blue line.
258258
Instead of estimating the autocorrelation function using a single chain,
259259
we can assume that each chain is sampled from the same stochastic
260260
process and average the estimate over ensemble members to reduce the
261-
variance. It turns out that well actually do this averaging later in
261+
variance. It turns out that we'll actually do this averaging later in
262262
the process below, but it can be useful to show the mean autocorrelation
263263
function for visualization purposes.
264264

@@ -285,15 +285,15 @@ function for visualization purposes.
285285
.. image:: autocorr_files/autocorr_9_0.png
286286

287287

288-
Now lets estimate the autocorrelation time using these estimated
288+
Now let's estimate the autocorrelation time using these estimated
289289
autocorrelation functions. Goodman & Weare (2010) suggested averaging
290290
the ensemble over walkers and computing the autocorrelation function of
291291
the mean chain to lower the variance of the estimator and that was what
292292
was originally implemented in emcee. Since then, @fardal on GitHub
293293
`suggested that other estimators might have lower
294294
variance <https://github.com/dfm/emcee/issues/209>`__. This is
295295
absolutely correct and, instead of the Goodman & Weare method, we now
296-
recommend computing the autocorrelation time for each walker (its
296+
recommend computing the autocorrelation time for each walker (it's
297297
actually possible to still use the ensemble to choose the appropriate
298298
window) and then average these estimates.
299299

@@ -357,16 +357,16 @@ fact, even for moderately long chains, the old method can give
357357
dangerously over-confident estimates. For comparison, we have also
358358
plotted the :math:`\tau = N/50` line to show that, once the estimate
359359
crosses that line, The estimates are starting to get more reasonable.
360-
This suggests that you probably shouldnt trust any estimate of
360+
This suggests that you probably shouldn't trust any estimate of
361361
:math:`\tau` unless you have more than :math:`F\times\tau` samples for
362362
some :math:`F \ge 50`. Larger values of :math:`F` will be more
363363
conservative, but they will also (obviously) require longer chains.
364364

365365
A more realistic example
366366
------------------------
367367

368-
Now, lets run an actual Markov chain and test these methods using those
369-
samples. So that the sampling isnt completely trivial, well sample a
368+
Now, let's run an actual Markov chain and test these methods using those
369+
samples. So that the sampling isn't completely trivial, we'll sample a
370370
multimodal density in three dimensions.
371371

372372
.. code:: python
@@ -384,12 +384,10 @@ multimodal density in three dimensions.
384384
385385
.. parsed-literal::
386386
387-
/Users/dforeman/anaconda/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
388-
from ._conv import register_converters as _register_converters
389-
100%|██████████| 500000/500000 [06:23<00:00, 1302.86it/s]
387+
100%|██████████| 500000/500000 [08:41<00:00, 958.87it/s]
390388
391389
392-
Heres the marginalized density in the first dimension.
390+
Here's the marginalized density in the first dimension.
393391

394392
.. code:: python
395393
@@ -405,7 +403,7 @@ Here’s the marginalized density in the first dimension.
405403
.. image:: autocorr_files/autocorr_16_0.png
406404

407405

408-
And heres the comparison plot showing how the autocorrelation time
406+
And here's the comparison plot showing how the autocorrelation time
409407
estimates converge with longer chains.
410408

411409
.. code:: python
@@ -449,11 +447,11 @@ fit an `autoregressive
449447
model <https://en.wikipedia.org/wiki/Autoregressive_model>`__ to the
450448
chain and using that to estimate the autocorrelation time.
451449

452-
As an example, well use `celerite <http://celerite.readthdocs.io>`__ to
450+
As an example, we'll use `celerite <http://celerite.readthdocs.io>`__ to
453451
fit for the maximum likelihood autocorrelation function and then compute
454452
an estimate of :math:`\tau` based on that model. The celerite model that
455-
were using is equivalent to a second-order ARMA model and it appears to
456-
be a good choice for this example, but were not going to promise
453+
we're using is equivalent to a second-order ARMA model and it appears to
454+
be a good choice for this example, but we're not going to promise
457455
anything here about the general applicability and we caution care
458456
whenever estimating autocorrelation times using short chains.
459457

@@ -509,6 +507,13 @@ whenever estimating autocorrelation times using short chains.
509507
thin = max(1, int(0.05*new[i]))
510508
ml[i] = autocorr_ml(chain[:, :n], thin=thin)
511509
510+
511+
.. parsed-literal::
512+
513+
/Users/dforeman/anaconda3/lib/python3.6/site-packages/autograd/numpy/numpy_vjps.py:444: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
514+
return lambda g: g[idxs]
515+
516+
512517
.. code:: python
513518
514519
# Plot the comparisons
@@ -523,17 +528,11 @@ whenever estimating autocorrelation times using short chains.
523528
plt.legend(fontsize=14);
524529
525530
526-
.. parsed-literal::
527-
528-
/Users/dforeman/anaconda/lib/python3.6/site-packages/matplotlib/scale.py:114: RuntimeWarning: invalid value encountered in less_equal
529-
out[a <= 0] = -1000
530-
531-
532531
533-
.. image:: autocorr_files/autocorr_22_1.png
532+
.. image:: autocorr_files/autocorr_22_0.png
534533

535534

536-
This figure is the same as the previous one, but weve added the maximum
535+
This figure is the same as the previous one, but we've added the maximum
537536
likelihood estimates for :math:`\tau` in green. In this case, this
538537
estimate seems to be robust even for very short chains with
539538
:math:`N \sim \tau`.
-26.3 KB
Loading
-4.98 KB
Loading
-26.5 KB
Loading
-27.9 KB
Loading
-51.2 KB
Loading
-31.1 KB
Loading
-28.3 KB
Loading

0 commit comments

Comments
 (0)