Learn R Programming

uGMAR (version 3.1.0)

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 or get_hessian calculates the gradient or Hessian matrix of the log-likelihood function at the parameter values of class 'gsmar' object. get_soc returns eigenvalues of the Hessian matrix.

Usage

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

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

get_gradient(gsmar, h = 6e-06)

get_hessian(gsmar, h = 6e-06)

get_soc(gsmar, h = 6e-06)

Arguments

x

a numeric vector specifying the point where the gradient or Hessian should be calculated.

fn

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

h

difference used to approximate the derivatives.

...

other arguments passed to fn

gsmar

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

Value

Gradient functions return numerical approximation of the gradient, and Hessian functions return numerical approximation of the Hessian.

Warning

No argument checks!

Details

Especially the functions get_gradient or get_hessian can be used to check whether the found estimates denote a (local) maximum point, a saddle point or something else.

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)

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

 # GMAR model:
 params12 <- c(0.18281409, 0.92657275, 0.00214552,
  0.85725129, 0.68210294, 0.01900299, 0.88342018)
 gmar12 <- GSMAR(logVIX, 1, 2, params12)
 get_gradient(gmar12)
 get_hessian(gmar12)
 get_soc(gmar12)
# }

Run the code above in your browser using DataLab