R/integrate2_normal.R

Defines functions integrate2_normal

Documented in integrate2_normal

#' 2-dimensional Gaussian Expectation
#'
#' Expected value of a function of two independent
#' standard Gaussian variables
#'
#' @return A numeric value
#' \deqn{
#' \int \int f(x, y) \varphi(x) \varphi(y) dx dy,
#' }
#' where \eqn{ \varphi(\cdot)} is the standard Gaussian density.
#'
#' @param f Function to integrate over. Input to
#' \code{f} should be a length 2 vector.
#' @param ... Additional arguments to \link[cubature]{hcubature}
#' @importFrom cubature hcubature
#' @importFrom stats dnorm
#' @examples
#' \dontrun{
#' # Second moment of a Gaussian
#' # tol = 0.0001, true value = 2, evaluated = 2.000001
#' f <- function(x) x[1]^2 + x[2]^2
#' integrate2_normal(f, tol = 1e-4)
#' }

integrate2_normal <- function(f, ...){
  integrand <- function(x) f(x) * dnorm(x[1]) * dnorm(x[2])

  hcubature(integrand, lowerLimit = c(-8,-8), upperLimit = c(8,8),
                      fDim = 1, maxEval = 0, tol = 1e-4,  ...)$integral
}
zq00/glmhd documentation built on April 7, 2023, 7:45 a.m.