e04yb {NAGFWrappers} R Documentation

## e04yb: Check user's function for calculating Hessian of a sum of squares

### Description

e04yb checks that a user-supplied function for evaluating the second derivative term of the Hessian matrix of a sum of squares is consistent with a user-supplied function for calculating the corresponding first derivatives.

### Usage

e04yb(m, lsqfun, lsqhes, x, lb, iw, w,
n = nrow(x))


### Arguments

 m integer lsqfun function lsqfun must calculate the vector of values f_i(x) and their first derivatives ( \partial f_i)/( \partial x_j) at any point x. (e04he gives you the option of resetting arguments of lsqfun to cause the minimization process to terminate immediately. e04yb will also terminate immediately, without finishing the checking process, if the argument in question is reset.) (IFLAG,FVEC,FJAC) = lsqfun(iflag,m,n,xc,ldfjac)  lsqhes function lsqhes must calculate the elements of the symmetric matrix B(x) = ∑_i = 1^mf_i(x)G_i(x), at any point x, where G_i(x) is the Hessian matrix of f_i(x). (As with lsqfun, a argument can be set to cause immediate termination.) (IFLAG,B) = lsqhes(iflag,m,n,fvec,xc,lb)  x double array x[j] for j=1 . . . n, must be set to the coordinates of a suitable point at which to check the b_jk calculated by lsqhes. ‘Obvious’ settings, such as 0 or 1, should not be used since, at such particular points, incorrect terms may take correct values (particularly zero), so that errors could go undetected. For a similar reason, it is preferable that no two elements of x should have the same value. lb integer iw integer array This array appears in the argument list purely so that, if e04yb is called by another library function, the library function can pass quantities to functions lsqfun and lsqhes via iw. iw is not examined or changed by e04yb. In general you must provide an array iw, but are advised not to use it.

integer array

This array appears in the argument list purely so that, if e04yb is called by another library function, the library function can pass quantities to functions lsqfun and lsqhes via iw. iw is not examined or changed by e04yb. In general you must provide an array iw, but are advised not to use it.

 w double array The actual length of w as declared in the function from which e04yb is called.

double array

The actual length of w as declared in the function from which e04yb is called.

 n integer: default = nrow(x) The number m of residuals, f_i(x), and the number n of variables, x_j.

### Details

R interface to the NAG Fortran routine E04YBF.

### Value

 FVEC double array Unless you set iflag negative in the first call of lsqfun, fvec[i] contains the value of f_i at the point supplied by you in x for i=1 . . . m. FJAC double array Unless you set iflag negative in the first call of lsqfun, fjac[i, j] contains the value of the first derivative ( \partial f_i)/( \partial x_j) at the point given in x, as calculated by lsqfun for j=1 . . . n for i=1 . . . m. B double array Unless you set iflag negative in lsqhes, b[j\times(j - 1)/2+k] contains the value of b_jk at the point given in x as calculated by lsqhes for k=1 . . . j for j=1 . . . n. IW integer array This array appears in the argument list purely so that, if e04yb is called by another library function, the library function can pass quantities to functions lsqfun and lsqhes via iw. iw is not examined or changed by e04yb. In general you must provide an array iw, but are advised not to use it.

integer array

This array appears in the argument list purely so that, if e04yb is called by another library function, the library function can pass quantities to functions lsqfun and lsqhes via iw. iw is not examined or changed by e04yb. In general you must provide an array iw, but are advised not to use it.

 W double array

double array

 IFAIL integer ifail =0 unless the function detects an error or a warning has been flagged (see the Errors section in Fortran library documentation).

NAG

### Examples


ifail <- 0
lsqfun = function(iflag, m, n, xc, ljc) {

fvec <- as.matrix(mat.or.vec(m, 1))
fjacc <- as.matrix(mat.or.vec(ljc, n))
for (i in c(1:m)) {
denom <- xc[2] %*% t[i, 2] + xc[3] %*% t[i, 3]

fvec[i] <- xc[1] + t[i, 1]/denom - y[i]

if (iflag != 0) {

fjacc[i, 1] <- 1

dummy <- -1/(denom %*% denom)

fjacc[i, 2] <- t[i, 1] %*% t[i, 2] %*% dummy

fjacc[i, 3] <- t[i, 1] %*% t[i, 3] %*% dummy

}
}
list(IFLAG = as.integer(iflag), FVEC = as.matrix(fvec), FJAC = as.matrix(fjacc))
}
lsqhes = function(iflag, m, n, fvec, xc, lb) {

b <- as.matrix(mat.or.vec(lb, 1))
sum22 <- 0
sum32 <- 0
sum33 <- 0
for (i in c(1:m)) {
dummy <- 2 %*% t[i, 1]/(xc[2] %*% t[i, 2] + xc[3] %*%
t[i, 3])^3

sum22 <- sum22 + fvec[i] %*% dummy %*% t[i, 2]^2

sum32 <- sum32 + fvec[i] %*% dummy %*% t[i, 2] %*% t[i,
3]

sum33 <- sum33 + fvec[i] %*% dummy %*% t[i, 3]^2
}
b[3] <- sum22
b[5] <- sum32
b[6] <- sum33
list(IFLAG = as.integer(iflag), B = as.matrix(b))
}

m <- 15

x <- matrix(c(0.19, -1.34, 0.88), nrow = 3, ncol = 1,
byrow = TRUE)

lb <- 6

iw <- as.matrix(mat.or.vec(1, 1))

w <- as.matrix(mat.or.vec(78, 1))

y <- matrix(c(0.14, 0.18, 0.22, 0.25, 0.29, 0.32,
0.35, 0.39, 0.37, 0.58, 0.73, 0.96, 1.34, 2.1, 4.39), nrow = 1,
ncol = 15, byrow = TRUE)

t <- matrix(c(1, 15, 1, 2, 14, 2, 3, 13, 3, 4, 12,
4, 5, 11, 5, 6, 10, 6, 7, 9, 7, 8, 8, 8, 9, 7, 7, 10, 6,
6, 11, 5, 5, 12, 4, 4, 13, 3, 3, 14, 2, 2, 15, 1, 1), nrow = 15,
ncol = 3, byrow = TRUE)

e04yb(m, lsqfun, lsqhes, x, lb, iw, w)



[Package NAGFWrappers version 24.0 Index]