gHgen 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.

gHgen(par, fn, gr=NULL, hess=NULL,
      control=list(ktrace=0), ...)

Arguments

par

Set of parameters, assumed to be at a minimum of the function fn.

fn

Name of the objective function.

gr

(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.

hess

(Optional) function to compute the Hessian of the objective function. This is rarely available, but is included for completeness.

control

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 gHgen (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.

Details

None

Value

ansout a list of four items,

gn

The approximation to the gradient vector.

Hn

The approximation to the Hessian matrix.

gradOK

TRUE if the gradient has been computed acceptably. FALSE otherwise.

hessOK

TRUE if the gradient has been computed acceptably and passes the symmetry test. FALSE otherwise.

nbm

Always 0. The number of active bounds and masks. Present to make function consistent with gHgenb.

Examples

# 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)
}

trad<-c(-1.2,1)
ans100fgh<-  gHgen(trad, genrose.f, gr=genrose.g, hess=genrose.h,
      control=list(ktrace=1)) 
#> Compute gradient approximation
#> [1] -211.2  -88.0
#> Compute Hessian approximation
#> is.null(hess) is FALSE -- trying hess()
#>      [,1] [,2]
#> [1,] 1328  480
#> [2,]  480  202
print(ans100fgh)
#> $gn
#> [1] -211.2  -88.0
#> 
#> $Hn
#>      [,1] [,2]
#> [1,] 1328  480
#> [2,]  480  202
#> 
#> $gradOK
#> [1] FALSE
#> 
#> $hessOK
#> [1] TRUE
#> 
#> $nbm
#> [1] 0
#> 
ans100fg<-  gHgen(trad, genrose.f, gr=genrose.g, 
      control=list(ktrace=1)) 
#> Compute gradient approximation
#> [1] -211.2  -88.0
#> Compute Hessian approximation
#> is.null(gr) is FALSE use numDeriv jacobian()
#> Hessian from jacobian:     [,1] [,2]
#> [1,] 1328  480
#> [2,]  480  202
#> Hn from jacobian is reported non-symmetric with asymmetry ratio 5.09011128647231e-12 
#> Warning: Hn from jacobian is reported non-symmetric with asymmetry ratio 5.09011128647231e-12
#> asym, ctrl$asymtol:  5.090111e-12 1e-07 
#> Force Hessian symmetric
#>      [,1] [,2]
#> [1,] 1328  480
#> [2,]  480  202
print(ans100fg)
#> $gn
#> [1] -211.2  -88.0
#> 
#> $Hn
#>      [,1] [,2]
#> [1,] 1328  480
#> [2,]  480  202
#> 
#> $gradOK
#> [1] FALSE
#> 
#> $hessOK
#> [1] TRUE
#> 
#> $nbm
#> [1] 0
#> 
ans100f<-  gHgen(trad, genrose.f, control=list(ktrace=1)) 
#> Compute gradient approximation
#> [1] -211.2  -88.0
#> Compute Hessian approximation
#> is.null(gr) is TRUE use numDeriv hessian()
#>      [,1] [,2]
#> [1,] 1328  480
#> [2,]  480  202
print(ans100f)
#> $gn
#> [1] -211.2  -88.0
#> 
#> $Hn
#>      [,1] [,2]
#> [1,] 1328  480
#> [2,]  480  202
#> 
#> $gradOK
#> [1] FALSE
#> 
#> $hessOK
#> [1] TRUE
#> 
#> $nbm
#> [1] 0
#> 
ans10fgh<-   gHgen(trad, genrose.f, gr=genrose.g, hess=genrose.h,
      control=list(ktrace=1), gs=10) 
#> Compute gradient approximation
#> [1] -21.12  -8.80
#> Compute Hessian approximation
#> is.null(hess) is FALSE -- trying hess()
#>       [,1] [,2]
#> [1,] 132.8   48
#> [2,]  48.0   22
print(ans10fgh)
#> $gn
#> [1] -21.12  -8.80
#> 
#> $Hn
#>       [,1] [,2]
#> [1,] 132.8   48
#> [2,]  48.0   22
#> 
#> $gradOK
#> [1] FALSE
#> 
#> $hessOK
#> [1] TRUE
#> 
#> $nbm
#> [1] 0
#> 
ans10fg<-   gHgen(trad, genrose.f, gr=genrose.g, 
      control=list(ktrace=1), gs=10) 
#> Compute gradient approximation
#> [1] -21.12  -8.80
#> Compute Hessian approximation
#> is.null(gr) is FALSE use numDeriv jacobian()
#> Hessian from jacobian:      [,1] [,2]
#> [1,] 132.8   48
#> [2,]  48.0   22
#> Hn from jacobian is reported non-symmetric with asymmetry ratio 5.83064342859778e-12 
#> Warning: Hn from jacobian is reported non-symmetric with asymmetry ratio 5.83064342859778e-12
#> asym, ctrl$asymtol:  5.830643e-12 1e-07 
#> Force Hessian symmetric
#>       [,1] [,2]
#> [1,] 132.8   48
#> [2,]  48.0   22
print(ans10fg)
#> $gn
#> [1] -21.12  -8.80
#> 
#> $Hn
#>       [,1] [,2]
#> [1,] 132.8   48
#> [2,]  48.0   22
#> 
#> $gradOK
#> [1] FALSE
#> 
#> $hessOK
#> [1] TRUE
#> 
#> $nbm
#> [1] 0
#> 
ans10f<-   gHgen(trad, genrose.f, control=list(ktrace=1), gs=10) 
#> Compute gradient approximation
#> [1] -21.12  -8.80
#> Compute Hessian approximation
#> is.null(gr) is TRUE use numDeriv hessian()
#>       [,1] [,2]
#> [1,] 132.8   48
#> [2,]  48.0   22
print(ans10f)
#> $gn
#> [1] -21.12  -8.80
#> 
#> $Hn
#>       [,1] [,2]
#> [1,] 132.8   48
#> [2,]  48.0   22
#> 
#> $gradOK
#> [1] FALSE
#> 
#> $hessOK
#> [1] TRUE
#> 
#> $nbm
#> [1] 0
#>