Author: bugman Date: Tue Aug 26 19:38:01 2014 New Revision: 25317 URL: http://svn.gna.org/viewcvs/relax?rev=25317&view=rev Log: Added a script and log file for calculating the numerical gradient for an exponential curve. This uses the data at http://thread.gmane.org/gmane.science.nmr.relax.devel/6807/focus=6840 and calculates the Hessian using the numdifftools.Hessian object construct and obtain the matrix, both at the minimum and at a point away from the minimum. The values will be used to construct a unit test to check the C module implementation. Added: trunk/test_suite/shared_data/curve_fitting/numeric_gradient/Hessian.log trunk/test_suite/shared_data/curve_fitting/numeric_gradient/Hessian.py - copied, changed from r25316, trunk/test_suite/shared_data/curve_fitting/numeric_gradient/integrate.py Added: trunk/test_suite/shared_data/curve_fitting/numeric_gradient/Hessian.log URL: http://svn.gna.org/viewcvs/relax/trunk/test_suite/shared_data/curve_fitting/numeric_gradient/Hessian.log?rev=25317&view=auto ============================================================================== --- trunk/test_suite/shared_data/curve_fitting/numeric_gradient/Hessian.log (added) +++ trunk/test_suite/shared_data/curve_fitting/numeric_gradient/Hessian.log Tue Aug 26 19:38:01 2014 @@ -0,0 +1,14 @@ + + +On-minimum: + +The Hessian at [1.0, 1000.0] is: +[[ 4.72548021e+03 6.00949488e-13] + [ 6.00949488e-13 0.00000000e+00]] + + +Off-minimum: + +The Hessian at [2.0, 500.0] is: +[[ -4.11964848e+02 2.70730426e-09] + [ 2.70730426e-09 0.00000000e+00]] Copied: trunk/test_suite/shared_data/curve_fitting/numeric_gradient/Hessian.py (from r25316, trunk/test_suite/shared_data/curve_fitting/numeric_gradient/integrate.py) URL: http://svn.gna.org/viewcvs/relax/trunk/test_suite/shared_data/curve_fitting/numeric_gradient/Hessian.py?p2=trunk/test_suite/shared_data/curve_fitting/numeric_gradient/Hessian.py&p1=trunk/test_suite/shared_data/curve_fitting/numeric_gradient/integrate.py&r1=25316&r2=25317&rev=25317&view=diff ============================================================================== --- trunk/test_suite/shared_data/curve_fitting/numeric_gradient/integrate.py (original) +++ trunk/test_suite/shared_data/curve_fitting/numeric_gradient/Hessian.py Tue Aug 26 19:38:01 2014 @@ -3,13 +3,16 @@ # Python module imports. from math import exp from numpy import array, float64 -from scipy.misc import derivative +from numdifftools import Hessian -def func_R(R): +def func(params): """Calculate the chi-squared value.""" global times, I, I0, errors + + # Unpack the parameters. + R, IO = params # The intensities. back_calc = [] @@ -20,31 +23,6 @@ chi2 = 0.0 for i in range(len(times)): chi2 += (I[i] - back_calc[i])**2 / errors[i]**2 - - # Printout. - print("dR: R=%f, I=%s, chi2=%f" % (R, back_calc, chi2)) - - # Return the value. - return chi2 - - -def func_I(I0): - """Calculate the chi-squared value.""" - - global times, I, R, errors - - # The intensities. - back_calc = [] - for i in range(len(times)): - back_calc.append(I0 * exp(-R*times[i])) - - # The chi2. - chi2 = 0.0 - for i in range(len(times)): - chi2 += (I[i] - back_calc[i])**2 / errors[i]**2 - - # Printout. - print("dI0: I0=%f, I=%s, chi2=%f" % (I0, back_calc, chi2)) # Return the value. return chi2 @@ -63,16 +41,17 @@ # The intensity errors. errors = [10.0, 10.0, 10.0, 10.0, 10.0] -# The numeric gradient at the minimum. +# Set up the Hessian function. +d2func = Hessian(func) + +# The numeric Hessian at the minimum. print("\n\nOn-minimum:\n") -grad_R = derivative(func_R, R, dx=1e-5, order=11) -grad_I = derivative(func_I, I0, dx=1e-5, order=11) -print("\nThe gradient at %s is:\n %s" % ([R, I0], [grad_R, grad_I])) +hess = d2func([R, I0]) +print("The Hessian at %s is:\n%s" % ([R, I0], hess)) -# The numeric gradient off the minimum. +# The numeric Hessian off the minimum. print("\n\nOff-minimum:\n") R = 2.0 I0 = 500.0 -grad_R = derivative(func_R, R, dx=1e-5, order=11) -grad_I = derivative(func_I, I0, dx=1e-5, order=11) -print("\nThe gradient at %s is:\n %s" % ([R, I0], [grad_R, grad_I])) +hess = d2func([R, I0]) +print("The Hessian at %s is:\n%s" % ([R, I0], hess))