Your function looks fairly simple to differentiate by hand, but if you have access to the gradient (or you estimate it numerically using scipy...), this function might do the job:def hessian ( x, the_func, epsilon=1e-8):"""Numerical approximation to the HessianParameters------------x: array-likeThe evaluation pointthe_func: functionThe function. We assume that the function returns the function value andthe associated gradient as the second return elementepsilon: floatThe size of the step"""N = x.sizeh = np.zeros((N,N))df_0 = the_func ( x )[1]for i in xrange(N):xx0 = 1.*x[i]x[i] = xx0 + epsilondf_1 = the_func ( x )[1]h[i,:] = (df_1 - df_0)/epsilonx[i] = xx0return hJose
On 8 August 2014 08:31, Kiko <kikocorreoso@gmail.com> wrote:_______________________________________________Hi all,I am trying to calculate a Hessian. I am using numdifftools for this (https://pypi.python.org/pypi/Numdifftools).My question is, is it possible to make it using pure numpy?.The actual code is like this:import numdifftools as ndimport numpy as npdef log_likelihood(params):sum1 = 0; sum2 = 0mu = params[0]; sigma = params[1]; xi = params[2]for z in data:x = 1 + xi * ((z-mu)/sigma)sum1 += np.log(x)sum2 += x**(-1.0/xi)return -((-len(data) * np.log(sigma)) - (1 + 1/xi)*sum1 - sum2) # negated so we can use 'minimum'kk = nd.Hessian(log_likelihood)Thanks in advance.
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion