| genaDiff | R Documentation |
Numeric estimation of the gradient and Hessian.
gena.grad( fn, par, eps = sqrt(.Machine$double.eps) * abs(par), method = "central-difference", fn.args = NULL ) gena.hessian( fn = NULL, gr = NULL, par, eps = sqrt(.Machine$double.eps) * abs(par), fn.args = NULL, gr.args = NULL )
fn |
function for which gradient or Hessian should be calculated. |
par |
point (parameters' value) at which |
eps |
numeric vector representing increment of the |
method |
numeric differentiation method: "central-difference" or "forward-difference". |
fn.args |
list containing arguments of |
gr |
gradient function of |
gr.args |
list containing arguments of |
It is possible to substantially improve numeric Hessian accuracy
by using analytical gradient gr. If both fn and gr
are provided then only gr will be used. If only fn is provided
for gena.hessian then eps will be transformed to
sqrt(eps) for numeric stability purposes.
Function gena.grad returns a vector that is a gradient of
fn at point par calculated via method numeric
differentiation approach using increment eps.
Function gena.hessian returns a matrix that is a Hessian of
fn at point par.
# Consider the following function
fn <- function(par, a = 1, b = 2)
{
val <- par[1] * par[2] - a * par[1] ^ 2 - b * par[2] ^ 2
}
# Calculate the gradient at point (2, 5) respect to 'par'
# when 'a = 1' and 'b = 1'
par <- c(2, 5)
fn.args = list(a = 1, b = 1)
gena.grad(fn = fn, par = par, fn.args = fn.args)
# Calculate Hessian at the same point
gena.hessian(fn = fn, par = par, fn.args = fn.args)
# Repeat calculation of the Hessian using analytical gradient
gr <- function(par, a = 1, b = 2)
{
val <- c(par[2] - 2 * a * par[1],
par[1] - 2 * b * par[2])
}
gena.hessian(gr = gr, par = par, gr.args = fn.args)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.