Learn R Programming

docopulae (version 0.3.2)

buildf: Build Density

Description

buildf builds the joint probabilty density given the marginal distributions and some copula.

Usage

buildf(margins, copula, names = NULL)

Arguments

margins
either
  • function(y, theta, ...), wherethetais a list of parameters. It shall return a column matrix of two, the probability densities and cumulative distributions.
  • list of pairs of expressions, named"pdf"
copula
if margins is
  • a function then either a copula object from packagecopulaorfunction(u, theta, ...), a probability density function.
  • a list of expressions then either a copula object from packagecopul
names
(if margins is a function and copula is a copula object) a vector of names or indices, the sequence of copula parameters in theta. 0 or "" identifies copula parameters to omit.

Value

  • buildf returns either
    • function(y, theta, ...), the joint probability density function, ifmarginsis a function.
    • the joint probabilty density as an expression, otherwise.

Details

Please note that expressions are not validated.

See Also

copula, expr2f, numDerivLogf, DerivLogf, fisherI

Examples

Run this code
## for an actual use case see examples for param

library(copula)
library(mvtnorm)

## build bivariate normal pdf
margins = function(y, theta) {
    mu = c(theta$mu1, theta$mu2)
    cbind(dnorm(y, mu), pnorm(y, mu))
}
copula = copula::normalCopula()

f = buildf(margins, copula, 'alpha')
f

## plot density
theta = list(mu1=2, mu2=-3, alpha=0.4)
y1 = seq(0, 4, length.out=51)
y2 = seq(-5, -1, length.out=51)
z = outer(y1, y2, function(y1, y2) apply(cbind(y1, y2), 1, f, theta))
contour(y1, y2, z)

## add theoretical density
copula@parameters = theta$alpha
z2 = outer(y1, y2, function(y1, y2)
    dmvnorm(cbind(y1 - theta$mu1, y2 - theta$mu2), sigma=getSigma(copula)))
contour(y1, y2, z2, col='red', add=TRUE)

## build bivariate pdf with normal margins and clayton copula
margins = list(alist(pdf=dnorm(y1, mu1, 1),
                     cdf=pnorm(y1, mu1, 1)),
               alist(pdf=dnorm(y2, mu2, 1),
                     cdf=pnorm(y2, mu2, 1)))
copula = claytonCopula()
ff = buildf(margins, copula)
f = expr2f(ff, yMap=list(y1=1, y2=2),
               thetaMap=list(mu1='mu1', mu2='mu2', alpha='alpha'))
f

margins = function(y, theta) {
    mu = c(theta$mu1, theta$mu2)
    cbind(dnorm(y, mu, 1), pnorm(y, mu, 1))
}
f2 = buildf(margins, copula, 'alpha')
f2

## plot both densities
theta = list(mu1=2, mu2=-3, alpha=2) # tau = 0.5

y1 = seq(0, 4, length.out=51)
y2 = seq(-5, -1, length.out=51)
z = outer(y1, y2, function(y1, y2) apply(cbind(y1, y2), 1, f, theta))
contour(y1, y2, z)

z2 = outer(y1, y2, function(y1, y2) apply(cbind(y1, y2), 1, f2, theta))
contour(y1, y2, z2, col='red', add=TRUE)

Run the code above in your browser using DataLab