You are on page 1of 22

Bayesian Statistics

3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Figure 3.4 (A first course in Bayesian methods)

> par(mfrow=c(2,2))
> theta<-seq(0,1,length=100)
> a<-1; b<-1
> n<-5 ; y<-1
> plot(theta,dbeta(theta,a+y,b+n-y),type="l",ylab=expression(paste(italic("p("),theta,"|y)",sep="")),
xlab=expression(theta), lwd=2)
> mtext(expression(paste("beta(1,1) prior, ", italic("n"),"=5 ",italic(sum(y[i])),"=1",sep="")), side=3,line=.1)
> #abline(v=c((a+y-1)/(a+b+n-2),(a+y)/(a+b+n)),col=c("black","gray"),lty=c(2,2))
> lines(theta,dbeta(theta,a,b),type="l",col="gray",lwd=2)
> legend(.45,2.4,legend=c("prior","posterior"),lwd=c(2,2),col=c("gray","black"), bty="n")
> a<-3; b<-2
> n<-5 ; y<-1
> plot(theta,dbeta(theta,a+y,b+n-y),type="l",ylab=expression(paste(italic("p("),theta,"|y)",sep="")),
xlab=expression(theta), lwd=2)
> #expression(italic(paste("p(",theta,"|y)",sep=""))), xlab=expression(theta),lwd=2)
> mtext(expression(paste("beta(3,2) prior, ", italic("n"),"=5 ",italic(sum(y[i])),"=1",sep="")), side=3,line=.1)
> #abline(v=c((a+y-1)/(a+b+n-2), (a+y)/(a+b+n)) , col=c("green","red") )
> lines(theta,dbeta(theta,a,b),type="l",col="gray",lwd=2)
> a<-1 ; b<-1
> n<-100; y<-20
> plot(theta,dbeta(theta,a+y,b+n-y),type="l",ylab=expression(paste(italic("p("),theta,"|y)",sep="")),
xlab=expression(theta), lwd=2)
> #expression(italic(paste("p(",theta,"|y)",sep=""))), xlab=expression(theta),lwd=2)
> mtext(expression(paste("beta(1,1) prior, ", italic("n"),"=100 ",italic(sum(y[i])),"=20",sep="")), side=3,line=.1)
> #abline(v=c((a+y-1)/(a+b+n-2), (a+y)/(a+b+n)) , col=c("green","red") )
> lines(theta,dbeta(theta,a,b),type="l",col="gray",lwd=2)
> a<-3 ; b<-2
> n<-100; y<-20
> plot(theta,dbeta(theta,a+y,b+n-y),type="l",ylab=expression(paste(italic("p("),theta,"|y)",sep="")),
xlab=expression(theta), lwd=2)
> #expression(italic(paste("p(",theta,"|y)",sep=""))),xlab=expression(theta),
> #lwd=2)
> mtext(expression(paste("beta(3,2) prior, ", italic("n"),"=100 ",italic(sum(y[i])),"=20",sep="")), side=3,line=.1)
> #abline(v=c((a+y-1)/(a+b+n-2), (a+y)/(a+b+n)) , col=c("green","red") )
> lines(theta,dbeta(theta,a,b),type="l",col="gray",lwd=2)
>

Page 1 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Figure 3.6 (A first course in Bayesian methods)

> theta.support<-seq(0,1,length=5000)
> plot(theta.support, dbeta(theta.support, a+y, b+n-y),
type="l",xlab=expression(theta),ylab=expression(paste(italic("p("),theta,"|y)")))
> pth<-dbeta(theta.support, a+y, b+n-y)
> pth<-pth
> ord<- order(-pth)
> xpx<-cbind(theta.support[ord], pth[ord])
> xpx<-cbind(xpx,cumsum(xpx[,2])/sum(xpx[,2]))
> hpd<-function(x,dx,p){
+ md<-x[dx==max(dx)]
+ px<-dx/sum(dx)
+ pxs<--sort(-px)
+ ct<-min(pxs[cumsum(pxs)< p])

Page 2 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

+ list(hpdr=range(x[px>=ct]),mode=md) }
> tmp<-hpd(xpx[,1],xpx[,2],.5)$hpdr
> lines( x=c(tmp[1],tmp[1],tmp[2],tmp[2]),
+ y=dbeta(c(0,tmp[1],tmp[2],0),a+y,b+n-y) ,col=gray(.75),lwd=2 )
> tmp<-hpd(xpx[,1],xpx[,2],.75)$hpdr
> lines( x=c(tmp[1],tmp[1],tmp[2],tmp[2]),
+ y=dbeta(c(0,tmp[1],tmp[2],0),a+y,b+n-y) ,col=gray(.5),lwd=2 )
> tmp<-hpd(xpx[,1],xpx[,2],.95)$hpdr
> lines( x=c(tmp[1],tmp[1],tmp[2],tmp[2]),
+ y=dbeta(c(0,tmp[1],tmp[2],0),a+y,b+n-y) ,col=gray(0),lwd=2 )
> tmp<-qbeta( c(.025,.975), a+y,b+n-y)
> lines( x=c(tmp[1],tmp[1],tmp[2],tmp[2]),
+ y=dbeta(c(0,tmp[1],tmp[2],0),a+y,b+n-y) ,col=gray(0),lwd=2 ,lty=2 )
> legend(.5, 2.75, c("50% HPD","75% HPD","95% HPD","95% quantile-
based"),col=c(gray(.75),gray(.5),gray(0),gray(0)),lty=c(1,1,1,2),lwd=c(2,2,2,2),bty="n")

Page 3 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Figure 3.8 (A first course in Bayesian methods)

> par(mfrow=c(2,3))
> a<-1 ; b<-1
> x<-seq(.001,10,length=100)
> plot(x, dgamma(x,a,b),type="l",
+ xlab=expression(theta), ylab=expression(italic(paste("p(",theta,")",sep=""))))
> mtext(expression(italic(paste("a=",1," b=",1,sep=""))),side=3,line=.12,cex=.8)
> a<-2 ; b<-2
> x<-seq(.001,10,length=100)
> plot(x, dgamma(x,a,b),type="l",
+ xlab=expression(theta), ylab=expression(italic(paste("p(",theta,")",sep=""))))
> mtext(expression(italic(paste("a=",2," b=",2,sep=""))),side=3,line=.12,cex=.8)
> a<-4 ; b<-4
> x<-seq(.001,10,length=100)
> plot(x, dgamma(x,a,b),type="l",
+ xlab=expression(theta), ylab=expression(italic(paste("p(",theta,")",sep=""))))
> mtext(expression(italic(paste("a=",4," b=",4,sep=""))),side=3,line=.12,cex=.8)
> a<-2 ; b<-1
> x<-seq(.001,10,length=100)
> plot(x, dgamma(x,a,b),type="l",
+ xlab=expression(theta), ylab=expression(italic(paste("p(",theta,")",sep=""))))
> mtext(expression(italic(paste("a=",2," b=",1,sep=""))),side=3,line=.12,cex=.8)
> a<-8 ; b<-4
> x<-seq(.001,10,length=100)
> plot(x, dgamma(x,a,b),type="l",
+ xlab=expression(theta), ylab=expression(italic(paste("p(",theta,")",sep=""))))
> mtext(expression(italic(paste("a=",8," b=",4,sep=""))),side=3,line=.12,cex=.8)
> a<-32 ; b<-16
> x<-seq(.001,10,length=100)
> plot(x, dgamma(x,a,b),type="l",
+ xlab=expression(theta), ylab=expression(italic(paste("p(",theta,")",sep=""))))
> mtext(expression(italic(paste("a=",32," b=",16,sep=""))),side=3,line=.12,cex=.8)

Page 4 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Figure 3.10 (A first course in Bayesian methods)

> par(mfrow=c(1,2))
> a<-2
> b<-1
> xtheta<-seq(0,5,length=1000)
plot(xtheta,dgamma(xtheta,a+s1,b+n1),type="l",col=gray(.5),xlab=expression(theta),ylab=expression(paste(italic("p
("),theta,"|",y[1],"...",y[n],")",sep="")))
> lines(xtheta,dgamma(xtheta,a+s2,b+n2),col=gray(0),lwd=2)
> lines(xtheta,dgamma(xtheta,a,b),type="l",lty=2,lwd=2)
> abline(h=0,col="black")
> y<-(0:12)
> plot(y-.1, dnbinom(y, size=(a+s1), mu=(a+s1)/(b+n1)) , col=gray(.5)
,type="h",ylab=expression(paste(italic("p("),y[n+1],"|",y[1],"...",y[n],")",sep="")),xlab=expression(italic(y[n+1])),yl
im=c(0,.35),lwd=3)
> points(y+.1, dnbinom(y, size=(a+s2), mu=(a+s2)/(b+n2)) , col=gray(0) ,type="h",lwd=3)
> legend(1,.375,legend=c("Less than bachelor's","Bachelor's or
higher"),bty="n",lwd=c(3,3),col=c(gray(.5),gray(0)))

Page 5 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Figure 4.2 (A first course in Bayesian methods)

> par(mfrow=c(1,3))
> set.seed(1)
> a<-2 ; b<-1
> sy<-66 ; n<-44
> nsim<-1000
> theta.sim<-rgamma(nsim,a+sy,b+n)
> #cumulative mean
> cmean<-cumsum(theta.sim)/(1:nsim)
> cvar<- cumsum(theta.sim^2)/(1:nsim) - cmean^2
> ccdf<- cumsum(theta.sim<1.75)/ (1:nsim)
> cq<-NULL
> for(j in 1:nsim){ cq<-c(cq,quantile(theta.sim[1:j],probs=0.975)) }
> sseq<- c(1,(1:100)*(nsim/100))
> cmean<-cmean[sseq]
> cq<-cq[sseq]
> ccdf<-ccdf[sseq]
> plot(sseq,cmean,type="l",xlab="# of Monte Carlo samples",ylab="cumulative mean",
+ col="black")
> abline(h= (a+sy)/(b+n),col="gray",lwd=2)
> plot(sseq,ccdf,type="l",xlab="# of Monte Carlo samples",ylab="cumulative cdf at 1.75",col="black")
> abline(h= pgamma(1.75,a+sy,b+n),col="gray",lwd=2)

Page 6 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

> plot(sseq,cq,type="l",xlab="# of Monte Carlo samples",ylab="cumulative 97.5% quantile",col="black")


> abline(h= qgamma(.975,a+sy,b+n),col="gray",lwd=2)

Figure 4.3 (A first course in Bayesian methods)

> set.seed(1)
> a<-1 ; b<-1
> theta.prior.sim<-rbeta(10000,a,b)
> gamma.prior.sim<- log( theta.prior.sim/(1-theta.prior.sim) )
> n0<-860-441 ; n1<-441
> theta.post.sim<-rbeta(10000,a+n1,b+n0)
> gamma.post.sim<- log( theta.post.sim/(1-theta.post.sim) )
> #pdf("fig4_3.pdf",family="Times",height=3.5,width=7)
> #par(mar=c(3,3,1,1),mgp=c(1.75,.75,0))
> par(mfrow=c(2,3))
> par(cex=.8)
> par(mfrow=c(1,2),mar=c(3,3,1,1), mgp=c(1.75,.75,.0))
> plot(density(gamma.prior.sim,adj=2),xlim=c(-5,5),main="", xlab=expression(gamma),
+ ylab=expression(italic(p(gamma))),col="gray")
> plot(density(gamma.post.sim,adj=2),xlim=c(-5,5),main="",xlab=expression(gamma),
+ # ylab=expression(italic(paste("p(",gamma,"|",y[1],"...",y[n],")"))))
+ ylab=expression(paste(italic("p("),gamma,"|",y[1],"...",y[n],")",
+ sep="")) )
> lines(density(gamma.prior.sim,adj=2),col="gray")

Page 7 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Figure 4.5 (A first course in Bayesian methods)

> set.seed(1)
> a<-2 ; b<-1
> sy1<-217 ; n1<-111
> sy2<-66 ; n2<-44
> theta1.mc<-rgamma(10000,a+sy1, b+n1)
> theta2.mc<-rgamma(10000,a+sy2, b+n2)
> y1.mc<-rpois(10000,theta1.mc)
> y2.mc<-rpois(10000,theta2.mc)
> mean(theta1.mc>theta2.mc)
[1] 0.9708
> mean(y1.mc>y2.mc)
[1] 0.4846
> #pdf("fig4_4.pdf",family="Times",height=3.5,width=7)
> #par(mar=c(3,3,1,1),mgp=c(1.75,.75,0))
> par(mfrow=c(1,1))
> plot(density(theta1.mc/theta2.mc,adj=2),main="",xlim=c(.75,2.25),
+ xlab=expression(gamma==theta[1]/theta[2]),
+ #ylab=expression(italic(paste("p(",gamma,"|",bold(y[1]),",",bold(y[2]),")",
+ # sep="")) ))
+ ylab=expression(paste(italic("p("),gamma,"|",bold(y[1]),",",bold(y[2]),")",
+ sep="")) )
> #pdf("fig4_5.pdf",family="Times",height=3.5,width=7)
> #par(mar=c(3,3,1,1),mgp=c(1.75,.75,0))
> par(mfrow=c(1,1))

Page 8 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

> diff.mc<-y1.mc-y2.mc
> ds<- -11:11
> plot(ds,(table(c(diff.mc,ds))-1)/length(diff), type="h",lwd=3,
+ xlab=expression(italic(D==tilde(Y)[1]-tilde(Y)[2])),
+ ylab=expression(paste(italic("p(D"),"|",bold(y[1]),",",bold(y[2]),")",sep="")))

Problem 2.8 (Bayesian data analysis)

1 n
2
180+ 2 150
40 20 1
a) |y N ( 1 n
,
1 n
)
+ +
402 202 402 202
1 n
2
180+ 2 150
40 20 1
b)  ~y |y N ( , +202 )
1 n 1 n
2
+ 2 2
+ 2
40 20 40 20

c) 95% posterior interval for θ | y = 150, n = 10: 150.7 ± 1.96(6.25) = [138, 163]
95% posterior interval for ˜y | y = 150, n = 10: 150.7 ± 1.96(20.95) = [110, 192]

d) 95% posterior interval for θ | y = 150, n = 100: [146, 154]

Page 9 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

95% posterior interval for ˜y | y = 150, n = 100: [111, 189]

Problem 2.9 (Bayesian data analysis)

a)

> theta <- seq(0,1,.001)


> dens <- dbeta(theta,1,.67)
> plot (theta, dens, xlim=c(0,1), ylim=c(0,3),
+ type="l", xlab="theta", ylab="", xaxs="i",
+ yaxs="i", yaxt="n", bty="n", cex=2)
> lines (c(1,1),c(0,3),col=0)
> lines (c(1,1),c(0,3),lty=3)

The density blows up at = 1 but has a finite integral.

b) n = 1000, y = 650, Posterior distribution is p (θ|y) = Beta (α+650, b+350) = Beta (651, 350.67).
The data dominate the prior distribution. E (θ|y) = 0.6499, sd (θ |y) = 0.015

> theta <- seq(0,1,.001)


> dens <- dbeta(theta,651,350.67)
> cond <- dens/max(dens) > 0.001
> plot (theta[cond], dens[cond],
+ type="l", xlab="theta", ylab="", xaxs="i",
+ yaxs="i", yaxt="n", bty="n", cex=2)
>

2.10

Page 10 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Problem 2.10 (Bayesian data analysis)

a)

b)

Page 11 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

c)

Page 12 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Problem 2.13 (Bayesian data analysis)

Page 13 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Page 14 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Problem 3.3 (Introduction Monte Carlo Methods with R)

> h=function(x){ 1/(x^2*sqrt(2*pi)*exp(1/(2*x^2)))}


> par(mfrow=c(2,1))
> curve(h,from=0,to=1/20,xlab="x",ylab="h(x)",lwd="2")
> I=1/20*h(runif(10^4)/20)

Page 15 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

> estint=cumsum(I)/(1:10^4)
> esterr=sqrt(cumsum((I-estint)^2))/(1:10^4)
> plot(estint,xlab="Iterations",ty="l",lwd=2,
+ ylim=mean(I)+20*c(-esterr[10^4],esterr[10^4]),ylab="")
> lines(estint+2*esterr,col="gold",lwd=2)
> lines(estint-2*esterr,col="gold",lwd=2)

> integrate(h,0,1/20)
2.759158e-89 with absolute error < 5.4e-89
> pnorm(-20)
[1] 2.753624e-89
>

Page 16 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Problem 3.4 (Introduction Monte Carlo Methods with R)

b)

> h=function(x){ exp(-(x-3)^2/2)+exp(-(x-6)^2/2)}


> smpl=matrix(h(rnorm(100*10^3)),ncol=100)
> cvrg=apply(smpl,2,cumsum)/(1:10^3)
> matplot(cvrg,type="l",col="grey56",lty=1,xlab="",ylab="")
> abline(h=0.07461577,lty=2,col="sienna")

c)

> h=function(t){
+ x=tan(pi*(t+4.5)/7)
+ dnorm(x)*pi*(exp(-.5*(x-3)^2)+exp(-.5*(x-6)^2))/cos(t)^2}
> smpl=matrix(h(runif(100*10^3,min=-8,max=-1)),ncol=100)
> cvrg=apply(smpl,2,cumsum)/(1:10^3)

Page 17 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

> matplot(cvrg,type="l",lty=1,xlab="",ylab="",ylim=c(0,.15))
> abline(h=0.07461577,lty=2,col="sienna")

> h=function(x){ 7*dnorm(x)*(exp(-(x-3)^2/2)+exp(-(x-6)^2/2))}


> smpl=matrix(h(runif(100*10^3,min=-8,max=-1)),ncol=100)
> cvrg=apply(smpl,2,cumsum)/(1:10^3)
> matplot(cvrg,type="l",col="grey56",lty=1,xlab="",ylab="")
> abline(h=0.07461577,lty=2,col="sienna")

Page 18 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

Problem 3.10 (Introduction Monte Carlo Methods with R)

Solution:

Problem 3.11 (Introduction Monte Carlo Methods with R)

> h=function(x){ exp(-x)*sqrt(x)/gamma(3/2)}


> X = rexp(10^4,1) + 12.5
> I=exp(-12.5)*sqrt(X)/gamma(3/2)
> estint=cumsum(I)/(1:10^4)

Page 19 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

> esterr=sqrt(cumsum((I-estint)^2))/(1:10^4)
> plot(estint,xlab="Iterations",ty="l",lwd=2,
+ ylim=mean(I)+20*c(-esterr[10^4],esterr[10^4]),ylab="")
> lines(estint+2*esterr,col="gold",lwd=2)
> lines(estint-2*esterr,col="gold",lwd=2)

> integrate(h,12.5,Inf)
1.544033e-05 with absolute error < 3.4e-06
> pchisq(25,3,low=F)
[1] 1.544050e-05

> h=function(x){ 1/sqrt(5*pi)*gamma(3)/gamma(2.5)*1/(1+x^2/5)^3}


> integrate(h,50,Inf)
3.023564e-08 with absolute error < 2e-08
> X = rexp(10^4,1) + 50
> I=1/sqrt(5*pi)*gamma(3)/gamma(2.5)*1/(1+X^2/5)^3*1/exp(-X+50)
> estint=cumsum(I)/(1:10^4)
> esterr=sqrt(cumsum((I-estint)^2))/(1:10^4)
> plot(estint,xlab="Mean and error range",type="l",lwd=2,
+ ylim=mean(I)+20*c(-esterr[10^4],esterr[10^4]),ylab="")
> lines(estint+2*esterr,col="gold",lwd=2)
> lines(estint-2*esterr,col="gold",lwd=2)

Page 20 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

As seen on the graph, this method induces jumps in the convergence patterns. Those jumps are indicative
of variance problems, as should be since the estimator does not have a finite variance in this case. The
value returned by this approach differs from alternatives evaluations:

> mean(I)
[1] 1.529655e-08
> sd(I)/10^2
[1] 9.328338e-10
> integrate(h,50,Inf)
3.023564e-08 with absolute error < 2e-08
> pt(50,5,low=F)
[1] 3.023879e-08

Page 21 of 22
Bayesian Statistics
3470: 589-001
HW - 05 Mir Shahnewaz Arefin
Fall 2016 Student ID No: 2824475

c)

> x=rexp(100)
> mean(dnorm(x+4.5)*exp(x))
[1] 2.817864e-06
> var(dnorm(x+4.5)*exp(x))/100
[1] 1.544983e-13

Page 22 of 22

You might also like