Skip to content

Commit

Permalink
Complete MaP estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
aaaakshat committed Nov 3, 2021
1 parent 66090c2 commit 968b587
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions ch08.r
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,38 @@ title("Truncated Poisson")
lines(theta, L, lwd=6, col="#0000BF")
grid()

#############
# Chapter 8.3 Maximum-a-Posteriori Estimation

## Influence of the priors

# R code
N = 5
mu0 = 0.0
sigma0 = 1
theta = 5
x = rnorm(N, 5, 1)
xbar = mean(x)
t = seq(-3, 7, (10)/1000)

q = numeric(1000)
for (i in 1:N) {
x = abs(t-x[i])
a = min(x)
q[match(a, x)] = 0.1
}

thetahat = (sigma0^2. * xbar + mu0/N)/(sigma0^2. + 1. /N)
sigmahat = sqrt(1. / (1. / sigma0^2 + N))
p0 = dnorm(t, xbar, 1.)
p1 = dnorm(t, thetahat, sigmahat)
prior = dnorm(t, mu0, sigma0)/10
plot(t, p1, type="n")
title("N = 5")
legend(-2, 0.9, legend=c("Likelihood", "Posterior", "Prior", "Data"), col=c("blue", "orange", "green", "purple"), lty=1:1)
lines(t, p0, lwd = 4, col="blue")
lines(t, p1, lwd = 4, col="orange")
lines(t, prior, lwd = 4, col="green")
lines(t, q, lwd = 4, col="purple")
grid()

0 comments on commit 968b587

Please sign in to comment.