def _leastSquaresFit(model, parameters, data, max_iterations=None, stopping_limit = 0.005): """General non-linear least-squares fit using the Levenberg-Marquardt algorithm and automatic derivatives.""" n_param = len(parameters) p = () i = 0 for param in parameters: p = p + (_DerivVar(param, i),) i = i + 1 id = num.identity(n_param) l = 0.001 chi_sq, alpha = _chiSquare(model, p, data) niter = 0 while 1: delta = solveLinEq(alpha+l*num.diagonal(alpha)*id,-0.5*num.array(chi_sq[1])) next_p = map(lambda a,b: a+b, p, delta) next_chi_sq, next_alpha = _chiSquare(model, next_p, data) if next_chi_sq > chi_sq: l = 10.*l else: l = 0.1*l if chi_sq[0] - next_chi_sq[0] < stopping_limit: break p = next_p chi_sq = next_chi_sq alpha = next_alpha niter = niter + 1 if max_iterations is not None and niter == max_iterations: pass return map(lambda p: p[0], next_p), next_chi_sq[0]
def _leastSquare(self, data, models, iterLimit=None, chiLimit=1e-3): """Least-square fitting. Adapted from the original code by Konrad Hinsen.""" normf = 100.0 / numpy.max(data) data *= normf params = [50.0] * len(models) id = numpy.identity(len(params)) chisq, alpha = self._chiSquare(data, models, params) l = 0.001 niter = 0 while True: CHECK_FORCE_QUIT() niter += 1 delta = solveLinEq(alpha + l * numpy.diagonal(alpha) * id, -0.5 * numpy.array(chisq[1])) next_params = list(map(lambda a, b: a + b, params, delta)) for x in range(len(next_params)): if next_params[x] < 0.0: next_params[x] = 0.0 next_chisq, next_alpha = self._chiSquare(data, models, next_params) if next_chisq[0] > chisq[0]: l = 5.0 * l elif chisq[0] - next_chisq[0] < chiLimit: break else: l = 0.5 * l params = next_params chisq = next_chisq alpha = next_alpha if iterLimit and niter == iterLimit: break next_params /= normf return next_params
def _leastSquare(self, data, models, iterLimit=None, chiLimit=1e-3): """Least-square fitting. Adapted from the original code by Konrad Hinsen.""" normf = 100./numpy.max(data) data *= normf params = [50.] * len(models) id = numpy.identity(len(params)) chisq, alpha = self._chiSquare(data, models, params) l = 0.001 niter = 0 while True: CHECK_FORCE_QUIT() niter += 1 delta = solveLinEq(alpha+l*numpy.diagonal(alpha)*id,-0.5*numpy.array(chisq[1])) next_params = map(lambda a,b: a+b, params, delta) for x in range(len(next_params)): if next_params[x] < 0.: next_params[x] = 0. next_chisq, next_alpha = self._chiSquare(data, models, next_params) if next_chisq[0] > chisq[0]: l = 5.*l elif chisq[0] - next_chisq[0] < chiLimit: break else: l = 0.5*l params = next_params chisq = next_chisq alpha = next_alpha if iterLimit and niter == iterLimit: break next_params /= normf return next_params
def _leastSquaresFit(model, parameters, data, maxIterations=None, limit=1e-7): """General non-linear least-squares fit using the Levenberg-Marquardt algorithm and automatic derivatives. Originally developed by Konrad Hinsen. """ n_param = len(parameters) p = () i = 0 for param in parameters: p = p + (_DerivVar(param, i),) i = i + 1 id = numpy.identity(n_param) l = 0.001 chi_sq, alpha = _chiSquare(model, p, data) niter = 0 while True: niter += 1 delta = solveLinEq( alpha + l * numpy.diagonal(alpha) * id, -0.5 * numpy.array(chi_sq[1]) ) next_p = list(map(lambda a, b: a + b, p, delta)) next_chi_sq, next_alpha = _chiSquare(model, next_p, data) if next_chi_sq > chi_sq: l = 10.0 * l elif chi_sq[0] - next_chi_sq[0] < limit: break else: l = 0.1 * l p = next_p chi_sq = next_chi_sq alpha = next_alpha if maxIterations and niter == maxIterations: break return [p[0] for p in next_p], next_chi_sq[0]
def _leastSquaresFit(model, parameters, data, maxIterations=None, limit=1e-7): """General non-linear least-squares fit using the Levenberg-Marquardt algorithm and automatic derivatives. Originally developed by Konrad Hinsen. """ n_param = len(parameters) p = () i = 0 for param in parameters: p = p + (_DerivVar(param, i),) i = i + 1 id = numpy.identity(n_param) l = 0.001 chi_sq, alpha = _chiSquare(model, p, data) niter = 0 while True: niter += 1 delta = solveLinEq(alpha + l * numpy.diagonal(alpha) * id, -0.5 * numpy.array(chi_sq[1])) next_p = map(lambda a, b: a + b, p, delta) next_chi_sq, next_alpha = _chiSquare(model, next_p, data) if next_chi_sq > chi_sq: l = 10.0 * l elif chi_sq[0] - next_chi_sq[0] < limit: break else: l = 0.1 * l p = next_p chi_sq = next_chi_sq alpha = next_alpha if maxIterations and niter == maxIterations: break return map(lambda p: p[0], next_p), next_chi_sq[0]