Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Generated by roxygen2: do not edit by hand

export(CCovEst)
export(CEllGenEst)
export(CMeanEst)
export(CondCovEst)
export(CondEllGenEst)
export(CondMeanEst)
export(Convert_g1_To_Fg1)
export(Convert_g1_To_Qg1)
export(Convert_g1_To_f1)
Expand Down
11 changes: 11 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
* New functions:

- `CondMeanEst`: nonparametric estimation of the conditional mean.
(#2, thanks to Rutger van der Spek)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, the numbering of the PR is off: you mention 2 but it should be #6 and this one #7


- `CondCovEst`: nonparametric estimation of the conditional covariance matrix.
(#2, thanks to Rutger van der Spek)

- `CondEllGenEst`: nonparametric estimation of the conditional density generator
of an elliptical distribution.
(#2, thanks to Rutger van der Spek)

# ElliptCopulas 0.1.4

Expand Down
154 changes: 85 additions & 69 deletions R/CCovEst.R → R/CondCovEst.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#' The supported estimators are:
#'
#' \describe{
#' \item{type = 1}{
#' \item{type = "`grid_mean`"}{
#' Covariance estimator using the conditional mean evaluated at the grid point:
#' \deqn{
#' \widehat{\Sigma}_n(z)
Expand All @@ -26,7 +26,7 @@
#' }
#' }
#'
#' \item{type = 2}{
#' \item{type = "`obs_mean`"}{
#' Covariance estimator using the conditional mean evaluated at each observation:
#' \deqn{
#' \widetilde{\Sigma}_n(z)
Expand All @@ -36,7 +36,7 @@
#' }
#' }
#'
#' \item{type = 3}{
#' \item{type = "`pairwise`"}{
#' Pairwise covariance estimator:
#' \deqn{
#' \widecheck{\Sigma}_n(z)
Expand All @@ -63,12 +63,21 @@
#'
#' @template param-Kernel
#'
#' @param type integer in \{1,2,3\} indicating which estimator to compute.
#' @param type indicates which estimator to use. Possible choices are "`grid_mean`",
#' "`obs_mean`" and "`pairwise`".
#'
#' @return An array of dimension \eqn{d \times d \times \code{length(gridZ)}},
#' \eqn{\widehat{\Sigma}_n(z)} containing the estimated conditional covariance
#' matrices of the \eqn{d}-dimensional random variable \eqn{X} at each point of `gridZ`.
#'
#' @details
#' Computational complexity:
#' \itemize{
#' \item `grid_mean`: O(n * d^2 * \code{length(gridZ))}.
#' \item `obs_mean`: O(n * d^2 * \code{length(gridZ))}.
#' \item `pairwise`: O(n^2 * d^2 * \code{length(gridZ))}.
#' }
#'
#' @examples
#' # Comparison between the estimated and true conditional covariance
#'
Expand All @@ -81,7 +90,7 @@
#' gridZ = seq(-2, 2, length.out = 50)
#' h = 0.2
#'
#' Sigma_est = CCovEst(X, Z, gridZ, h, type = 1)
#' Sigma_est = CondCovEst(X, Z, gridZ, h, type = 1)
#' cov_X1X2 = sapply(1:length(gridZ), function(i) Sigma_est[1,2,i])
#' true_cov = 0.3 * gridZ
#'
Expand All @@ -93,8 +102,8 @@
#'
#' @export
#'
CCovEst <- function(dataMatrix, observedZ, gridZ, h , Kernel = "epanechnikov",
type = 1)
CondCovEst <- function(dataMatrix, observedZ, gridZ, h , Kernel = "epanechnikov",
type = "grid_mean")
{
d = ncol( dataMatrix )
n = nrow( dataMatrix )
Expand All @@ -108,89 +117,96 @@ CCovEst <- function(dataMatrix, observedZ, gridZ, h , Kernel = "epanechnikov",
class = "DifferentLengthsError") )
}

if(type == 1){
meanEst = CMeanEst(
dataMatrix = dataMatrix, observedZ = observedZ,
gridZ = gridZ, h = h,
Kernel = Kernel)
estimate = switch(type,

grid_mean = {

meanEst = CondMeanEst(
dataMatrix = dataMatrix, observedZ = observedZ,
gridZ = gridZ, h = h,
Kernel = Kernel)

matrixWeights = matrix(data = NA, nrow = n, ncol = nz)
matrixWeights = matrix(data = NA, nrow = n, ncol = nz)

for(i in 1:nz){
matrixWeights[,i] = computeWeights(
vectorZ = observedZ,h = h,
pointZ = gridZ[i], Kernel = Kernel,
normalization = TRUE)
}
for(i in 1:nz){
matrixWeights[,i] = computeWeights(
vectorZ = observedZ,h = h,
pointZ = gridZ[i], Kernel = Kernel,
normalization = TRUE)
}

estimate = array(data = NA, dim = c(d,d,nz))
estimate = array(data = NA, dim = c(d,d,nz))

for(i in 1:nz){
S = matrix(0,d,d)
for(j in 1:n){
diff = dataMatrix[j,] - meanEst[,i]
S = S + matrixWeights[j,i] * (diff %*% t(diff))
for(i in 1:nz){
diffMat = t(t(dataMatrix) - meanEst[,i])
W = diag(matrixWeights[,i])
estimate[,,i] = t(diffMat) %*% W %*% diffMat
}
estimate[,,i] = S
}

}
estimate
},

if(type == 2){
obs_mean = {

matrixWeights = matrix(data = NA, nrow = n, ncol = nz)
matrixWeights = matrix(data = NA, nrow = n, ncol = nz)

for(i in 1:nz){
matrixWeights[,i] = computeWeights(
vectorZ = observedZ, h = h,
pointZ = gridZ[i], Kernel = Kernel,
normalization = TRUE)
}
for(i in 1:nz){
matrixWeights[,i] = computeWeights(
vectorZ = observedZ, h = h,
pointZ = gridZ[i], Kernel = Kernel,
normalization = TRUE)
}

estimate = array(data = NA, dim = c(d,d,nz))
estimate = array(data = NA, dim = c(d,d,nz))

for(i in 1:nz){
meanEst = CMeanEst(
meanEst = CondMeanEst(
dataMatrix = dataMatrix, observedZ = observedZ,
gridZ = observedZ[i], h = h,
gridZ = observedZ, h = h,
Kernel = Kernel)
S = matrix(0,d,d)
for(j in 1:n){
diff = dataMatrix[j,] - meanEst
S = S + matrixWeights[j,i] * (diff %*% t(diff))

diffMat = dataMatrix - t(meanEst)

for(i in 1:nz){
diffMat = dataMatrix - t(meanEst)
estimate[,,i] = t(diffMat) %*% (diffMat * matrixWeights[,i])
}
estimate[,,i] = S
}
}

if(type == 3){
estimate
},

matrixWeights = matrix(data = NA, nrow = n, ncol = nz)
pairwise = {

for(i in 1:nz){
matrixWeights[,i] = computeWeights(
vectorZ = observedZ, h = h,
pointZ = gridZ[i], Kernel = Kernel,
normalization = FALSE)
}
matrixWeights = matrix(data = NA, nrow = n, ncol = nz)

estimate = array(data = NA, dim = c(d,d,nz))
for(i in 1:nz){
matrixWeights[,i] = computeWeights(
vectorZ = observedZ, h = h,
pointZ = gridZ[i], Kernel = Kernel,
normalization = FALSE)
}

for(i in 1:nz){
S = matrix(0,d,d)
denom = 0
for(j in 1:(n-1)){
for(k in (j+1):n){
w = matrixWeights[j,i] * matrixWeights[k,i]
diff = dataMatrix[j,] - dataMatrix[k,]
S = S + w * (diff %*% t(diff))
denom = denom + w
estimate = array(data = NA, dim = c(d,d,nz))

for(i in 1:nz){
S = matrix(0,d,d)
denom = 0
for(j in 1:(n-1)){
for(k in (j+1):n){
w = matrixWeights[j,i] * matrixWeights[k,i]
diff = dataMatrix[j,] - dataMatrix[k,]
S = S + w * (diff %*% t(diff))
denom = denom + w
}
}
S = S / (2* denom)
estimate[,,i] = S
}
S = S/ (2* denom)
estimate[,,i] = S
}
}

estimate
},

stop("Invalid type. Must be one of 'grid_mean', 'obs_mean', or 'pairwise'.")
)

return(estimate)
}
Loading