## code to accompany the Normal density/distr ## example from lecture 2 of PSTAT 215A, ## Bayesian Inference, UCSB ## ## Robert B. Gramacy adapted from Peter Hoff ## Generating figure 2.2 from book par(mfrow=c(1,2)) ## setting up the y-vaues, the mean, and sd y <- seq(8,14,length=1000) mu <- 10.75 sigma <- sqrt(0.64) ## making two plots plot(y, pnorm(y, mu, sigma), type="l", lwd=2, xlab="y", ylab="F(y)") abline(h=c(0,0.5,1), col="gray") plot(y, dnorm(y, mu, sigma), type="l", lwd=2, xlab="y", ylab="f(y)") abline(v=mu, col="gray") ## comparing normal to log normal (Fig 2.3) par(mfrow=c(1,2), bty="n") plot(y, dnorm(y, mu, sigma), type="l", lwd=2, xlab="y", ylab="f(y)", main="normal") abline(v=c(mu, mu, mu), col=1:3, lty=1:3, lwd=2) sigma.2 <- 0.8 y2 <- seq(0, 300000, length=1000) f2 <- dlnorm(y2, mu, sigma.2) plot(y2, 10^5*f2, type="l", lwd=2, xlab="y", ylab="10^5*f(y)", main="log-normal") m <- exp(mu + 0.5*0.8*sigma.2^2) med <- exp(mu) mode <- y2[which.max(f2)] abline(v=c(m, med, mode), col=1:3, lty=1:3, lwd=2) legend("topright", c("mean", "median", "mode"), col=1:3, lty=1:3, lwd=2)