Last active
November 30, 2015 22:57
-
-
Save dfm/56cb93cb677eb591c1eb to your computer and use it in GitHub Desktop.
The starting IPython notebook for Bayesianism Python seminar at eScience
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"%matplotlib inline\n", | |
"%config InlineBackend.figure_format = \"retina\"\n", | |
"from matplotlib import rcParams\n", | |
"rcParams[\"savefig.dpi\"] = 100" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import emcee # http://dfm.io/emcee\n", | |
"import corner # https://github.com/dfm/corner.py\n", | |
"\n", | |
"import numpy as np\n", | |
"import matplotlib.pyplot as pl" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"print(\"emcee:\", emcee.__version__)\n", | |
"print(\"corner:\", corner.__version__)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"**Generate some fake data:**" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def mean_model(theta, x):\n", | |
" return theta[0] * x + theta[1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"np.random.seed(1234) # live coding\n", | |
"\n", | |
"theta_true = np.array([1.234, -0.01, 2*np.log(0.9)]) # m, b, ln(s^2)\n", | |
"\n", | |
"x = np.sort(np.random.uniform(-5, 5, 20))\n", | |
"yerr = np.random.uniform(0.3, 0.5, len(x))\n", | |
"y = mean_model(theta_true, x)\n", | |
"y += np.sqrt(yerr**2 + np.exp(theta_true[2])) * np.random.randn(len(y))\n", | |
"\n", | |
"x0 = np.linspace(-5, 5, 500)\n", | |
"\n", | |
"pl.errorbar(x, y, yerr=yerr, fmt=\".k\", capsize=0)\n", | |
"pl.plot(x0, mean_model(theta_true, x0), \"g\", lw=1.25)\n", | |
"pl.xlabel(\"x\")\n", | |
"pl.ylabel(\"y\")\n", | |
"pl.xlim(-5.1, 5.1)\n", | |
"pl.ylim(-5.8, 5.8);" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"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.4.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment