According to Wikipedia:
M&M’s are multi-colored button-shaped chocolates, each of which has the letter “m” printed in lower case in white on one side, consisting of a candy shell surrounding a filling which varies depending upon the variety of M&M’s. The original candy has a semi-sweet chocolate filling which, upon introduction of other variations, was branded as the “plain, normal” variety.
The candy originated in the United States in 1941. They are produced in different colors, some of which have changed over the years. The candy-coated chocolate concept was inspired by a method used to allow soldiers in the Spanish Civil War (1936–1939) to carry chocolate in warm climates without it melting. A traditional milk chocolate M&M weighs about 0.91 grams and has about 4.7 calories of food energy’’.
Let’s start this practical session by making inference about the proportion of red M&M’s in a very huge box. If you are following this practice in person, the instructor will give you a bag of M&M’s to make the practice more fun even if we have to be a bit flexible about the theoretical conditions of the experiment. If you are working on line it will be a bit more boring but we will comply better with the theoretical conditions of the experiment.
For those of us who physically have the bag of M&M’s, the most appropriate experiment would be to take candies out of the bag one at a time and replace them, recording whether their colour is red or not, until the number n of candies defined in advance is reached. We consider n=20.
In case you are on line or you don’t want to carry out the experiment physically, you can use the following result of the experiment:
Let Y be the random variable that describes the number of red M&M’s (successes) out of the n = 20 sampled. Given the proportion θ of red candies in the bag, Y is a binomial random variable with parameters n = 20 and probability θ associated with drawing a red candy.
Y ∣ θ ∼ Bi(n, θ)
whose conditional probability function, expectation and variance are:
Recall that the beta distribution is conjugate with respect to the binomial probability model. If we elicit a prior beta distribution Be(α0, β0) for θ, its density is
$$\pi(\theta \mid \alpha_0, \beta_0)=\frac{\Gamma(\alpha_0+\beta_0)}{\Gamma(\alpha_0)\,\Gamma(\beta_0)}\, \theta^{\alpha_0-1}\,(1-\theta)^{\beta_0-1}, \quad 0<\theta<1$$ with expectation and variance
We will start by working with the non-informative prior distribution Be(α0 = 0.5, β0 = 0.5) whose prior expectation and standard deviation is 𝔼(θ) = 0.5 and 𝕊𝔻(θ) = 0.3535, respectively. Its graphic is obtained via
The likelihood function of θ is a function of θ whose expression requires the observed data 𝒟 = {n, r}. It is defined as follows
$$L(\theta \mid \mathcal D) = P(Y=r \mid \theta, n) = \binom{n}{r}\, \theta^r\,(1- \theta)^{n-r},$$
where r is the observed number of successes in the sample (here, the number of red candies) of size n. In our first experiment, the likelihood function of θ for the data 𝒟1 = {r1 = 4 red candies in n1 = 20 sampled} is
$$L(\theta \mid \mathcal D_1) = \binom{20}{4}\, \theta^4\,(1- \theta)^{16},$$
The posterior distribution of θ is also a beta distribution with parameters
π(θ ∣ 𝒟) = Be(α = α0 + r, β = β0 + n − r).
In our case, the posterior distribution of the proportion of red candies is
π(θ ∣ 𝒟1) = Be(α = 4.5, β = 16.5) whose graphic is
curve(
dbeta(x, mm_sample$r + 0.5, mm_sample$n - mm_sample$r + 0.5),
col = "darkgreen", lwd = 4,
xlim = c(0, 1), ylim = c(0, 5),
xlab = expression(paste("Proportion of red candies ", theta)),
ylab = 'posterior'
)
The posterior mean and posterior standard deviation of the proportion of red candies is
$$\mathbb{E}(\theta \mid \mathcal D)=\frac{4.5}{4.5+16.5}=0.2143, \quad\quad \mathbb{SD}(\theta \mid \mathcal D)=\sqrt{\frac{4.5 \cdot 16.5}{(4.5+16.5)^2 \cdot (4.5+16.5+1)}}=0.0875.$$ A 95% credible interval for θ is
and the posterior probability that the proportion of red candies is between 0.1 and 0.3 is
You can see these posterior summaries in the Figure below
Recall that the posterior predictive distribution of the number of successes Y′ in a new sample of size n′ is a beta-binomial distribution Bb(α, β, n′) with parameters α and β from the posterior distribution π(θ ∣ 𝒟) and the size n′ of the new experiment. Its probability function is
$$P(Y^{\prime} = r^{\prime} \mid \mathcal{D}) = \binom{n^\prime}{r^\prime}\, \frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha) \,\Gamma(\beta)} \,\frac{\Gamma(\alpha+r^{\prime})\,\Gamma(\beta+n^{\prime}-r^{\prime})}{\Gamma(\alpha+\beta+n^{\prime})}, \,\, r^{\prime}=0,1,\ldots, n^{\prime}$$
with expectation and variance,
Suppose now that we are going to randomly draw n′ = 10 new M&M’s from the bag. Before performing the experiment, the posterior predictive distributions quantifies the relative plausibility of each possible outcome. It is a beta-binomial distribution with parameters α = 4.5 and β = 16.5 from the posterior distribution π(θ ∣ 𝒟), and n′ = 10, which is the size of the new experiment.
library(extraDistr)
curve(
dbbinom(x, size = 10, mm_sample$r + 0.5, mm_sample$n - mm_sample$r + 0.5),
col = "purple", lwd = 4, type = 'h', n = 11,
xlim = c(0, 10), ylim = c(0, 0.4),
xlab = expression(paste("Number of red candies")),
ylab = 'probability'
)
We observe that the most likely numbers of red candies in the new sample are 1 and 2. The posterior mean and posterior standard deviation of the number of red candies is this new sample is
𝔼(Y′ ∣ 𝒟) = 2.14, 𝕊𝔻(Y′ ∣ 𝒟) = 1.54.
We propose below an individual exercise that pursues that we can
consolidate the basic concepts that we have learned in the previous
theoretical session and that we have been practising in this session.
You can code the exercise yourself in R using the examples above, or use
vibass_app(1)
to launch the interactive application
provided in the vibass
R-package.
EXERCISE
You have two friends, Arnau and Mary, who know a little bit about Bayesian inference and a lot about M&M’s. Their opinions about the proportion θ of red candies are expressed in terms of the following prior distributions:
How different are your two friends’ opinions on θ? A good idea to answer this question would be to plot both densities and calculate the mean and standard deviation of the subsequent prior distributions.
From the results of the previous experiment (r = 4 red candies out of a total of n = 20 sampled), compare the posterior distribution for θ that Mary and Arnau would obtain.
Compare both posterior distributions with the one we have obtained in Subsection 2.5 of the practical through their density curves, posterior means, standard deviations and 95% credibility intervals.
Compute the posterior predictive distribution for the new experiment described in Subsection 2.6 (to randomly draw n′ = 10 new M&M’s from the bag) and compare both posterior predictive distributions with the one we have obtained in Subsection 2.6 of the practical through their density curves, posterior means and standard deviations.