Created
February 17, 2016 14:13
-
-
Save fonnesbeck/25c6e992985d0b79d869 to your computer and use it in GitHub Desktop.
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": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import pymc3 as pm" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Simulate data from Gaussian with mean=2 and SD=5 and try to recover these parameters with PyMC3." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"x = np.random.normal(2, 5, size=1000)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Applied log-transform to sigma and added transformed sigma_log to model.\n" | |
] | |
} | |
], | |
"source": [ | |
"with pm.Model() as model:\n", | |
" \n", | |
" mu = pm.Normal('mu', 0, sd=1e4)\n", | |
" sigma = pm.HalfCauchy('sigma', 25)\n", | |
" \n", | |
" likelihood = pm.Normal('likelihood', mu, sd=sigma, observed=x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Assigned NUTS to mu\n", | |
"Assigned NUTS to sigma_log\n", | |
" [-----------------100%-----------------] 1000 of 1000 complete in 0.7 sec" | |
] | |
} | |
], | |
"source": [ | |
"with model:\n", | |
" tr = pm.sample(1000)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"\n", | |
"mu:\n", | |
"\n", | |
" Mean SD MC Error 95% HPD interval\n", | |
" -------------------------------------------------------------------\n", | |
" \n", | |
" 2.174 0.203 0.008 [1.851, 2.509]\n", | |
"\n", | |
" Posterior quantiles:\n", | |
" 2.5 25 50 75 97.5\n", | |
" |--------------|==============|==============|--------------|\n", | |
" \n", | |
" 1.836 2.069 2.184 2.301 2.497\n", | |
"\n", | |
"\n", | |
"sigma_log:\n", | |
"\n", | |
" Mean SD MC Error 95% HPD interval\n", | |
" -------------------------------------------------------------------\n", | |
" \n", | |
" 1.609 0.042 0.001 [1.563, 1.651]\n", | |
"\n", | |
" Posterior quantiles:\n", | |
" 2.5 25 50 75 97.5\n", | |
" |--------------|==============|==============|--------------|\n", | |
" \n", | |
" 1.565 1.592 1.609 1.623 1.655\n", | |
"\n", | |
"\n", | |
"sigma:\n", | |
"\n", | |
" Mean SD MC Error 95% HPD interval\n", | |
" -------------------------------------------------------------------\n", | |
" \n", | |
" 5.003 0.280 0.009 [4.774, 5.212]\n", | |
"\n", | |
" Posterior quantiles:\n", | |
" 2.5 25 50 75 97.5\n", | |
" |--------------|==============|==============|--------------|\n", | |
" \n", | |
" 4.782 4.912 4.996 5.069 5.231\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"pm.summary(tr)" | |
] | |
} | |
], | |
"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.5.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment