Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Conjugate Normal Distributions (known variance)\n",
"\n",
"We are trying to learn about the unknown mean of a normal distribution with known variance. \n",
"We choose a prior distribution is normal with mean $\\mu_{0}$ and variance $\\tau_{0}^2$. \n",
"We draw $n$ values $y_1,\\ldots, y_n$ from the distribution with known variance $\\sigma^2$. The posterior distribution\n",
"$p(\\mu|y_1,\\ldots,y_n)=p(y_1,\\ldots,y_n|\\mu)p(\\mu)$ is again normal. Let \n",
"$$\n",
"\\overline{y}=\\frac{1}{n}\\sum_{i=1}^{n} y_i\n",
"$$\n",
"be the sample mean. \n",
"\n",
"The posterior variance\n",
"is\n",
"$$\\frac{1}{\\tau_1^2}=\\frac{1}{\\tau_0^2}+\\frac{n}{\\sigma^2}$$\n",
"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",
"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": 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\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": null,
"metadata": {},
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}