############################################################

## Name: Joshua M. Tebbs

## Date: 29 Sep 09

## Purpose: Construct bivariate normal pdf

############################################################

 

mu1<-0 # setting the expected value of y1

mu2<-0 # setting the expected value of y2

s11<-1 # setting the variance of y1

# s12<-0 # setting the covariance between y1 and y2

s22<-1 # setting the variance of y2

rho<-0.0 # setting the correlation coefficient between y1 and y2

y1<-seq(-10,10,length=100) # generating the vector series y1

y2<-y1

 

## setting up the bivariate normal density

 

f<-function(y1,y2)

{

term1<-1/(2*pi*sqrt(s11*s22*(1-rho^2)))

term2<--1/(2*(1-rho^2))

term3<-(y1-mu1)^2/s11

term4<-(y2-mu2)^2/s22

term5<--2*rho*((y1-mu1)*(y2-mu2))/(sqrt(s11)*sqrt(s22))

term1*exp(term2*(term3+term4-term5))

}

 

## calculating the density values

 

z<-outer(y1,y2,f)

 

## produces the 3-D plot

 

persp(y1, y2, z,

main="Bivariate normal distribution",

col="grey",

theta=30, phi=20,

r=50,

d=0.1,

expand=0.5,

ltheta=90, lphi=180,

shade=0.75,

ticktype="detailed",

nticks=5)

mtext(expression(list(mu[1]==0,mu[2]==0,sigma[11]==1,sigma[22]==1,sigma[12

]==0,rho==0)), side=3)