Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
updated useful formulae
  • Loading branch information
jet08013 committed Apr 27, 2018
1 parent aa114e2 commit 1715a6d
Showing 1 changed file with 86 additions and 3 deletions.
89 changes: 86 additions & 3 deletions Useful Formulae.ipynb
Expand Up @@ -21,20 +21,103 @@
"and the posterior mean is\n",
"$$\n",
"\\mu_1=\\frac{\\frac{\\mu_0}{\\tau_0^2}+\\frac{n\\overline{y}}{\\sigma^2}}{\\frac{1}{\\tau_{1}^2}}\n",
"$$\n"
"$$\n",
"\n",
"The posterior sampling distribution $\\theta$ is\n",
"$$\n",
"p( z |y)=\\int_{\\theta} p(z|\\theta) d\\theta\n",
"$$\n",
"is a normal distribution with mean equal to the posterior mean $\\mu_1$ and variance equal to $\\sigma^2+\\tau_1^2$\n",
"where $\\tau_1$ is the posterior variance.\n",
"\n",
"See Pages 39-42 of BDA (Section 2.5) for more information."
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"from scipy.stats import norm\n",
"import numpy as np\n",
"def posterior(prior_mean,prior_variance,sample_mean,pop_variance,n):\n",
" post_var=1/((1/prior_variance) + n/pop_variance)\n",
" post_mean=(prior_mean/prior_variance+sample_mean*n/pop_variance)/(1/post_var)\n",
" return post_mean, post_var"
" return post_mean, post_var\n",
"\n",
"def post_sample(y,prior_mean,prior_variance,sample_mean,pop_variance,n):\n",
" post_mean,post_var=posterior(prior_mean,prior_variance,sample_mean,pop_variance,n)\n",
" return norm.pdf(y,post_mean,np.sqrt(pop_variance+post_var))\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.7403867575800461"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"post_sample(-.25,1,.25,-.25,1,10)+post_sample(-.25,-1,.25,-.25,1,10)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.05095226579074726"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
".1*post_sample(-.25,-1,.25,-.25,1,10)/(post_sample(-.25,1,.25,-.25,1,10)+post_sample(-.25,-1,.25,-.25,1,10))"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.4414296078832747"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
".9*post_sample(-.25,1,.25,-.25,1,10)/(post_sample(-.25,1,.25,-.25,1,10)+post_sample(-.25,-1,.25,-.25,1,10))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 1715a6d

Please sign in to comment.