Search code examples
rcopula

How to represent the joint PDF via marginal distribution function and copula function?


In R language , I want to represent the joint probability density function (PDF) \ joint cumulative distribution function (CDF) by the combination of copula function and marginal probability function \ distribution function \ copula function \ distribution function \ Copula function. For example, there are two dimensions of data that follow the normal distribution, the mean is 3 and 4, the standard deviation is 1 and 1, and the Gaussian copula (parameter is 0.5) is selected as the copula function. Can someone teach me? Thank you so much! For example, here is the code to build PDFS and CDFS without using copula functions.Who can help me to represent Jpdf through the Copula function, JPDF looks like the following form,

      mux=c(3,4)
      sigmax=c(1,1)
      x=mux
      cdfx=c(pnorm(x[1],mux[1],sigmax[1]),pnorm(x[2],mux[2],sigmax[2]))
      pdfx=c(dnorm(x[1],mux[1],sigmax[1]),dnorm(x[2],mux[2],sigmax[2]))

Solution

  • library(copula)
    
    # Gaussian copula
    cpl <- normalCopula(param = 0.2, dim = 2)
    
    # parameters of marginal distributions
    mux <- 3
    muy <- 4
    sigmax <- 1
    sigmay <- 1
    
    # make grid
    npts <- 40
    x_ <- seq(-4*sigmax, 4*sigmax, length.out = npts) + mux
    y_ <- seq(-4*sigmay, 4*sigmay, length.out = npts) + muy
    Grid <- expand.grid(X = x_, Y = y_)
    
    # points at which the density will be evaluated
    M <- cbind(
      pnorm(Grid$X, mux, sigmax),
      pnorm(Grid$Y, muy, sigmay)
    )
    
    # evaluate density
    dnsty <- dCopula(M, cpl) * 
      dnorm(Grid$X, mux, sigmax) * dnorm(Grid$Y, muy, sigmay)
    dim(dnsty) <- c(npts, npts)
    
    # plot
    library(rgl)
    persp3d(x = x_, y = y_, z = dnsty, front = "lines", back = "lines")
    

    enter image description here


    EDIT

    Evaluation of the cdf and the pdf

    library(copula)
    
    # Gaussian copula
    cpl <- normalCopula(param = 0.5, dim = 2)
    
    # parameters of marginal distributions
    mux <- 3
    muy <- 4
    sigmax <- 1
    sigmay <- 1
    
    # evaluates cdf  P(X < x, Y < y)
    x <- 2.9
    y <- 3.2
    pCopula(c(pnorm(x, mux, sigmax), pnorm(y, muy, sigmay)), cpl)
    
    # evaluates pdf at (x, y)
    dCopula(c(pnorm(x, mux, sigmax), pnorm(y, muy, sigmay)), cpl) * 
      dnorm(x, mux, sigmax) * dnorm(y, muy, sigmay)