Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimization of a piecewise function in Scipy/python

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.

like image 688
RoboCopNixon Avatar asked Oct 19 '22 12:10

RoboCopNixon


1 Answers

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

like image 53
yjhuoh Avatar answered Oct 21 '22 10:10

yjhuoh