#' Conditional Variance Estimator
#'
#' Conditional Variance Estimator (CVE) is a novel sufficient dimension
#'  reduction (SDR) method for regressions satisfying E(Y|X) = E(Y|B'X),
#'  where B'X is a lower dimensional projection of the predictors.
#'
#' @param X A matrix of type numeric of dimensions N times dim where N is the number
#'  of entries with dim as data dimension.
#' @param Y A vector of type numeric of length N (coresponds to \code{x}).
#' @param k Guess for rank(B).
#' @param nObs As describet in the paper.
#'  
#' @param tol Tolerance for optimization stopping creteria.
#' @export
#'
#' @seealso TODO: \code{vignette("CVE_paper", package="CVE")}.
#'
#' @references Fertl Likas, Bura Efstathia. Conditional Variance Estimation for Sufficient Dimension Reduction, 2019
cve <- function(X, Y, k,
                nObs = sqrt(nrow(X)),
                tauInitial = 1.0,
                tol = 1e-3,
                slack = -1e-10,
                maxIter = 50L,
                attempts = 10L
) {
    # check data parameter types
    stopifnot(
        is.matrix(X),
        is.vector(Y),
        typeof(X) == 'double',
        typeof(Y) == 'double'
    )

    # call CVE C++ implementation
    return(cve_cpp(X, Y, k,
                   nObs,
                   tauInitial,
                   tol,
                   slack,
                   maxIter,
                   attempts
    ))
}
