ETC 2420/5242 Lab 10 2016: Purpose
ETC 2420/5242 Lab 10 2016: Purpose
Purpose
Question 1
A situation where Bayesian analysis is routinely used is your spam filter in your mail server. The message is
scrutinized for the appearance of key words which make it likely that the message is spam. Let us describe
how one one of these filters might work. We imagine that the evidence for spam is that the subject message
of the mail contains the sentence “check this out”. We define events spam (the message is spam) and check
this out (the subject line contains this sentence).
From previous experience we know that 40% of emails are spam, 1% of spam email have “check this out” in
the subject line, and .4% of non-spam emails have this sentence in the subject line.
Explain the different steps to compute the conditional probability P(spam | check this out).
P (check this out|spam)P (spam)
P (spam|check this out) = P (check this out)
P (spam) = 0.4
check this out|spam = 0.01
P (check this out) = P (check this out|spam)P (spam) + P (check this out|not spam)P (not spam)
= 0.01 × 0.4 + 0.004 × 0.6 = 0.0064
Question 2
Discuss the behavior of π(θ|x1 , . . . , xn ) as n increases and the impact of the prior distribution.
1
set.seed(1986)
theta <- 2
sigma_0 <- 3
for(n in alln){
x <- rnorm(n, mean = theta, sd= sigma_0)
x_bar <- mean(x)
Y <- cbind(dnorm(xx_prior, mean = prior_mu, sd= prior_tau), dnorm(xx_post, mean = post_mu, sd = post
X <- cbind(xx_prior, xx_post)
matplot(X, Y, type = 'l', lty = 1, main = paste("n = ", n))
abline(v = x_bar, lty = 1)
}
}
# [1] 1.957306
n= 1
0.20
0.10
Y
0.00
−10 0 5 10
# [1] 2.376356
2
n= 2
0.20
0.10
Y
0.00
−5 0 5 10
# [1] 1.561813
n= 5
0.30
0.15
Y
0.00
−5 0 5 10
# [1] 2.718445
n = 10
0.0 0.2 0.4
Y
−5 0 5 10
# [1] 2.307785
3
n = 100
0 1 2 3 4
Y
−5 0 5 10
# [1] 2.028544
n = 10000
400
200
Y
−5 0 5 10
# [1] 4.495602
n= 1
0.4
0.2
Y
0.0
0 2 4 6 8 10
# [1] 4.879569
4
n= 2
0.4
Y
0.2
0.0
0 2 4 6 8 10
# [1] 3.914996
n= 5
0.6
0.3
Y
0.0
0 2 4 6 8 10
# [1] 2.632301
n = 10
0.8
0.4
Y
0.0
0 2 4 6 8 10
# [1] 2.529208
5
n = 100
4
Y
2
0
0 2 4 6 8 10
# [1] 2.069093
n = 10000
400
200
Y
0 2 4 6 8 10
Question 3
Suppose there is a Beta(4, 4) prior distribution on the the probability θ that a coin will yield a “head” when
spun in a specified maner. The coin is independently spun ten times, and “heads” appear fewer than 3 times.
You are not told how many heads were seen, only that the number is less than 3. Calculate your exact
posterior density (up to a proportionality constant) for θ and plot it.
Prior density:
π(θ) ∝ θ3 (1 − θ)3
Likelihood:
10 0 10 1 10 2
f (data|θ) = θ (1 − θ)1 0 + θ (1 − θ)9 + θ (1 − θ)8
0 1 2
= (1 − θ)10 + 10θ(1 − θ)9 + 45θ2 (1 − θ)8
Posterior density:
π(θ|data) ∝ θ3 (1 − θ)13 + 10θ4 (1 − θ)12 + 45θ5 (1 − θ)11
6
theta <- seq(0, 1, .01)
dens <- theta^3 * (1-theta)^13 + 10 * theta^4 * (1-theta)^12 + 45 * theta^5 * (1-theta)^11
plot (theta, dens, ylim=c(0,1.1*max(dens)), type="l", xlab="theta", ylab="", xaxs="i",yaxs="i", yaxt="n"
theta
Question 4
Suppose your prior distribution for θ, the proportion of Californians who support the deat penalty, is beta
with mean 0.6 and standard deviation 0.3.
a. Determine the parameters α and β of your prior distribution. Plot the prior density function.
b. A random sample of 1000 Californians is taken, and 65% support the death penalty. What are your
posterior mean and variance for θ? Plot the posterior density function.
E[θ](1−E[θ])
α+β = var(θ) − 1 = 1.67
α = (α + β)E[θ] = 1
β = (α + β)(1 − E[θ]) = 0.67
theta
7
Posterior distribution:
π(θ|data) = Beta(α + 650, β + 350) = Beta(651, 350.67)
E(θ|data) = 0.6499
sd(θ|data) = 0.015
theta
Question 5
10 Prussian cavalry corp were monitored for 20 years (200 Corp-Years) and the number of fatalities due to
horse kicks were recorded:
i.i.d
Let xi , i = 1, . . . , 200, be the number of deaths in observation i. Assume that xi ∼ Poisson(θ).
θ̂MLE = x̄ = 122
200 = 0.61
Suppose θ ∼ Gamma(α, β).
E[θ] = α
β
V ar[θ] = α
β2
8
b. What is the posterior distribution π(θ|x)?
Gamma(α + n ∗ x̄, β + n)
E[θ|x] = α+n∗x̄
β+n
V ar[θ|x] = α+n∗x̄
(β+n)2
a. α=β = 0.5
b. α=β =1
c. α=β = 10
d. α=β = 100
n <- 200
DT <- data.frame(c(0, 1, 2, 3, 4), c(109, 65, 22, 3, 1))
xbar <- sum(DT[, 1] * DT[, 2])/n
matplot(x, cbind(dens, dens_posterior), lty = 1, type = 'l', ylab = "Density", xlab = "theta")
abline(v = xbar)
9
6
Density
4
2
0
0.0 0.5 1.0 1.5 2.0
theta
6
Density
4
2
0
theta
6
Density
4
2
0
theta
8
6
Density
4
2
0
theta
10
TURN IN
Resources
11