Last active
October 24, 2016 18:41
-
-
Save fonnesbeck/f17225ea9c6e268241da2ed5d0e00048 to your computer and use it in GitHub Desktop.
Untitled1.ipynb
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": [ | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "%matplotlib inline\nimport pymc3 as pm\nimport numpy as np\nimport seaborn as sns", | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "First, let's run an analysis of 100 binomial samples, with zero positive outcomes:" | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "n1 = 100\nx1 = 0", | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "with pm.Model() as first_dataset:\n θ = pm.Beta('θ', 1, 1)\n x = pm.Binomial('x', n=n1, p=θ, observed=x1)\n \n trace1 = pm.sample(2000)", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Applied logodds-transform to θ and added transformed θ_logodds_ to model.\nAssigned NUTS to θ_logodds_\n100%|██████████| 2000/2000 [00:00<00:00, 2721.90it/s]\n", | |
"name": "stderr" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "The parameter estimate is 0.012, with a credible interval of length 0.031." | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "pm.summary(trace1)", | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "\nθ:\n\n Mean SD MC Error 95% HPD interval\n -------------------------------------------------------------------\n \n 0.012 0.025 0.001 [0.000, 0.031]\n\n Posterior quantiles:\n 2.5 25 50 75 97.5\n |--------------|==============|==============|--------------|\n \n 0.000 0.003 0.008 0.014 0.037\n\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Now, let's add another 100 samples, but this time with 10 positive outcomes:" | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "n2 = 100\nx2 = 10\n\nwith pm.Model() as combined_dataset:\n θ = pm.Beta('θ', 1, 1)\n x = pm.Binomial('x', n=n1+n2, p=θ, observed=x1+x2)\n \n trace2 = pm.sample(2000)", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Applied logodds-transform to θ and added transformed θ_logodds_ to model.\nAssigned NUTS to θ_logodds_\n100%|██████████| 2000/2000 [00:00<00:00, 3415.85it/s]\n", | |
"name": "stderr" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "pm.summary(trace2)", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "\nθ:\n\n Mean SD MC Error 95% HPD interval\n -------------------------------------------------------------------\n \n 0.056 0.030 0.002 [0.025, 0.087]\n\n Posterior quantiles:\n 2.5 25 50 75 97.5\n |--------------|==============|==============|--------------|\n \n 0.027 0.042 0.053 0.064 0.091\n\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "Notice that the credible interval is twice as large, even with a doubling of the sample size!" | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python [default]", | |
"language": "python" | |
}, | |
"anaconda-cloud": {}, | |
"latex_envs": { | |
"eqNumInitial": 0, | |
"eqLabelWithNumbers": true, | |
"current_citInitial": 1, | |
"cite_by": "apalike", | |
"bibliofile": "biblio.bib" | |
}, | |
"language_info": { | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"mimetype": "text/x-python", | |
"version": "3.5.2", | |
"file_extension": ".py" | |
}, | |
"gist": { | |
"id": "f17225ea9c6e268241da2ed5d0e00048", | |
"data": { | |
"description": "Untitled1.ipynb", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/f17225ea9c6e268241da2ed5d0e00048" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment