Skip to content

Instantly share code, notes, and snippets.

@wviechtb
Last active April 23, 2023 01:09
Show Gist options
  • Save wviechtb/0e80dcc4335e05e4ed7dcaaf8bb8c9a6 to your computer and use it in GitHub Desktop.
Save wviechtb/0e80dcc4335e05e4ed7dcaaf8bb8c9a6 to your computer and use it in GitHub Desktop.
Code to generate wallpaper (bivariate normal distribution)
rm(list=ls())
setwd("/home/wviechtb/misc/wallpapers/bivariate_normal")
library(mvtnorm)
library(ellipse)
### specify the correlation for the bivariate normal distribution
rho <- 0.4
### number of points for x and y at which to evaluate the density of the bivariate normal distribution
n <- 501
sigma <- matrix(c(1,rho,rho,1), nrow=2)
x <- seq(-3.5, 3.5, length=n)
y <- seq(-3.5, 3.5, length=n)
z <- matrix(NA, nrow=n, ncol=n)
for (i in seq_along(x)) {
for (j in seq_along(y)) {
z[i,j] <- dmvnorm(c(x[i],y[j]), sigma=sigma)
}
}
z[z <= .001] <- 0
jpeg("bivariate_normal_2.jpg", width=2732, height=1536, quality=100, bg="gray10", type="cairo")
### bivariate normal surface
par(mar=c(6,0,6,0))
nrz <- nrow(z)
ncz <- ncol(z)
cols <- colorRampPalette(c(rgb(36,36,36,maxColorValue=255), "gray60"))
nbcol <- 10000
color <- cols(nbcol)
zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
facetcol <- cut(zfacet, nbcol)
col <- color[facetcol]
val <- .136
col[c(zfacet) <= .001] <- rgb(val,val,val)
sav <- persp(x, y, z, theta=0, phi=25, r=100, d=1, box=FALSE, col=col, border=NA, expand=0.4, ltheta=-125, lphi=-10, shade=.3)
### add pdf text
text(0, 0.006, col="gray70", pos=1, cex=2.6,
expression(frac(1, 2 ~ pi ~ sigma[x] ~ sigma[y] ~ sqrt(1 - rho^2)) ~
exp ~ bgroup("[", -frac(1,2*(1-rho^2)) ~ bgroup("(", frac((x-mu[x])^2, sigma[x]^2) ~ + ~
frac((y-mu[y])^2, sigma[y]^2) ~ - ~
frac(2 ~ rho ~ (x-mu[x]) ~ (y-mu[y]), sigma[x] ~ sigma[y]),
")"),
"]")
)
)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment