Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created November 26, 2014 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abikoushi/fc65567d00c6cd1e1bb7 to your computer and use it in GitHub Desktop.
Save abikoushi/fc65567d00c6cd1e1bb7 to your computer and use it in GitHub Desktop.
probability density function of bivariate normal distribution
dnorm2 <- function(x, y, r=0.7){
det <- 1- r^2
return(
1/(2 * pi * sqrt(det)) * exp(-(x^2 -2 * r * y + y^2)/(2*det) )
)
}
@abikoushi
Copy link
Author

Argument

x: x-coordinate
y: y-coordinate
r: correlation coefficient

Example

  x<- seq(-3,3,length = 100)
  y<-x
  z <- outer(x,y,r=-0.8,normal2dens)
#3D plot
 persp(x,y,z)

#contour plot
 contour(x,y,z)
 abline(0,1,lty=2)
 abline(v=0)
 abline(h=0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment