forked from minaskar/zeus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
124 changed files
with
13,269 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
269 changes: 269 additions & 0 deletions
269
docs/_build/doctrees/nbsphinx/notebooks/convergence.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Incrementally saving progress to a file" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"In many cases it is useful to save the chain to a file. This makes iit easier to post-process a long chain and makes things less disastrous if the computer crashes somewhere in the midle of an expensive MCMC run.\n", | ||
"\n", | ||
"In this recipe we are going to use the callback interface to save the samples and their corresponding log-probability values in a `.h5` file. To do this you need to have [``h5py``](https://docs.h5py.org/en/latest/build.html#pre-built-installation-recommended) installed.\n", | ||
"\n", | ||
"We will set up a simple problem of sampling from a normal/Gaussian distribution as an example:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import zeus\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"ndim = 2\n", | ||
"nwalkers = 10\n", | ||
"nsteps = 1000\n", | ||
"\n", | ||
"def log_prob(x):\n", | ||
" return -0.5*np.dot(x,x)\n", | ||
"\n", | ||
"x0 = 1e-3 * np.random.randn(nwalkers, ndim)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Where ``x0`` is the initial positions of the walkers.\n", | ||
"\n", | ||
"We will then initialise the sampler and start the MCMC run by providing the ``zeus.callbacks.SaveProgressCallback`` callback function." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"Initialising ensemble of 10 walkers...\n", | ||
"Sampling progress : 100%|██████████| 1000/1000 [00:01<00:00, 656.62it/s]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"sampler = zeus.EnsembleSampler(nwalkers, ndim, log_prob)\n", | ||
"sampler.run_mcmc(x0, nsteps, callbacks=zeus.callbacks.SaveProgressCallback(\"saved_chains.h5\", ncheck=100))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The above piece of code saved the chain incrementally every ``ncheck=100`` steps to a file named ``saved_chains.h5``. We can now access the chains using the ``h5py`` package as follows:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"(1000, 10, 2)\n", | ||
"(1000, 10)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import h5py \n", | ||
"\n", | ||
"with h5py.File('saved_chains.h5', \"r\") as hf:\n", | ||
" samples = np.copy(hf['samples'])\n", | ||
" logprob_samples = np.copy(hf['logprob'])\n", | ||
"\n", | ||
"print(samples.shape)\n", | ||
"print(logprob_samples.shape)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "42ef9c41c9809f9bfe38b73fa705c16bbb3d6fadc6a1917ff578a20446617baf" | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3.7.10 64-bit ('nbodykit-env': conda)", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.10" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Sphinx build info version 1 | ||
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. | ||
config: 0755be5405a74e16d41e5187e65c4512 | ||
config: 51cd34c90186d950f94f7df346d54026 | ||
tags: 645f666f9bcd5a90fca523b33c5a78b7 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
============= | ||
The Callbacks | ||
============= | ||
|
||
Starting from version 2.4.0, ``zeus`` supports callback functions. Those are functions that are | ||
called in every iteration of a run. Among other things, these can be used to monitor useful quantities, | ||
assess convergence, and save the chains to disk. Custom callback functions can also be used. Sampling | ||
terminates if a callback function returns ``True`` and continues running while ``False`` or ``None`` is | ||
returned. | ||
|
||
Autocorrelation Callback | ||
======================== | ||
|
||
.. autoclass:: zeus.callbacks.AutocorrelationCallback | ||
:members: | ||
|
||
|
||
Split-R Callback | ||
================ | ||
|
||
.. autoclass:: zeus.callbacks.SplitRCallback | ||
:members: | ||
|
||
|
||
Minimum Iterations Callback | ||
=========================== | ||
|
||
.. autoclass:: zeus.callbacks.MinIterCallback | ||
:members: | ||
|
||
|
||
Save Progress Callback | ||
====================== | ||
|
||
.. autoclass:: zeus.callbacks.SaveProgressCallback | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.