Copula contour plot

92 Views Asked by At

I am trying to get some nice contour plots of various Copulas with Gaussian marginals, but somehow I do not at all get what I would expect - what am I doing wrong ?

My R code:

library(copula)
library(mvtnorm)

#Gaussian Density & distribution
G_copula = norm.cop <- normalCopula(0.70)
cp <- contour(G_copula, dCopula, n=200, nlevels=20, delta=0.01)
persp(G_copula, dCopula)
contour(cp$x,cp$y,cp$z)
contour(qnorm(cp$x),qnorm(cp$y),cp$z)

What I would like to get is something like this enter image description here

But what I get is this enter image description here

2

There are 2 best solutions below

1
Stéphane Laurent On BEST ANSWER

If you want the contours plot of the copula with margins N(0,1) and N(0,1), one option is to "manually" define its density:

library(copula)

cop <- normalCopula(0.7)

# density of the bivariate distribution with copula 'cop' and margins N(0,1), N(0,1)
f <- function(x, y) {
  dCopula(c(pnorm(x), pnorm(y)), cop) * dnorm(x) * dnorm(y)
}

Then you can use the usual contour function.

But it's easier to create this distribution with mvdc:

library(copula)
mv <- mvdc(
  normalCopula(0.7), margins = c("norm", "norm"),
  paramMargins = list(list(mean = 0, sd = 1), list(mean = 0, sd = 1))
)

Then you can directly call contour on this multivariate distribution:

contour(mv, dMvdc, xlim = c(-3, 3), ylim = c(-3, 3), asp = 1)

enter image description here

0
mfg3z0 On

Based on the example you've provided, this plot you are trying to recreate just looks like the bivariate normal distribution, which is used to create your copula. You can create a contour plot of the bivariate normal distribution, matching the parameters you used for your copula, as follows:

library(ggplot2)

mu <- c(0, 0)
sigma <- matrix(c(1, 0.7, 0.7, 1), 2, 2) # Use a correlation of 0.7, matching your copula
bivariate_normal <- rmvnorm(100000, mean = mu, sigma = sigma)
colnames(bivariate_normal) <- c("x", "y")

df <- tibble::as_tibble(bivariate_normal)

df |>
  ggplot(aes(x = x, y = y)) +
  geom_density_2d() +
  labs(title = "Bivariate Normal Distribution (Correlation = 0.7)",
       x = "X", y = "Y")

The output looks like this: enter image description here