From 968b587098d396a35d6709b1c232f7fc6c0e54e0 Mon Sep 17 00:00:00 2001 From: aaaakshat <33050725+aaaakshat@users.noreply.github.com> Date: Wed, 3 Nov 2021 11:03:56 -0400 Subject: [PATCH] Complete MaP estimation --- ch08.r | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/ch08.r b/ch08.r index 3ff9cb5..5e44ea7 100644 --- a/ch08.r +++ b/ch08.r @@ -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() +