流れに従ひて己を失はず.

日々の研究について書きます

ScipyのLevenberg-Marquardt法で大域的最適解に落ちるか試した(だけ)

scipy.optimize.least_squaresライブラリを使ってLevenberg-Marquardt法を試しました.

局所最適解はx=8, 大域的最適解はx=2.
深い考察はありません.

from scipy.optimize import least_squares
import numpy as np
from matplotlib import pylab as plt

### 関数
def func(param):
    if param>5:
        funcval= (param-8)**2+100
    else:
        funcval= (param-2)**2+10
    return funcval

data = np.ones(1000)
x = np.linspace(-15,15,1000)
for i in range(0,1000):
    data[i] = func(x[i])

plt.figure()
plt.plot(x,data)
plt.ylabel("y")
plt.xlabel("x")
plt.show()



###どの初期値から始めても大域的最適解に収束するか?
data = []

#設定
jac='2-point'
bounds=(-np.inf, np.inf)
method='lm'

for i in range(-1000,1000):
    param = i
    result = least_squares(func,param,jac,bounds,method)
    data.append([i,result.x[0]])
data = np.array(data)

plt.figure()
plt.plot(data[:,0],data[:,1])
plt.ylabel("estimated param")
plt.xlabel("initial value")
plt.show()

関数の形状
f:id:Yasutchi:20200427000226p:plain

初期値と推定解
f:id:Yasutchi:20200427000250p:plain