Debian Codegraph @

man/relErr.Rd

raw
1 \name{relErr}
2 \title{Relative Error When Appropriate, Absolute Otherwise}
3 \alias{relErrV}
4 \alias{relErr}
5 \description{
6 \describe{
7 \item{\code{relErrV()}:}{
8 Compute the signed relative error componentwise (\dQuote{vectorized})
9 between the \code{target} and \code{current} vectors,
10 using the \emph{absolute} error, i.e., the difference
11 in case the relative error is not well defined, i.e., when \code{target}
12 is zero or infinite.}
13 \item{\code{relErr()}:}{simply the \emph{mean} absolute value of the
14 relative errors between \code{target} and \code{current} vectors;
15 typically the \dQuote{same} as
16 \code{\link{all.equal.numeric}(target, vector, tolerance=0, countEQ=TRUE)}.
17
18 Currently useful only when both vectors are finite.
19 }
20 }
21 }
22 \usage{
23 relErrV(target, current, eps0 = .Machine$double.xmin)
24 relErr (target, current)
25 }
26 \arguments{
27 \item{target}{numeric, possibly scalar.}
28 \item{current}{numeric vector of \code{\link{length}()} a multiple of
29 \code{length(target)}; if an \code{\link{array}} (incl
30 \code{matrix}), dimensions are preserved; for vectors,
31 \code{names(target)} are preserved.}
32 \item{eps0}{non-negative number; values \code{abs(target) < eps0} should
33 be treated as zero (and hence \emph{absolute} instead of relative error
34 be computed). This may be crucial when \code{target} is an
35 \code{"mpfr"}-number vector.}
36 }
37 \value{
38 \describe{
39 \item{\code{relErrV()}:}{a numeric vector of the same length (or array
40 of the same dimension) as \code{current}.}% target can be scalar
41 \item{\code{relErr()}:}{a single number.}
42 }
43 }
44 \author{Martin Maechler,
45 originally as part of \CRANpkg{Matrix} package's \file{test-tools.R}.
46 }
47 \seealso{
48 \code{\link{all.equal.numeric}()} is similar in spirit but returns \code{TRUE} or
49 string containing the \emph{mean} relative or absolute error.
50 }
51 \examples{
52 ## relErrV() test example: showing how it works fine with {NA, Inf, 0} :
53 eps <- 1e-4*c(-9, -8, -6, -4, 0.5, 1, 5)
54 target <- c(-1:1, 0, 0, NA, NaN, Inf, -Inf, Inf, 0 , Inf, 1 , -3:3)
55 current <- c(-1:1,1e-7,NaN,NA, 0 , Inf, Inf, 0, Inf, 1, Inf, -3:3+ eps)
56 cbind(target, current, absE = current-target,
57 relE = relErrV(target,current)) -> M ; M
58 stopifnot(exprs = {
59 is.logical(isFr <- is.finite(rF <- M[,"relE"]))
60 target==current | isFr == is.finite(aF <- M[,"absE"])
61 identical(aF[!isFr] , rF[!isFr])
62 identical(numeric(), relErrV(numeric(), integer())) # length 0 {used to fail}
63 })
64 tools::assertError(relErrV(1, numeric()), verbose=TRUE) # no longer allowed
65 ## relErr() is pretty simple --- (possibly too simple, currently)
66 relErr
67 relErr(target, current) # NA (of course)
68 all.equal.numeric(target, current) ## "'is.NA' value mismatch ..."
69
70 ## comparison after dropping NA's :
71 hasN <- is.na(target) | is.na(current)
72 all.equal(target[!hasN], current[!hasN], tolerance=0) # "Mean abs. diff.: Inf"
73 relErr(target[!hasN], current[!hasN]) # NaN (to improve?)
74
75 ## comparison after only keeping cases where both are finite:
76 finN <- is.finite(target) & is.finite(current)
77 all.equal(target[finN], current[finN], tol=0) # "Mean abs.d.: 0.000279.."
78 all.equal(target[finN], current[finN], tol=0, countEQ=TRUE) # " " : 0.000239..
79 relErr(target[finN], current[finN]) # 0.0002392929
80 }
81 \keyword{arith}
82 \keyword{utilities}