R/pmvn.R

Defines functions .pmvn

#' Multivariate normal probability of a rectangle
#'
#' Uses \code{mvtnorm::pmvnorm()} with the Miwa algorithm, which is
#' deterministic, so that root finding on the bounds is stable, for
#' rectangles of at most three coordinates and for covariance matrices of
#' Markov sequences (\code{.is_markov()}), such as the correlation of
#' statistics with independent increments. The number of grid points can be
#' changed with \code{options(DesignComplexSurvival.miwa_steps = ...)}
#' (default 128). Rectangles of four or more coordinates with other
#' covariance matrices, such as those of \code{variance = "alternative"} and
#' \code{"studentized"}, are computed by \code{.pmvn_genz()}: for them the
#' Miwa algorithm was found to be inaccurate (errors of about 1e-3 with four
#' looks and above 0.1 with six or more looks, against exact values computed
#' in Python by tools/check_miwa_accuracy.py in the project folder).
#' Rectangles of nine or more coordinates with two finite limits for some
#' coordinate are also computed by \code{.pmvn_genz()}: for them mvtnorm
#' computes the Miwa probability by inclusion-exclusion over the limits and
#' leaves out the terms of nine or more coordinates (errors of 7.5e-4 with
#' nine and 6.9e-3 with ten coordinates under the correlation of independent
#' increments, tools/logs/check_switch_accuracy.txt in the project folder).
#' With one-sided limits only, the Miwa algorithm computes one orthant
#' probability and is used.
#'
#' When some coordinates have two finite limits and others only one, the
#' Miwa algorithm of mvtnorm replaces the infinite limits by plus or minus
#' 1000 on the standardized scale and issues a warning (\code{probval.Miwa()}
#' in mvtnorm). The same replacement is made here beforehand, which gives the
#' same probability without the warning.
#'
#' @param lower,upper Limits (may be infinite).
#' @param mean Mean vector.
#' @param sigma Covariance matrix.
#' @return The probability.
#' @noRd
.pmvn <- function(lower, upper, mean, sigma) {
  k <- length(lower)
  if (k == 1L) {
    sd <- sqrt(sigma[1L, 1L])
    if (is.infinite(upper)) {
      return(stats::pnorm(lower, mean, sd, lower.tail = FALSE))
    }
    if (is.infinite(lower)) {
      return(stats::pnorm(upper, mean, sd))
    }
    return(max(stats::pnorm(upper, mean, sd) - stats::pnorm(lower, mean, sd), 0))
  }
  if ((k >= 4L && !.is_markov(sigma)) ||
      (k >= 9L && any(is.finite(lower) & is.finite(upper)))) {
    return(.pmvn_genz(lower, upper, mean, sigma))
  }
  lo_inf <- is.infinite(lower)
  up_inf <- is.infinite(upper)
  if (any(!lo_inf & !up_inf) && any(xor(lo_inf, up_inf))) {
    sd <- sqrt(diag(sigma))
    i <- lo_inf & !up_inf
    lower[i] <- mean[i] - 1000 * sd[i]
    i <- up_inf & !lo_inf
    upper[i] <- mean[i] + 1000 * sd[i]
  }
  steps <- getOption("DesignComplexSurvival.miwa_steps", 128L)
  p <- mvtnorm::pmvnorm(lower = lower, upper = upper, mean = mean,
                        sigma = sigma, algorithm = mvtnorm::Miwa(steps = steps))
  max(as.numeric(p), 0)
}

Try the DesignComplexSurvival package in your browser

Any scripts or data that you put into this service are public.

DesignComplexSurvival documentation built on Nov. 23, 2026, 5:07 p.m.