Re: How to calculate Hessian Matrix



On 5 août, 09:32, fj <francois.j...@xxxxxxx> wrote:
On 5 août, 04:45, John <john131...@xxxxxxxxx> wrote:

Hi,

Does anyone know where there is any function/subroutine can compute
the Hessian Matrix of a multivariable function? Thanks a lot!

John

First of all, your are not posting in the right group.

It is very easy to compute an Hessian matrix using numerical
derivatives. The only problem is the cost especially if the multi
variable function is itself a "vector" function.

The simplest way consists in writing a routine computing the gradient.
After that, you write the function computing the Hessian in the same
way. Let us assume the the function is only scalar :

double precision function f(n,x)
subroutine gradient(n,x,f,df)

subroutine hessian(n,x,f,h)
implicit none
integer,intent(in) :: n
double precision,intent(in) :: x(n)
double precision,intent(out) :: h(n,n)
external :: f,gradient
double precision :: xx(n),df0(n),df(n),xx0
double precision,parameter :: epsilon=1.d-5
integer :: j
call gradient(n,x,f,df0)
xx=x
do j=1,n
xx0=xx(j)+epsilon
call gradient(n,xx,f,df)
h(:,j)=(df-df0)/epsilon
enddo
end subroutine

Of course, you might improve this simple solution : the parameter
"epsilon" may be passed as argument and may be a vector. You might
also prefer central derivatives rather than "right" derivatives and
define routines f and gradient using interfaces rather than EXTERNAL
declaration ...

Oups ! The instructions in the loop are wrong and should be replaced
by :

do j=1,n
xx0=xx(j)
xx(j)=xx0+epsilon
call gradient(n,xx,f,df)
h(:,j)=(df-df0)/epsilon
xx(j)=xx0
enddo

Sorry, I was not awake !

.



Relevant Pages

  • Re: How to calculate Hessian Matrix
    ... It is very easy to compute an Hessian matrix using numerical ... double precision function f ... subroutine gradient ... also prefer central derivatives rather than "right" derivatives and ...
    (comp.lang.fortran)
  • Re: How to calculate Hessian Matrix
    ... evalued but has no explicit expression. ... Hessian matrix at some point to see the shape. ... For each mathematical operation on the way to computing ... for each intermediate value also compute the derivatives ...
    (comp.lang.fortran)
  • Re: How to calculate Hessian Matrix
    ... Hessian matrix at some point to see the shape. ... How do you evaluate it without an explicit expression? ... For each mathematical operation on the way to computing ... for each intermediate value also compute the derivatives ...
    (comp.lang.fortran)
  • Re: How to calculate Hessian Matrix
    ... you write the function computing the Hessian in the same ... also prefer central derivatives rather than "right" derivatives and ... as the Hessian matrix is symmetrical (d/dxi(df/ ... dxj)=d/dxj) the previous algorithm is not optimal. ...
    (comp.lang.fortran)