Example of "real life" use of Bayesian inference on $\mu$ from a normal distribution? - Cross Validated most recent 30 from stats.stackexchange.com 2019-08-19T16:20:27Z https://stats.stackexchange.com/feeds/question/82292 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://stats.stackexchange.com/q/82292 7 Example of "real life" use of Bayesian inference on $\mu$ from a normal distribution? Tal Galili https://stats.stackexchange.com/users/253 2014-01-14T22:10:54Z 2018-01-18T19:04:09Z <p>A classic example for students, when teaching Bayesian statistics, is to make inference on the mean parameter $\mu$ of a normal distribution, when it has a prior normal distribution.</p> <p>I would like to find a real-life example of why such a construction would be not only elegant, but also useful.</p> <p>Can anyone direct me to such an example?</p> https://stats.stackexchange.com/questions/82292/-/82330#82330 6 Answer by Rasmus Bååth for Example of "real life" use of Bayesian inference on $\mu$ from a normal distribution? Rasmus Bååth https://stats.stackexchange.com/users/6920 2014-01-15T11:48:11Z 2014-01-15T11:48:11Z <p>From my perspective the Normal distribution is useful as a prior on the the mean of a Normal distribution because:</p> <ol> <li><p>The Normal is the <a href="http://en.wikipedia.org/wiki/Conjugate_prior#Continuous_distributions" rel="noreferrer">conjugate prior</a> to the mean of a Normal distribution. Using conjugate priors can really speed up computation in, for example, <a href="http://mcmc-jags.sourceforge.net/" rel="noreferrer">JAGS</a></p></li> <li><p>It is pretty easy to make the Normal distribution both informative, the mean and SD are parameters that are easy to set as it is relativley clear how they affect the shape of the distribution, and non-informative, just make the SD extremely large and the prior will approach the uniform distribution.</p></li> </ol> <p>Here is a silly but "real life" example of how the Normal could be used as a slightly informative distribution. :)</p> <h2>Farmer Jöns and Milk Production.</h2> <p>Farmer Jöns has a huge number of cows. He would like to know how much milk a cow is expected to produce on average and thus measures the number of liters of milk that six cows produce during one month:</p> <pre><code>milk &lt;- c(651, 374, 601, 401, 767, 709) </code></pre> <p>He sends this data to his statistician friend who tells him that, "Well it's not that much data to work with, but the estimate might get a bit better if you tell me how much milk <em>in your experience</em> a cow produces per month on average". "I haven't thought that much about it", replies Jöns, "but perhaps around 500-600 liters/month is usual."</p> <p>The statistician decides to run a Bayesian analysis where <code>milk</code> is assumed to be normally distributed:</p> <p>$$\mathrm{milk} \sim \mathrm{Norm}(\mu, \sigma)$$</p> <p>The statistician encodes the information he got from Jöns in the prior on $\mu$ which is pretty easy to do as he choose to use a Normal distribution for this:</p> <p>$$\mu \sim \mathrm{Norm}(550, 100)$$</p> <p>This Normal prior is centered around 550 and has a standard deviation of 100 basically saying that prior to looking at the data the mean is most likely to be in the range 450-650. </p> <p> </p> <p>As he forgot to ask about the spread of milk production of different cows he puts a flat prior on $\sigma$:</p> <p>$$\sigma \sim \mathrm{Unif}(0, 1000)$$</p> <p>An implementation of this analysis in R using the <a href="http://cran.r-project.org/web/packages/rjags/index.html" rel="noreferrer">rjags</a> package would look like this:</p> <pre class="lang-r prettyprint-override"><code>library(rjags) model_string &lt;- "model { for(i in 1:length(milk)) { milk[i] ~ dnorm(mu, 1/(sigma * sigma) ) } mu ~ dnorm(550, 1 / (100*100)) sigma ~ dunif(0, 1000)\n }" # The reason for the 1/ (sigma*sigma) 'thing' is because dnorm in JAGS is # parameterized by precision (1 / SD^2) instead of SD as in R. model &lt;- jags.model(textConnection(model_string), data = list(milk = milk)) fit &lt;- coda.samples(model, variable.names = c("mu", "sigma"), n.iter = 30000) summary(fit) </code></pre> <p>&nbsp;</p> <pre><code>## ## Iterations = 1001:31000 ## Thinning interval = 1 ## Number of chains = 1 ## Sample size per chain = 30000 ## ## 1. Empirical mean and standard deviation for each variable, ## plus standard error of the mean: ## ## Mean SD Naive SE Time-series SE ## mu 570 63.3 0.365 0.368 ## sigma 208 89.3 0.516 1.173 ## ## 2. Quantiles for each variable: ## ## 2.5% 25% 50% 75% 97.5% ## mu 440 530 571 611 693 ## sigma 106 150 187 241 435 </code></pre> <p>Looking at the quantiles for the posterior of $\mu$ the statistician tells Jöns that a good guess for the number of liters of milk a cow is expected to produce on average is 571 liters/month and the average is probably not lower than 440 liters/month or higher than 693 liters/month. </p> <p>What we gained from using a normal distribution as the prior on $\mu$ was a slightly tighter estimate (and hopefully better too). Compare with the 95 % CI using <code>t.test</code>:</p> <pre class="lang-r prettyprint-override"><code>t.test(milk) </code></pre> <p>&nbsp;</p> <pre><code>## ## One Sample t-test ## ## data: milk ## t = 8.819, df = 5, p-value = 0.0003113 ## alternative hypothesis: true mean is not equal to 0 ## 95 percent confidence interval: ## 413.7 754.0 ## sample estimates: ## mean of x ## 583.8 </code></pre> https://stats.stackexchange.com/questions/82292/-/323805#323805 1 Answer by Jon for Example of "real life" use of Bayesian inference on $\mu$ from a normal distribution? Jon https://stats.stackexchange.com/users/82338 2018-01-18T19:04:09Z 2018-01-18T19:04:09Z <p>Before delving directly into an example, I'd like to review some of the math for Normal-Normal Bayesian data models. Consider a random sample of n continuous values denoted by $y_1, ..., y_n$. Here the vector $y = (y_1, ..., y_n)^T$ represents the data gathered. The probability model for Normal data with known variance and independent and identically distributed (i.i.d.) samples is</p> <p>$$y_1, ..., y_n | \theta \sim N(\theta, \sigma^2)$$</p> <p>Or as more typically written by Bayesian,</p> <p>$$y_1, ..., y_n | \theta \sim N(\theta, \tau)$$</p> <p>where $\tau = 1 / \sigma^2$; $\tau$ is known as the precision</p> <p>With this notation, the density for $y_i$ is then </p> <p>$$f(y_i | \theta, \tau) = \sqrt(\frac{\tau}{2 \pi}) \times exp\left( -\tau (y_i - \theta)^2 / 2 \right)$$</p> <p>Classical statistics (i.e. maximum likelihood) gives us an estimate of $\hat{\theta} = \bar{y}$</p> <p>In a Bayesian perspective, we append maximum likelihood with prior information. A choice of priors for this Normal data model is another Normal distribution for $\theta$. The Normal distribution is <a href="https://en.wikipedia.org/wiki/Conjugate_prior#Continuous_distributions" rel="nofollow noreferrer">conjugate</a> to the Normal distribution.</p> <p>$$\theta \sim N(a,1/b)$$</p> <p>The posterior distribution we obtain from this Normal-Normal (after a lot of algebra) data model is another Normal distribution (so many Normals!)</p> <p>$$\theta | y \sim N(\frac{b}{b + n\tau} a + \frac{n \tau}{b + n \tau} \bar{y}, \frac{1}{b + n\tau})$$</p> <p>The posterior precision is $b + n\tau$ and mean is a weighted mean between $a$ and $\bar{y}$, $\frac{b}{b + n\tau} a + \frac{n \tau}{b + n \tau} \bar{y}$.</p> <p>To your comment</p> <blockquote> <p>I would like to find a real-life example of why such a construction would be not only elegant, but also useful.</p> </blockquote> <p>The usefulness comes from the fact that you obtain a distribution of $\theta | y$ rather than just an estimate since $\theta$ is viewed as a random variable rather than a fixed (unknown) value. In addition, your estimate of $\theta$ in this model is a weighted average between the empirical mean and prior information. </p> <p>That said, you can now use any Normal-data textbook example to illustrate this. I'll use the data set <code>airquality</code> within R. Consider the problem of estimating average wind speeds (MPH).</p> <pre><code>&gt; ## New York Air Quality Measurements &gt; &gt; help("airquality") &gt; &gt; ## Estimating average wind speeds &gt; &gt; wind = airquality\$Wind &gt; hist(wind, col = "gray", border = "white", xlab = "Wind Speed (MPH)") &gt; </code></pre> <p><a href="https://i.stack.imgur.com/SWaeo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SWaeo.png" alt="enter image description here"></a></p> <pre><code>&gt; n = length(wind) &gt; ybar = mean(wind) &gt; ybar  9.957516 ## "frequentist" estimate &gt; tau = 1/sd(wind) &gt; &gt; &gt; ## but based on some research, you felt avgerage wind speeds were closer to 12 mph &gt; ## but probably no greater than 15, &gt; ## then a potential prior would be N(12, 2) &gt; &gt; a = 12 &gt; b = 2 &gt; &gt; ## Your posterior would be N((1/)) &gt; &gt; postmean = 1/(1 + n*tau) * a + n*tau/(1 + n*tau) * ybar &gt; postsd = 1/(1 + n*tau) &gt; &gt; set.seed(123) &gt; posterior_sample = rnorm(n = 10000, mean = postmean, sd = postsd) &gt; hist(posterior_sample, col = "gray", border = "white", xlab = "Wind Speed (MPH)") &gt; abline(v = median(posterior_sample)) &gt; abline(v = ybar, lty = 3) &gt; </code></pre> <p><a href="https://i.stack.imgur.com/bc0Au.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bc0Au.png" alt="enter image description here"></a></p> <pre><code>&gt; median(posterior_sample)  10.00324 &gt; quantile(x = posterior_sample, probs = c(0.025, 0.975)) ## confidence intervals 2.5% 97.5% 9.958984 10.047404 </code></pre> <p>In this analysis, the researcher (you) can say that given data + prior information, your estimate of average wind, using the 50th percentile, speeds should be 10.00324, greater than simply using the average from the data. You also obtain a full distribution, from which you can extract a 95% credible interval using the 2.5 and 97.5 quantiles.</p> <p>Below I include two references, I highly recommend reading Casella's short paper. It's specifically aimed at empirical Bayes methods, but explains the general Bayesian methodology for Normal models.</p> <p>References:</p> <ol> <li><p>Casella, G. (1985). An Introduction to Empirical Bayes Data Analysis. The American Statistician, 39(2), 83-87.</p></li> <li><p>Gelman, A. (2004). Bayesian data analysis (2nd ed., Texts in statistical science). Boca Raton, Fla.: Chapman &amp; Hall/CRC.</p></li> </ol>