Description Usage Arguments Value Author(s) See Also Examples
Computes finite difference gradient and/or hessian. genGradient
function does forward, backward or central differences, the
genHessian
function uses only central differences.
1 2 3 | genGradient(x, func, h = 0.001, diff.type = 0)
genHessian(x, func, h = 0.001)
|
x |
Point at which to compute the gradient or hessian. |
func |
function that takes only |
h |
Step length for the finite difference. |
diff.type |
Type of finite difference, |
gradient vector or Hessian matrix.
Johan Lindstrom
Other numerical derivatives: loglikeSTGrad
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #create a two variable function
f.test <- function(x){sin(x[1])*cos(x[2])}
#compute the gradient using forward difference
genGradient(c(.5,.5), f.test, diff.type=1)
#and central difference
genGradient(c(.5,.5), f.test, diff.type=0)
#compared to the true value
c(cos(.5)*cos(.5),-sin(.5)*sin(.5))
#Compute the Hessian
genHessian(c(.5,.5), f.test, h=1e-4)
#and compare to the true value
matrix(c(-sin(.5)*cos(.5),-cos(.5)*sin(.5),
-cos(.5)*sin(.5),-sin(.5)*cos(.5)),2,2)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.