Setting the step function

I am trying to set the step function using scipy.optimize.leastsq. Consider the following example:

import numpy as np
from scipy.optimize import leastsq

def fitfunc(p, x):
    y = np.zeros(x.shape)
    y[x < p[0]] = p[1]
    y[p[0] < x] = p[2]
    return y

errfunc = lambda p, x, y: fitfunc(p, x) - y # Distance to the target function

x = np.arange(1000)
y = np.random.random(1000)

y[x < 250.] -= 10

p0 = [500.,0.,0.]
p1, success = leastsq(errfunc, p0, args=(x, y))

print p1

parameters - this is the location of the step and level on both sides. It is strange that the first free parameter never changes, if you run this scipy, it will give

[  5.00000000e+02  -4.49410173e+00   4.88624449e-01]

when the first parameter is optimal when set to 250, and the second to -10.

Does anyone have an idea of ​​why this might not work and how to make it work?

If I run

print np.sum(errfunc(p1, x, y)**2.)
print np.sum(errfunc([250.,-10.,0.], x, y)**2.)

I find:

12547.1054663
320.679545235

where the first number is what the smallest finds, and the second is the value for the actual optimal function that it should find.

+3
source share
4 answers

, , epsfcn = lesssq:

p1, success = leastsq(errfunc, p0, args=(x, y), epsfcn=10.)

[ 248.00000146   -8.8273455     0.40818216]

, , , , epsfcn - , - .

+2

, - . , . .

? , , .

? .

+1

. inifinite slope "" x (1,0 ). . x , xp, xp-0.5 y xp + 0,5 y [xp-0,5; xp + 0,5] .

, ( ) , y y 0.0 0.0.


2 :

1) np.random.random() 0,0 1,0. , +0,5, ( 0.0). -9.5 (+0.5 - 10.0) -10.0.

,

print np.sum(errfunc([250.,-10.,0.], x, y)**2.)

print np.sum(errfunc([250.,-9.5,0.5], x, y)**2.)

2) fitfunc() y 0.0, x p [0]. , ​​ ( ). . , 500.

+1
source

Most likely, your optimization is stuck in local lows. I don't know what lesssq actually works, but if you give it an initial estimate of (0, 0, 0), it will get stuck too.

You can check the gradient in the initial estimate numerically (evaluate to +/- epsilon for a very small epsilon and divide bei 2 * epsilon, accept the difference), and I'm sure it will be 0.

0
source

Source: https://habr.com/ru/post/1719180/


All Articles