## Learning about a correlation

We’re starting the chapter on Bayesian computation and I thought I’d use a new example to illustrate the basic methods. I collect the high school GPA and the ACT math score for 50 BGSU freshmen and I’m interested in learning about the correlation. If I standardize both measurements (by subtracting the mean and dividing by the standard deviation), then I can assume the standardized pairs are a random sample from a bivariate normal density with 0 means, unit standard deviations, and correlation . If I assume a uniform prior on , then the posterior density is given by

I begin by writing a short function to compute the log posterior of . Here data is a matrix of two columns of the standardized x and standardized y.

cor.sampling=function(rho,data)

{

x=data[,1]

y=data[,2]

sum(-0.5*log(1-rho^2)-0.5/(1-rho^2)*(x^2-2*rho*x*y+y^2))

}

gr=sapply(r,cor.sampling,dz)

gr=exp(gr-max(gr))/sum(exp(gr-max(gr)))/0.01

plot(r,gr,type=”l”,xlim=c(0,1),col=”red”,lwd=2)

fit

[1] 0.5015625

[1,] 0.00912294

legend(“topleft”,c(“Exact”,”Approximation”),col=c(“red”,”blue”),lty=c(1,1),lwd=c(2,2))