I have been trying to pass a piecewise function through the scipy optimizer. The example I've constructed below shows the problem:
args = (6,6,7,1,2,4,6,6)
def _alpha(params, *args):
knot = params[0]
rate = np.asarray(args)
where_knot = np.where(rate>knot, 1, 0)
return np.sum(where_knot)
seed_vals = (5,)
bounds = ((1,7), )
res1 = optimize.minimize(_alpha, seed_vals, args=args, method='L-BFGS-B', bounds=bounds)
res1.x
>>> array([ 5.])
However, this is obviously not the solution:
print _alpha((5,), args)
>>> 5
print _alpha((7,), args)
>>> 0
Is there a way to do this that works?
EDIT: I've also tried the numpy piecewise function and get the same results.
you'll need to adjust your approximation stepsize using this: http://docs.scipy.org/doc/scipy/reference/optimize.minimize-lbfgsb.html#optimize-minimize-lbfgsb
the default is something like .0000001 so it will estimate a 0 gradient for knot
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With