PROBLEM No. 1:

a.Show algebraically that a=b=5.06

\(E(Y)=\frac{a}{a+b}\implies0.5=\frac{a}{a+b}\implies0.5(a+b)=a\implies0.5a+0.5b=a\implies0.5b=a-0.5a\)

Since \(a=b\).

\(\frac{a^2}{(2a)^2(2a+1)}=0.15^2\implies\frac{a^2}{(8a^3+4a^2)}=0.0225\implies\frac{a^2}{a^2(8a+4)}=0.0225\implies\frac{1}{0.0225}=8a+4\implies\frac{1}{0.0225}-4=8a \implies 8a=\frac{364}{9}\implies a=\frac{364\times1}{9\times8}\approx5.06\)

Thus, \(a=b=5.06\).

a = 5.06

b =  5.06

E_Y = a/(a+b)
E_Y 
## [1] 0.5
V_Y = a*b/(a+b)^2*(a+b+1)
V_Y
## [1] 2.78
sd = sqrt(V_Y)
sd
## [1] 1.667333

\[E(y) = a/(a+b)\] \[E(y) = 5.06/(5.06+5.06)\] \[= 0.5\]

\[V(Y) = ab/(a+b)^2 (a+b+1)\] \[V(Y) = (5.06)(5.06)/(5.06+5.06)^2 (5.06+5.06+1)\] \[=2.78\] \[sd = sqrt(2.78)\] \[sd = 1.6733\]

#b.Out of the 68 students that she polls, y=21support the current president. Determine posterior distribution using the Beta(5.06,5.06)prior.

p <- seq(0,1, length = 68)
a <- 5.06
b <- 5.06
s <- 1.67
f <- 21
 
 
prior <- dbeta(p, a, b)
like <- dbeta(p, s+1, f+1)
post <- dbeta(p, a+s, b+f)
plot(p, post, type = "l", ylab = "Density", lty = 2, lwd = 3)
lines(p, like, lty = 1, lwd = 3)
lines(p, prior, lty = 3, lwd = 3)
legend(0.7, 4, c("Prior", "Likelihood", "Posterior"),
       lty=c(3, 1, 2), lwd = c( 3, 3, 3))

\[p^ = a'/a'+b'\] \[p^ = 5.06/5.06+5.06\] \[ = 0.5\]

beta(5.06,5.06)
## [1] 0.001451501
  1. Plot both the prior and posterior distributions.
#The plots for both prior and posterior distributions are shown below:


prior <- dbeta(p, a, b)
#like <- dbeta(p, s+1, f+1)
post <- dbeta(p, a+s, b+f)
plot(p, post, type = "l", ylab = "Density", lty = 2, lwd = 3)
#lines(p, like, lty = 1, lwd = 3)
lines(p, prior, lty = 3, lwd = 3)
legend(0.7, 4, c("Prior", "Posterior"),
       lty=c(3, 1, 2), lwd = c( 3, 3, 3))

##d. Construct and interpret a 90% credible interval for p.

ProbBayes::beta_interval(0.9,c(5.06, 5.06))

There is a 90% probability that the true (unknown) estimate would be within the interval 0.250 to 0.422, given the evidence provided by the observed data.

library(ProbBayes)
## Loading required package: LearnBayes
## Loading required package: ggplot2
## Loading required package: gridExtra
## Loading required package: shiny
ProbBayes::beta_interval(0.9,c(5.06, 5.06))

##Interpretation

Problem No.2

a.Find a and b.

a = 5.1 b = 5.1

beta.select(list(x=0.393,p=0.25),
            list(x=0.607,p=0.75))
## [1] 5.1 5.1

b.Find and plot the posterior distribution.

p <- seq(0,1, length= 1000)
a = 5.1
b = 5.1
s = 21 
f = 47

#prior <- dbeta(p, a, b)
#like <- dbeta(p, s+1, f+1)
post <- dbeta(p, a+s, b+f)
plot(p, post, type = "l", ylab = "Density", lty = 2, lwd = 3)
#lines(p, like, lty = 1, lwd = 3)
lines(p, lty = 3, lwd = 3)
legend(0.7, 4, c("Prior", "Posterior"),
       lty=c(3, 1, 2), lwd = c( 3, 3, 3))

c

1-pbeta(0.85,26.1,52.1)
## [1] 0

d.Sophie claims that at least 85% of the students support the current president. Are we going to reject or not her claim? Support your answer.

pred_p_sim <- rbeta(100, 26.1, 52.1)
pred_y_sim <- rbinom(100, 21,pred_p_sim)
print(mean(pred_y_sim))
## [1] 6.79

Based on the data above, there are around 7 students who support the current president in the survey.

e.Does the posterior distribution describe well the prediction in (d)? Explain.

sim.p <- rbeta(100,26.1, 52.1)
sim.y <- rbinom(100, 21, sim.p)
hist(sim.y, xlab = "Simulated Y", main = " ")
abline(v = 7.16, col = "red", lwd = 3, lty = 2)

Yes, the bulk of the posterior distribution is in between0.2-0.5 with its density of more or less than 7.