def NIST_Dataset(DataSet, method='leastsq', start='start2', plot=False, verbose=False): NISTdata = ReadNistData(DataSet) resid, npar, dimx = Models[DataSet] y = NISTdata['y'] x = NISTdata['x'] params = Parameters() for i in range(npar): pname = 'b%i' % (i + 1) cval = NISTdata['cert_values'][i] cerr = NISTdata['cert_stderr'][i] pval1 = NISTdata[start][i] params.add(pname, value=pval1) myfit = minimize(resid, params, method=method, args=(x, ), kws={'y': y}) digs, buff = Compare_NIST_Results(DataSet, myfit, myfit.params, NISTdata) if verbose: print(buff) return digs > 1
def NIST_Test(DataSet, start='start2', plot=True): NISTdata = ReadNistData(DataSet) resid, npar, dimx = Models[DataSet] y = NISTdata['y'] x = NISTdata['x'] vals = [] for i in range(npar): pval1 = NISTdata[start][i] vals.append(pval1) maxfev = 2500 * (npar + 1) factor = 100 xtol = 1.e-14 ftol = 1.e-14 epsfcn = 1.e-13 gtol = 1.e-14 diag = None print(" Fit with: ", factor, xtol, ftol, gtol, epsfcn, diag) _best, out, ier = _minpack._lmdif(resid, vals, (x, y), 1, ftol, xtol, gtol, maxfev, epsfcn, factor, diag) digs = Compare_NIST_Results(DataSet, _best, NISTdata) if plot and HASPYLAB: fit = -resid(_best, x, ) plt.plot(x, y, 'ro') plt.plot(x, fit, 'k+-') plt.show() return digs > 2
def NIST_Test(DataSet, method='leastsq', start='start2', plot=True): NISTdata = ReadNistData(DataSet) resid, npar, dimx = Models[DataSet] y = NISTdata['y'] x = NISTdata['x'] params = Parameters() for i in range(npar): pname = 'b%i' % (i + 1) cval = NISTdata['cert_values'][i] cerr = NISTdata['cert_stderr'][i] pval1 = NISTdata[start][i] params.add(pname, value=pval1) myfit = minimize(resid, params, method=method, args=(x, ), kws={'y': y}) digs = Compare_NIST_Results(DataSet, myfit, params, NISTdata) if plot and HASPYLAB: fit = -resid( params, x, ) pylab.plot(x, y, 'ro') pylab.plot(x, fit, 'k+-') pylab.show() return digs > 2
def NIST_Dataset(DataSet, method='leastsq', start='start2', plot=False, verbose=False): NISTdata = ReadNistData(DataSet) resid, npar, dimx = Models[DataSet] y = NISTdata['y'] x = NISTdata['x'] params = Parameters() for i in range(npar): pname = 'b%i' % (i + 1) pval1 = NISTdata[start][i] params.add(pname, value=pval1) try: myfit = minimize(resid, params, method=method, args=(x, ), kws={'y': y}, nan_policy='raise') except ValueError: if verbose: print("Fit failed... nans?") return False digs, buff = Compare_NIST_Results(DataSet, myfit, myfit.params, NISTdata) if verbose: print(buff) return digs > 2
def NIST_Test(DataSet, start='start2', plot=True): NISTdata = ReadNistData(DataSet) resid, npar, dimx = Models[DataSet] y = NISTdata['y'] x = NISTdata['x'] vals = [] for i in range(npar): pname = 'b%i' % (i + 1) cval = NISTdata['cert_values'][i] cerr = NISTdata['cert_stderr'][i] pval1 = NISTdata[start][i] vals.append(pval1) maxfev = 2500 * (npar + 1) factor = 100 xtol = 1.e-14 ftol = 1.e-14 epsfcn = 1.e-13 gtol = 1.e-14 diag = None print(" Fit with: ", factor, xtol, ftol, gtol, epsfcn, diag) # _best, out, ier = _minpack._lmdif(resid, vals, (x, y), 1, # ftol, xtol, gtol, # maxfev, epsfcn, factor, diag) from scipy.optimize import least_squares res = least_squares(resid, vals, args=(x, y), ftol=ftol, xtol=xtol, gtol=gtol, diff_step=np.sqrt(epsfcn), scaling=1., method='dogbox') _best = res.x digs = Compare_NIST_Results(DataSet, _best, NISTdata) if plot and HASPYLAB: fit = -resid( _best, x, ) pylab.plot(x, y, 'ro') pylab.plot(x, fit, 'k+-') pylab.show() return digs > 2
def RunNIST_Model(model): dset = ReadNistData(model) func, npar, dimx = Models[model] rss = (func(dset['cert_values'], x=dset['x'], y=dset['y'])**2).sum() tiny_rss = 1.e-16 print(rss, dset['sum_squares'], tiny_rss) if dset['sum_squares'] < tiny_rss: assert rss < tiny_rss else: assert_allclose(rss, dset['sum_squares']) out1 = NIST_Dataset(model, start='start1', plot=False, verbose=False) out2 = NIST_Dataset(model, start='start2', plot=False, verbose=False) assert (out1 or out2) return out1 or out2
def NIST_Test(DataSet, start='start2', plot=True): NISTdata = ReadNistData(DataSet) resid, npar, dimx = Models[DataSet] y = NISTdata['y'] x = NISTdata['x'] params = [] param_names = [] for i in range(npar): pname = 'b%i' % (i+1) cval = NISTdata['cert_values'][i] cerr = NISTdata['cert_stderr'][i] pval1 = NISTdata[start][i] params.append(pval1) param_names.append(pname) # myfit = Minimizer(resid, params, fcn_args=(x,), fcn_kws={'y':y}, # scale_covar=True) # print 'lsout ', params lsout = leastsq(resid, params, args=(x, y), full_output=True) print 'lsout ', lsout print params , len(x), len(y) digs = Compare_NIST_Results(DataSet, params, NISTdata) if plot and HASPYLAB: fit = -resid(params, x, ) pylab.plot(x, y, 'r+-') pylab.plot(x, fit, 'ko--') pylab.show() return digs > 2
def NIST_Test(DataSet, start='start2', plot=True): NISTdata = ReadNistData(DataSet) resid, npar, dimx = Models[DataSet] y = NISTdata['y'] x = NISTdata['x'] params = Parameters() for i in range(npar): pname = 'b%i' % (i + 1) cval = NISTdata['cert_values'][i] cerr = NISTdata['cert_stderr'][i] pval1 = NISTdata[start][i] params.add(pname, value=pval1) myfit = Minimizer(resid, params, fcn_args=(x, ), fcn_kws={'y': y}, scale_covar=True) myfit.prepare_fit() myfit.leastsq() digs = Compare_NIST_Results(DataSet, myfit, params, NISTdata) if plot and HASPYLAB: fit = -resid( params, x, ) pylab.plot(x, y, 'r+') pylab.plot(x, fit, 'ko--') pylab.show() return digs > 2