Learn R Programming

gmvarkit (version 1.4.1)

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 estimates of a class 'gmvar' object. get_soc returns eigenvalues of the Hessian matrix, and get_foc is the same as get_gradient but named conveniently.

Usage

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

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

get_gradient(gmvar, h = 6e-06)

get_hessian(gmvar, h = 6e-06)

get_soc(gmvar, h = 6e-06)

get_foc(gmvar, 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

gmvar

an object of class 'gmvar' created with fitGMVAR or GMVAR.

Value

Gradient functions return numerical approximation of the gradient and Hessian functions return numerical approximation of the Hessian. get_soc returns eigenvalues of the Hessian matrix.

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. Note that profile log-likelihood functions can be conveniently plotted with the function profile_logliks.

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)

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

  
# }
# NOT RUN {
  # These examples below use the data 'eurusd' which comes
  # with the package, but in a scaled form.
  data <- cbind(10*eurusd[,1], 100*eurusd[,2])
  colnames(data) <- colnames(eurusd)

  # GMVAR(1,2), d=2 model:
  params122 <- c(0.623, -0.129, 0.959, 0.089, -0.006, 1.006, 1.746,
    0.804, 5.804, 3.245, 7.913, 0.952, -0.037, -0.019, 0.943, 6.926,
    3.982, 12.135, 0.789)
  mod122 <- GMVAR(data, p=1, M=2, params=params122)
  get_gradient(mod122)
  get_hessian(mod122)
  get_soc(mod122)
  
# }

Run the code above in your browser using DataLab