Skip to content

Commit

Permalink
Fix documentation example of blob returns for case when ll or lp is n…
Browse files Browse the repository at this point in the history
…ot finite (#399)
  • Loading branch information
adrn committed Jul 31, 2021
1 parent 530a64b commit e7f03a7
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 15 deletions.
20 changes: 16 additions & 4 deletions docs/tutorials/autocorr.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"%config InlineBackend.figure_format = \"retina\"\n",
"\n",
"from matplotlib import rcParams\n",
"\n",
"rcParams[\"savefig.dpi\"] = 100\n",
"rcParams[\"figure.dpi\"] = 100\n",
"rcParams[\"font.size\"] = 20"
Expand Down Expand Up @@ -258,7 +259,11 @@
"for n, ax in zip([10, 100, 1000], axes):\n",
" nn = int(true_tau * n)\n",
" ax.plot(tau / true_tau, f0, \"k\", label=\"true\")\n",
" ax.plot(tau / true_tau, autocorr_func_1d(y[0, :nn])[: window + 1], label=\"estimate\")\n",
" ax.plot(\n",
" tau / true_tau,\n",
" autocorr_func_1d(y[0, :nn])[: window + 1],\n",
" label=\"estimate\",\n",
" )\n",
" ax.set_title(r\"$N = {0}\\,\\tau_\\mathrm{{true}}$\".format(n), fontsize=14)\n",
" ax.set_xlabel(r\"$\\tau / \\tau_\\mathrm{true}$\")\n",
"\n",
Expand Down Expand Up @@ -307,7 +312,10 @@
" nn = int(true_tau * n)\n",
" ax.plot(tau / true_tau, f0, \"k\", label=\"true\")\n",
" f = np.mean(\n",
" [autocorr_func_1d(y[i, :nn], norm=False)[: window + 1] for i in range(len(y))],\n",
" [\n",
" autocorr_func_1d(y[i, :nn], norm=False)[: window + 1]\n",
" for i in range(len(y))\n",
" ],\n",
" axis=0,\n",
" )\n",
" f /= f[0]\n",
Expand Down Expand Up @@ -447,7 +455,9 @@
"\n",
"sampler = emcee.EnsembleSampler(32, 3, log_prob)\n",
"sampler.run_mcmc(\n",
" np.concatenate((np.random.randn(16, 3), 4.0 + np.random.randn(16, 3)), axis=0),\n",
" np.concatenate(\n",
" (np.random.randn(16, 3), 4.0 + np.random.randn(16, 3)), axis=0\n",
" ),\n",
" 500000,\n",
" progress=True,\n",
");"
Expand Down Expand Up @@ -584,7 +594,9 @@
" # Build the GP model\n",
" tau = max(1.0, init / thin)\n",
" kernel = terms.RealTerm(\n",
" np.log(0.9 * np.var(z)), -np.log(tau), bounds=[(-5.0, 5.0), (-np.log(N), 0.0)]\n",
" np.log(0.9 * np.var(z)),\n",
" -np.log(tau),\n",
" bounds=[(-5.0, 5.0), (-np.log(N), 0.0)],\n",
" )\n",
" kernel += terms.RealTerm(\n",
" np.log(0.1 * np.var(z)),\n",
Expand Down
5 changes: 4 additions & 1 deletion docs/tutorials/line.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"%config InlineBackend.figure_format = \"retina\"\n",
"\n",
"from matplotlib import rcParams\n",
"\n",
"rcParams[\"savefig.dpi\"] = 100\n",
"rcParams[\"figure.dpi\"] = 100\n",
"rcParams[\"font.size\"] = 20"
Expand Down Expand Up @@ -429,7 +430,9 @@
"pos = soln.x + 1e-4 * np.random.randn(32, 3)\n",
"nwalkers, ndim = pos.shape\n",
"\n",
"sampler = emcee.EnsembleSampler(nwalkers, ndim, log_probability, args=(x, y, yerr))\n",
"sampler = emcee.EnsembleSampler(\n",
" nwalkers, ndim, log_probability, args=(x, y, yerr)\n",
")\n",
"sampler.run_mcmc(pos, 5000, progress=True);"
]
},
Expand Down
9 changes: 7 additions & 2 deletions docs/tutorials/monitor.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"%config InlineBackend.figure_format = \"retina\"\n",
"\n",
"from matplotlib import rcParams\n",
"\n",
"rcParams[\"savefig.dpi\"] = 100\n",
"rcParams[\"figure.dpi\"] = 100\n",
"rcParams[\"font.size\"] = 20"
Expand Down Expand Up @@ -305,7 +306,9 @@
"source": [
"new_backend = emcee.backends.HDFBackend(filename)\n",
"print(\"Initial size: {0}\".format(new_backend.iteration))\n",
"new_sampler = emcee.EnsembleSampler(nwalkers, ndim, log_prob, backend=new_backend)\n",
"new_sampler = emcee.EnsembleSampler(\n",
" nwalkers, ndim, log_prob, backend=new_backend\n",
")\n",
"new_sampler.run_mcmc(None, 100)\n",
"print(\"Final size: {0}\".format(new_backend.iteration))"
]
Expand Down Expand Up @@ -345,7 +348,9 @@
"# Rinse, Wash, and Repeat as above\n",
"coords = np.random.randn(32, 5)\n",
"nwalkers, ndim = coords.shape\n",
"sampler2 = emcee.EnsembleSampler(nwalkers, ndim, log_prob2, backend=run2_backend)\n",
"sampler2 = emcee.EnsembleSampler(\n",
" nwalkers, ndim, log_prob2, backend=run2_backend\n",
")\n",
"\n",
"# note: this is *not* necessarily the right number of iterations for this\n",
"# new prior. But it will suffice to demonstrate the second backend.\n",
Expand Down
13 changes: 11 additions & 2 deletions docs/tutorials/moves.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"%config InlineBackend.figure_format = \"retina\"\n",
"\n",
"from matplotlib import rcParams\n",
"\n",
"rcParams[\"savefig.dpi\"] = 100\n",
"rcParams[\"figure.dpi\"] = 100\n",
"rcParams[\"font.size\"] = 20"
Expand Down Expand Up @@ -107,7 +108,11 @@
"sampler0 = emcee.EnsembleSampler(nwalkers, ndim, logprob)\n",
"sampler0.run_mcmc(init, 5000)\n",
"\n",
"print(\"Autocorrelation time: {0:.2f} steps\".format(sampler0.get_autocorr_time()[0]))"
"print(\n",
" \"Autocorrelation time: {0:.2f} steps\".format(\n",
" sampler0.get_autocorr_time()[0]\n",
" )\n",
")"
]
},
{
Expand Down Expand Up @@ -201,7 +206,11 @@
")\n",
"sampler.run_mcmc(init, 5000)\n",
"\n",
"print(\"Autocorrelation time: {0:.2f} steps\".format(sampler.get_autocorr_time()[0]))\n",
"print(\n",
" \"Autocorrelation time: {0:.2f} steps\".format(\n",
" sampler.get_autocorr_time()[0]\n",
" )\n",
")\n",
"\n",
"plt.plot(sampler.get_chain()[:, 0, 0], \"k\", lw=0.5)\n",
"plt.xlim(0, 5000)\n",
Expand Down
13 changes: 10 additions & 3 deletions docs/tutorials/parallel.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"%config InlineBackend.figure_format = \"retina\"\n",
"\n",
"from matplotlib import rcParams\n",
"\n",
"rcParams[\"savefig.dpi\"] = 100\n",
"rcParams[\"figure.dpi\"] = 100\n",
"rcParams[\"font.size\"] = 20\n",
Expand Down Expand Up @@ -407,7 +408,9 @@
" multi_data_time = end - start\n",
" print(\"Multiprocessing took {0:.1f} seconds\".format(multi_data_time))\n",
" print(\n",
" \"{0:.1f} times faster(?) than serial\".format(serial_data_time / multi_data_time)\n",
" \"{0:.1f} times faster(?) than serial\".format(\n",
" serial_data_time / multi_data_time\n",
" )\n",
" )"
]
},
Expand Down Expand Up @@ -460,12 +463,16 @@
"\n",
"\n",
"with Pool() as pool:\n",
" sampler = emcee.EnsembleSampler(nwalkers, ndim, log_prob_data_global, pool=pool)\n",
" sampler = emcee.EnsembleSampler(\n",
" nwalkers, ndim, log_prob_data_global, pool=pool\n",
" )\n",
" start = time.time()\n",
" sampler.run_mcmc(initial, nsteps, progress=True)\n",
" end = time.time()\n",
" multi_data_global_time = end - start\n",
" print(\"Multiprocessing took {0:.1f} seconds\".format(multi_data_global_time))\n",
" print(\n",
" \"Multiprocessing took {0:.1f} seconds\".format(multi_data_global_time)\n",
" )\n",
" print(\n",
" \"{0:.1f} times faster than serial\".format(\n",
" serial_data_time / multi_data_global_time\n",
Expand Down
7 changes: 6 additions & 1 deletion docs/tutorials/quickstart.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"%config InlineBackend.figure_format = \"retina\"\n",
"\n",
"from matplotlib import rcParams\n",
"\n",
"rcParams[\"savefig.dpi\"] = 100\n",
"rcParams[\"figure.dpi\"] = 100\n",
"rcParams[\"font.size\"] = 20"
Expand Down Expand Up @@ -300,7 +301,11 @@
}
],
"source": [
"print(\"Mean acceptance fraction: {0:.3f}\".format(np.mean(sampler.acceptance_fraction)))"
"print(\n",
" \"Mean acceptance fraction: {0:.3f}\".format(\n",
" np.mean(sampler.acceptance_fraction)\n",
" )\n",
")"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions docs/user/blobs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ To do this, we would update the above example as follows:
def log_prob(params):
lp = log_prior(params)
if not np.isfinite(lp):
return -np.inf, -np.inf
return -np.inf, -np.inf, -np.inf
ll = log_like(params)
if not np.isfinite(ll):
return lp, -np.inf
return lp, -np.inf, -np.inf
return lp + ll, lp, np.mean(params)
coords = np.random.randn(32, 3)
Expand Down

0 comments on commit e7f03a7

Please sign in to comment.