4

As you know, the Gradient of a function is the following vector:

the Gradient

and the Hessian is the following matrix:

the Hessian

Now, I wonder, is there any way to calculate these in R for a user defined function at a given point?

First, I've found a package named numDeriv, which seems to have the necessary functions grad and hessian but now I can't get the correct results... Thus, here's my workflow:

Let's say that we are given the function f(x,y) = x^2 * x^3, and we need to calculate the Gradient and the Hessian at the point (x=1, y=2).

That's been said, I define this function within R:

dummy <- function(x,y) {
  rez <- (z^2)*(y^3)
  rez
}

and then use grad the following way:

grad(func=dummy, x=1, y=2)

which gives me result 16 -- and the problem is that this only the first value from a gradient vector, the correct version of which is

[16, 12]

Same goes with the hessian:

hessian(func=dummy, x=1, y=2)

which gives my 1x1 matrix with the value 16 instead of the 2x2 matrix

     [,1] [,2]
[1,]   16   24
[2,]   24   12

So, the question is what am I doing wrong?

Thank you.

A S
  • 1,195
  • 2
  • 12
  • 26
  • On a side note, I've also [tried](http://reference.wolfram.com/language/tutorial/Differentiation.html) **Wolfram|Alpha** but also failed to get the correct results... – A S Jan 28 '15 at 10:31
  • 1
    `dummy <- function(x) {(x[1]^2)*(x[2]^3)};grad(func=dummy, x=c(1,2));hessian(func=dummy, x=c(1,2))` – Khashaa Jan 28 '15 at 10:48

2 Answers2

22

You can use the pracma library, such as:

library(pracma)

dummy <- function(x) {
  z <- x[1]; y <- x[2]
  rez <- (z^2)*(y^3)
  rez
}

grad(dummy, c(1,2))
[1] 16 12

hessian(dummy, c(1,2))
     [,1] [,2]
[1,]   16   24
[2,]   24   12
  • 4
    @AS for future reference, load the `sos` package. It makes it easy to search for key terms. In your case you'd just type `???hessian` to track down this function and package. – Carl Witthoft Jan 28 '15 at 13:55
1

The following code is an extension of the answer provided. It treats the case where you have the values of the function and not the actual function. Here the function has 1 parameter. The Grad function calculates in a single point. If you have 3 parameters then you need to provide them to x0 with c(x1,x2,x3).

#i is an index, s_carvone$val contains the values of the function
dummy <- function(i) 
{
  return (s_carvone$val[i])
}

#function that calculates the gradient in a specific point i
calc_grad <- function(i)
{
  return (pracma::grad(dummy, x0=i, heps=1))
}

#calculates the derivative from point 2 to 61
first_derivative = unlist(purrr::map(calc_grad, .x = c(2:61)));

plot(first_derivative);
Anton Andreev
  • 2,052
  • 1
  • 22
  • 23