def test_UnbinnedLH(self): f = gaussian assert list(describe(f)) == ['x', 'mean', 'sigma'] lh = UnbinnedLH(gaussian, self.data, ) assert list(describe(lh)) == ['mean', 'sigma'] assert_allclose(lh(0, 1), 28188.201229348757) minuit = iminuit.Minuit(lh) assert_allclose(minuit.errordef, 0.5)
def test_BinnedChi2(self): f = gaussian assert list(describe(f)) == ['x', 'mean', 'sigma'] lh = BinnedChi2(gaussian, self.data, bound=[-3, 3]) assert list(describe(lh)) == ['mean', 'sigma'] assert_allclose(lh(0, 1), 19951.005399882044, atol=1) minuit = iminuit.Minuit(lh) assert_allclose(minuit.errordef, 1.0)
def test_BinnedChi2(self): f = gaussian assert_equal(list(describe(f)), ['x', 'mean', 'sigma']) lh = BinnedChi2(gaussian, self.data, bound=[-3, 3]) assert_equal(list(describe(lh)), ['mean', 'sigma']) assert_almost_equal(lh(0, 1), 19951.005399882044, 1) minuit = iminuit.Minuit(lh) assert_equal(minuit.errordef, 1.0)
def test_UnbinnedLH(self): f = gaussian assert_equal(list(describe(f)), ['x', 'mean', 'sigma']) lh = UnbinnedLH(gaussian, self.data,) assert_equal(list(describe(lh)), ['mean', 'sigma']) assert_almost_equal(lh(0, 1), 28188.201229348757) minuit = iminuit.Minuit(lh) assert_equal(minuit.errordef, 0.5)
def test_BinnedLH(self): # write a better test... this depends on subtraction f = gaussian assert list(describe(f)) == ["x", "mean", "sigma"] lh = BinnedLH(gaussian, self.data, bound=[-3, 3]) assert list(describe(lh)) == ["mean", "sigma"] assert_allclose(lh(0, 1), 20.446130781601543, atol=1) minuit = iminuit.Minuit(lh) assert_allclose(minuit.errordef, 0.5)
def test_BinnedLH(self): # write a better test... this depends on subtraction f = gaussian assert list(describe(f)) == ['x', 'mean', 'sigma'] lh = BinnedLH(gaussian, self.data, bound=[-3, 3]) assert list(describe(lh)) == ['mean', 'sigma'] assert_allclose(lh(0, 1), 20.446130781601543, atol=1) minuit = iminuit.Minuit(lh) assert_allclose(minuit.errordef, 0.5)
def test_BinnedLH(self): # write a better test... this depends on subtraction f = gaussian assert_equal(list(describe(f)), ['x', 'mean', 'sigma']) lh = BinnedLH(gaussian, self.data, bound=[-3, 3]) assert_equal(list(describe(lh)), ['mean', 'sigma']) assert_almost_equal(lh(0, 1), 20.446130781601543, 1) minuit = iminuit.Minuit(lh) assert_equal(minuit.errordef, 0.5)
def test_UnbinnedLH(self): f = gaussian assert_equal(list(describe(f)), ['x', 'mean', 'sigma']) lh = UnbinnedLH( gaussian, self.data, ) assert_equal(list(describe(lh)), ['mean', 'sigma']) assert_almost_equal(lh(0, 1), 28188.201229348757) minuit = iminuit.Minuit(lh) assert_equal(minuit.errordef, 0.5)
def test_UnbinnedLH(self): f = gaussian assert list(describe(f)) == ['x', 'mean', 'sigma'] lh = UnbinnedLH( gaussian, self.data, ) assert list(describe(lh)) == ['mean', 'sigma'] assert_allclose(lh(0, 1), 28188.201229348757) minuit = iminuit.Minuit(lh) assert_allclose(minuit.errordef, 0.5)
def test_polynomial(): p = pdf.Polynomial(1) assert describe(p) == ['x', 'c_0', 'c_1'] assert_allclose(p(2, 2, 1), 4) integral = p.integrate((0, 1), 1, 2, 1) assert_allclose(integral, 2.5) p = pdf.Polynomial(2) assert describe(p) == ['x', 'c_0', 'c_1', 'c_2'] assert_allclose(p(2, 3, 4, 5), 31) integral = p.integrate((2, 10), 10, 1, 2, 3) analytical = 8 + 2 / 2. * (10 ** 2 - 2 ** 2) + 3 / 3. * (10 ** 3 - 2 ** 3) assert_allclose(integral, analytical)
def test_polynomial(): p = pdf.Polynomial(1) assert_equal(describe(p), ['x', 'c_0', 'c_1']) assert_equal(p(2, 2, 1), 4) integral = p.integrate((0, 1), 1, 2, 1) assert_equal(integral, 2.5) p = pdf.Polynomial(2) assert_equal(describe(p), ['x', 'c_0', 'c_1', 'c_2']) assert_equal(p(2, 3, 4, 5), 31) integral = p.integrate((2, 10), 10, 1, 2, 3) analytical = 8 + 2 / 2.*(10 ** 2 - 2 ** 2) + 3 / 3.*(10 ** 3 - 2 ** 3) assert_equal(integral, analytical)
def test_Chi2Regression(self): x = np.linspace(1, 10, 10) y = 10 * x + 1 f = linear assert list(describe(f)) == ["x", "m", "c"] lh = Chi2Regression(f, x, y) assert list(describe(lh)) == ["m", "c"] assert_allclose(lh(10, 1), 0) assert_allclose(lh(10, 0), 10.0) minuit = iminuit.Minuit(lh) assert_allclose(minuit.errordef, 1.0)
def test_Chi2Regression(self): x = np.linspace(1, 10, 10) y = 10 * x + 1 f = linear assert_equal(list(describe(f)), ['x', 'm', 'c']) lh = Chi2Regression(f, x, y) assert_equal(list(describe(lh)), ['m', 'c']) assert_almost_equal(lh(10, 1), 0) assert_almost_equal(lh(10, 0), 10.) minuit = iminuit.Minuit(lh) assert_equal(minuit.errordef, 1.0)
def test_Chi2Regression(self): x = np.linspace(1, 10, 10) y = 10 * x + 1 f = linear assert list(describe(f)) == ['x', 'm', 'c'] lh = Chi2Regression(f, x, y) assert list(describe(lh)) == ['m', 'c'] assert_allclose(lh(10, 1), 0) assert_allclose(lh(10, 0), 10.) minuit = iminuit.Minuit(lh) assert_allclose(minuit.errordef, 1.0)
def merge_func_code(*arg): """ merge function arguments.:: def f(x,y,z): return do_something(x,y,z) def g(x,z,p): return do_something(x,y,z) fc, pos = merge_func_code(f,g) #fc is now ('x','y','z','p') """ all_arg = [] for i, f in enumerate(arg): tmp = [] first = False for vn in describe(f): newv = vn first = False tmp.append(newv) all_arg.append(tmp) #FIXME: do something smarter merge_arg = [] for a in all_arg: for v in a: if v not in merge_arg: merge_arg.append(v) #build the map list of numpy int array pos = [] for a in all_arg: tmp = [] for v in a: tmp.append(merge_arg.index(v)) pos.append(np.array(tmp, dtype=np.int)) return MinimalFuncCode(merge_arg), pos
def __init__(self, model, x, y, dx, dy): self.model = model # model predicts y value for given x value self.x = np.array(x) # the x values self.y = np.array(y) # the y values self.dx = np.array(dx) # the x-axis uncertainties self.dy = np.array(dy) # the y-axis uncertainties self.func_code = make_func_code(describe(self.model)[1:])
def __init__(self, f, data, weights=None, bound=None, badvalue=-100000, extended=False, extended_bound=None, extended_nint=100): if bound is not None: data = np.array(data) mask = (data >= bound[0]) & (data <= bound[1]) data = data[mask] if (weights is not None): weights = weights[mask] self.f = f # model predicts PDF for given x self.data = np.array(data) self.weights = set_var_if_None(weights, self.data) self.bad_value = badvalue self.extended = extended self.extended_bound = extended_bound self.extended_nint = extended_nint if extended and extended_bound is None: self.extended_bound = (np.min(data), np.max(data)) self.func_code = make_func_code(describe(self.f)[1:])
def _validate_minuit_args(cost_func: Union[cost_function.CostFunctionBase, cost_function.SimultaneousFit], minuit_args: T_FitArguments) -> None: """ Validate the arguments provided for Minuit. Checks that there are sufficient and valid arguments for each parameter in the fit function. Args: cost_func: Cost function to be used with Minuit. minuit_args: Arguments for minuit. Need to set the initial value, limits, and error (step) of each parameter. Returns: None. An exception is raised if there's a problem with any of the arguments. """ # Need the parameters in the function to check the arguments. Recall that "x" is skipped because it's a cost # function (which is useful, because we want to skip x since it's not a parameter). parameters = iminuit.describe(cost_func) # Loop over the available parameters because each one needs to be specified in the Minuit args. for p in parameters: if p in minuit_args: if f"limit_{p}" not in minuit_args: raise ValueError(f"Limits on parameter '{p}' must be specified.") if f"error_{p}" not in minuit_args: raise ValueError(f"Initial error on parameter '{p}' must be specified.") # If p and limit_p and error_p were specified, then the parameter is fully specified. elif p.replace("fix", "") in minuit_args: # The parameter is fixed and therefore needs no further specification. pass else: # The parameter wasn't specified. raise ValueError(f"Parameter '{p}' must be specified in the fit arguments.")
def test_doublecrystalball(): assert describe(pdf.doublecrystalball) == [ "x", "alpha", "alpha2", "n", "n2", "mean", "sigma", ] assert_allclose(pdf.doublecrystalball(10, 1, 1, 2, 2, 10, 2), 1.0) assert_allclose(pdf.doublecrystalball(11, 1, 1, 2, 2, 10, 2), 0.8824969025845955) assert_allclose(pdf.doublecrystalball(12, 1, 1, 2, 2, 10, 2), 0.6065306597126334) assert_allclose(pdf.doublecrystalball(14, 1, 1, 2, 2, 10, 2), 0.26956918209450376) assert_allclose(pdf.doublecrystalball(6, 1, 1, 2, 2, 10, 2), 0.26956918209450376) assert_allclose(pdf.doublecrystalball(-10, 1, 5, 3, 4, 10, 2), 0.00947704155801) assert_allclose(pdf.doublecrystalball(0, 1, 5, 3, 4, 10, 2), 0.047744395954055) assert_allclose(pdf.doublecrystalball(11, 1, 5, 3, 4, 10, 2), 0.8824969025846) assert_allclose(pdf.doublecrystalball(20, 1, 5, 3, 4, 10, 2), 0.0000037266531720786) assert_allclose(pdf.doublecrystalball(25, 1, 5, 3, 4, 10, 2), 0.00000001287132228271)
def fit_fmpe(x, y, y_err, init_params, limit_params): chi2 = Chi2Regression(fmpe_pdf_10, x=x, y=y, error=y_err) bin_width = np.diff(x) bin_width = np.mean(bin_width) params = describe(fmpe_pdf_10, verbose=False)[1:] fixed_params = {} for param in params: if param not in init_params.keys(): fixed_params['fix_{}'.format(param)] = True m = Minuit( chi2, **init_params, **limit_params, **fixed_params, bin_width=bin_width, print_level=0, pedantic=False, ) m.migrad() return m
def test_crystalball(): assert describe(pdf.crystalball) == ['x', 'alpha', 'n', 'mean', 'sigma'] assert_allclose(pdf.crystalball(10, 1, 2, 10, 2), 1.) assert_allclose(pdf.crystalball(11, 1, 2, 10, 2), 0.8824969025845955) assert_allclose(pdf.crystalball(12, 1, 2, 10, 2), 0.6065306597126334) assert_allclose(pdf.crystalball(14, 1, 2, 10, 2), 0.1353352832366127) assert_allclose(pdf.crystalball(6, 1, 2, 10, 2), 0.26956918209450376)
def test_crystalball(): assert describe(pdf.crystalball) == ["x", "alpha", "n", "mean", "sigma"] assert_allclose(pdf.crystalball(10, 1, 2, 10, 2), 1.0) assert_allclose(pdf.crystalball(11, 1, 2, 10, 2), 0.8824969025845955) assert_allclose(pdf.crystalball(12, 1, 2, 10, 2), 0.6065306597126334) assert_allclose(pdf.crystalball(14, 1, 2, 10, 2), 0.1353352832366127) assert_allclose(pdf.crystalball(6, 1, 2, 10, 2), 0.26956918209450376)
def test_doublegaussian(): assert_equal(tuple(describe(pdf.doublegaussian)), ('x', 'mean', 'sigma_L', 'sigma_R')) assert_almost_equal(pdf.doublegaussian(0., 0., 1., 2.), 1.) assert_almost_equal(pdf.doublegaussian(-1., 0., 1., 2.), 0.6065306597126334) assert_almost_equal(pdf.doublegaussian(1., 0., 1., 2.), 0.8824969025845955)
def __init__(self, f, data): self.f = f self.data = data f_sig = describe(f) # this is how you fake function # signature dynamically self.func_code = make_func_code(f_sig[1:]) # docking off independent variable self.func_defaults = None # this keeps np.vectorize happy
def __init__(self, model, x, y, cov_inv, regulator=0, regulator_val=1): self.model = model self.regulator = regulator self.regulator_val = regulator_val self.x = np.array(x) self.y = np.array(y) self.cov_inv = np.array(cov_inv) self.func_code = make_func_code(describe(self.model)[1:])
def test_crystalball(): assert_equal(tuple(describe(pdf.crystalball)), ('x', 'alpha', 'n', 'mean', 'sigma')) assert_almost_equal(pdf.crystalball(10, 1, 2, 10, 2), 1.) assert_almost_equal(pdf.crystalball(11, 1, 2, 10, 2), 0.8824969025845955) assert_almost_equal(pdf.crystalball(12, 1, 2, 10, 2), 0.6065306597126334) assert_almost_equal(pdf.crystalball(14, 1, 2, 10, 2), 0.1353352832366127) assert_almost_equal(pdf.crystalball(6, 1, 2, 10, 2), 0.26956918209450376)
def __init__(self, f, x, y, y_err): self.x = x self.y = y self.y_err = y_err self.f = f args = describe(f) # extract function signature self.func_code = Struct( co_varnames=args[1:], # dock off independent param co_argcount=len(args) - 1)
def test_cruijff(): assert describe(pdf.cruijff) == ['x', 'm_0', 'sigma_L', 'sigma_R', 'alpha_L', 'alpha_R'] val = pdf.cruijff(0, 0, 1., 2., 1., 2.) assert_allclose(val, 1.) vl = pdf.cruijff(0, 1, 1., 1., 2., 2.) vr = pdf.cruijff(2, 1, 1., 1., 2., 2.) assert_allclose(vl, vr) assert_allclose(vl, 0.7788007830714) assert_allclose(vr, 0.7788007830714)
def __init__(self, f, x, y, sy=None, weights=None): self.f = f # model predicts y for given x self.x = np.array(x) self.y = np.array(y) self.sy = set_var_if_None(sy, self.x) self.weights = set_var_if_None(weights, self.x) self.func_code = make_func_code(describe(self.f)[1:])
def __init__(self, data1D, func): self.data = data1D self.pdf = func # take function signature func_signature = iminuit.describe(func) # overwrite func_code to provide func signature self.func_code = iminuit.util.make_func_code(func_signature[1:])
def __init__(self, f, data, bins=40, weights=None, weighterrors=None, bound=None, badvalue=1000000, extended=False, use_w2=False, nint_subdiv=1): if bound is not None: data = np.array(data) mask = (data >= bound[0]) & (data <= bound[1]) data = data[mask] if (weights is not None): weights = weights[mask] if (weighterrors is not None): weighterrors = weighterrors[mask] self.weights = set_var_if_None(weights, data) self.f = f self.use_w2 = use_w2 self.extended = extended if bound is None: bound = (np.min(data), np.max(data)) self.mymin, self.mymax = bound h, self.edges = np.histogram(data, bins, range=bound, weights=weights) self.bins = bins self.h = h self.N = np.sum(self.h) if weights is not None: if weighterrors is None: self.w2, _ = np.histogram(data, bins, range=bound, weights=weights**2) else: self.w2, _ = np.histogram(data, bins, range=bound, weights=weighterrors**2) else: self.w2, _ = np.histogram(data, bins, range=bound, weights=None) self.badvalue = badvalue self.nint_subdiv = nint_subdiv self.func_code = make_func_code(describe(self.f)[1:]) self.ndof = np.sum(self.h > 0) - (self.func_code.co_argcount - 1)
def __init__(self,f,x,y): self.f = f self.x = x self.y = y f_sig = describe(f) #this is how you fake function #signature dynamically self.func_code = make_func_code(f_sig[1:])#docking off independent variable self.func_defaults = None #this keeps np.vectorize happy
def test_cruijff(): iterable_equal(tuple(describe(pdf.cruijff)), ('x', 'm_0', 'sigma_L', 'sigma_R', 'alpha_L', 'alpha_R')) val = pdf.cruijff(0, 0, 1., 2., 1., 2.) assert_almost_equal(val, 1.) vl = pdf.cruijff(0, 1, 1., 1., 2., 2.) vr = pdf.cruijff(2, 1, 1., 1., 2., 2.) assert_almost_equal(vl, vr, msg='symmetric test') assert_almost_equal(vl, 0.7788007830714) assert_almost_equal(vr, 0.7788007830714)
def __init__(self, model, x, y, dx, dy): self.model = model # model predicts y value for given x value self.x = np.array(x) # the x values self.y = np.array(y) # the y values self.dx = np.array(dx) # the x-axis uncertainties self.dy = np.array(dy) # the y-axis uncertainties self.func_code = make_func_code(describe(self.model)[1:]) self.h = ( x[-1] - x[0] ) / 10000 # this is the step size for the numerical calculation of the df/dx = last value in x (x[-1]) - first value in x (x[0])/10000
def PerformFitToBKG(): print 'About to start MINUIT ' stepSigma = 0.01 m = Minuit(Chi2_BKG, v2_t=0.20, limit_v2_t=(0, 0.50), error_v2_t=0.01, v2_a=0.10, limit_v2_a=(0, 0.50), error_v2_a=0.01, v4_t=0.05, limit_v4_t=(0, 0.10), error_v4_t=0.01, v4_a=0.05, limit_v4_a=(0, 0.10), error_v4_a=0.01, V3=0, limit_V3=(0, 0.1), error_V3=0.01, B=2.0, limit_B=(0.1, 10.0), error_B=0.01) m.migrad() print ' Describe ', describe(m) #m.minos() #print ' values' #print(m.values) # {'x': 2,'y': 3,'z': 4} #print ' errors' #print(m.errors) # {'x': 1,'y': 1,'z': 1} #print('covariance', m.covariance) #print('matrix()', m.matrix()) #covariance #print('matrix(correlation=True)', m.matrix(correlation=True)) #correlation #m.print_matrix() #correlation axes = {} f, (axes["Inplane"], axes["Midplane"], axes["Outplane"], axes["AllAngles"]) = plt.subplots(1, 4, sharex=True, sharey=True) for key in data.keys(): x = data[key]["Bkg"]["x_center"] # y = data[key]["Bkg"]["y"] dy = data[key]["Bkg"]["dy"] axes[key].errorbar(x, y, yerr=dy, fmt='o') xfit = np.linspace(-0.5, 1.5, num=50, endpoint=True) model = Background(xfit, m.values, phi_s[key], c[key]) axes[key].plot(xfit, model, '-r') ##plot fit function x = data[key]["All"]["x_center"] y = data[key]["All"]["y"] dy = data[key]["All"]["dy"] axes[key].errorbar(x, y, yerr=dy, fmt='o') plt.show()
def test_johnsonSU(): assert describe(pdf.johnsonSU), ["x", "mean", "sigma", "nu", "tau"] assert_allclose(pdf.johnsonSU(1.0, 1.0, 1.0, 1.0, 1.0), 0.5212726124342) assert_allclose(pdf.johnsonSU(1.0, 2.0, 1.0, 1.0, 1.0), 0.1100533373219) assert_allclose(pdf.johnsonSU(1.0, 2.0, 2.0, 1.0, 1.0), 0.4758433826682) j = pdf.johnsonSU assert hasattr(j, "integrate") integral = j.integrate((-100, 100), 0, 1.0, 1.0, 1.0, 1.0) assert_allclose(integral, 1.0) integral = j.integrate((0, 2), 0, 1.0, 1.0, 1.0, 1.0) assert_allclose(integral, 0.8837311663857358)
def __init__(self,f,x,y): self.f = f self.x = x self.y = y f_sig = describe(f) #this is how you fake function #signature dynamically self.func_code = Struct( co_varnames = f_sig[1:], #dock off independent variable co_argcount = len(f_sig)-1 ) self.func_defaults = None #this keeps np.vectorize happy
def __init__(self, chi2, q_bins, dG, m_P, m_D, m_Pr, t0, num_var): self.chi2 = chi2 self.q_bins = q_bins self.dG = dG self.m_P = m_P self.m_D = m_D self.m_Pr = m_Pr self.t0 = t0 self.num_var = num_var args = minuit.describe(chi2) self.func_code = minuit.Struct( co_varnames = args[len(args)-self.num_var:], co_argcount = len(args)-self.num_var )
def test_simultaneous(self): np.random.seed(0) data = np.random.randn(10000) shifted = data + 3. g1 = rename(gaussian, ['x', 'lmu', 'sigma']) g2 = rename(gaussian, ['x', 'rmu', 'sigma']) ulh1 = UnbinnedLH(g1, data) ulh2 = UnbinnedLH(g2, shifted) sim = SimultaneousFit(ulh1, ulh2) assert describe(sim) == ['lmu', 'sigma', 'rmu'] minuit = iminuit.Minuit(sim, sigma=1.2, pedantic=False, print_level=0) minuit.migrad() assert minuit.migrad_ok() assert_allclose(minuit.values['lmu'], 0., atol=2 * minuit.errors['lmu']) assert_allclose(minuit.values['rmu'], 3., atol=2 * minuit.errors['rmu']) assert_allclose(minuit.values['sigma'], 1., atol=2 * minuit.errors['sigma'])
def _setup_fitter_kwargs(self, fitarg, kwargs=None): """Setup initial fitter kwargs Use this to pass default fitargs and parameter names to Minuit. This allows to initialize a Model class with only a fitfunc and no boilerplate chi2 function. """ # This gurantees correct oder and names of fitparameters # we start at 1 because running value (x or t) must be skipped self.parameter_names = describe(self.fit_func)[1:] # The oder of parameters is important fitarg['forced_parameters'] = self.parameter_names if not kwargs: kwargs = {} if not kwargs.get('fitarg'): kwargs['fitarg'] = {} kwargs['fitarg'] = {**fitarg, **kwargs['fitarg']} # DODO add check that fitargs and parameter_names fit together return kwargs
def fit_dist(self,data,pinit = {}, error = {}, fix = {}, limit = {}): self.data = data pars = iminuit.describe(self.func) if not len(fix.keys()): for k in pars: fix[k] = 0 if not len(pinit.keys()): for k in pars: pinit[k] = 1. if not len(error.keys()): for k in pars: error[k] = pinit[k] * self.int_steps if not len(limit.keys()): for k in pars: limit[k] = [pinit[k] / 1e5, pinit[k] * 1e5] fitarg = {} for k in pars: fitarg[k] = pinit[k] fitarg['error_{0:s}'.format(k)] = error[k] fitarg['fix_{0:s}'.format(k)] = fix[k] fitarg['limit_{0:s}'.format(k)] = limit[k] m = iminuit.Minuit(self.func, errordef = self.errordef, pedantic = self.pedantic, print_level = self.print_level, **fitarg ) m.tol = self.tol m.strategy = self.strategy m.migrad(ncall = self.ncall) return (kstest(self.data,self.distr,args = m.values.values(), mode = self.ksmode)), m.values, m.errors
def test_poly2(): assert_equal(describe(pdf.poly2), ['x', 'a', 'b', 'c']) assert_almost_equal(pdf.poly2(2, 3, 4, 5), 25)
def test_linear(): assert_equal(describe(pdf.linear), ['x', 'm', 'c']) assert_almost_equal(pdf.linear(1, 2, 3), 5) assert(hasattr(pdf.linear, 'integrate')) integral = pdf.linear.integrate((0., 1.), 1, 1, 1) assert_equal(integral, 1.5)
def test_argus(): assert_equal(tuple(describe(pdf.argus)), ('x', 'c', 'chi', 'p')) assert_almost_equal(pdf.argus(6., 10, 2, 3), 0.004373148605400128) assert_almost_equal(pdf.argus(10., 10, 2, 3), 0.) assert_almost_equal(pdf.argus(8., 10, 2, 3), 0.0018167930603254737)
def test_poly2(): assert describe(pdf.poly2) == ['x', 'a', 'b', 'c'] assert_allclose(pdf.poly2(2, 3, 4, 5), 25)
def test_doublegaussian(): assert_equal( tuple(describe(pdf.doublegaussian)), ('x', 'mean', 'sigma_L', 'sigma_R')) assert_almost_equal(pdf.doublegaussian(0., 0., 1., 2.), 1.) assert_almost_equal(pdf.doublegaussian(-1., 0., 1., 2.), 0.6065306597126334) assert_almost_equal(pdf.doublegaussian(1., 0., 1., 2.), 0.8824969025845955)
def test_cauchy(): assert_equal(describe(pdf.cauchy), ['x', 'm', 'gamma']) assert_almost_equal(pdf.cauchy(1, 1, 1.), 0.3183098861837907) assert_almost_equal(pdf.cauchy(1, 1, 2.), 0.15915494309189535) assert_almost_equal(pdf.cauchy(1, 2, 4.), 0.07489644380795074)
def test_rtv_breitwigner(): assert_equal(describe(pdf.rtv_breitwigner), ['x', 'm', 'gamma']) assert_almost_equal(pdf.rtv_breitwigner(1, 1, 1.), 0.8194496535636714) assert_almost_equal(pdf.rtv_breitwigner(1, 1, 2.), 0.5595531041435416) assert_almost_equal(pdf.rtv_breitwigner(1, 2, 3.), 0.2585302502852219)
def test_novosibirsk(): assert_equal(describe(pdf.novosibirsk), ['x', 'width', 'peak', 'tail']) assert_almost_equal(pdf.novosibirsk(3, 2, 3, 4), 1.1253517471925912e-07)
def test_poly3(): assert_equal(describe(pdf.poly3), ['x', 'a', 'b', 'c', 'd']) assert_almost_equal(pdf.poly3(2, 3, 4, 5, 6), 56.)
# # We know easily that the answer has is # $x=1$, $y=2$, $z=3$ and the minimum value should be $-1$ # <codecell> def f(x,y,z): return (x-1.)**2 + (y-2*x)**2 + (z-3.*x)**2 -1. # <markdowncell> # iminuit relies on Python introspection. If you wonder what kind of function iminuit sees, you can use # <codecell> describe(f) # <markdowncell> # ###Contruct Minuit Object # To minimize we need to construct a Minuit object # <codecell> m=Minuit(f, x=2, error_x=0.2, limit_x=(-10.,10.), y=3., fix_y=True, print_level=1) # <markdowncell> # The initial value/error are optional but it's nice to do it # and here is how to use it #
def test_ugaussian(): assert_equal(tuple(describe(pdf.ugaussian)), ('x', 'mean', 'sigma')) assert_almost_equal(pdf.ugaussian(0, 0, 1), 1.) assert_almost_equal(pdf.ugaussian(-1, 0, 1), 0.6065306597126334) assert_almost_equal(pdf.ugaussian(1, 0, 1), 0.6065306597126334)
def test_gaussian(): assert_equal(tuple(describe(pdf.gaussian)), ('x', 'mean', 'sigma')) assert_almost_equal(pdf.gaussian(0, 0, 1), 0.3989422804014327) assert_almost_equal(pdf.gaussian(-1, 0, 1), 0.24197072451914337) assert_almost_equal(pdf.gaussian(1, 0, 1), 0.24197072451914337)
from iminuit import Minuit, describe # <codecell> %load_ext inumpy # <codecell> #define a functino to minimize def f(x,y,z): return (x-2)**2 + (y-3)**2 + (z-4)**2 # <codecell> #iminuit relies on python introspection to read function signature describe(f) # one of the most useful function from iminuit # <codecell> #notice here that it automatically knows about x,y,z #no RooRealVar etc. needed here m = Minuit(f) #it warns about every little thing that might go wrong # <codecell> #the most frequently asked question is Does my fit converge #also look at your console it print progress if you use print_level=2 m.migrad(); # <markdowncell>