{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Import relevant modules\n", "import pymc\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Some data\n", "n = 5 * np.ones(4, dtype=int)\n", "x = np.array([-.86, -.3, -.05, .73])" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Priors on unknown parameters\n", "alpha = pymc.Normal('alpha', mu=0, tau=.01)\n", "beta = pymc.Normal('beta', mu=0, tau=.01)\n", "\n", "# Arbitrary deterministic function of parameters\n", "@pymc.deterministic\n", "def theta(a=alpha, b=beta):\n", " \"\"\"theta = logit^{-1}(a+b)\"\"\"\n", " return pymc.invlogit(a + b * x)\n", "\n", "# Binomial likelihood for data\n", "d = pymc.Binomial('d', n=n, p=theta, value=np.array([0., 1., 3., 5.]),\n", " observed=True)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "mymodel = pymc.Model([theta, d])\n", "S = pymc.MCMC(mymodel, db='pickle')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\r", " [-----------------100%-----------------] 10000 of 10000 complete in 0.3 sec" ] } ], "source": [ "\n", "S.sample(iter=10000, burn=5000, thin=2)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Plotting theta_0\n", "Plotting theta_1\n", "Plotting theta_2\n", "Plotting theta_3\n" ] } ], "source": [ "pymc.Matplot.plot(S)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }