Supplementary Exercises 4.3#
Warning
This page contains solutions! We recommend attempting each problem before peeking.
1. Expectation and Variance#
Find
, ; , .
Solution
, , , ,
2. Uniform on Unit Circle#
Let bivariate random variable
Find
.What is the conditional density
for ?
Solution
We have
Logically, the domain for this half-circle is
to make sure.
3. Fungi Spores#
Fungi spores are elliptical in shape with minor and major axes used to describe their size. Let
Find:
joint distribution
; ; ;constant
, such that ;Are
and independent?
Solution
Since
, we have .We use the given
equation with . We have
.We need to find the marginal distribution of
by integrating out from the joint likelihood from part (a). Once we have that, we can find the desired constant by integration/numerical solvers. We do this in two parts:
First, find
Then, we would like to match this to a known distribution if possible. We know that a Gamma distribution has the pdf of
For independence to be the case, we would have to be able to separate out the joint pdf
into two separate functions and , i.e., . Pay special attention to the region of definition for the functions. If you can do this, and are independent.
4. Joint, Marginals and Conditionals #1#
Let random variables
Find:
marginal densities
and ;conditional densities of
given , , and of given , ;the CDF of
, .
Solution
It is useful to draw the support (domain) of the joint density. This is an unbounded region in the first quadrant of the
From
we find .
From we find .
.
where the domain of integration
If
If
For
For
So after integration, our joint CDF is:
Show code cell content
import matplotlib.pyplot as plt
import numpy as np
from myst_nb import glue
x = 2
y = 3
fig, ax = plt.subplots()
# region to fill
ax.fill_between([0, x], [0, x], y, color="lightcoral", alpha=0.3)
# x = y
x_vals = np.linspace(0, max(x, y), 400)
y_vals = x_vals
ax.plot(x_vals, y_vals, label="$x = y$", color="black")
# domain boundaries
ax.plot([x, x], [0, y], "g--")
ax.plot([0, x], [y, y], "g--")
ax.set_xlim(0, max(x, y) * 1.1)
ax.set_ylim(0, max(x, y) * 1.1)
ax.set_xlabel("$x$")
ax.set_ylabel("$y$")
ax.set_title("Integration Domain")
ax.legend()
plt.grid(True)
plt.savefig("../images/q4_plot1.png")

Show code cell content
x = 4
y = 2
fig, ax = plt.subplots()
# region to fill
ax.fill_between([0, y], [0, y], y, color="lightcoral", alpha=0.3)
# x = y
x_vals = np.linspace(0, max(x, y), 400)
y_vals = x_vals
ax.plot(x_vals, y_vals, label="$x = y$", color="black")
# domain boundaries
ax.plot([x, x], [0, y], "g--")
ax.plot([0, x], [y, y], "g--")
ax.set_xlim(0, max(x, y) * 1.1)
ax.set_ylim(0, max(x, y) * 1.1)
ax.set_xlabel("$x$")
ax.set_ylabel("$y$")
ax.set_title("Integration Domain")
ax.legend()
plt.grid(True)
plt.savefig("../images/q4_plot2.png")

5. Joint, Marginals and Conditionals #2#
Let random variables
Show that
and
Find
and .
Solution
Show that
and
First, plotting really does help me get an idea of what’s going on.
To show that
The limits here are from the problem description. You could also do it this way:
Now let’s find
Then
Find
and .
Show code cell content
import numpy as np
import matplotlib.pyplot as plt
from myst_nb import glue
x = np.linspace(0, 1, 1000)
y = 1 - x
fig, ax = plt.subplots()
ax.fill_between(x, 0, y, color="lightblue", label="Domain of $(X, Y)$")
ax.plot(x, y, color="blue", label="$y = 1 - x$")
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.legend()
plt.savefig("../images/q5_plot.png")

6. Joint, Marginals and Conditionals #3#
Let random variables
Find:
marginal densities
and ;conditional densities of
given , , and of given , .Are
and independent?
Solution
Since
, the components and are not independent.
7. Joint, Marginals and Conditionals #4#
Let random variables
Find
constant
.
Thus,
marginal densities
and .
This is Gamma
conditional distributions
and .
Solution
or simpler,
For the marginal density of x, we integrate over the domain of y. We’re interested in varying y values for fixed x. But we still need to pay attention to the other constraint that
Which is
For the marginal density of y, we integrate over the domain of x, but what is the domain of x?
We can rewrite the second condition as
Then the full domain for x is
The conditional
Show code cell content
import matplotlib.pyplot as plt
import numpy as np
# x values (this could go to infinity but this is enough to get an idea)
x_vals = np.linspace(0, 5, 100)
# upper and lower bounds for y based on x
y_upper = x_vals
y_lower = -x_vals
plt.fill_between(x_vals, y_lower, y_upper, color="blue", alpha=0.5)
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Domain of $f_{X,Y}(x, y)$")
plt.grid(True)
plt.savefig("../images/q7_plot.png")

8. A 2D PDF#
Let
be a bivariate PDF of a random vector
Show that
is a density.Show that marginal distributions are
and
Show
and .Show that conditional distributions are
Solution
Contributed by Jason Naramore:
Show that the integral evaluates to 1:
9. In a Circle#
Let random variables
Find:
constant
, andmarginal densities
and .
Solution
Use polar coordinates:
After some calculation,
After setting
Because of symmetry:
10. Weibull - Gamma#
Let
Assume a
Is the problem conjugate?
Find the posterior distribution.
If
, , and are observed from and is gamma , what is the posterior in this case? What are the mean and variance of according to this posterior distribution?
Solution
Greg’s solution from S21:
Assume a Gamma(α, β) prior on λ, we have associated pdf:
(1, 2): Is the problem conjugate? For that to be the case, since the prior is a Gamma distribution, the posterior would have to also be gamma. First we calculate the joint likelihood:
Then we calculate the posterior:
which is the kernel of a Gamma(a, b) distribution with parameters
(3) If
Answer: the posterior has the following distribution:
The mean and variance of a
11. Normal - Inverse Gamma#
Let
Is the problem conjugate?
If the sample
:
[4.3430, 0.5850, 4.4345, 6.2605, 3.9778, 4.4538, 2.3931, 3.5877, 1.4254, 5.0694]
comes from normal
Solution
Given the information:
The posterior distribution is derived from the joint likelihood of the observations and the prior distribution. The joint likelihood is given by:
For the normal likelihood:
And for the inverse gamma prior:
Combining these, we get:
Simplifying, we get:
This is the kernel of an inverse gamma distribution:
Plugging in the provided data yields
12. Uniform - Pareto#
Suppose
Solution
Likelihood Function
The likelihood function is calculated using the product of
Prior Distribution (Pareto)
The pdf of the Pareto distribution (prior) is given by:
Posterior Distribution: With the likelihood and prior provided above, the posterior is proportional to the product of those two in the case of a conjugate pair. After removing constants, the posterior is:
To ensure that all the indicator functions give 1, you need to have:
This is where the first parameter of the posterior comes from. The second parameter can be directly found by comparing the posterior and the kernel of the Pareto distribution. This equates to:
You may refer to Pareto distribution on Wikipedia for the correct form of the prior for
13. Gamma - Inverse Gamma#
Let
Solution
The likelihood is proportional to:
And the prior is proportional to:
Find their product and match the distribution for
The product of the likelihood and the prior is:
Ignoring the constant terms, this simplifies to:
This is the un-normalized density of the inverse gamma distribution
The posterior distribution for
14. Negative Binomial - Beta#
If
Solution
The negative binomial
A random variable
Here the likelihood is proportional to:
The prior
which is a kernel of the beta
15. Horse-Kick Fatalities and Gamma Prior#
During the latter part of the nineteenth century, Prussian officials gathered information on the hazards that horses posed to cavalry soldiers. Fatal accidents for 10 cavalry corps were collected over a period of 20 years (Preussischen Statistik). The number of fatalities due to kicks,
Observed number of corps-years |
|
---|---|
0 |
109 |
1 |
65 |
2 |
22 |
3 |
3 |
4 |
1 |
Total |
200 |
Altogether there were 122 fatalities (
If the prior on
Solution
Show that the likelihood is proportional to:
Use the conjugate structure of the likelihood/prior pair.
Prior:
Likelihood:
Posterior:
16. Counts of Alpha#
Rutherford and Geiger (Rutherford, Geiger and Bateman, Phil. Mag. 20, 698, 19) provided counts of α-particles in their experiment. The counts, given in the table below, can be well–modeled by the Poisson distribution:
Particle ct. |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Interval ct. |
57 |
203 |
383 |
525 |
532 |
408 |
273 |
139 |
45 |
27 |
10 |
4 |
2 |
Theoretical |
54 |
210 |
407 |
525 |
508 |
394 |
254 |
140 |
68 |
29 |
11 |
4 |
6 |
Find sample size n and sample mean
. In calculations for take ≥ 12 as 12.Elicit a gamma prior for λ with rate parameter β = 10 and shape parameter α selected in such a way that the prior mean is 7.
Find the Bayes estimator of λ using the prior from (1). Is the problem conjugate? Use the fact that
.Write a script that simulates the Bayes estimator for λ and compare its output with the analytic solution from (3).
Solution
See this page
17. Estimating Chemotherapy Response Rates#
An oncologist believes that
Hint: For elicitation of the prior use
What are the likelihood and posterior distributions?
Plot the prior, likelihood, and posterior in a single figure.
Using a PPL, find the Bayes estimator of the response rate and compare it to the posterior mean.
Solution
What are the likelihood and posterior distributions?
A binomial likelihood fits the available data and is conjugate with the beta prior. We’ll call the number of total experiments
I will show the full derivation of the conjugacy below anyways because of some confusion around the binomial parameters
Beta prior and binomial likelihood conjugate posterior derivation
Let’s assume you have
Prior
Let’s assume the prior distribution for
Likelihood
For each of the
Since there are
Posterior
Combining terms, we get:
Recognizing this as another beta distribution, we identify the posterior as:
I realize now after writing this that the conjugate table does the opposite with
We need to elicit a beta prior where the mean is
The beta distribution mean is :
Based on the hint, we have
Then our beta prior will be:
making our posterior:
Plot the prior, likelihood, and posterior in a single figure.
Using a PPL, find the Bayes estimator of the response rate and compare it to the posterior mean.
See the second hidden code cell below.
Show code cell content
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import beta, binom
xx = np.linspace(0, 1, 1000)
prior_alpha, prior_beta = 31.5, 3.5
post_alpha, post_beta = 53.5, 11.5
prior_densities = beta.pdf(xx, prior_alpha, prior_beta)
posterior_densities = beta.pdf(xx, post_alpha, post_beta)
n = 30 # trials
k = 22 # successes
p = xx
likelihood = binom.pmf(k, n, p)
# Normalize the likelihood for comparison
likelihood /= np.max(likelihood)
# Plotting the prior, likelihood and posterior
fig, axs = plt.subplots(3, 1, figsize=(12, 8))
fig.suptitle("Chemotherapy Response Rate")
# Plot prior
prior_mean = prior_alpha / (prior_alpha + prior_beta)
axs[0].plot(xx, prior_densities, label="Prior")
axs[0].axvline(x=prior_mean, color="g", linestyle="--")
axs[0].text(
prior_mean + 0.01,
y=np.max(prior_densities) * 0.5,
s=f"{prior_mean:.3f}",
color="g",
ha="left",
)
axs[0].text(
x=0.02,
y=np.max(prior_densities) * 0.85,
s=f"Be({prior_alpha}, {prior_beta}) prior",
color="black",
)
axs[0].set_ylabel("Density")
axs[0].set_xlim(0, 1)
# Plot likelihood
mle = k / n
axs[1].plot(xx, likelihood, label="Likelihood")
axs[1].axvline(x=mle, color="g", linestyle="--")
axs[1].text(
x=mle + 0.01,
y=np.max(likelihood) * 0.5,
s=f"{mle:.3f}",
color="g",
ha="left",
)
axs[1].text(
x=0.02,
y=np.max(likelihood) * 0.85,
s=f"Bin(n={n}, x={k}, p)",
color="black",
)
axs[1].set_ylabel("Density")
axs[1].set_xlim(0, 1)
# Plot posterior
post_mean = post_alpha / (post_alpha + post_beta)
axs[2].plot(xx, posterior_densities, label="Posterior")
axs[2].axvline(x=post_mean, color="g", linestyle="--")
axs[2].text(
x=post_mean + 0.01,
y=np.max(posterior_densities) * 0.5,
s=f"{post_mean:.3f}",
color="g",
ha="left",
)
axs[2].text(
x=0.02,
y=np.max(posterior_densities) * 0.85,
s=f"Be({post_alpha}, {post_beta}) posterior",
color="black",
)
axs[2].set_ylabel("Density")
axs[2].set_xlabel("Probability of Success")
axs[2].set_xlim(0, 1)
plt.savefig("../images/q17_plot.png")

Show code cell content
import pymc as pm
import arviz as az
with pm.Model():
prior_p = pm.Beta("p", 31.5, 3.5)
pm.Binomial("likelihood", n=n, p=prior_p, observed=k)
trace = pm.sample(3000)
az.summary(trace)
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [p]
Sampling 4 chains for 1_000 tune and 3_000 draw iterations (4_000 + 12_000 draws total) took 1 seconds.
mean | sd | hdi_3% | hdi_97% | mcse_mean | mcse_sd | ess_bulk | ess_tail | r_hat | |
---|---|---|---|---|---|---|---|---|---|
p | 0.823 | 0.047 | 0.735 | 0.907 | 0.001 | 0.0 | 4950.0 | 7351.0 | 1.0 |
18. Jeremy and Variance from Single Observation#
Jeremy believes that his IQ test scores have a normal distribution with mean 110 and unknown variance
Show that inverse gamma prior
is the conjugate for if the observation is normal with known. What is the posterior?Find a Bayes estimator of
and its standard deviation in Jeremy’s model if the prior on is an inverse gamma .Hint: Random variable
is said to have an inverse gamma distribution if its density is given by:The mean of
is , and the variance is , .
Use WinBUGS to solve this problem and compare the MCMC approximations with exact values from (2).
Hint: Express the likelihood in terms of precision
with gamma prior, but then calculate and monitor .
Solution
The posterior is proportional to
so we conclude that the posterior is inverse gamma:
For Jeremy’s data,
leading to
See the hidden code cell below.
Show code cell content
import pymc as pm
import arviz as az
x = 98
mu = 110
with pm.Model() as m:
sigma2 = pm.InverseGamma("sigma2", 3, 100)
pm.Normal("likelihood", mu, sigma=pm.math.sqrt(sigma2), observed=x)
trace = pm.sample(10000)
az.summary(trace)
Auto-assigning NUTS sampler...
Initializing NUTS using jitter+adapt_diag...
Multiprocess sampling (4 chains in 4 jobs)
NUTS: [sigma2]
Sampling 4 chains for 1_000 tune and 10_000 draw iterations (4_000 + 40_000 draws total) took 2 seconds.
mean | sd | hdi_3% | hdi_97% | mcse_mean | mcse_sd | ess_bulk | ess_tail | r_hat | |
---|---|---|---|---|---|---|---|---|---|
sigma2 | 69.07 | 58.186 | 15.007 | 149.345 | 0.577 | 0.408 | 13099.0 | 14720.0 | 1.0 |
19. Easy 2D#
Joint distribution for
Find:
Marginal distributions
and .Conditional distributions
and . . .
Hint: Because of the factorization theorem,
For (3) and (4) conditioning is needed. For example, for random variables
Solution
No solution written up for this yet!
20. Bayes and Bats#
By careful examination of sound and film records it is possible to measure the distance at which a bat first detects an insect. The measurements are modeled by a normal distribution
Experts believe that the prior suitably expressing uncertainty about
Find the posterior distribution of
given these observations.Test the hypothesis,
, that in a Bayesian fashion.What is the 95% credible set for
?
For 2 and 3, use the posterior obtained in 1.
Solution
Find the posterior distribution of
given these observations.
Hint: since three measurements are taken and
This is the first conjugate pair on the table. We can use the following to find our posterior:
where
This makes our posterior
Test the hypothesis,
, that in a Bayesian fashion. See code below.
‘Probability of H_0: 0.9452’
What is the 95% credible set for
? See code below.
‘Equi-tailed credible set: (48.2002, 67.7998)’
‘HPD credible set: (48.2002, 67.7998)’
Show code cell content
from scipy.optimize import fsolve
from scipy.stats import norm
posterior = norm(58, 5)
print(f"Probability of H_0: {1 - posterior.cdf(50):.4f}") # 0.9452
alpha = 0.05
print(
f"Equi-tailed credible set: ({posterior.ppf(alpha/2):.4f}, {posterior.ppf(1 - alpha/2):.4f})" # 'Equi-tailed credible set: (48.2002, 67.7998)'
)
guess_lwr, guess_upr = 50, 70
def conditions(x, posterior, alpha):
lwr, upr = x
cond_1 = posterior.pdf(upr) - posterior.pdf(lwr)
cond_2 = posterior.cdf(upr) - posterior.cdf(lwr) - (1 - alpha)
return cond_1, cond_2
# since this distribution is symmetrical, HPD should be the same as EQT
hpd = fsolve(conditions, (guess_lwr, guess_upr), args=(posterior, alpha))
print(
f"HPD credible set: ({hpd[0]:.4f}, {hpd[1]:.4f})"
) # 'HPD credible set: (48.2002, 67.7998)'