gHgenb.RdgHgenb is used to generate the gradient and Hessian of an objective
function used for optimization. If a user-provided gradient function
gr is available it is used to compute the gradient, otherwise
package numDeriv is used. If a user-provided Hessian function
hess is available, it is used to compute a Hessian. Otherwise, if
gr is available, we use the function jacobian() from
package numDeriv to compute the Hessian. In both these cases we
check for symmetry of the Hessian. Computational Hessians are commonly
NOT symmetric. If only the objective function fn is provided, then
the Hessian is approximated with the function hessian from
package numDeriv which guarantees a symmetric matrix.
gHgenb(par, fn, gr=NULL, hess=NULL, bdmsk=NULL, lower=NULL, upper=NULL,
control=list(ktrace=0), ...)Set of parameters, assumed to be at a minimum of the function fn.
Name of the objective function.
(Optional) function to compute the gradient of the objective function. If present, we use the Jacobian of the gradient as the Hessian and avoid one layer of numerical approximation to the Hessian.
(Optional) function to compute the Hessian of the objective function. This is rarely available, but is included for completeness.
An integer vector of the same length as par. When an element
of this vector is 0, the corresponding parameter value is fixed (masked)
during an optimization. Non-zero values indicate a parameter is free (1),
at a lower bound (-3) or at an upper bound (-1), but this routine only
uses 0 values.
Lower bounds for parameters in par.
Upper bounds for parameters in par.
A list of controls to the function. Currently asymptol (default of 1.0e-7 which tests for asymmetry of Hessian approximation (see code for details of the test); ktrace, a logical flag which, if TRUE, monitors the progress of gHgenb (default FALSE), and stoponerror, defaulting to FALSE to NOT stop when there is an error or asymmetry of Hessian. Set TRUE to stop.
Extra data needed to compute the function, gradient and Hessian.
None
ansout a list of four items,
gnThe approximation to the gradient vector.
HnThe approximation to the Hessian matrix.
gradOKTRUE if the gradient has been computed acceptably. FALSE otherwise.
hessOKTRUE if the gradient has been computed acceptably and passes the symmetry test. FALSE otherwise.
nbmThe number of active bounds and masks.
require(numDeriv)
# genrose function code
genrose.f<- function(x, gs=NULL){ # objective function
## One generalization of the Rosenbrock banana valley function (n parameters)
n <- length(x)
if(is.null(gs)) { gs=100.0 }
fval<-1.0 + sum (gs*(x[1:(n-1)]^2 - x[2:n])^2 + (x[2:n] - 1)^2)
return(fval)
}
genrose.g <- function(x, gs=NULL){
# vectorized gradient for genrose.f
# Ravi Varadhan 2009-04-03
n <- length(x)
if(is.null(gs)) { gs=100.0 }
gg <- as.vector(rep(0, n))
tn <- 2:n
tn1 <- tn - 1
z1 <- x[tn] - x[tn1]^2
z2 <- 1 - x[tn]
gg[tn] <- 2 * (gs * z1 - z2)
gg[tn1] <- gg[tn1] - 4 * gs * x[tn1] * z1
return(gg)
}
genrose.h <- function(x, gs=NULL) { ## compute Hessian
if(is.null(gs)) { gs=100.0 }
n <- length(x)
hh<-matrix(rep(0, n*n),n,n)
for (i in 2:n) {
z1<-x[i]-x[i-1]*x[i-1]
z2<-1.0-x[i]
hh[i,i]<-hh[i,i]+2.0*(gs+1.0)
hh[i-1,i-1]<-hh[i-1,i-1]-4.0*gs*z1-4.0*gs*x[i-1]*(-2.0*x[i-1])
hh[i,i-1]<-hh[i,i-1]-4.0*gs*x[i-1]
hh[i-1,i]<-hh[i-1,i]-4.0*gs*x[i-1]
}
return(hh)
}
maxfn<-function(x, top=10) {
n<-length(x)
ss<-seq(1,n)
f<-top-(crossprod(x-ss))^2
f<-as.numeric(f)
return(f)
}
negmaxfn<-function(x) {
f<-(-1)*maxfn(x)
return(f)
}
parx<-rep(1,4)
lower<-rep(-10,4)
upper<-rep(10,4)
bdmsk<-c(1,1,0,1) # masked parameter 3
fval<-genrose.f(parx)
gval<-genrose.g(parx)
Ahess<-genrose.h(parx)
gennog<-gHgenb(parx,genrose.f)
cat("results of gHgenb for genrose without gradient code at ")
#> results of gHgenb for genrose without gradient code at
print(parx)
#> [1] 1 1 1 1
print(gennog)
#> $gn
#> [1] 1.604439e-12 1.604047e-12 1.604047e-12 0.000000e+00
#>
#> $Hn
#> [,1] [,2] [,3] [,4]
#> [1,] 8.000000e+02 -4.000000e+02 9.490508e-13 2.321482e-14
#> [2,] -4.000000e+02 1.002000e+03 -4.000000e+02 1.010644e-12
#> [3,] 9.490508e-13 -4.000000e+02 1.002000e+03 -4.000000e+02
#> [4,] 2.321482e-14 1.010644e-12 -4.000000e+02 2.020000e+02
#>
#> $gradOK
#> [1] FALSE
#>
#> $hessOK
#> [1] TRUE
#>
#> $nbm
#> [1] 0
#>
cat("compare to g =")
#> compare to g =
print(gval)
#> [1] 0 0 0 0
cat("and Hess\n")
#> and Hess
print(Ahess)
#> [,1] [,2] [,3] [,4]
#> [1,] 800 -400 0 0
#> [2,] -400 1002 -400 0
#> [3,] 0 -400 1002 -400
#> [4,] 0 0 -400 202
cat("\n\n")
#>
#>
geng<-gHgenb(parx,genrose.f,genrose.g)
cat("results of gHgenb for genrose at ")
#> results of gHgenb for genrose at
print(parx)
#> [1] 1 1 1 1
print(gennog)
#> $gn
#> [1] 1.604439e-12 1.604047e-12 1.604047e-12 0.000000e+00
#>
#> $Hn
#> [,1] [,2] [,3] [,4]
#> [1,] 8.000000e+02 -4.000000e+02 9.490508e-13 2.321482e-14
#> [2,] -4.000000e+02 1.002000e+03 -4.000000e+02 1.010644e-12
#> [3,] 9.490508e-13 -4.000000e+02 1.002000e+03 -4.000000e+02
#> [4,] 2.321482e-14 1.010644e-12 -4.000000e+02 2.020000e+02
#>
#> $gradOK
#> [1] FALSE
#>
#> $hessOK
#> [1] TRUE
#>
#> $nbm
#> [1] 0
#>
cat("compare to g =")
#> compare to g =
print(gval)
#> [1] 0 0 0 0
cat("and Hess\n")
#> and Hess
print(Ahess)
#> [,1] [,2] [,3] [,4]
#> [1,] 800 -400 0 0
#> [2,] -400 1002 -400 0
#> [3,] 0 -400 1002 -400
#> [4,] 0 0 -400 202
cat("*****************************************\n")
#> *****************************************
parx<-rep(0.9,4)
fval<-genrose.f(parx)
gval<-genrose.g(parx)
Ahess<-genrose.h(parx)
gennog<-gHgenb(parx,genrose.f,control=list(ktrace=TRUE), gs=9.4)
#> Compute gradient approximation
#> [1] -3.0456 -1.5536 -1.5536 1.4920
#> Compute Hessian approximation
#> is.null(hess) is TRUE
#> is.null(gr) is TRUE use numDeriv hessian()
#> [,1] [,2] [,3] [,4]
#> [1,] 5.752800e+01 -3.384000e+01 -1.375487e-12 2.961084e-15
#> [2,] -3.384000e+01 7.832800e+01 -3.384000e+01 -1.030281e-13
#> [3,] -1.375487e-12 -3.384000e+01 7.832800e+01 -3.384000e+01
#> [4,] 2.961084e-15 -1.030281e-13 -3.384000e+01 2.080000e+01
cat("results of gHgenb with gs=",9.4," for genrose without gradient code at ")
#> results of gHgenb with gs= 9.4 for genrose without gradient code at
print(parx)
#> [1] 0.9 0.9 0.9 0.9
print(gennog)
#> $gn
#> [1] -3.0456 -1.5536 -1.5536 1.4920
#>
#> $Hn
#> [,1] [,2] [,3] [,4]
#> [1,] 5.752800e+01 -3.384000e+01 -1.375487e-12 2.961084e-15
#> [2,] -3.384000e+01 7.832800e+01 -3.384000e+01 -1.030281e-13
#> [3,] -1.375487e-12 -3.384000e+01 7.832800e+01 -3.384000e+01
#> [4,] 2.961084e-15 -1.030281e-13 -3.384000e+01 2.080000e+01
#>
#> $gradOK
#> [1] FALSE
#>
#> $hessOK
#> [1] TRUE
#>
#> $nbm
#> [1] 0
#>
cat("compare to g =")
#> compare to g =
print(gval)
#> [1] -32.4 -14.6 -14.6 17.8
cat("and Hess\n")
#> and Hess
print(Ahess)
#> [,1] [,2] [,3] [,4]
#> [1,] 612 -360 0 0
#> [2,] -360 814 -360 0
#> [3,] 0 -360 814 -360
#> [4,] 0 0 -360 202
cat("\n\n")
#>
#>
geng<-gHgenb(parx,genrose.f,genrose.g, control=list(ktrace=TRUE))
#> Compute gradient approximation
#> [1] -32.4 -14.6 -14.6 17.8
#> Compute Hessian approximation
#> is.null(hess) is TRUE
#> is.null(gr) is FALSE use numDeriv jacobian()
#> Hessian from Jacobian: [,1] [,2] [,3] [,4]
#> [1,] 612 -360 0 0
#> [2,] -360 814 -360 0
#> [3,] 0 -360 814 -360
#> [4,] 0 0 -360 202
cat("results of gHgenb for genrose at ")
#> results of gHgenb for genrose at
print(parx)
#> [1] 0.9 0.9 0.9 0.9
print(gennog)
#> $gn
#> [1] -3.0456 -1.5536 -1.5536 1.4920
#>
#> $Hn
#> [,1] [,2] [,3] [,4]
#> [1,] 5.752800e+01 -3.384000e+01 -1.375487e-12 2.961084e-15
#> [2,] -3.384000e+01 7.832800e+01 -3.384000e+01 -1.030281e-13
#> [3,] -1.375487e-12 -3.384000e+01 7.832800e+01 -3.384000e+01
#> [4,] 2.961084e-15 -1.030281e-13 -3.384000e+01 2.080000e+01
#>
#> $gradOK
#> [1] FALSE
#>
#> $hessOK
#> [1] TRUE
#>
#> $nbm
#> [1] 0
#>
cat("compare to g =")
#> compare to g =
print(gval)
#> [1] -32.4 -14.6 -14.6 17.8
cat("and Hess\n")
#> and Hess
print(Ahess)
#> [,1] [,2] [,3] [,4]
#> [1,] 612 -360 0 0
#> [2,] -360 814 -360 0
#> [3,] 0 -360 814 -360
#> [4,] 0 0 -360 202
gst<-5
cat("\n\nTest with full calling sequence and gs=",gst,"\n")
#>
#>
#> Test with full calling sequence and gs= 5
gengall<-gHgenb(parx,genrose.f,genrose.g,genrose.h, control=list(ktrace=TRUE),gs=gst)
#> Compute gradient approximation
#> [1] -1.62 -0.92 -0.92 0.70
#> Compute Hessian approximation
#> is.null(hess) is FALSE -- trying hess()
#> [,1] [,2] [,3] [,4]
#> [1,] 30.6 -18.0 0.0 0
#> [2,] -18.0 42.6 -18.0 0
#> [3,] 0.0 -18.0 42.6 -18
#> [4,] 0.0 0.0 -18.0 12
#> [,1] [,2] [,3] [,4]
#> [1,] 30.6 -18.0 0.0 0
#> [2,] -18.0 42.6 -18.0 0
#> [3,] 0.0 -18.0 42.6 -18
#> [4,] 0.0 0.0 -18.0 12
print(gengall)
#> $gn
#> [1] -1.62 -0.92 -0.92 0.70
#>
#> $Hn
#> [,1] [,2] [,3] [,4]
#> [1,] 30.6 -18.0 0.0 0
#> [2,] -18.0 42.6 -18.0 0
#> [3,] 0.0 -18.0 42.6 -18
#> [4,] 0.0 0.0 -18.0 12
#>
#> $gradOK
#> [1] FALSE
#>
#> $hessOK
#> [1] TRUE
#>
#> $nbm
#> [1] 0
#>
top<-25
x0<-rep(2,4)
cat("\n\nTest for maximization and top=",top,"\n")
#>
#>
#> Test for maximization and top= 25
cat("Gradient and Hessian will have sign inverted")
#> Gradient and Hessian will have sign inverted
maxt<-gHgen(x0, maxfn, control=list(ktrace=TRUE), top=top)
#> Compute gradient approximation
#> [1] -24 0 24 48
#> Compute Hessian approximation
#> is.null(gr) is TRUE use numDeriv hessian()
#> [,1] [,2] [,3] [,4]
#> [1,] -3.200000e+01 3.126795e-11 8.000000e+00 1.600000e+01
#> [2,] 3.126795e-11 -2.400000e+01 3.128447e-11 5.315713e-12
#> [3,] 8.000000e+00 3.128447e-11 -3.200000e+01 -1.600000e+01
#> [4,] 1.600000e+01 5.315713e-12 -1.600000e+01 -5.600000e+01
print(maxt)
#> $gn
#> [1] -24 0 24 48
#>
#> $Hn
#> [,1] [,2] [,3] [,4]
#> [1,] -3.200000e+01 3.126795e-11 8.000000e+00 1.600000e+01
#> [2,] 3.126795e-11 -2.400000e+01 3.128447e-11 5.315713e-12
#> [3,] 8.000000e+00 3.128447e-11 -3.200000e+01 -1.600000e+01
#> [4,] 1.600000e+01 5.315713e-12 -1.600000e+01 -5.600000e+01
#>
#> $gradOK
#> [1] FALSE
#>
#> $hessOK
#> [1] TRUE
#>
#> $nbm
#> [1] 0
#>
cat("test against negmaxfn\n")
#> test against negmaxfn
gneg <- grad(negmaxfn, x0)
Hneg<-hessian(negmaxfn, x0)
# gdiff<-max(abs(gneg-maxt$gn))/max(abs(maxt$gn))
# Hdiff<-max(abs(Hneg-maxt$Hn))/max(abs(maxt$Hn))
# explicitly change sign
gdiff<-max(abs(gneg-(-1)*maxt$gn))/max(abs(maxt$gn))
Hdiff<-max(abs(Hneg-(-1)*maxt$Hn))/max(abs(maxt$Hn))
cat("gdiff = ",gdiff," Hdiff=",Hdiff,"\n")
#> gdiff = 0 Hdiff= 9.516197e-17