Learn R Programming

uGMAR (version 3.2.6)

calc_gradient: Calculate gradient or Hessian matrix

Description

calc_gradient or calc_hessian calculates the gradient or Hessian matrix of the given function at the given point using central difference numerical approximation. get_gradient (and get_foc) or get_hessian calculates the gradient or Hessian matrix of the log-likelihood function at the parameter values of a class 'gsmar' object. get_soc returns eigenvalues of the Hessian matrix.

Usage

calc_gradient(x, fn, h = 6e-06, varying_h = NULL, ...)

calc_hessian(x, fn, h = 6e-06, varying_h = NULL, ...)

get_gradient(gsmar, custom_h = NULL)

get_foc(gsmar, custom_h = NULL)

get_hessian(gsmar, custom_h = NULL)

get_soc(gsmar, custom_h = NULL)

Arguments

x

a numeric vector specifying the point at which the gradient or Hessian should be evaluated.

fn

a function that takes in the argument x as the first argument.

h

the difference used to approximate the derivatives.

varying_h

a numeric vector with the same length as x specifying the difference h for each dimension separately. If NULL (default), then the difference given as parameter h will be used for all dimensions.

...

other arguments passed to fn.

gsmar

object of class 'gsmar' created with the function fitGSMAR or GSMAR.

custom_h

same as varying_h but if NULL (default), then the difference h used for differentiating overly large degrees of freedom parameters is adjusted to avoid numerical problems, and the difference is 6e-6 for the other parameters.

Value

The gradient functions return numerical approximation of the gradient, and the Hessian functions return numerical approximation of the Hessian. get_soc returns eigenvalues of the Hessian matrix, get_foc is the same as get_gradient but named conveniently.

Warning

No argument checks!

Details

In particular, the functions get_foc and get_soc can be used to check whether the found estimates denote a (local) maximum point, a saddle point, or something else.

See Also

profile_logliks

Examples

Run this code
# NOT RUN {
 # Simple function
 foo <- function(x) x^2 + x
 calc_gradient(x=1, fn=foo)
 calc_gradient(x=-0.5, fn=foo)
 calc_hessian(x=2, fn=foo)

 # More complicated function
 foo <- function(x, a, b) a*x[1]^2 - b*x[2]^2
 calc_gradient(x=c(1, 2), fn=foo, a=0.3, b=0.1)
 calc_hessian(x=c(1, 2), fn=foo, a=0.3, b=0.1)

 # StMAR model:
params43 <- c(0.09, 1.31, -0.46, 0.33, -0.23, 0.04, 0.01, 1.15,
 -0.3, -0.03, 0.03, 1.54, 0.06, 1.19, -0.3, 0.42, -0.4, 0.01,
  0.57, 0.22, 8.05, 2.02, 1000)
 stmar43 <- GSMAR(T10Y1Y, 4, 3, params43, model="StMAR")
 get_gradient(stmar43)
 get_foc(stmar43)
 get_hessian(stmar43)
 get_soc(stmar43)
# }

Run the code above in your browser using DataLab