dpnorm: Stable evaluation of difference between normal c.d.f.s

View source: R/misc.r

dpnormR Documentation

Stable evaluation of difference between normal c.d.f.s

Description

Evaluates the difference between two N(0,1) cumulative distribution functions avoiding cancellation error.

Usage

dpnorm(x0,x1)

Arguments

x0

vector of lower values at which to evaluate standard normal distribution function.

x1

vector of upper values at which to evaluate standard normal distribution function.

Details

Equivalent to pnorm(x1)-pnorm(x0), but stable when x0 and x1 values are very close, or in the upper tail of the standard normal.

Author(s)

Simon N. Wood simon.wood@r-project.org

Examples

require(mgcv)
x <- seq(-10,10,length=10000)
eps <- 1e-10
y0 <- pnorm(x+eps)-pnorm(x) ## cancellation prone
y1 <- dpnorm(x,x+eps)       ## stable
## illustrate stable computation in black, and
## cancellation prone in red...
par(mfrow=c(1,2),mar=c(4,4,1,1))
plot(log(y1),log(y0),type="l")
lines(log(y1[x>0]),log(y0[x>0]),col=2)
plot(x,log(y1),type="l")
lines(x,log(y0),col=2)


mgcv documentation built on May 29, 2024, 4:34 a.m.