Title: | Bivariate Empirical Subcopula |
---|---|
Description: | Calculate empirical subcopula and dependence measures from a given bivariate sample, and Bernstein copula approximations. |
Authors: | Arturo Erdely |
Maintainer: | Arturo Erdely <[email protected]> |
License: | GPL-3 |
Version: | 1.3 |
Built: | 2024-11-06 06:29:43 UTC |
Source: | CRAN |
Bernstein copula approximation from the empirical subcopula of given bivariate data.
Bcopula(mat.xy, m, both.cont = FALSE, tolimit = 1e-05)
Bcopula(mat.xy, m, both.cont = FALSE, tolimit = 1e-05)
mat.xy |
2-column matrix with bivariate observations of a random vector |
m |
integer value of approximation order, where |
both.cont |
logical value, if TRUE then (X,Y) are considered (both) as continuos random variables, and jittering will be applied to repeated values (if any). |
tolimit |
tolerance limit in numerical approximation of the inverse of the first partial derivatives of the estimated Bernstein copula. |
Each of the random variables and
may be of any kind (discrete, continuous, or mixed). NA values are not allowed.
A list containing the following components:
copula |
bivariate Bernstein Copula function (BC) of order |
du |
bivariate function |
dv |
bivariate function |
du.inv |
inverse of |
dv.inv |
inverse of |
density |
bivariate Bernstein copula density function of order |
bilinearCopula |
bivariate function of bilinear approximation of copula |
bilinearSubcopula |
|
sample.size |
sample size of bivariate observations |
order |
approximation order |
both.cont |
logical value, TRUE if both variables considered as continuous |
tolimit |
tolerance limit in numerical approximation of |
subcopemObject |
list object with the output from |
Development of this code was partially supported by Programa UNAM DGAPA PAPIIT through project IN115817.
If both and
are continuous random variables it is faster and better to set
both.cont = TRUE
.
Arturo Erdely https://sites.google.com/site/arturoerdely
Erdely, A. (2017) A subcopula based dependence measure. Kybernetika 53(2), 231-243. DOI: 10.14736/kyb-2017-2-0231
Nelsen, R.B. (2006) An Introduction to Copulas. Springer, New York.
Sancetta, A., Satchell, S. (2004) The Bernstein copula and its applications to modeling and approximations of multivariate distributions. Econometric Theory 20, 535-562. DOI: 10.1017/S026646660420305X
## (X,Y) continuous random variables with copula FGM(param = 1) # Theoretical formulas FGMcopula <- function(u, v) u*v*(1 + (1 - u)*(1 - v)) dFGM.du <- function(u, v) (2*u - 1)*(v^2) + 2*v*(1 - u) dFGM.dv <- function(u, v) (2*v - 1)*(u^2) + 2*u*(1 - v) A1 <- function(u) 2*(1 - u) A2 <- function(u, z) sqrt(A1(u)^2 - 4*(A1(u) - 1)*z) dFGM.du.inv <- function(u, z) 2*z/(A1(u) + A2(u, z)) FGMdensity <- function(u, v) 2*(1 - u - v + 2*u*v) # Simulating FGM observations n <- 3000 U <- runif(n) Z <- runif(n) V <- mapply(dFGM.du.inv, U, Z) # Applying Bcopula to FGM simulated values B <- Bcopula(cbind(U, V), 50, TRUE) str(B) # Comparing theoretical values versus Bernstein and Bilinear approximations u <- 0.70; v <- 0.55 FGMcopula(u, v); B[["copula"]](u, v); B[["bilinearCopula"]](u, v) dFGM.du(u, v); B[["du"]](u, v) dFGM.dv(u, v); B[["dv"]](u, v) dFGM.du.inv(u, 0.8); B[["du.inv"]](u, 0.8) FGMdensity(u, v); B[["density"]](u, v)
## (X,Y) continuous random variables with copula FGM(param = 1) # Theoretical formulas FGMcopula <- function(u, v) u*v*(1 + (1 - u)*(1 - v)) dFGM.du <- function(u, v) (2*u - 1)*(v^2) + 2*v*(1 - u) dFGM.dv <- function(u, v) (2*v - 1)*(u^2) + 2*u*(1 - v) A1 <- function(u) 2*(1 - u) A2 <- function(u, z) sqrt(A1(u)^2 - 4*(A1(u) - 1)*z) dFGM.du.inv <- function(u, z) 2*z/(A1(u) + A2(u, z)) FGMdensity <- function(u, v) 2*(1 - u - v + 2*u*v) # Simulating FGM observations n <- 3000 U <- runif(n) Z <- runif(n) V <- mapply(dFGM.du.inv, U, Z) # Applying Bcopula to FGM simulated values B <- Bcopula(cbind(U, V), 50, TRUE) str(B) # Comparing theoretical values versus Bernstein and Bilinear approximations u <- 0.70; v <- 0.55 FGMcopula(u, v); B[["copula"]](u, v); B[["bilinearCopula"]](u, v) dFGM.du(u, v); B[["du"]](u, v) dFGM.dv(u, v); B[["dv"]](u, v) dFGM.du.inv(u, 0.8); B[["du.inv"]](u, 0.8) FGMdensity(u, v); B[["density"]](u, v)
Calculation of pairwise monotone and supremum dependence, monotone/supremum dependence ratio, and proportion of pairwise NAs.
dependence(mat, cont = NULL, sc.order = 0)
dependence(mat, cont = NULL, sc.order = 0)
mat |
|
cont |
vector of column numbers to consider/coerce as continuous random variables (optional). |
sc.order |
order of subcopula approximation (continuous random variables). If |
Each of the random variables in the -dimensional random vector under consideration may be of any kind (discrete, continuous, or mixed). NA values are allowed.
A 3-dimensional array with pairwise monotone and supremum dependence, monotone/supremum dependence ratio, and proportion of pairwise NAs.
NA values are allowed.
Arturo Erdely https://sites.google.com/site/arturoerdely
Erdely, A. (2017) A subcopula based dependence measure. Kybernetika 53(2), 231-243. DOI: 10.14736/kyb-2017-2-0231
Nelsen, R.B. (2006) An Introduction to Copulas. Springer, New York.
V <- runif(300) # Continuous Uniform(0,1) W <- V*(1-V) # Continuous transform of V # X given V=v as continuous Uniform(0,v) X <- mapply(runif, rep(1, length(V)), rep(0, length(V)), V) Y <- 1*(0.2 < X)*(X < 0.6) # Discrete transform of X Z <- X*(0.1 < X)*(X < 0.9) + 1*(X >= 0.9) # Mixed transform of X V[1:10] <- NA # Introducing some NAs W[3:12] <- NA # Introducing some NAs Y[5:25] <- NA # Introducing some NAs vector5D <- cbind(V, W, X, Y, Z) # Matrix of 5-variate observations # Monotone and supremum dependence, ratio and proportion of NAs: (deparray <- dependence(vector5D, cont = c(1, 2, 3), 30)) # Pearson's correlations: cor(vector5D, method = "pearson", use = "pairwise.complete.obs") # Spearman's correlations: cor(vector5D, method = "spearman", use = "pairwise.complete.obs") # Kendall's correlations: cor(vector5D, method = "kendall", use = "pairwise.complete.obs") pairs(vector5D) # Matrix of pairwise scatterplots
V <- runif(300) # Continuous Uniform(0,1) W <- V*(1-V) # Continuous transform of V # X given V=v as continuous Uniform(0,v) X <- mapply(runif, rep(1, length(V)), rep(0, length(V)), V) Y <- 1*(0.2 < X)*(X < 0.6) # Discrete transform of X Z <- X*(0.1 < X)*(X < 0.9) + 1*(X >= 0.9) # Mixed transform of X V[1:10] <- NA # Introducing some NAs W[3:12] <- NA # Introducing some NAs Y[5:25] <- NA # Introducing some NAs vector5D <- cbind(V, W, X, Y, Z) # Matrix of 5-variate observations # Monotone and supremum dependence, ratio and proportion of NAs: (deparray <- dependence(vector5D, cont = c(1, 2, 3), 30)) # Pearson's correlations: cor(vector5D, method = "pearson", use = "pairwise.complete.obs") # Spearman's correlations: cor(vector5D, method = "spearman", use = "pairwise.complete.obs") # Kendall's correlations: cor(vector5D, method = "kendall", use = "pairwise.complete.obs") pairs(vector5D) # Matrix of pairwise scatterplots
Calculation of bivariate empirical subcopula matrix, induced partitions, standardized bivariate sample, and dependence measures for a given bivariate sample.
subcopem(mat.xy, display = FALSE)
subcopem(mat.xy, display = FALSE)
mat.xy |
2-column matrix with bivariate observations of a random vector |
display |
logical value indicating if graphs and dependence measures should be displayed. |
Each of the random variables and
may be of any kind (discrete, continuous, or mixed). NA values are not allowed.
A list containing the following components:
depMon |
monotone standardized supremum distance in |
depMonNonSTD |
monotone non-standardized supremum distance |
depSup |
standardized supremum distance in |
depSupNonSTD |
non-standardized supremum distance |
matrix |
matrix with empirical subcopula values. |
part1 |
vector with partition induced by first variable |
part2 |
vector with partition induced by second variable |
sample.size |
numeric value of sample size. |
std.sample |
2-column matrix with the standardized bivariate sample. |
sample |
2-column matrix with the original bivariate sample of |
If display = TRUE
then the values of depMon
, depMonNonSTD
, depSup
, and depSupNonSTD
will be displayed, and the following graphs will be generated: marginal histograms of and
, scatterplots of the original and the standardized bivariate sample, contour and image bivariate graphs of the empirical subcopula.
If both and
are continuous random variables it is faster and better to use
subcopemc
.
Arturo Erdely https://sites.google.com/site/arturoerdely
Durante, F. and Sempi, C. (2016) Principles of Copula Theory. Taylor and Francis Group, Boca Raton.
Erdely, A. (2017) A subcopula based dependence measure. Kybernetika 53(2), 231-243. DOI: 10.14736/kyb-2017-2-0231
Nelsen, R.B. (2006) An Introduction to Copulas. Springer, New York.
## Example 1: Discrete-discrete Poisson positive dependence n <- 1000 # sample size X <- rpois(n, 5) # Poisson(parameter = 5) p <- 2 # another parameter Y <- mapply(rpois, rep(1, n), 1 + p*X) # creating dependence XY <- cbind(X, Y) # 2-column matrix with bivariate sample cor(XY, method = "pearson")[1, 2] # Pearson's correlation cor(XY, method = "spearman")[1, 2] # Spearman's correlation cor(XY, method = "kendall")[1, 2] # Kendall's correlation SC <- subcopem(XY, display = TRUE) str(SC) ## Example 2: Continuous-discrete non-monotone dependence n <- 1000 # sample size X <- rnorm(n) # Normal(0,1) Y <- 2*(X > 1) - 1*(X > -1) # Discrete({-1, 0, 1}) XY <- cbind(X, Y) # 2-column matrix with bivariate sample cor(XY, method = "pearson")[1, 2] # Pearson's correlation cor(XY, method = "spearman")[1, 2] # Spearman's correlation cor(XY, method = "kendall")[1, 2] # Kendall's correlation SC <- subcopem(XY, display = TRUE) str(SC)
## Example 1: Discrete-discrete Poisson positive dependence n <- 1000 # sample size X <- rpois(n, 5) # Poisson(parameter = 5) p <- 2 # another parameter Y <- mapply(rpois, rep(1, n), 1 + p*X) # creating dependence XY <- cbind(X, Y) # 2-column matrix with bivariate sample cor(XY, method = "pearson")[1, 2] # Pearson's correlation cor(XY, method = "spearman")[1, 2] # Spearman's correlation cor(XY, method = "kendall")[1, 2] # Kendall's correlation SC <- subcopem(XY, display = TRUE) str(SC) ## Example 2: Continuous-discrete non-monotone dependence n <- 1000 # sample size X <- rnorm(n) # Normal(0,1) Y <- 2*(X > 1) - 1*(X > -1) # Discrete({-1, 0, 1}) XY <- cbind(X, Y) # 2-column matrix with bivariate sample cor(XY, method = "pearson")[1, 2] # Pearson's correlation cor(XY, method = "spearman")[1, 2] # Spearman's correlation cor(XY, method = "kendall")[1, 2] # Kendall's correlation SC <- subcopem(XY, display = TRUE) str(SC)
Calculation of bivariate empirical subcopula matrix of given approximation order, induced partitions, standardized bivariate sample, and dependence measures for a given continuous bivariate sample.
subcopemc(mat.xy, m = nrow(mat.xy), display = FALSE)
subcopemc(mat.xy, m = nrow(mat.xy), display = FALSE)
mat.xy |
2-column matrix with bivariate observations of a continuous random vector |
m |
integer value of approximation order, where |
display |
logical value indicating if graphs and dependence values should be displayed. |
Both random variables and
must be continuous, and therefore repeated values in the sample are not expected. If found,
jitter
will be applied to break ties. NA values are not allowed.
A list containing the following components:
depMon |
monotone standardized supremum distance in |
depMonNonSTD |
monotone non-standardized supremum distance |
depSup |
standardized supremum distance in |
depSupNonSTD |
non-standardized supremum distance |
matrix |
matrix with empirical subcopula values. |
part1 |
vector with partition induced by first variable |
part2 |
vector with partition induced by second variable |
sample.size |
numeric value of sample size. |
order |
numeric value of approximation order. |
std.sample |
2-column matrix with the standardized bivariate sample. |
sample |
2-column matrix with the original bivariate sample of |
If display = TRUE
then the values of depMon
, depMonNonSTD
, depSup
, and depSupNonSTD
will be displayed, and the following graphs will be generated: marginal histograms of and
, scatterplots of the original and the standardized bivariate sample, contour and image bivariate graphs of the empirical subcopula.
If approximation order calculation may take more than 2 minutes. Usually
would be enough for an acceptable approximation.
Arturo Erdely https://sites.google.com/site/arturoerdely
Durante, F. and Sempi, C. (2016) Principles of Copula Theory. Taylor and Francis Group, Boca Raton.
Erdely, A. (2017) A subcopula based dependence measure. Kybernetika 53(2), 231-243. DOI: 10.14736/kyb-2017-2-0231
Nelsen, R.B. (2006) An Introduction to Copulas. Springer, New York.
## Example 1: Independent Normal and Gamma n <- 300 # sample size X <- rnorm(n) # Normal(0,1) Y <- rgamma(n, 2, 3) # Gamma(2,3) XY <- cbind(X, Y) # 2-column matrix with bivariate sample cor(XY, method = "pearson")[1, 2] # Pearson's correlation cor(XY, method = "spearman")[1, 2] # Spearman's correlation cor(XY, method = "kendall")[1, 2] # Kendall's correlation SC <- subcopemc(XY,, display = TRUE) str(SC) ## Approximation of order m = 15 SCm15 <- subcopemc(XY, 15, display = TRUE) str(SCm15) ## Example 2: Non-monotone dependence n <- 300 # sample size Theta <- runif(n, 0, 2*pi) # Uniform circular distribution X <- cos(Theta) Y <- sin(Theta) XY <- cbind(X, Y) # 2-column matrix with bivariate sample cor(XY, method = "pearson")[1, 2] # Pearson's correlation cor(XY, method = "spearman")[1, 2] # Spearman's correlation cor(XY, method = "kendall")[1, 2] # Kendall's correlation SC <- subcopemc(XY,, display = TRUE) str(SC) ## Approximation of order m = 15 SCm15 <- subcopemc(XY, 15, display = TRUE) str(SCm15)
## Example 1: Independent Normal and Gamma n <- 300 # sample size X <- rnorm(n) # Normal(0,1) Y <- rgamma(n, 2, 3) # Gamma(2,3) XY <- cbind(X, Y) # 2-column matrix with bivariate sample cor(XY, method = "pearson")[1, 2] # Pearson's correlation cor(XY, method = "spearman")[1, 2] # Spearman's correlation cor(XY, method = "kendall")[1, 2] # Kendall's correlation SC <- subcopemc(XY,, display = TRUE) str(SC) ## Approximation of order m = 15 SCm15 <- subcopemc(XY, 15, display = TRUE) str(SCm15) ## Example 2: Non-monotone dependence n <- 300 # sample size Theta <- runif(n, 0, 2*pi) # Uniform circular distribution X <- cos(Theta) Y <- sin(Theta) XY <- cbind(X, Y) # 2-column matrix with bivariate sample cor(XY, method = "pearson")[1, 2] # Pearson's correlation cor(XY, method = "spearman")[1, 2] # Spearman's correlation cor(XY, method = "kendall")[1, 2] # Kendall's correlation SC <- subcopemc(XY,, display = TRUE) str(SC) ## Approximation of order m = 15 SCm15 <- subcopemc(XY, 15, display = TRUE) str(SCm15)