def test_powell(): # Powell's function. def func(p): y = np.empty(2) y[0] = p[0] y[1] = 10.0 * p[0] / (p[0] + 0.1) + 2.0 * p[1] * p[1] return y def jacf(p): j = np.empty((2, 2)) j[0, 0] = 1.0 j[0, 1] = 0.0 j[1, 0] = 1.0 / ((p[0] + 0.1) * (p[0] + 0.1)) j[1, 1] = 4.0 * p[1] return j y = np.zeros(2) p0 = [3.0, 1.0] pt = [0.0, 0.0] p, pcov, info = levmar.levmar(func, p0, y, jacf=jacf, **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar(func, p0, y, **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar(func, p0, y, cdiff=True, **OPTS) assert_array_almost_equal(p, pt, decimal=4)
def test_rosen(): # Rosenbrock function def func(p): y = np.empty(2) y[0] = 1.0 - p[0] y[1] = 10.0 * (p[1] - p[0]*p[0]) return y def jacf(p): j = np.empty((2, 2)) j[0, 0] = -1.0 j[0, 1] = 0.0 j[1, 0] = -20.0 * p[0] j[1, 1] = 10.0 return j y = np.zeros(2) p0 = [-1.2, 1.0] pt = [ 1.0, 1.0] p, pcov, info = levmar.levmar(func, p0, y, jacf=jacf, **OPTS) assert_array_almost_equal(p, pt) p, pcov, info = levmar.levmar(func, p0, y, **OPTS) assert_array_almost_equal(p, pt) p, pcov, info = levmar.levmar(func, p0, y, cdiff=True, **OPTS) assert_array_almost_equal(p, pt)
def test_osborne(): # Osborne's problem def func(p, x): y = p[0] + p[1] * np.exp(-p[3] * x) + p[2] * np.exp(-p[4] * x) return y def jacf(p, x): j= np.empty((33, 5)) tmp1 = np.exp(-p[3] * x) tmp2 = np.exp(-p[4] * x) j[:, 0] = 1.0 j[:, 1] = tmp1 j[:, 2] = tmp2 j[:, 3] = -p[1] * x * tmp1 j[:, 4] = -p[2] * x * tmp2 return j x = 10.0 * np.arange(33) y = np.asarray([8.44e-1, 9.08e-1, 9.32e-1, 9.36e-1, 9.25e-1, 9.08e-1, 8.81e-1, 8.50e-1, 8.18e-1, 7.84e-1, 7.51e-1, 7.18e-1, 6.85e-1, 6.58e-1, 6.28e-1, 6.03e-1, 5.80e-1, 5.58e-1, 5.38e-1, 5.22e-1, 5.06e-1, 4.90e-1, 4.78e-1, 4.67e-1, 4.57e-1, 4.48e-1, 4.38e-1, 4.31e-1, 4.24e-1, 4.20e-1, 4.14e-1, 4.11e-1, 4.06e-1]) p0 = [0.5, 1.5, -1.0, 1.0e-2, 2.0e-2] pt = [0.3754, 1.9358, -1.4647, 0.0129, 0.0221] p, pcov, info = levmar.levmar(func, p0, y, args=(x,), jacf=jacf, **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar(func, p0, y, args=(x,), **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar(func, p0, y, args=(x,), cdiff=True, **OPTS) assert_array_almost_equal(p, pt, decimal=4)
def test_modros(): # Modified Rosenbrock problem MODROSLAM = 1E+02 def func(p): y = np.empty(3) y[0] = 10.0 * (p[1] - p[0] * p[0]) y[1] = 1.0 - p[0] y[2] = MODROSLAM return y def jacf(p): j = np.empty((3, 2)) j[0, 0] = -20.0 * p[0] j[0, 1] = 10.0 j[1, 0] = -1.0 j[1, 1] = 0.0 j[2, 0] = 0.0 j[2, 1] = 0.0 return j y = np.zeros(3) p0 = [-1.2, 2.0] pt = [1.0, 1.0] p, pcov, info = levmar.levmar(func, p0, y, jacf=jacf, **OPTS) assert_array_almost_equal(p, pt) p, pcov, info = levmar.levmar(func, p0, y, **OPTS) assert_array_almost_equal(p, pt) p, pcov, info = levmar.levmar(func, p0, y, cdiff=True, **OPTS) assert_array_almost_equal(p, pt)
def test_meyer(): # Meyer's (reformulated) problem def func(p, x): y = p[0] * np.exp(10.0 * p[1] / (x + p[2]) - 13.0) return y def jacf(p, x): p0, p1, p2 = p j = np.empty((16, 3)) tmp = np.exp(10.0 * p1 / (x + p2) - 13.0) j[:, 0] = tmp j[:, 1] = 10.0 * p0 * tmp / (x + p2) j[:, 2] = -10.0 * p0 * p1 * tmp / ((x + p2) * (x + p2)) return j x = 0.45 + 0.05 * np.arange(16) y = np.asarray([ 34.780, 28.610, 23.650, 19.630, 16.370, 13.720, 11.540, 9.744, 8.261, 7.030, 6.005, 5.147, 4.427, 3.820, 3.307, 2.872 ]) p0 = [8.85, 4.00, 2.50] pt = [2.48, 6.18, 3.45] p, pcov, info = levmar.levmar(func, p0, y, args=(x, ), jacf=jacf, **OPTS) assert_array_almost_equal(p, pt, decimal=1) p, pcov, info = levmar.levmar(func, p0, y, args=(x, ), **OPTS) assert_array_almost_equal(p, pt, decimal=1) p, pcov, info = levmar.levmar(func, p0, y, args=(x, ), cdiff=True, **OPTS) assert_array_almost_equal(p, pt, decimal=1)
def test_osborne(): # Osborne's problem def func(p, x): y = p[0] + p[1] * np.exp(-p[3] * x) + p[2] * np.exp(-p[4] * x) return y def jacf(p, x): j = np.empty((33, 5)) tmp1 = np.exp(-p[3] * x) tmp2 = np.exp(-p[4] * x) j[:, 0] = 1.0 j[:, 1] = tmp1 j[:, 2] = tmp2 j[:, 3] = -p[1] * x * tmp1 j[:, 4] = -p[2] * x * tmp2 return j x = 10.0 * np.arange(33) y = np.asarray([ 8.44e-1, 9.08e-1, 9.32e-1, 9.36e-1, 9.25e-1, 9.08e-1, 8.81e-1, 8.50e-1, 8.18e-1, 7.84e-1, 7.51e-1, 7.18e-1, 6.85e-1, 6.58e-1, 6.28e-1, 6.03e-1, 5.80e-1, 5.58e-1, 5.38e-1, 5.22e-1, 5.06e-1, 4.90e-1, 4.78e-1, 4.67e-1, 4.57e-1, 4.48e-1, 4.38e-1, 4.31e-1, 4.24e-1, 4.20e-1, 4.14e-1, 4.11e-1, 4.06e-1 ]) p0 = [0.5, 1.5, -1.0, 1.0e-2, 2.0e-2] pt = [0.3754, 1.9358, -1.4647, 0.0129, 0.0221] p, pcov, info = levmar.levmar(func, p0, y, args=(x, ), jacf=jacf, **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar(func, p0, y, args=(x, ), **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar(func, p0, y, args=(x, ), cdiff=True, **OPTS) assert_array_almost_equal(p, pt, decimal=4)
def test_rosen(): # Rosenbrock function def func(p): y = np.empty(2) y[0] = 1.0 - p[0] y[1] = 10.0 * (p[1] - p[0] * p[0]) return y def jacf(p): j = np.empty((2, 2)) j[0, 0] = -1.0 j[0, 1] = 0.0 j[1, 0] = -20.0 * p[0] j[1, 1] = 10.0 return j y = np.zeros(2) p0 = [-1.2, 1.0] pt = [1.0, 1.0] p, pcov, info = levmar.levmar(func, p0, y, jacf=jacf, **OPTS) assert_array_almost_equal(p, pt) p, pcov, info = levmar.levmar(func, p0, y, **OPTS) assert_array_almost_equal(p, pt) p, pcov, info = levmar.levmar(func, p0, y, cdiff=True, **OPTS) assert_array_almost_equal(p, pt)
def test_modros(): # Modified Rosenbrock problem MODROSLAM = 1E+02 def func(p): y = np.empty(3) y[0] = 10.0 * (p[1] - p[0]*p[0]) y[1] = 1.0 - p[0] y[2] = MODROSLAM return y def jacf(p): j = np.empty((3, 2)) j[0, 0] = -20.0 * p[0] j[0, 1] = 10.0 j[1, 0] = -1.0 j[1, 1] = 0.0 j[2, 0] = 0.0 j[2, 1] = 0.0 return j y = np.zeros(3) p0 = [-1.2, 2.0] pt = [ 1.0, 1.0] p, pcov, info = levmar.levmar(func, p0, y, jacf=jacf, **OPTS) assert_array_almost_equal(p, pt) p, pcov, info = levmar.levmar(func, p0, y, **OPTS) assert_array_almost_equal(p, pt) p, pcov, info = levmar.levmar(func, p0, y, cdiff=True, **OPTS) assert_array_almost_equal(p, pt)
def test_powell(): # Powell's function. def func(p): y = np.empty(2) y[0] = p[0] y[1] = 10.0 * p[0] / (p[0] + 0.1) + 2.0 * p[1]*p[1] return y def jacf(p): j = np.empty((2, 2)) j[0, 0] = 1.0 j[0, 1] = 0.0 j[1, 0] = 1.0 / ((p[0] + 0.1) * (p[0] + 0.1)) j[1, 1] = 4.0 * p[1] return j y = np.zeros(2) p0 = [3.0, 1.0] pt = [0.0, 0.0] p, pcov, info = levmar.levmar(func, p0, y, jacf=jacf, **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar(func, p0, y, **OPTS) assert_array_almost_equal(p, pt, decimal=4) p, pcov, info = levmar.levmar(func, p0, y, cdiff=True, **OPTS) assert_array_almost_equal(p, pt, decimal=4)
def test_meyer(): # Meyer's (reformulated) problem def func(p, x): y = p[0] * np.exp(10.0 * p[1] / (x + p[2]) - 13.0) return y def jacf(p, x): p0, p1, p2 = p j = np.empty((16, 3)) tmp = np.exp(10.0 * p1 / (x + p2) - 13.0) j[:,0] = tmp j[:,1] = 10.0 * p0 * tmp / (x + p2) j[:,2] = -10.0 * p0 * p1 * tmp / ((x + p2) * (x + p2)) return j x = 0.45 + 0.05 * np.arange(16) y = np.asarray([34.780, 28.610, 23.650, 19.630, 16.370, 13.720, 11.540, 9.744, 8.261, 7.030, 6.005, 5.147, 4.427, 3.820, 3.307, 2.872]) p0 = [8.85, 4.00, 2.50] pt = [2.48, 6.18, 3.45] p, pcov, info = levmar.levmar(func, p0, y, args=(x,), jacf=jacf, **OPTS) assert_array_almost_equal(p, pt, decimal=1) p, pcov, info = levmar.levmar(func, p0, y, args=(x,), **OPTS) assert_array_almost_equal(p, pt, decimal=1) p, pcov, info = levmar.levmar(func, p0, y, args=(x,), cdiff=True, **OPTS) assert_array_almost_equal(p, pt, decimal=1)
def bundleAdjustment_LM(pts1_3xn, pts2_3xn): def errorFunc(twist, pts1, pts2): return (pts1 - unHomo(se3ToSE3(twist).dot(h**o(pts2)))).T.ravel() def calJacobian(twist, pts1, pts2): trans_pts2 = unHomo(se3ToSE3(twist).dot(h**o(pts2))) Jacobian = [] for i in range(trans_pts2.shape[1]): x, y, z = trans_pts2[:, i] J = np.array([ [0, -z, y, -1, 0, 0], [z, 0, -x, 0, -1, 0], [-y, x, 0, 0, 0, -1], ]) Jacobian.append(J) return np.vstack(Jacobian) t0 = time.time() opt_twist1, _, info1 = levmar.levmar(errorFunc, np.zeros(6), [0] * pts1_3xn.size, (pts1_3xn, pts2_3xn)) t1 = time.time() opt_twist2, _, info2 = levmar.levmar(errorFunc, np.zeros(6), [0] * pts1_3xn.size, (pts1_3xn, pts2_3xn), calJacobian) t2 = time.time() print("numerical levmar used time: {}".format(t1 - t0)) print("analystic levmar used time: {}".format(t2 - t1)) return se3ToSE3(opt_twist2)
def _solve_levmar(self, intern_x0, tol=1e-8, **kwargs): import warnings warnings.warn("levmar interface is provisional, may change.") import levmar if 'eps1' in kwargs or 'eps2' in kwargs or 'eps3' in kwargs: pass else: kwargs['eps1'] = kwargs['eps2'] = kwargs['eps3'] = tol def _f(*args): return np.asarray(self.f_cb(*args)) def _j(*args): return np.asarray(self.j_cb(*args)) _x0 = np.asarray(intern_x0) _y0 = np.zeros(self.nf) with warnings.catch_warnings(record=True) as wrns: warnings.simplefilter("always") p_opt, p_cov, info = levmar.levmar(_f, _x0, _y0, args=(self.internal_params,), jacf=_j, **kwargs) success = len(wrns) == 0 and np.all(np.abs(_f(p_opt, self.internal_params)) < tol) for w in wrns: raise w e2p0, (e2, infJTe, Dp2, mu_maxJTJii), nit, reason, nfev, njev, nlinsolv = info return {'x': p_opt, 'cov': p_cov, 'nfev': nfev, 'njev': njev, 'nit': nit, 'message': reason, 'nlinsolv': nlinsolv, 'success': success}
def _solve_levmar(self, intern_x0, tol=1e-8, **kwargs): import warnings warnings.warn("levmar interface is provisional, may change.") import levmar if 'eps1' in kwargs or 'eps2' in kwargs or 'eps3' in kwargs: pass else: kwargs['eps1'] = kwargs['eps2'] = kwargs['eps3'] = tol def _f(*args): return np.asarray(self.f_callback(*args)) def _j(*args): return np.asarray(self.j_callback(*args)) _x0 = np.asarray(intern_x0) _y0 = np.zeros(self.nf) with warnings.catch_warnings(record=True) as wrns: warnings.simplefilter("always") p_opt, p_cov, info = levmar.levmar(_f, _x0, _y0, args=(self.internal_params,), jacf=_j, **kwargs) success = len(wrns) == 0 and np.all(np.abs(_f(p_opt, self.internal_params)) < tol) for w in wrns: raise w e2p0, (e2, infJTe, Dp2, mu_maxJTJii), nit, reason, nfev, njev, nlinsolv = info return {'x': p_opt, 'cov': p_cov, 'nfev': nfev, 'njev': njev, 'nit': nit, 'message': reason, 'nlinsolv': nlinsolv, 'success': success}
def estimatePoseDirect(pts3D_nx3, grayScales, grayImg, cameraMatrix): def errorFunc(twist, pts3D_3xn, grayScales, grayImg, cameraMatrix, mask): local_pts3d_3xn = unHomo(se3ToSE3(twist).dot(h**o(pts3D_3xn))) local_pts2d_2xn = unHomo(cameraMatrix.dot(local_pts3d_3xn))[:2] local_pts2d_nx2 = local_pts2d_2xn.T valid_pts2d_nx2, mask[:] = filterPts(local_pts2d_nx2, 4, 4, grayImg.shape[::-1]) cur_gray = np.array( [getPixelValue(grayImg, p[0], p[1]) for p in valid_pts2d_nx2]) errors = np.zeros_like(grayScales, dtype=np.float) errors[mask] = cur_gray - grayScales[mask] # errors[np.logical_not(mask)] = np.nan return errors def calJacobian(twist, pts3D_3xn, grayScales, grayImg, cameraMatrix, mask): fx, fy, cx, cy = cameraMatrix[0, 0], cameraMatrix[1, 1], cameraMatrix[ 0, 2], cameraMatrix[1, 2] trans_pts2 = unHomo(se3ToSE3(twist).dot(h**o(pts3D_3xn))) Jacobian = [] for i, m in zip(range(trans_pts2.shape[1]), mask): if not m: Jacobian.append(np.zeros((1, 6))) continue x, y, z = trans_pts2[:, i] invz = 1.0 / z invz_2 = invz * invz u = x * fx * invz + cx v = y * fy * invz + cy J1 = np.array([[ -x * y * invz_2 * fx, (1 + (x * x * invz_2)) * fx, -y * invz * fx, invz * fx, 0, -x * invz_2 * fx ], [ -(1 + y * y * invz_2) * fy, x * y * invz_2 * fy, x * invz * fy, 0, invz * fy, -y * invz_2 * fy ]]) J2 = np.array([[ getPixelValue(grayImg, u + 1, v) - getPixelValue(grayImg, u - 1, v), getPixelValue(grayImg, u, v + 1) - getPixelValue(grayImg, u, v - 1) ]]) / 2 Jacobian.append(J2.dot(J1)) return np.vstack(Jacobian) # t0 = time.time() # opt_twist1, _, info1 = levmar.levmar( # errorFunc, np.zeros(6), [0] * len(grayScales), (pts3D_nx3.T, grayScales, grayImg, cameraMatrix) # ) # print(info1) t1 = time.time() mask = np.zeros(pts3D_nx3.shape[0], dtype=np.bool) opt_twist2, _, info2 = levmar.levmar( errorFunc, np.zeros(6), [0] * len(grayScales), (pts3D_nx3.T, grayScales, grayImg, cameraMatrix, mask), calJacobian) t2 = time.time() # print("numerical levmar used time: {}".format(t1 - t0)) print("analystic levmar used time: {}".format(t2 - t1)) return se3ToSE3(opt_twist2)
def test_helval(): # Helical valley function def func(p): p0, p1, p2 = p y = np.empty(3) if p0 < 0: theta = atan(p1 / p0) / (2 * pi) + 0.5 elif p0 > 0: theta = atan(p1 / p0) / (2 * pi) else: theta = 0.25 if p1 >= 0 else -0.25 y[0] = 10.0*(p2 - 10.0 * theta) y[1] = 10.0*(sqrt(p0*p0 + p1*p1) - 1.0) y[2] = p2 return y def jacf(p): p0, p1, p2 = p j = np.empty((3, 3)) tmp = p0*p0 + p1*p1 j[0, 0] = 50.0 * p1 / (pi * tmp) j[0, 1] = -50.0 * p0 / (pi * tmp) j[0, 2] = 10.0 j[1, 0] = 10.0 * p0 / sqrt(tmp) j[1, 1] = 10.0 * p1 / sqrt(tmp) j[1, 2] = 0.0 j[2, 0] = 0.0 j[2, 1] = 0.0 j[2, 2] = 1.0 return j y = np.zeros(3) p0 = [-1.0, 2.0, 2.0] pt = [ 1.0, 0.0, 0.0] p, pcov, info = levmar.levmar(func, p0, y, jacf=jacf, **OPTS) assert_array_almost_equal(p, pt) p, pcov, info = levmar.levmar(func, p0, y, **OPTS) assert_array_almost_equal(p, pt) p, pcov, info = levmar.levmar(func, p0, y, cdiff=True, **OPTS) assert_array_almost_equal(p, pt)
def test_helval(): # Helical valley function def func(p): p0, p1, p2 = p y = np.empty(3) if p0 < 0: theta = atan(p1 / p0) / (2 * pi) + 0.5 elif p0 > 0: theta = atan(p1 / p0) / (2 * pi) else: theta = 0.25 if p1 >= 0 else -0.25 y[0] = 10.0 * (p2 - 10.0 * theta) y[1] = 10.0 * (sqrt(p0 * p0 + p1 * p1) - 1.0) y[2] = p2 return y def jacf(p): p0, p1, p2 = p j = np.empty((3, 3)) tmp = p0 * p0 + p1 * p1 j[0, 0] = 50.0 * p1 / (pi * tmp) j[0, 1] = -50.0 * p0 / (pi * tmp) j[0, 2] = 10.0 j[1, 0] = 10.0 * p0 / sqrt(tmp) j[1, 1] = 10.0 * p1 / sqrt(tmp) j[1, 2] = 0.0 j[2, 0] = 0.0 j[2, 1] = 0.0 j[2, 2] = 1.0 return j y = np.zeros(3) p0 = [-1.0, 2.0, 2.0] pt = [1.0, 0.0, 0.0] p, pcov, info = levmar.levmar(func, p0, y, jacf=jacf, **OPTS) assert_array_almost_equal(p, pt) p, pcov, info = levmar.levmar(func, p0, y, **OPTS) assert_array_almost_equal(p, pt) p, pcov, info = levmar.levmar(func, p0, y, cdiff=True, **OPTS) assert_array_almost_equal(p, pt)
def test_wood(): # Wood's function def func(p): p0, p1, p2, p3 = p y = np.empty(6) y[0] = 10.0 * (p1 - p0 * p0) y[1] = 1.0 - p0 y[2] = sqrt(90.0) * (p3 - p2 * p2) y[3] = 1.0 - p2 y[4] = sqrt(10) * (p1 + p3 - 2.0) y[5] = (p1 - p3) / sqrt(10.0) return y y = np.zeros(6) p0 = [-3.0, -1.0, -3.0, -1.0] pt = [1.0, 1.0, 1.0, 1.0] p, pcov, info = levmar.levmar(func, p0, y, **OPTS) assert_array_almost_equal(p, pt) p, pcov, info = levmar.levmar(func, p0, y, cdiff=True, **OPTS) assert_array_almost_equal(p, pt)
def test_wood(): # Wood's function def func(p): p0, p1, p2, p3 = p y = np.empty(6) y[0] = 10.0 * (p1 - p0*p0) y[1] = 1.0 - p0 y[2] = sqrt(90.0) * (p3 - p2*p2) y[3] = 1.0 - p2 y[4] = sqrt(10) * (p1 + p3 - 2.0) y[5] = (p1 - p3) / sqrt(10.0) return y y = np.zeros(6) p0 = [-3.0, -1.0, -3.0, -1.0] pt = [ 1.0, 1.0, 1.0, 1.0] p, pcov, info = levmar.levmar(func, p0, y, **OPTS) assert_array_almost_equal(p, pt) p, pcov, info = levmar.levmar(func, p0, y, cdiff=True, **OPTS) assert_array_almost_equal(p, pt)
def expf(p, x): return p[0] * np.exp(-p[1] * x) + p[2] def jac_expf(p, x): jac = np.empty((x.shape[0], 3)) jac[:, 0] = np.exp(-p[1]*x) jac[:, 1] = -p[0] * x * np.exp(-p[1]*x) jac[:, 2] = np.ones(x.size) return jac # Create input data x_mea = np.arange(40, dtype=np.float64) p_tru = [5.0, 0.1, 1.0] y_tru = expf(p_tru, x_mea) y_mea = y_tru + 0.2 * np.random.randn(x_mea.size) # Initial estimate p_ini = [1.0, 0.5, 0.5] # Fitting with analytic Jacobian p_opt, p_cov, info = levmar.levmar(expf, p_ini, y_mea, args=(x_mea,), jacf=jac_expf) # Print the result print("Expected:") print("{0[0]:9f} {0[1]:9f} {0[2]:9f}".format(p_tru)) print("Estimate:") print("{0[0]:9f} {0[1]:9f} {0[2]:9f}".format(p_opt)) print("")
def gauss(p, x): return p[0] * np.exp(-__4ln2*((x-p[1])/p[2])**2) + p[3] ## Create input data x = np.linspace(-5, 5) pt = [230, -0.4, 2.0, 0.1] yt = gauss(pt, x) y = yt + np.sqrt(yt) * np.random.randn(x.size) ## Initial estimate p0 = [1.0, -3.0, 3.0, 1.0] ## Ensure the width is (0, Inf). bounds = [None, None, (1e-6, None), (-10, 10)] ## Run fitting routine output = levmar.levmar(gauss, p0, y, args=(x,), bounds=bounds, full_output=True) ## Print the result print(":Expected:") print("{0[0]:9f} {0[1]:9f} {0[2]:9f}".format(pt)) print(":Estimate:") print("{0[0]:9f} {0[1]:9f} {0[2]:9f}".format(output.p)) print("") print(" Summary ".center(60, '*')) output.pprint() print(''.center(60, '*')) ## Plot the result
def jac_expf(p, x): jac = np.empty((x.shape[0], 3)) jac[:, 0] = np.exp(-p[1] * x) jac[:, 1] = -p[0] * x * np.exp(-p[1] * x) jac[:, 2] = np.ones(x.size) return jac # Create input data x_mea = np.arange(40, dtype=np.float64) p_tru = [5.0, 0.1, 1.0] y_tru = expf(p_tru, x_mea) y_mea = y_tru + 0.2 * np.random.randn(x_mea.size) # Initial estimate p_ini = [1.0, 0.5, 0.5] # Fitting with analytic Jacobian p_opt, p_cov, info = levmar.levmar(expf, p_ini, y_mea, args=(x_mea, ), jacf=jac_expf) # Print the result print("Expected:") print("{0[0]:9f} {0[1]:9f} {0[2]:9f}".format(p_tru)) print("Estimate:") print("{0[0]:9f} {0[1]:9f} {0[2]:9f}".format(p_opt)) print("")
jac[:,0] = np.exp(-p[1]*x) jac[:,1] = -p[0] * x * np.exp(-p[1]*x) jac[:,2] = np.ones(x.size) return jac ## Create input data x = np.arange(40, dtype=np.float64) pt = [5.0, 0.1, 1.0] yt = expf(pt, x) y = yt + 0.2 * np.random.randn(x.size) ## Initial estimate p0 = [1.0, 0.5, 0.5] ## Fitting with analytic Jacobian output = levmar.levmar(expf, p0, y, jacf=jac_expf, args=(x,), full_output=True) ## Print the result print(":Expected:") print("{0[0]:9f} {0[1]:9f} {0[2]:9f}".format(pt)) print(":Estimate:") print("{0[0]:9f} {0[1]:9f} {0[2]:9f}".format(output.p)) print("") print(" Summary ".center(60, '*')) output.pprint() print(''.center(60, '*')) ## Plot the result
f[idx:] = psd_voigt((1, xc, w_r, m), x[idx:]) return peak * f + const ## Create input data x = np.linspace(-6, 6) pt = [300, -0.4, 2.0, 3.0, 0.4, 0.1] yt = asym_psd_voigt(pt, x) y = yt + np.sqrt(yt) * np.random.randn(x.size) ## Initial estimate p0 = [100, -1.0, 3.0, 4.0, 0.3, 0.2] ## Ensure the widths are (0, Inf), and the mixing ratio is [0, 1]. bounds = [(None, 5e+2), None, (1e-6, None), (1e-6, None), (1e-6, 1), (-10, 10)] ## Run fitting routine output = levmar.levmar(asym_psd_voigt, p0, y, args=(x,), bounds=bounds, full_output=True) ## Print the result print(":Expected:") print("{0[0]:9f} {0[1]:9f} {0[2]:9f}".format(pt)) print(":Estimate:") print("{0[0]:9f} {0[1]:9f} {0[2]:9f}".format(output.p)) print("") print(" Summary ".center(60, '*')) output.pprint() print(''.center(60, '*')) ## Plot the result