Problem 1

(1)

P = (.86*.16)/((.86*.16)+(.08*(1-.16)))

\(Pr(D|+)=\frac{Pr(+|D)Pr(D)}{Pr(+|D)Pr(D)+Pr(+|H)Pr(H)}=\frac{.86*.16}{.86*.16+.08*(1-.16)}=\) 0.671875

(2)

This depends entirely on if the disease is contagious, and how much contact they have had with the locals. It would be unreasonable to assume they have the same base rate as a local population. If we did make that assumption, it would look like this:

P = (.86*.60)/((.86*.60)+(.08*(1-.60)))

\(Pr(D|+) = \frac{Pr(+|D)Pr(D)}{Pr(+|D)Pr(D)+Pr(+|H)Pr(H)}=\frac{.86*.60}{.86*.60+.08*(1-.60)}=\) 0.9416058

Problem 2

(1)

\(\theta_1 = 0.1\) \(\theta_2 = 0.2\)

Prior: \(p(\theta_1) = p(\theta_2) = 0.5\)

Where \(\theta_1\) is the probability that the first machine has the higher payout \(\theta_2\) is the probability that the second machine has a higher payout

First observation: \(x = 0\)

Since we want machine 2, we will update our estimate that this is that machine \(\theta_2\) is the one we put money into.

\(P(\theta_2|x=0) = \frac{P(x=0|\theta_2)P(\theta_2)}{P(x=0|\theta_2)P(\theta_2)+P(x=0|\theta_1)P(\theta_1)}\)

\(P(\theta_2|x=0) = \frac{0.8*0.5}{0.8*0.5+0.9*0.5}\)

P = (.8*.5)/((.8*.5)+(.9*.5))

\(P(\theta_2|x=0) =\) 0.4705882

(2)

\(P(\theta_2|x=1) = \frac{P(x=1|\theta_2)P(\theta_2)}{P(x=1|\theta_2)P(\theta_2)+P(x=1|\theta_1)P(\theta_1)}\)

\(P(\theta_2|x=1) = \frac{0.2*0.5}{0.2*0.5+0.1*0.5}\)

P = (.2*.5)/((.2*.5)+(.1*.5))

\(P(\theta_2|x=1) =\) 0.6666667

Problem 3

(1)

P = .008+.008+.008+.008

Prior probability that \(\theta > .05 =\) 0.032

(2)

theta = c(0,.125,.250,.375,.5,.625,.750,.875,1)
p = c(.001,.001,.95,.008,.008,.008,.008,.008,.008)
n = 10
y = 7

mp = sum(dbinom(7, size=10, prob=theta)*p)

Marginal probability for getting 7 right out of 10: 0.0087419

(3)

y = 6

postProb = sum(dbinom(6, size=10, prob= theta[which(theta > .5)])* p[which(theta > .5)])/sum(dbinom(6, size=10, prob= theta)* p)

Posterior probability for \(p(\theta>.5|x=6) =\) 0.1579494

(4)

I would have used a Beta prior with parameters a = 1 and b = 4 to correspond with 5 trials and an \(E(\theta) = 0.2\). The beta parameters can directly correspond to the number of successes and number of failures.

(5)

\(P(\theta|x=6) \propto \theta^6(1-\theta)^7\)

(6)

\(E(\theta|y)=\frac{a+y}{a+b+n}=\frac{1+6}{1+4+10}=7/15=\) 0.4666667

\(E(\theta)=\frac{a}{a+b}=\frac{1}{1+4}=1/5=\) 0.2

The expected value of the posterior distribution is more than twice of the prior. This indicates the observed information was very informative/surprising or our prior may have been misspecified.

(7)

scale = 1:100/100;
#likelihood
plot(x= scale,y= dbinom(x = 6,10,prob = scale),type= "p",col="black",lwd = 2)

#prior
plot(x=scale,y = dbeta(x = scale,1,4),type= "l",col="red",lwd = 2)

#posterior
points(x= scale,y = dbeta(x = scale,7,8),type= "l",col="blue",lwd = 2)

legend(x=.8,y=3.5, legend=c("Prior", "Posterior"),
       col=c("red", "blue"), lty=1:1, cex=0.8)

(8)

#posterior predictive
#a = 7 b = 8
barplot(dbetabinom(y=0:10,m=7/15,s=15,size=10),names.arg=0:10)

(9)

Since we have already run this experiment once, we can use our posterior (Beta(7,8)) from the previous experiment as the prior for this one. This has updated our previous belief with more information.

#posterior
#a = 7+5 b = 8+5
plot(x=scale,y=dbeta(x =scale,7+5,8+5),type= "l",col="blue",lwd = 2)

\(E(\theta|y)=\frac{a+y}{a+b+n}=\frac{7+5}{7+8+10}=12/25=\) 0.48

Problem 4

(1)

mu = c(20,30,40,50,60,70)
prior = c(.1,.15,.25,.25,.15,.1)

(2)

y = c(38.6,42.4,57.5,40.5,51.7,67.1,33.4,60.9,64.1,40.1,40.7,6.4)
ybar = mean(y)

ybar = 45.2833333

(3)

\[ \begin{aligned} L(\mu,\sigma^2) &= \prod_{i=1}^{n}(2\pi\sigma^2)^{1/2}exp(-\frac{1}{2\sigma^2}(x_i - \mu)^2)\\ &= (2\pi\sigma^2)^{n/2}exp(-\frac{1}{2\sigma^2}(x_i - \mu)^2)\\ &= (2\pi\sigma^2)^{n/2}exp(-\frac{1}{2\sigma^2}\sum_{i=1}^{n}(x_i - \mu)^2)\\\\ \text{Rearrange this section:}\\ \sum_{i=1}^{n}(x_i - \mu)^2 &= \sum_{i=1}^{n}(x_i - \bar{x} + \bar{x} - \mu)^2\\ &= \sum_{i=1}^{n}[(x_i - \bar{x})^2+2(x_i-\bar{x})(x-\bar{\mu})+(\bar{x}-\mu)^2]\\\\ \text{So now we have:}\\ L(\mu,\sigma^2)&=(2\pi\sigma^2)^{n/2}exp(-\frac{1}{2\sigma^2}(\sum_{i=1}^{n}(x_i-\bar{x})^2+n(\bar{x}-\mu)^2)\\ L(\mu) &\propto exp-(\frac{n}{2\sigma^2}(\bar{x}-\mu)^2)\\ \end{aligned} \]

(4)

ybar = mean(y)
StD = 10
n=length(y)

a=(n/(2*StD^2))
b=((ybar-mu)^2)
like = exp(-a*b)
like
## [1] 2.201480e-17 8.192991e-07 1.873425e-01 2.632064e-01 2.272076e-06
## [6] 1.205079e-16

(5)

post = prior*like/sum(prior*like)
post
## [1] 1.954479e-17 1.091063e-06 4.158078e-01 5.841881e-01 3.025731e-06
## [6] 1.069871e-16

(6)

\(\mu|y \sim N(\frac{\tau_0}{\tau_0+n\tau_*}\mu_0+\frac{n\tau_*}{\tau_0+n\tau_*}\bar{y},\frac{1}{\tau_0+n\tau_*})\)

\(\mu|y \sim N(\frac{1/10}{1/10+12/100}40+\frac{12/100}{1/10+12/100}\bar{y},\frac{1}{1/10+12/100})\)

snow = 300:600/10
mu0 = 40
tau0 = 1/10
tauStar = 1/100
postMean = (tau0/(tau0+n*tauStar))*mu0+((n*tauStar)/(tau0+n*tauStar))*ybar
postMean
## [1] 42.88182
postSD = sqrt(1/(tau0+n*tauStar))
plot(x = snow,dnorm(x=snow,mean = postMean,sd = postSD),type= "l",lwd = 2)

ybar
## [1] 45.28333

The prior seems to be placed around a good value and only slightly reduces the posterior expectation relative to the data.

(7)

#posterior
plot(snow,dnorm(x=snow,mean = postMean,sd = postSD),type= "l",col="blue",lwd = 2)

#prior
points(snow,dnorm(x=snow,mean = mu0,sd = sqrt(1/tau0)),type= "l",col="red",lwd = 2)

legend(x=52,y=.16, legend=c("Prior", "Posterior"),
       col=c("red", "blue"), lty=1:1, cex=0.8)

The posterior distribution seems to have a much lower variance than the prior, this may be an indication of higher certainty given the data.

(8)

qnorm(c(.10,.90),postMean,postSD)
## [1] 40.14954 45.61410

(9)

\[ \begin{aligned} p(\mu|y) &\propto exp-(\frac{n\tau_*}{2}(\bar{y}-\mu)^2)*exp-(\frac{\tau_0}{2}(\mu-\mu_0)^2)\\ &\propto exp-\frac{1}{2}(n\tau_*(\bar{y}-\mu)^2+\tau_0(\mu-\mu_0)^2)\\ &\propto exp-\frac{1}{2}(n\tau_*(\bar{y}^2-2\bar{y}\mu+\mu^2)+\tau_0(\mu^2-2\mu\mu_0+\mu_0^2))\\ &\propto exp-\frac{1}{2}(\mu^2(n\tau_*+\tau_0)+2\mu(\tau_0\mu_0+\tau_*\bar{y}n))\\ &\propto exp-\frac{n\tau_*+\tau_0}{2}(\mu^2-\frac{2\mu(\tau_0\mu_0+\tau_*\bar{y}n)}{(n\tau_*+\tau_0)})\\ &\propto exp-\frac{n\tau_*+\tau_0}{2}(\mu^2-\frac{2\mu(\tau_0\mu_0+\tau_*\bar{y}n)}{(n\tau_*+\tau_0)}+(\frac{(\tau_0\mu_0+\tau_*\bar{y}n)}{(n\tau_*+\tau_0)})^2-(\frac{(\tau_0\mu_0+\tau_*\bar{y}n)}{(n\tau_*+\tau_0)})^2)\\ &\propto exp-(\frac{n\tau_*+\tau_0}{2}(\mu-\frac{(\tau_0\mu_0+\tau_*\bar{y}n)}{(n\tau_*+\tau_0)})^2)\\ \mu|y &\sim N(\frac{(\tau_0\mu_0+\tau_*\bar{y}n)}{(n\tau_*+\tau_0)},\frac{1}{\tau_0+n\tau_*}) \end{aligned} \]

Problem 5

(1)

\[ \begin{aligned} y|\theta &\sim Exponential(\theta)\\ \theta &\sim Gamma(\alpha, \beta)\\ p(\theta|x) &\propto p(\theta)l(\theta|x)\\ &\propto \frac{\beta^\alpha}{\Gamma(\alpha)}\theta^{\alpha-1}e^{-\beta\theta}\prod_{i=1}^{n}\theta e^{-\theta x_i}\\ &\propto \theta^{\alpha+n-1}e^{-(\beta+n\bar{x})\theta}\\ \text{and since this is a gamma density such that:}\\ \theta|x &\sim Gamma(\alpha+n,\beta+n\bar{x})\\ \text{The gamma prior is conjugate to the exponential} \end{aligned} \]

(2) \[ \begin{aligned} \text{We can continue where we left off on (1)}\\ p(\theta|x) &\propto \theta^{\alpha+n-1}e^{-(\beta+n\bar{x})\theta}\\ \text{and reparameterize:} \phi = 1/\theta\\ &\propto \theta^{-\alpha+n-1}exp(-\frac{(\beta+n\bar{x})}{\theta})\\ \text{Which is the inverse gamma with parameters:}\\ \theta|x &\sim InvGamma(\alpha+n,\beta+n\bar{x}) \end{aligned} \] (3)

\[ \begin{aligned} \Gamma(\alpha+n,\beta+\sum_{i=1}^{n}x_i)\\ \mu &= \alpha/\beta\\ \sigma^2 &= \alpha/\beta^2\\ Coefficient Of Variation &= \sigma/\mu\\ Coefficient Of Variation &= \frac{\sqrt{\alpha/\beta^2}}{\alpha/\beta}\\ .5 &= \frac{1}{\sqrt{\alpha}} &.1= \frac{1}{\sqrt{\alpha_0}}\\ \alpha &= 4 &\alpha_0=100\\\\\\ \alpha_0 &= \alpha+n\\ 100 &= 4+n\\ n &= 96 \end{aligned} \]

(4)

\[ \begin{aligned} CoefficientOfVariation &= \phi = 1/\theta\\ \mu &= \frac{\beta}{\alpha-1}\\ \sigma^2 &= \frac{\beta^2}{(\alpha-1)^2(\alpha-2)}\\ Coefficient Of Variation &= \sigma/\mu\\ Coefficient Of Variation &= \frac{\beta}{(\alpha-1)\sqrt{(\alpha-2)}}*\frac{\alpha-1}{\beta}\\ Coefficient Of Variation &= \frac{1}{\sqrt{(\alpha-2)}}\\ .5 &= \frac{1}{\sqrt{(\alpha-2)}} &.1= \frac{1}{\sqrt{(\alpha_0-2)}}\\ \alpha &= 6 &\alpha_0=102\\\\\\ \alpha_0 &= \alpha+n\\ 102 &= 6+n\\ n &= 96 \end{aligned} \]

(5)

\[ \begin{aligned} p(\theta|y\geq100)&\propto p(y\geq100|\theta)p(\theta)\\ &\propto exp(-100\theta)\theta^{\alpha-1}exp(-\beta\theta)\\ &\propto Gamma(\alpha,\beta+100)\\ \text{where posterior mean and var:}\\ \mu&= \frac{\alpha}{\beta+100} &\sigma^2=\frac{\alpha}{(\beta+100)^2} \end{aligned} \]

(6)

\[ \begin{aligned} p(\theta|y=100)&\propto p(y=100|\theta)p(\theta)\\ &\propto \theta exp(-100\theta)\theta^{\alpha-1}exp(-\beta\theta)\\ &\propto Gamma(\alpha+1,\beta+100)\\ \text{where posterior mean and var:}\\ \mu&= \frac{\alpha+1}{\beta+100} &\sigma^2=\frac{\alpha+1}{(\beta+100)^2} \end{aligned} \]