[SciPy-Dev] [PATCH] proper broadcasting for epsilon in scipy.optimize.approx_fprime()

Maxim Ivanov maxim.ivanov@viewdle....
Wed Sep 14 12:29:36 CDT 2011


Hi SciPy developers!

Many of the functions in scipy.optimize module (e.g. fmin_bfgs) accept
the 'epsilon' parameter, which is meant to be the step size coefficient.
For multidimensional optimization one may want to set different values
of the coefficient for different dimensions. But, at least in 0.8, the
fmin_bfgs function, contrary to what its docs say, can't handle vectors
as epsilon argument, only scalars work.

The patch which I'm submitting uses Numpy broadcasting for the purpose
of accepting both scalars AND vectors of appropriate size as the epsilon
parameter. 

It may not apply clearly in trunk, as was developed (or, rather, quickly
hacked around) for 0.8. Please rebase if needed.


Best regards,
Maxim Ivanov


--- optimize.py	2010-07-26 17:48:33.000000000 +0300
+++ optimize.py	2011-09-14 19:49:11.040484658 +0300
@@ -617,8 +617,9 @@
     grad = numpy.zeros((len(xk),), float)
     ei = numpy.zeros((len(xk),), float)
     for k in range(len(xk)):
-        ei[k] = epsilon
-        grad[k] = (f(*((xk+ei,)+args)) - f0)/epsilon
+        ei[k] = 1.0
+        d = epsilon * ei
+        grad[k] = (f(*((xk+d,)+args)) - f0)/d[k]
         ei[k] = 0.0
     return grad
 





More information about the SciPy-Dev mailing list