def fmin_minuit(func, x0, names=None, verbose=False): inits = dict() if verbose: print_level = 2 else: print_level = 0 if names is None: names = map(lambda x: 'param' + str(x), range(len(x0))) else: assert(len(x0) == len(names)) for n, x in zip(names, x0): inits[n] = x # TODO use a method to set this correctly inits['error_' + n] = 1 m = Minuit(Min_Func(func, names), print_level=print_level, errordef=1, **inits) a, b = m.migrad() return OptimizeResult( x=m.values, fun=a['fval'], edm=a['edm'], nfev=a['nfcn'], is_valid=a['is_valid'], has_valid_parameters=a['has_valid_parameters'], )
def _fit_amplitude_minuit(counts, background, model, flux): """Fit amplitude using minuit. Parameters ---------- counts : `~numpy.ndarray` Slice of count map. background : `~numpy.ndarray` Slice of background map. model : `~numpy.ndarray` Model template to fit. flux : float Starting value for the fit. Returns ------- amplitude : float Fitted flux amplitude. niter : int Number of function evaluations needed for the fit. """ from iminuit import Minuit def stat(x): return f_cash(x, counts, background, model) minuit = Minuit(f_cash, x=flux, pedantic=False, print_level=0) minuit.migrad() return minuit.values['x'], minuit.ncalls
def optimise_minuit(target_time): from iminuit import Minuit def minimizer(on1, on2, on3, on4, on5, on6, on7, on8, on9, on10, on11, on12, delta1, delta2, delta3, delta4, delta5, delta6, delta7, delta8, delta9, delta10, delta11, delta12): ontimes = np.array([on1, on2, on3, on4, on5, on6, on7, on8, on9, on10, on11, on12]) offtimes = np.array([on1 + delta1, on2 + delta2, on3 + delta3, on4 + delta4, on5 + delta5, on6 + delta6, on7 + delta7, on8 + delta8, on9 + delta9, on10 + delta10, on11 + delta11, on12 + delta12]) #currents = [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12] currents = [243.]*12 flyer.prop.overwriteCoils(ontimes.ctypes.data_as(c_double_p), offtimes.ctypes.data_as(c_double_p)) flyer.preparePropagation(currents) flyer.propagate(0) pos = flyer.finalPositions[0] vel = flyer.finalVelocities[0] ind = np.where((pos[:, 2] > 268.) & (vel[:, 2] < 1.1*target_speed) & (vel[:, 2] > 0.9*target_speed))[0] # all particles that reach the end print 'good particles:', ind.shape[0] return -1.*ind.shape[0] initvals = {} for i in np.arange(12) + 1: initvals['on' + str(i)] = flyer.ontimes[i - 1] initvals['limit_on' + str(i)] = (0, 600) initvals['error_on' + str(i)] = 50 initvals['delta' + str(i)] = flyer.offtimes[i - 1] - flyer.ontimes[i - 1] initvals['limit_delta' + str(i)] = (0, 85) initvals['error_delta' + str(i)] = 5 #initvals['c' + str(i)] = 243. #initvals['limit_c' + str(i)] = (0, 300) #initvals['error_c' + str(i)] = 50 m = Minuit(minimizer, **initvals) m.set_strategy(2) m.migrad(ncall=20) print m.values
def test_minos_single_fixed_raising(): m = Minuit(func3, pedantic=False, print_level=0, fix_x=True) m.migrad() with warnings.catch_warnings(): warnings.simplefilter('error') ret = m.minos('x')
def test_minos_all(): m = Minuit(func3, pedantic=False, print_level=0) m.migrad() m.minos() assert_almost_equal(m.merrors[('x', -1.0)], -sqrt(5)) assert_almost_equal(m.merrors[('x', 1.0)], sqrt(5)) assert_almost_equal(m.merrors[('y', 1.0)], 1.)
def test_minos_single_fixed_result(): m = Minuit(func3, pedantic=False, print_level=0, fix_x=True) m.migrad() with warnings.catch_warnings(): warnings.simplefilter('ignore') ret = m.minos('x') assert_equal(ret, None)
def test_reverse_limit(): # issue 94 def f(x, y, z): return (x - 2) ** 2 + (y - 3) ** 2 + (z - 4) ** 2 m = Minuit(f, limit_x=(3., 2.), pedantic=False) m.migrad()
def test_fix_param(self): m = Minuit(func4,print_level=0) m.migrad() m.minos() val = m.values self.assertAlmostEqual(val['x'],2.) self.assertAlmostEqual(val['y'],5.) self.assertAlmostEqual(val['z'],7.) err = m.errors#second derivative # self.assertAlmostEqual(err['x'],5.) # self.assertAlmostEqual(err['y'],10.) # self.assertAlmostEqual(err['z'],4.) #now fix z = 10 m = Minuit(func4,print_level=-1,y=10.,fix_y=True) m.migrad() val = m.values self.assertAlmostEqual(val['x'],2.) self.assertAlmostEqual(val['y'],10.) self.assertAlmostEqual(val['z'],7.) self.assertAlmostEqual(m.fval,10.+2.5) free_param = m.list_of_vary_param() fix_param = m.list_of_fixed_param() self.assertIn('x', free_param) self.assertNotIn('x', fix_param) self.assertIn('y', fix_param) self.assertNotIn('y', free_param) self.assertNotIn('z', fix_param)
def functesthelper(self,f): m = Minuit(f,print_level=0) m.migrad() val = m.values self.assertAlmostEqual(val['x'],2.) self.assertAlmostEqual(val['y'],5.) self.assertAlmostEqual(m.fval,10.)
def test_rosenbrock(): random.seed(0.258) m = Minuit(rosenbrock, x=0, y=0, pedantic=False, print_level=1) m.tol = 1e-4 m.migrad() assert_less(m.fval, 1e-6) assert_almost_equal(m.values['x'], 1., places=3) assert_almost_equal(m.values['y'], 1., places=3)
def fit_spectrum(f, m, w): def chi2(a0): return numpy.sum(numpy.power(f - a0 * m, 2) * w) minuit = Minuit(chi2, a0=1.0, error_a0=1.0, print_level=-1, errordef=0.01) minuit.migrad() return minuit.values["a0"]
def test_extended_ulh(self): eg = Extended(gaussian) lh = UnbinnedLH(eg, self.data, extended=True, extended_bound=(-20, 20)) minuit = Minuit(lh, mean=4.5, sigma=1.5, N=19000., pedantic=False, print_level=0) minuit.migrad() assert_allclose(minuit.values['N'], 20000, atol=sqrt(20000.)) assert minuit.migrad_ok()
def test_printfmin_uninitialized(): # issue 85 def f(x): return 2 + 3 * x fitter = Minuit(f, pedantic=False) with pytest.raises(RuntimeError): fitter.print_fmin()
def test_rosenbrock(): random.seed(0.258) m = Minuit(rosenbrock, x=0, y=0, pedantic=False, print_level=1) m.tol = 1e-4 m.migrad() assert_allclose(m.fval, 0, atol=1e-6) assert_allclose(m.values['x'], 1., atol=1e-3) assert_allclose(m.values['y'], 1., atol=1e-3)
def test_matyas(): random.seed(0.258) m = Minuit(matyas, x=random.random(), y=random.random(), pedantic=False, print_level=0) m.migrad() assert m.fval < 1e-26 assert_array_almost_equal(m.args, [0, 0], decimal=12)
def m(): m = Minuit(f1, x=0, y=0, pedantic=False, print_level=1) m.tol = 1e-4 m.migrad() assert_allclose(m.fval, 0, atol=1e-6) assert_allclose(m.values['x'], 1, atol=1e-3) assert_allclose(m.values['y'], 1, atol=1e-3) return m
def test_extended_ulh_2(self): eg = Extended(gaussian) lh = UnbinnedLH(eg, self.data, extended=True) minuit = Minuit(lh, mean=4.5, sigma=1.5, N=19000., pedantic=False, print_level=0) minuit.migrad() assert(minuit.migrad_ok()) assert_almost_equal(minuit.values['N'], 20000, delta=sqrt(20000.))
def test_reverse_limit(): # issue 94 def f(x, y, z): return (x - 2) ** 2 + (y - 3) ** 2 + (z - 4) ** 2 m = Minuit(f, limit_x=(3., 2.), pedantic=False) with pytest.raises(ValueError): m.migrad()
def fit_spectrum(f,m,w,l): def chi2(a0,a1): return numpy.sum( numpy.power(f-(a0+a1*l)*m,2)*w ) minuit = Minuit(chi2,a0=1.,error_a0=1.,a1=0.001,error_a1=1.,print_level=-1, errordef=0.01) minuit.migrad() return minuit.values['a0'], minuit.values['a1']
def test_matyas_oneside(): '''One-side limit when the minimum is in the forbidden region''' random.seed(0.258) m = Minuit(matyas, x=2 + random.random(), y=random.random(), limit_x=(1, None), pedantic=False, print_level=0) m.migrad() assert_array_almost_equal(m.args, [1, 0.923], decimal=3)
def functesthelper(f): m = Minuit(f, print_level=0, pedantic=False) m.migrad() val = m.values assert_almost_equal(val['x'], 2.) assert_almost_equal(val['y'], 5.) assert_almost_equal(m.fval, 10.) assert(m.matrix_accurate()) assert(m.migrad_ok())
def test_beale(): random.seed(0.258) m = Minuit(beale, x=random.random(), y=0.5 * random.random(), pedantic=False, print_level=0) m.migrad() assert_array_almost_equal(m.args, [3, 0.5], decimal=3) assert m.fval < 1e-6
def test_matyas(): random.seed(0.258) m = Minuit(matyas, x=random.random(), y=random.random(), pedantic=False, print_level=0) m.tol = 1e-4 m.migrad() assert m.fval < 1e-26 assert_allclose(m.args, [0, 0], atol=1e-12)
def test_matyas_oneside(): """One-sided limit when the minimum is in the forbidden region""" random.seed(0.258) m = Minuit(matyas, x=2 + random.random(), y=random.random(), limit_x=(1, None), pedantic=False, print_level=0) m.tol = 1e-4 m.migrad() assert_allclose(m.args, [1, 0.923], atol=1e-3)
def test_beale(): random.seed(0.258) m = Minuit(beale, x=random.random(), y=0.5 * random.random(), pedantic=False, print_level=0) m.tol = 1e-4 m.migrad() assert_allclose(m.fval, 0, atol=1e-6) assert_allclose(m.args, [3, 0.5], atol=1e-3) assert m.fval < 1e-6
def test_ackleys(): random.seed(0.258) m = Minuit(ackleys, x=1.5 * random.random(), y=1.5 * random.random(), error_x=1.7, error_y=1.7, pedantic=False, print_level=0) m.migrad() assert m.fval < 1e-5 assert_array_almost_equal(m.args, [0, 0], decimal=3)
def _create_new_minuit_object(self, args): # Now create the new minimizer _contour_minuit = Minuit(self._function, **args) _contour_minuit.tol = 100 return _contour_minuit
def test_chi2_fit(): """Fit a curve to data.""" m = Minuit(chi2, s=2., error_A=0.1, errordef=0.01, print_level=0, pedantic=False) m.migrad() output = [round(10 * m.values['A']), round(100 * m.values['s']), round(100 * m.values['m'])] expected = [round(10 * 64.375993), round(100 * 4.267970), round(100 * 9.839172)] assert_array_almost_equal(output, expected)
def functesthelper(f): m = Minuit(f, print_level=0, pedantic=False) m.migrad() val = m.values assert_allclose(val['x'], 2.) assert_allclose(val['y'], 5.) assert_allclose(m.fval, 10.) assert m.matrix_accurate() assert m.migrad_ok() return m
def fit_power_lawab(x, y, ey, **kwargs): ''' Use chisq regression to fit for y = b * x^a Return two dictionaries: one for the values of a and b. and the second for the errors. ''' x2reg= Chi2Regression(powerlaw, x, y, ey) m = Minuit(x2reg, print_level=0, a= -0.5, error_a= 0.1, b=10, error_b=1) m.migrad() m.hesse() return m.values, m.errors
def SecondIteration(values): ant1_pxb = values[0] ant1_pyb = values[1] ant1_pzb = values[2] ant1_pxt = values[0] ant1_pyt = values[1] ant1_pzt = values[3] ant2_pxb = values[4] ant2_pyb = values[5] ant2_pzb = values[6] ant2_pxt = values[4] ant2_pyt = values[5] ant2_pzt = values[7] ant3_pxb = values[8] ant3_pyb = values[9] ant3_pzb = values[10] ant3_pxt = values[8] ant3_pyt = values[9] ant3_pzt = values[11] ant4_pxb = values[12] ant4_pyb = values[13] ant4_pzb = values[14] ant4_pxt = values[12] ant4_pyt = values[13] ant4_pzt = values[15] cal_pz = values[16] SPICE_x_guess = values[17] SPICE_y_guess = values[18] c0p = values[19] c1p = values[20] c8p = values[21] c9p = values[22] c16p = values[23] c17p = values[24] c24p = values[25] c25p = values[26] initial_step = 0.1 #m m = Minuit( f2,\ ant1_xb=ant1_pxb,\ ant1_yb=ant1_pyb,\ ant1_zb=ant1_pzb,\ ant1_xt=ant1_pxt,\ ant1_yt=ant1_pyt,\ ant1_zt=ant1_pzt,\ ant2_xb=ant2_pxb,\ ant2_yb=ant2_pyb,\ ant2_zb=ant2_pzb,\ ant2_xt=ant2_pxt,\ ant2_yt=ant2_pyt,\ ant2_zt=ant2_pzt,\ ant3_xb=ant3_pxb,\ ant3_yb=ant3_pyb,\ ant3_zb=ant3_pzb,\ ant3_xt=ant3_pxt,\ ant3_yt=ant3_pyt,\ ant3_zt=ant3_pzt,\ ant4_xb=ant4_pxb,\ ant4_yb=ant4_pyb,\ ant4_zb=ant4_pzb,\ ant4_xt=ant4_pxt,\ ant4_yt=ant4_pyt,\ ant4_zt=ant4_pzt,\ cal_z = cal_pz,\ SPICE_x=SPICE_x_guess,\ SPICE_y=SPICE_y_guess,\ c0 = c0p,\ c1 = c1p,\ c8 = c8p,\ c9 = c9p,\ c16 = c16p,\ c17 = c17p,\ c24 = c24p,\ c25 = c25p,\ error_ant1_xb=initial_step,\ error_ant1_yb=initial_step,\ error_ant1_zb=initial_step,\ error_ant1_xt=initial_step,\ error_ant1_yt=initial_step,\ error_ant1_zt=initial_step,\ error_ant2_xb=initial_step,\ error_ant2_yb=initial_step,\ error_ant2_zb=initial_step,\ error_ant2_xt=initial_step,\ error_ant2_yt=initial_step,\ error_ant2_zt=initial_step,\ error_ant3_xb=initial_step,\ error_ant3_yb=initial_step,\ error_ant3_zb=initial_step,\ error_ant3_xt=initial_step,\ error_ant3_yt=initial_step,\ error_ant3_zt=initial_step,\ error_ant4_xb=initial_step,\ error_ant4_yb=initial_step,\ error_ant4_zb=initial_step,\ error_ant4_xt=initial_step,\ error_ant4_yt=initial_step,\ error_ant4_zt=initial_step,\ error_cal_z=initial_step,\ error_SPICE_x=initial_step,\ error_SPICE_y=initial_step,\ error_c0 = initial_step,\ error_c1 = initial_step,\ error_c8 = initial_step,\ error_c9 = initial_step,\ error_c16 = initial_step,\ error_c17 = initial_step,\ error_c24 = initial_step,\ error_c25 = initial_step,\ errordef = 1.0,\ limit_ant1_xb=(ant1_pxb-10,ant1_pxb+10),\ limit_ant1_yb=(ant1_pyb-10,ant1_pyb+10),\ limit_ant1_zb=(ant1_pzb-1,ant1_pzb+2),\ limit_ant1_xt=(ant1_pxt-10,ant1_pxt+10),\ limit_ant1_yt=(ant1_pyt-10,ant1_pyt+10),\ limit_ant1_zt=(ant1_pzt-1,ant1_pzt+2),\ limit_ant2_xb=(ant2_pxb-10,ant2_pxb+10),\ limit_ant2_yb=(ant2_pyb-10,ant2_pyb+10),\ limit_ant2_zb=(ant2_pzb-1,ant2_pzb+2),\ limit_ant2_xt=(ant2_pxt-10,ant2_pxt+10),\ limit_ant2_yt=(ant2_pyt-10,ant2_pyt+10),\ limit_ant2_zt=(ant2_pzt-1,ant2_pzt+2),\ limit_ant3_xb=(ant3_pxb-10,ant3_pxb+10),\ limit_ant3_yb=(ant3_pyb-10,ant3_pyb+10),\ limit_ant3_zb=(ant3_pzb-1,ant3_pzb+2),\ limit_ant3_xt=(ant3_pxt-10,ant3_pxt+10),\ limit_ant3_yt=(ant3_pyt-10,ant3_pyt+10),\ limit_ant3_zt=(ant3_pzt-1,ant3_pzt+2),\ limit_ant4_xb=(ant4_pxb-10,ant4_pxb+10),\ limit_ant4_yb=(ant4_pyb-10,ant4_pyb+10),\ limit_ant4_zb=(ant4_pzb-1,ant4_pzb+2),\ limit_ant4_xt=(ant4_pxt-10,ant4_pxt+10),\ limit_ant4_yt=(ant4_pyt-10,ant4_pyt+10),\ limit_ant4_zt=(ant4_pzt-1,ant4_pzt+2),\ limit_SPICE_x=(SPICE_x_guess-5.0,SPICE_x_guess+5.0),\ limit_SPICE_y=(SPICE_y_guess-5.0,SPICE_y_guess+5.0),\ limit_c0 = (c0p-15.0,c0p+15.0),\ limit_c1 = (c1p-15.0,c1p+15.0),\ limit_c8 = (c8p-15.0,c8p+15.0),\ limit_c9 = (c9p-15.0,c9p+15.0),\ limit_c16 = (c16p-15.0,c16p+15.0),\ limit_c17 = (c17p-15.0,c17p+15.0),\ limit_c24 = (c24p-15.0,c24p+15.0),\ limit_c25 = (c25p-15.0,c25p+15.0),\ limit_cal_z = (-175.0,-170.0) ) result = m.migrad() print(result) print(m.values) #m.hesse() #m.minos() #print(m.get_fmin()) print(result) np.save('best_ARA_values.npy',m.values) return(m.values)
from ROOT import * from iminuit import Minuit import numpy as np from math import * def flogl(tau): val = 0 # Definisco logl return val #Main h = TH1D("h", "", 20, 0, 10) for line in open("exp.dat"): h.Fill(float(line)) m = Minuit(flogl, tau=2, norm=1000) # Istruisco fir di logl # m.migrad() # tau = m.values[0] h.Draw("E") # Disegno del fit f = TF1("f", "[0]*1/[1]*exp(-x/[1])", 0, 20) gApplication.Run(True)
def fit_muon(self, centre_x, centre_y, radius, pixel_x, pixel_y, image): """ Parameters ---------- centre_x: float Centre of muon ring in the field of view from circle fitting centre_y: float Centre of muon ring in the field of view from circle fitting radius: float Radius of muon ring from circle fitting pixel_x: ndarray X position of pixels in image from circle fitting pixel_y: ndarray Y position of pixel in image from circle fitting image: ndarray Amplitude of image pixels Returns ------- MuonIntensityParameters """ # First store these parameters in the class so we can use them in minimisation self.image = image self.pixel_x = pixel_x.to(u.deg) self.pixel_y = pixel_y.to(u.deg) self.unit = pixel_x.unit radius.to(u.deg) centre_x.to(u.deg) centre_y.to(u.deg) # Return interesting stuff fitoutput = MuonIntensityParameter() init_params = {} init_errs = {} init_constrain = {} init_params['impact_parameter'] = 4. init_params['phi'] = 0. init_params['radius'] = radius.value init_params['centre_x'] = centre_x.value init_params['centre_y'] = centre_y.value init_params['ring_width'] = 0.1 init_params['optical_efficiency_muon'] = 0.1 init_errs['error_impact_parameter'] = 2. init_constrain['limit_impact_parameter'] = (0., 25.) init_errs['error_phi'] = 0.1 init_errs['error_ring_width'] = 0.001 * radius.value init_errs['error_optical_efficiency_muon'] = 0.05 init_constrain['limit_phi'] = (-np.pi, np.pi) init_constrain['fix_radius'] = True init_constrain['fix_centre_x'] = True init_constrain['fix_centre_y'] = True init_constrain['limit_ring_width'] = (0., 1.) init_constrain['limit_optical_efficiency_muon'] = (0., 1.) print("radius =", radius, " pre migrad") parameter_names = init_params.keys() # Create Minuit object with first guesses at parameters # strip away the units as Minuit doesnt like them minuit = Minuit( self.likelihood, #forced_parameters=parameter_names, **init_params, **init_errs, **init_constrain, errordef=1., print_level=1 #pedantic=False ) # Perform minimisation minuit.migrad() # Get fitted values fit_params = minuit.values fitoutput.impact_parameter = fit_params['impact_parameter'] * u.m #fitoutput.phi = fit_params['phi']*u.rad fitoutput.impact_parameter_pos_x = fit_params[ 'impact_parameter'] * np.cos(fit_params['phi'] * u.rad) * u.m fitoutput.impact_parameter_pos_y = fit_params[ 'impact_parameter'] * np.sin(fit_params['phi'] * u.rad) * u.m fitoutput.ring_width = fit_params['ring_width'] * self.unit fitoutput.optical_efficiency_muon = fit_params[ 'optical_efficiency_muon'] fitoutput.prediction = self.prediction return fitoutput
def Chi2_Gauss(x_data, N, mu, sigma, N_bins, plot=False, title=False): """ IMPORTANT: It is not possible to give estimations for the Chi2 fit. INPUT: x = x-data, array-like N = number of experiments: mu = mean sigma = uncertainty of y-data, integer plots = whether to plot (True or False) title = title of plot in string: ex. 'title' """ def Gauss(x, N, mu, sigma): return N * binwidth * stats.norm.pdf(x, mu, sigma) counts, bin_edges = np.histogram(x_data, bins=N_bins) bin_centers = 0.5 * (bin_edges[1:] + bin_edges[:-1]) bin_width = bin_edges[1] - bin_edges[0] s_counts = np.sqrt(counts) x = bin_centers[counts > 0] y = counts[counts > 0] sy = s_counts[counts > 0] chi2_gaussian = Chi2Regression(Gauss, x, y, sy) minuit_gaussian = Minuit(chi2_gaussian, pedantic=False, N=N_bins, mu=mu, sigma=sigma) minuit_gaussian.migrad() print(minuit_gaussian.values['N']) Chi2 = minuit_gaussian.fval Ndof = len(x) - 3 p = stats.chi2.sf(Chi2, Ndof) xmin = mu - 4 * sigma xmax = mu + 4 * sigma xaxis = np.linspace(xmin, xmax, 1000) if plot: fig, ax = plt.subplots(figsize=(12, 8)) ax.hist(x_data, bins=N_bins, color='r', alpha=0.3) ax.hist(x_data, bins=N_bins, histtype='step', color='r') ax.errorbar(x, y, yerr=sy, fmt='ko', ecolor='k', elinewidth=1, capsize=1, capthick=1, label='Measurements') ax.plot(xaxis, Gauss(xaxis, *minuit_gaussian.args), color='k', linewidth=2) d = { 'Mean': [minuit_gaussian.args[1], minuit_gaussian.args[2]], 'N': minuit_gaussian.args[0], 'Chi2': Chi2, 'Ndof': Ndof, 'Prob': p, } text = nice_string_output(d, extra_spacing=2, decimals=3) add_text_to_ax(0.02, 0.95, text, ax, fontsize=18) fig.suptitle(title, fontsize=25) """ OUTPUT: Chi2 = Chi2 value p = Probabillity of this fit or worse minuit_fit.args = fitted value of Chi2 Plot of the function, along with the Chi2 fit """ return mean, sigma, Chi2, p
def linearFitFunction(x, a, b): return a * x + b for data in dataListDf: #We want to find the best fit using rms, since we don't know the errors (yet) #This is equvialent to using χ² with σᵢ= 1 ∀i. np.ones_like(data.values[:, 0]) lsLinearTimeReg = Chi2Regression(linearFitFunction, data.values[:, 0], data.values[:, 1], np.ones_like(data.values[:, 0])) a_start = 7.0 miniutFit = Minuit(lsLinearTimeReg, pedantic=False, a=a_start, b=0) miniutFit.migrad() ls_a = miniutFit.args[0] ls_b = miniutFit.args[1] fittedLinear = partial(linearFitFunction, a=ls_a, b=ls_b) residuals = list(map((lambda x: fittedLinear(x[0]) - x[1]), data.values)) y, x_edges = np.histogram(residuals, bins=10) x_center = x_edges[:-1] + (x_edges[1] - x_edges[0]) / 2 plt.plot(x_center, y) plt.show()
#Notice it has no offset. #FIXME: Trying with offset def linearFitFunction(x, a, b): return a * x + b #We want to find the best fit using rms, since we don't know the errors (yet) #This is equvialent to using χ² with σᵢ= 1 ∀i. np.ones_like(data.values[:, 0]) lsLinearTimeReg = Chi2Regression(linearFitFunction, data.values[:, 0], data.values[:, 1], np.ones_like(data.values[:, 0])) a_start = 7.0 miniutFit = Minuit(lsLinearTimeReg, pedantic=False, a=a_start, b=0) miniutFit.migrad() ls_a = miniutFit.args[0] ls_b = miniutFit.args[1] fittedLinear = partial(linearFitFunction, a=ls_a, b=ls_b) residuals = list(map((lambda x: fittedLinear(x[0]) - x[1]), dataMatrix)) ndfData = np.transpose([range(0, len(residuals)), residuals]) ndf = pd.DataFrame(data=ndfData, columns=["x-val", "Residuals"]) #newDataPlot = sns.relplot(x = "x-val",y = "Residuals",data = ndf,kind = "scatter") #plt.show()
def test_NormalConstraint_2(): lsq1 = NormalConstraint(("a", "b"), (1, 2), (2, 2)) lsq2 = lsq1 + NormalConstraint("b", 2, 0.1) + NormalConstraint("a", 1, 0.01) sa = 0.1 sb = 0.02 rho = 0.5 cov = ((sa ** 2, rho * sa * sb), (rho * sa * sb, sb ** 2)) lsq3 = lsq1 + NormalConstraint(("a", "b"), (1, 2), cov) assert lsq1.func_code.co_varnames == ("a", "b") assert lsq2.func_code.co_varnames == ("a", "b") assert lsq3.func_code.co_varnames == ("a", "b") m = Minuit(lsq1, 0, 0) m.migrad() assert_allclose(m.values, (1, 2), atol=1e-3) assert_allclose(m.errors, (2, 2), rtol=1e-3) m = Minuit(lsq2, 0, 0) m.migrad() assert_allclose(m.values, (1, 2), atol=1e-3) assert_allclose(m.errors, (0.01, 0.1), rtol=1e-2) m = Minuit(lsq3, 0, 0) m.migrad() assert_allclose(m.values, (1, 2), atol=1e-3) assert_allclose(m.errors, (sa, sb), rtol=1e-2) assert_allclose(m.covariance, cov, rtol=1e-2)
# -*- coding: utf-8 -*- from iminuit import Minuit from matplotlib import pyplot as plt from numpy.random import randn, seed from probfit import BinnedChi2, Extended, gaussian seed(0) data = randn(1000) * 2 + 1 ext_gauss = Extended(gaussian) ulh = BinnedChi2(ext_gauss, data) m = Minuit(ulh, mean=0.0, sigma=0.5, N=800) plt.figure(figsize=(8, 3)) plt.subplot(121) ulh.draw(m) plt.title("Before") m.migrad() # fit plt.subplot(122) ulh.draw(m) plt.title("After")
###################################################################### print "***** initializating *****" # Initialize a Lilith object lilithcalc = lilith.Lilith(verbose, timer) # Read experimental data lilithcalc.readexpinput(myexpinput) # Initialize the fit; parameter starting values and limits m = Minuit(getL, CGa=1, limit_CGa=(0, 3), Cg=1, limit_Cg=(0, 3), BRinv=0.2, limit_BRinv=(0, 0.9), errordef=1, error_CGa=0.1, error_Cg=0.1, error_BRinv=0.1) print "\n***** performing model fit with iminuit *****" # Minimization and error estimation m.migrad() m.minos() print "\n***** fit summary *****" print "\nbest-fit point:", m.values
# ax1 = P_DBACK.plot(kind='scatter', y ='gammaxpos', x='Back_Counts', label = 'phot',color='b') # elif whatDetector == 'Difference': # ax1 = P_DDIFF.plot(kind='scatter', y ='gammaxpos', x='Difference_Counts', label = 'phot',color='b') # elif whatDetector == 'Aug': # ax1 = P_DAUG.plot(kind='scatter', y ='gammaxpos', x='Augmented_Difference_Counts', label = 'phot',color='b') P_DDIFF = P_DDIFF.dropna() doiListPhot = P_DDIFF.gammaxpos.tolist() differenceCountsPhot = P_DDIFF.Difference_Counts.tolist() DATA_DIFFERENCE = DATA_DIFFERENCE.dropna() doiList = DATA_DIFFERENCE.gammaxpos.tolist() differenceCounts = DATA_DIFFERENCE.Difference_Counts.tolist() m = Minuit(LeastSquares(differenceCountsPhot, doiListPhot, 10.0, Line), m=-500, b=3000) m.migrad() m.hesse() # print(m.values) # x = range(int(min(differenceCounts)), int(max(differenceCounts))) # y = [Line(k, *m.values[0:2]) for k in x] # ax1.plot(x, y, color = 'k') # C = pd.DataFrame(eid_c) # if not C.empty: # C.columns = ['EventID'] # C_DFRONT = pd.merge(C,DATA_FRONT, on='EventID') # C_DBACK = pd.merge(C,DATA_BACK, on='EventID') # C_DDIFF = pd.merge(C,DATA_DIFFERENCE, on='EventID') # C_DDIFF = pd.merge(C,DATA_DIFFERENCE, on='EventID') # C_DAUG = pd.merge(C,DATA_AUG, on='EventID')
def test_draw_ulh_with_minuit(): np.random.seed(0) data = np.random.randn(1000) ulh = UnbinnedLH(gaussian, data) minuit = Minuit(ulh, mean=0, sigma=1) ulh.draw(minuit)
def fitPhysicalModel(self, frame, tabs, args, direction, lo, hi, offset): # initial parameter guesses loGuess = lo hiGuess = hi t0Guess = offset DGuess = 1.7 LGuess = 1020. dthetaGuess = 4.66 # initialize model prediction global prediction prediction = numpy.zeros_like(frame.samples) # define chi-square function to use def chiSquare(t0, lo, hi, D, L, dtheta): global prediction prediction = physicalModel(t0, direction, lo, hi, tabs, D, L, dtheta, nsamples=args.nsamples, adcTick=args.adc_tick) residuals = frame.samples - prediction return numpy.dot(residuals, residuals) # initialize fitter print_level = 1 if args.verbose else 0 engine = Minuit(chiSquare, errordef=1.0, print_level=print_level, t0=t0Guess, error_t0=1., lo=loGuess, error_lo=1., hi=hiGuess, error_hi=1., D=DGuess, error_D=0.1, fix_D=False, L=LGuess, error_L=10., fix_L=True, dtheta=dthetaGuess, error_dtheta=0.5, fix_dtheta=False) engine.tol = 10. # do the fit minimum = engine.migrad() if not minimum[0]['has_valid_parameters']: raise RuntimeError('Fit failed!') # calculate the best fit model chiSquare(*engine.args) # return best-fit parameter values and best-fit model prediction return engine.args, prediction
def test_matyas(): m = Minuit(matyas, x=0.5, y=0.5, pedantic=False) m.tol = 1e-4 m.migrad() assert_allclose(m.fval, 0, atol=1e-14) assert_allclose(m.args, [0, 0], atol=1e-14)
def FirstIteration(): print('done') #SPICE_delays = np.load('all_SPICE_delays_int.npy',allow_pickle=True) #SPICE_depths = np.load('all_SPICE_depths_int.npy',allow_pickle=True) #print('SPICE is', SPICE_delays,SPICE_depths) ant2_pxb = 19.79 ant2_pyb = 64.73 ant2_pzb = -195.31 ant2_pxt = 19.79 ant2_pyt = 64.73 ant2_pzt = -165.46 ant1_pxb = 44.89 ant1_pyb = 49.59 ant1_pzb = -194.52 ant1_pxt = 44.89 ant1_pyt = 49.59 ant1_pzt = -164.67 ant3_pxb = 27.06 ant3_pyb = 21.85 ant3_pzb = -191.28 ant3_pxt = 27.06 ant3_pyt = 21.85 ant3_pzt = -161.34 ant4_pxb = 5.27 ant4_pyb = 38.95 ant4_pzb = -177.72 ant4_pxt = 5.27 ant4_pyt = 38.95 ant4_pzt = -145.01 cal_pz = -171.278558 SPICE_x_guess = +3072.295822 SPICE_y_guess = +2861.7688200 c0p = +138.485012 c1p = +17.547130 c8p = +133.911865 c9p = +18.789163 c16p = +131.291872 c17p = +13.460643 c24p = +132.996322 c25p = +16.938025 rvals = np.linspace(4100.0,4190.0,90) zs_vals = np.linspace(-1450,-1050,20) zd_vals = np.linspace(-200.0,-140.0,120) initial_step = 0.1 #m print('trying minimization now') #-12 ft on pulser locations relative to antennas to account for additional mast elevation. m = Minuit( f1,\ ant1_xb=ant1_pxb,\ ant1_yb=ant1_pyb,\ ant1_zb=ant1_pzb,\ ant1_zt=ant1_pzt,\ ant2_xb=ant2_pxb,\ ant2_yb=ant2_pyb,\ ant2_zb=ant2_pzb,\ ant2_zt=ant2_pzt,\ ant3_xb=ant3_pxb,\ ant3_yb=ant3_pyb,\ ant3_zb=ant3_pzb,\ ant3_zt=ant3_pzt,\ ant4_xb=ant4_pxb,\ ant4_yb=ant4_pyb,\ ant4_zb=ant4_pzb,\ ant4_zt=ant4_pzt,\ cal_z = cal_pz,\ SPICE_x=SPICE_x_guess,\ SPICE_y=SPICE_y_guess,\ c0 = c0p,\ c1 = c1p,\ c8 = c8p,\ c9 = c9p,\ c16 = c16p,\ c17 = c17p,\ c24 = c24p,\ c25 = c25p,\ error_ant1_xb=initial_step,\ error_ant1_yb=initial_step,\ error_ant1_zb=initial_step,\ error_ant1_zt=initial_step,\ error_ant2_xb=initial_step,\ error_ant2_yb=initial_step,\ error_ant2_zb=initial_step,\ error_ant2_zt=initial_step,\ error_ant3_xb=initial_step,\ error_ant3_yb=initial_step,\ error_ant3_zb=initial_step,\ error_ant3_zt=initial_step,\ error_ant4_xb=initial_step,\ error_ant4_yb=initial_step,\ error_ant4_zb=initial_step,\ error_ant4_zt=initial_step,\ error_cal_z=initial_step,\ error_SPICE_x=initial_step,\ error_SPICE_y=initial_step,\ error_c0 = initial_step,\ error_c1 = initial_step,\ error_c8 = initial_step,\ error_c9 = initial_step,\ error_c16 = initial_step,\ error_c17 = initial_step,\ error_c24 = initial_step,\ error_c25 = initial_step,\ errordef = 1.0,\ limit_ant1_xb=(ant1_pxb-10,ant1_pxb+10),\ limit_ant1_yb=(ant1_pyb-10,ant1_pyb+10),\ limit_ant1_zb=(ant1_pzb-1,ant1_pzb+2),\ limit_ant1_zt=(ant1_pzt-1,ant1_pzt+2),\ limit_ant2_xb=(ant2_pxb-10,ant2_pxb+10),\ limit_ant2_yb=(ant2_pyb-10,ant2_pyb+10),\ limit_ant2_zb=(ant2_pzb-1,ant2_pzb+2),\ limit_ant2_zt=(ant2_pzt-1,ant2_pzt+2),\ limit_ant3_xb=(ant3_pxb-10,ant3_pxb+10),\ limit_ant3_yb=(ant3_pyb-10,ant3_pyb+10),\ limit_ant3_zb=(ant3_pzb-1,ant3_pzb+2),\ limit_ant3_zt=(ant3_pzt-1,ant3_pzt+2),\ limit_ant4_xb=(ant4_pxb-10,ant4_pxb+10),\ limit_ant4_yb=(ant4_pyb-10,ant4_pyb+10),\ limit_ant4_zb=(ant4_pzb-1,ant4_pzb+2),\ limit_ant4_zt=(ant4_pzt-1,ant4_pzt+2),\ limit_SPICE_x=(SPICE_x_guess-5.0,SPICE_x_guess+5.0),\ limit_SPICE_y=(SPICE_y_guess-5.0,SPICE_y_guess+5.0),\ limit_c0 = (c0p-15.0,c0p+15.0),\ limit_c1 = (c1p-15.0,c1p+15.0),\ limit_c8 = (c8p-15.0,c8p+15.0),\ limit_c9 = (c9p-15.0,c9p+15.0),\ limit_c16 = (c16p-15.0,c16p+15.0),\ limit_c17 = (c17p-15.0,c17p+15.0),\ limit_c24 = (c24p-15.0,c24p+15.0),\ limit_c25 = (c25p-15.0,c25p+15.0),\ limit_cal_z = (-175.0,-170.0) ) result = m.migrad() values = m.values print(result) print(m.values) #m.hesse() #m.minos() #print(m.get_fmin()) print(result) #print(m.values['ant1_xb']) print(m.values[0]) np.save('first_iteration.npy',m.values) return(values)
from iminuit import Minuit def f(x, y, z): return (x - 1) ** 2 + (y - x) ** 2 + (z - 2) ** 2 m = Minuit(f, print_level=0, pedantic=False) m.migrad() m.draw_mncontour('x', 'y')
def LLwrapper(params): """Need this for scipy.optimize. There might be a better way of doing it.""" NLL = LogLikelihood(gauss, s) return NLL(params[0], params[1]) def gauss(x, mu, sigma): return 1. / (np.sqrt(2 * np.pi * sigma**2)) * np.exp(-(x - mu)**2 / (2 * sigma**2)) NLL = LogLikelihood(gauss, time_diff.values) m = Minuit(NLL, mu=-14., sigma=1., limit_mu=(-16., -10.), limit_sigma=(0.05, 2.), print_level=1, errordef=0.5) result = m.migrad() # hesse called at the end of migrad print m.values print m.errors max_time = -10. min_time = -20. nbins = 50 width = float(max_time - min_time) / nbins fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(6, 6)) time_diff.plot(kind='hist', bins=np.arange(min_time, max_time, width), ax=axes) axes.set_xlabel("Time difference [ns]") axes.set_ylabel("Entries / (%0.2f ns)" % width)
# -*- coding: utf-8 -*- import numpy as np from iminuit import Minuit from probfit import AddPdf, BinnedLH, Extended, gen_toy from probfit.pdf import HistogramPdf bound = (0, 10) np.random.seed(0) bkg = gen_toy(lambda x: x**2, 100000, bound=bound) # a parabola background sig = np.random.randn(50000) + 5 # a Gaussian signal data = np.concatenate([sig, bkg]) # fill histograms with large statistics hsig, be = np.histogram(sig, bins=40, range=bound) hbkg, be = np.histogram(bkg, bins=be, range=bound) # randomize data data = np.random.permutation(data) fitdata = data[:1000] psig = HistogramPdf(hsig, be) pbkg = HistogramPdf(hbkg, be) epsig = Extended(psig, extname="N1") epbkg = Extended(pbkg, extname="N2") pdf = AddPdf(epbkg, epsig) blh = BinnedLH(pdf, fitdata, bins=40, bound=bound, extended=True) m = Minuit(blh, N1=330, N2=670, error_N1=20, error_N2=30) # m.migrad() blh.draw(m, parts=True)
def minimise(self, params, step, limits, minimiser_name="minuit"): """ Parameters ---------- params step limits minimiser_name Returns ------- """ if minimiser_name == "minuit": min = Minuit(self.get_likelihood, print_level=1, source_x=params[0], error_source_x=step[0], limit_source_x=limits[0], source_y=params[1], error_source_y=step[1], limit_source_y=limits[1], core_x=params[2], error_core_x=step[2], limit_core_x=limits[2], core_y=params[3], error_core_y=step[3], limit_core_y=limits[3], energy=params[4], error_energy=step[4], limit_energy=limits[4], x_max_scale=params[5], error_x_max_scale=step[5], limit_x_max_scale=limits[5], fix_x_max_scale=False, errordef=1) min.migrad() min.tol *= 1000 min.set_strategy(0) # Perform minimisation fit_params = min.values errors = min.errors return (fit_params["source_x"], fit_params["source_y"], fit_params["core_x"], fit_params["core_y"], fit_params["energy"], fit_params[ "x_max_scale"]),\ (errors["source_x"], errors["source_y"], errors["core_x"], errors["core_x"], errors["energy"], errors["x_max_scale"]) elif minimiser_name in ("lm", "trf", "dogleg"): self.array_return = True limits = np.array(limits) min = least_squares(self.get_likelihood_min, params, method=minimiser_name, x_scale=step, xtol=1e-10, ftol=1e-10 ) return min.x, (0, 0, 0, 0, 0, 0) else: min = minimize(self.get_likelihood_min, params, method=minimiser_name, bounds=limits ) return min.x, (0, 0, 0, 0, 0, 0)
# initilize model model = Quarks(boundary_type=boundary_type) chi_squared_threshold = 10. N = 100 # number of trials x = np.array([None]) # 2-step optimization process # fit masses then fit full for i in tqdm(range(N)): # Step 1: fitting the masses only params = model.generate_random_inputs(c_min=1., c_max=5.) least_squares = LeastSquares(x, model.output_values[:, :6], model.output_errors[:, :6], model.get_output_masses) optimizer = Minuit(least_squares, params, name=model.input_names) optimizer.limits = model.input_limits optimizer.fixed[:36] = True try: optimizer.migrad() optimizer.hesse() except ValueError: pass # Step 2: fitting all at once # Only run if step 1 is error-free if optimizer.fval: params_2 = np.array(optimizer.values) # feed forward y_2 = model.get_output_full(x, params_2) chi_squared_2 = get_chi_squared(y_2, model.output_values, model.output_errors)
dppLg = np.zeros_like(dppHg) pedestalLg = np.zeros_like(pedestalHg) if GUI: fig, ax = plt.subplots() for r in range(row): for c in range(col): channelHgPe = matrixHg[r, c, :] channelLgADC = matrixLg[r, c, :] x = channelHgPe[(channelHgPe > INF) & (channelHgPe < SUP)] y = channelLgADC[(channelHgPe > INF) & (channelHgPe < SUP)] lsq = LeastSquares(x, y, np.ones_like(y), line) fit = Minuit(lsq, m=1, q=60) fit.migrad() fit.hesse() dppLg[r, c] = fit.values["m"] pedestalLg[r, c] = fit.values["q"] print( f"Channel {r} - {c} ADC/pe: {fit.values[0]:.2f} +/- {fit.errors[0]:.2f} Pedestal: {fit.values[1]:.2f} +/- {fit.errors[1]:.2f}" ) if GUI: xFit = np.arange(channelHgPe.min(), channelHgPe.max(), 0.1) yFit = line(xFit, *fit.values) par_b = np.random.multivariate_normal(fit.values, fit.covariance, size=100)
initialValues = (1.89278, 0.0291215, 0.273023, 8.96853, 353.824, 2.50639, -5.92725, 0., 0.) ### NNPDF #initialValues=(1.54728, 0.04678,0.1982, 26.49689,2727.9766, 3.00668, -23.54749, 0.,0.) ##MMHT14 #initialValues=(2.34665, 0.022735,0.27706, 24.8883, 1241.34, 2.66869, -23.7589, 0., 0.) ##CT14 #initialValues=(1.92659, 0.036548,0.218079, 17.9138, 926.078, 2.5431, -15.5469, 0. ,0.) ##PDF4LHC initialErrors = (0.1, 0.01, 0.05, 0.1, 10., 0.05, 0.05, 0.1, 0.1) searchLimits = ((1.4, 4.5), (0.001, 5.0), (0.0, 4.0), (8., 25.0), (0.0, 400), (0., 5), None, None, None) parametersToMinimize = (False, False, False, False, False, False, False, True, True) m = Minuit.from_array_func(chi_2, initialValues, error=initialErrors, limit=searchLimits, fix=parametersToMinimize, errordef=1) #m.get_param_states() m.tol = 0.0001 * totalN * 10000 ### the last 0.0001 is to compensate MINUIT def m.strategy = 1 SaveToLog("MINIMIZATION STARTED", str(m.params)) #%% m.tol = 0.0001 * totalN * 10000 ### the last 0.0001 is to compensate MINUIT def m.strategy = 1 m.migrad()
def fit_template(events, pulse_width=(4, 5), rise_time=12, template=PULSE_TEMPLATE): for event in events: adc_samples = event.data.adc_samples.copy() time = np.arange(adc_samples.shape[-1]) * 4 pulse_indices = event.data.pulse_mask pulse_indices = np.argwhere(pulse_indices) pulse_indices = [tuple(arr) for arr in pulse_indices] amplitudes = np.zeros(adc_samples.shape) * np.nan times = np.zeros(adc_samples.shape) * np.nan plt.figure() for pulse_index in pulse_indices: left = pulse_index[-1] - int(pulse_width[0]) left = max(0, left) right = pulse_index[-1] + int(pulse_width[1]) + 1 right = min(adc_samples.shape[-1] - 1, right) y = adc_samples[pulse_index[0], left:right] t = time[left:right] where_baseline = np.arange(adc_samples.shape[-1]) where_baseline = (where_baseline < left) + \ (where_baseline >= right) where_baseline = adc_samples[pulse_index[0]][where_baseline] baseline_0 = np.mean(where_baseline) baseline_std = np.std(where_baseline) limit_baseline = (baseline_0 - baseline_std, baseline_0 + baseline_std) t_0 = time[pulse_index[-1]] - rise_time limit_t = (t_0 - 2 * 4, t_0 + 2 * 4) amplitude_0 = np.max(y) limit_amplitude = (max(np.min(y), 0), amplitude_0 * 1.2) chi2 = Chi2Regression(template, t, y) m = Minuit(chi2, t_0=t_0, amplitude=amplitude_0, limit_t=limit_t, limit_amplitude=limit_amplitude, baseline=baseline_0, limit_baseline=limit_baseline, print_level=0, pedantic=False) m.migrad() adc_samples[pulse_index[0]] -= template(time, **m.values) amplitudes[pulse_index] = m.values['amplitude'] times[pulse_index] = m.values['t'] event.data.reconstructed_amplitude = amplitudes event.data.reconstructed_time = times yield event
import random as rnd from iminuit import Minuit # Will overwrite the values passed from param.py # if (sys.argv[1].isnumeric()): loc = int(sys.argv[1]) # if (sys.argv[2].isnumeric()): bin_min = int(sys.argv[2]) # if (sys.argv[3].isnumeric()): num_of_bins = int(sys.argv[3]) # Other variable assosiated with the run rnd_seed = int( sys.argv[1]) directory = sys.argv[2] job_id = os.environ.get('SLURM_JOB_ID') # First run is assgned the default choice of profiles i.e., "rnd_seed = -1" bins, Y_exp, V_inv = experimental_input_all_a(bin_min_all_a, num_of_bins_all_a) chi = set_chi_all_a(Q, bins, Y_exp, V_inv, rnd_seed) print("Minuit: " ) m = Minuit(chi, 0.11, 0.4) m.migrad() # run optimiser fit = np.append(np.array(m.values), rnd_seed ) out_name = directory + "fits_"+ job_id +".txt" np.savetxt(out_name, fit , delimiter='\t') # export data
def minimize(fun, x0, args=(), method=None, jac=None, hess=None, hessp=None, bounds=None, constraints=None, tol=None, callback=None, options=None): """An interface to MIGRAD using the ``scipy.optimize.minimize`` API. For a general description of the arguments, see ``scipy.optimize.minimize``. The ``method`` argument is ignored. The optimisation is always done using MIGRAD. The `options` argument can be used to pass special settings to Minuit. All are optional. **Options:** - *disp* (bool): Set to true to print convergence messages. Default: False. - *maxfev* (int): Maximum allowed number of iterations. Default: 10000. - *eps* (sequence): Initial step size to numerical compute derivative. Minuit automatically refines this in subsequent iterations and is very insensitive to the initial choice. Default: 1. **Returns: OptimizeResult** (dict with attribute access) - *x* (ndarray): Solution of optimization. - *fun* (float): Value of objective function at minimum. - *message* (str): Description of cause of termination. - *hess_inv* (ndarray): Inverse of Hesse matrix at minimum (may not be exact). - nfev (int): Number of function evaluations. - njev (int): Number of jacobian evaluations. - minuit (object): Minuit object internally used to do the minimization. Use this to extract more information about the parameter errors. """ from scipy.optimize import OptimizeResult if method not in {None, 'migrad'}: warnings.warn("method argument is ignored") if constraints is not None: raise ValueError( "Constraints are not supported by Minuit, only bounds") if hess or hessp: warnings.warn( "hess and hessp arguments cannot be handled and are ignored") if tol: warnings.warn("tol argument has no effect on Minuit") def wrapped(func, args, callback=None): if callback is None: return lambda x: func(x, *args) else: def f(x): callback(x) return func(x, *args) return f wrapped_fun = wrapped(fun, args, callback) maxfev = 10000 error = None kwargs = {'print_level': 0, 'errordef': 1} if options: if "disp" in options: kwargs['print_level'] = 1 if "maxiter" in options: warnings.warn("maxiter not supported, acts like maxfev instead") maxfev = options["maxiter"] if "maxfev" in options: maxfev = options["maxfev"] if "eps" in options: error = options["eps"] # prevent warnings from Minuit about missing initial step if error is None: error = np.ones_like(x0) if bool(jac): if jac is True: raise ValueError("jac=True is not supported, only jac=callable") assert hasattr(jac, "__call__") wrapped_grad = wrapped(jac, args) else: wrapped_grad = None m = Minuit.from_array_func(wrapped_fun, x0, error=error, limit=bounds, grad=wrapped_grad, **kwargs) m.migrad(ncall=maxfev) message = "Optimization terminated successfully." success = m.migrad_ok() if not success: message = "Optimization failed." fmin = m.get_fmin() if fmin.has_reached_call_limit: message += " Call limit was reached." if fmin.is_above_max_edm: message += " Estimated distance to minimum too large." return OptimizeResult( x=m.np_values(), success=success, fun=m.fval, hess_inv=m.np_covariance(), message=message, nfev=m.get_num_call_fcn(), njev=m.get_num_call_grad(), minuit=m, )
def test_matyas_oneside(): """One-sided limit when the minimum is in the forbidden region.""" m = Minuit(matyas, x=2 + 0.5, y=0.5, limit_x=(1, None), pedantic=False) m.tol = 1e-4 m.migrad() assert_allclose(m.args, [1, 0.923], atol=1e-3)
m1,m2 = 100,200 ############# FIT num. 1 ##########################<3 print('Fit number 1:\nExponential') #background pdf #1: exponential, copying formula from the question def B_1(x,alpha): pdf = alpha*np.exp(-alpha*x)/(np.exp(-alpha*m1)-np.exp(-alpha*m2)) return pdf #define the NLL corresponding to B_1 def NLL_1(S,B,alpha): temp = np.log(S*S_pdf(mass)+B*B_1(mass,alpha)) return S + B -temp.sum() #start to fit m_fit_1 = Minuit(NLL_1,S=30.,B = 170., alpha = 1., print_level = 1, errordef=0.5,error_S=1.0,error_B=1.0,error_alpha = 1.0) m_fit_1.migrad() m_fit_1.minos() m_fit_1.print_param() fittedB1 = m_fit_1.values['B'] fittedS1 = m_fit_1.values['S'] fitteda = m_fit_1.values['alpha'] ####################################### end of fit 1 print('end of fit 1') ############# FIT num. 2 ##########################<3 print('Fit number 2:\nLinear') #background pdf #2: linear, y = -kx+b. By normalization, #b = 1/(m2-m1)+1/2*k*(m2+m1)
return lilithcalc.l ###################################################################### # Calculations ###################################################################### # Initialize a Lilith object lilithcalc = lilith.Lilith(verbose, timer) # Read experimental data lilithcalc.readexpinput(myexpinput) print "\n***** performing model fit: migrad, hesse, minos *****" # Initialize the fit; parameter starting values and limits m = Minuit(getL, CV=1, limit_CV=(0,3), CF=1, limit_CF=(0,3), print_level=0, errordef=1, error_CV=0.2, error_CF=0.2) # Minimization and error estimation m.migrad() m.hesse() # run covariance estimator m.minos() print "\nbest-fit point:", m.values print "\nHesse errors:", m.errors print "\nMinos errors:" for key, value in m.merrors.items(): print key, value print "\nCorrelation matrix:\n", m.matrix(correlation=True) print "\nCovariance matrix:\n", m.matrix()
def xyzResolution(errorPosIN, uninteractedEvents): guess = [[0, 100], [0, 50], [0, 50]] displayBinsMM = np.array([0.5, 0.5, 5]) #*0.25 axes = ["X(Depth/Width) ", "Y(Height) ", "Z(Length) "] # fig, axs = plt.subplots(3) # for i in range(3): # axs[i].hist(errorPosIN[:,i],bins=200) # plt.savefig(Options.plotDIR+"test.png") # plt.close() fig, axs = plt.subplots(3) for i in range(3): errorPosI = errorPosIN[:, i] errorPosI = errorPosI[np.isfinite(errorPosI)] print("-------------------\n", i, "- axis events: ", len(errorPosI), " NaN events: ", (Options.nEvents - len(errorPosI))) xmin, xmax = -U[i], U[i] #np.min(errorPosI),np.max(errorPosI) axs[i].set_xlim(xmin / 2, xmax / 2) nbins = len(errorPosI) dbins = int(L[i] / displayBinsMM[i]) #8*int(2*U[i]/10) axs[i].hist(errorPosI, bins=dbins, range=(xmin, xmax), density=False) hist, bin_edges = np.histogram(errorPosI, bins=np.arange(xmin, xmax, L[i] / nbins)) ###hist = hist*nbins/(len(errorPosI)*(xmax-xmin)) #- enable if density=true hist = (nbins / dbins) * len(errorPosI) * hist / np.sum(hist) print(np.sum(hist) * (L[i] / nbins)) bins = bin_edges[0:(len(bin_edges) - 1)] + 0.5 * (L[i] / nbins) lnspc = np.linspace(xmin, xmax, len(hist)) def pdf(x, m, s): ###return (1/(math.sqrt(2*math.pi)*s))*np.exp(-0.5*((x-m)**2)/(s**2)) #- enable if density=true return (nbins / dbins) * len(errorPosI) * (L[i] / nbins) * ( 1 / (math.sqrt(2 * math.pi) * s)) * np.exp(-0.5 * ((x - m)**2) / (s**2)) def spdf(x, df, scale): return (len(errorPosI) * (L[i] / dbins)) * stats.t.pdf(x, df, 0, scale) def fcn(m, s): return np.sum(np.power((hist - pdf(bins, m, s)), 2)) def sfcn(df, scale): return np.sum(np.power((hist - spdf(bins, df, scale)), 2)) fcn.errordef = Minuit.LEAST_SQUARES sfcn.errordef = Minuit.LEAST_SQUARES param = guess[i] m = Minuit(fcn, param[0], param[1]) m.migrad() param = m.values pdf_g = pdf(lnspc, param[0], param[1]) axs[i].plot(lnspc, pdf_g, label='Norm') axs[i].text(0.01, 0.99, "Total Events = %1.0f " % (Options.nEvents), verticalalignment='top', horizontalalignment='left', transform=axs[i].transAxes, fontsize=12) axs[i].text(0.01, 0.79, "Interacted Events = %1.0f " % (Options.nEvents - uninteractedEvents), verticalalignment='top', horizontalalignment='left', transform=axs[i].transAxes, fontsize=12) axs[i].text(0.01, 0.59, "Usable Events = %1.0f " % (len(errorPosI)), verticalalignment='top', horizontalalignment='left', transform=axs[i].transAxes, fontsize=12) axs[i].text(0.01, 0.39, "NaN Events = %1.0f " % (Options.nEvents - len(errorPosI) - uninteractedEvents), verticalalignment='top', horizontalalignment='left', transform=axs[i].transAxes, fontsize=12) axs[i].text(0.01, 0.19, "Bin Size = %1.2f mm" % (displayBinsMM[i]), verticalalignment='top', horizontalalignment='left', transform=axs[i].transAxes, fontsize=12) axs[i].text(0.99, 0.99, "Mean = %4.2f mm" % (param[0]), verticalalignment='top', horizontalalignment='right', transform=axs[i].transAxes, fontsize=12) axs[i].text(0.99, 0.79, "STD = %4.2f mm" % (param[1]), verticalalignment='top', horizontalalignment='right', transform=axs[i].transAxes, fontsize=12) axs[i].text(0.99, 0.59, "FWHM = %4.2f mm" % (FWHM * param[1]), verticalalignment='top', horizontalalignment='right', transform=axs[i].transAxes, fontsize=12) axs[i].text(0.99, 0.39, "Error = +-%4.2f mm" % (0.5 * FWHM * param[1]), verticalalignment='top', horizontalalignment='right', transform=axs[i].transAxes, fontsize=12) axs[i].text(0.99, 0.19, "RSQ = %1.2f" % (RSQ(hist, pdf_g)), verticalalignment='top', horizontalalignment='right', transform=axs[i].transAxes, fontsize=12) axs[i].set_title(axes[i] + "predicted/expected difference histogram in mm") #axs[i].margins(2, 2) print("MU =", param[0]) print("STD =", param[1]) print("FWHM =", FWHM * param[1], " mm") print("Error = +-", 0.5 * FWHM * param[1], " mm") plt.tight_layout() if Options.Strip_Based_Reconstruction: plt.savefig(Options.plotDIR + 'PositionResolution_StripCounts_AR' + str(Options.ArrayNumber) + '.png') else: if Options.SiPM_Based_Reconstruction: plt.savefig(Options.plotDIR + 'PositionResolution_SiPMCounts_True_AR' + str(Options.ArrayNumber) + '.png') else: plt.savefig(Options.plotDIR + 'PositionResolution_Strip_Endcounts_AR' + str(Options.ArrayNumber) + '.png') plt.show()
def fit_muon(self, centre_x, centre_y, radius, pixel_x, pixel_y, image): """ Parameters ---------- centre_x: float Centre of muon ring in the field of view from circle fitting centre_y: float Centre of muon ring in the field of view from circle fitting radius: float Radius of muon ring from circle fitting pixel_x: ndarray X position of pixels in image from circle fitting pixel_y: ndarray Y position of pixel in image from circle fitting image: ndarray Amplitude of image pixels Returns ------- MuonIntensityParameters """ # First store these parameters in the class so we can use them in minimisation self.image = image self.pixel_x = pixel_x.to(u.deg) self.pixel_y = pixel_y.to(u.deg) self.unit = pixel_x.unit radius.to(u.deg) centre_x.to(u.deg) centre_y.to(u.deg) # Return interesting stuff fitoutput = MuonIntensityParameter() # Create Minuit object with first guesses at parameters # strip away the units as Minuit doesnt like them minuit = Minuit( self.likelihood, impact_parameter=4, limit_impact_parameter=(0, 25), error_impact_parameter=5, phi=0, limit_phi=(-np.pi, np.pi), error_phi=0.1, radius=radius.value, fix_radius=True, error_radius=0., centre_x=centre_x.value, fix_centre_x=True, error_centre_x=0., centre_y=centre_y.value, fix_centre_y=True, error_centre_y=0., ring_width=0.1, error_ring_width=0.001 * radius.value, limit_ring_width=(0, 1), optical_efficiency_muon=0.1, error_optical_efficiency_muon=0.05, limit_optical_efficiency_muon=(0, 1), throw_nan=False, print_level=0, pedantic=False, errordef=1, ) # Perform minimisation minuit.migrad() # Get fitted values fit_params = minuit.values fitoutput.impact_parameter = fit_params['impact_parameter'] * u.m #fitoutput.phi = fit_params['phi']*u.rad fitoutput.ring_width = fit_params['ring_width'] * self.unit fitoutput.optical_efficiency_muon = fit_params[ 'optical_efficiency_muon'] #embed() # fitoutput.prediction = self.prediction return fitoutput
# -*- coding: utf-8 -*- from iminuit import Minuit from matplotlib import pyplot as plt from numpy.random import randn from probfit import BinnedLH, Extended, gaussian data = randn(1000) * 2 + 1 # Unextended blh = BinnedLH(gaussian, data) # if you wonder what it looks like call describe(blh) m = Minuit(blh, mean=0.0, sigma=0.5) plt.figure(figsize=(8, 6)) plt.subplot(221) blh.draw(m) plt.title("Unextended Before") m.migrad() # fit plt.subplot(222) blh.draw(m) plt.title("Unextended After") # Extended ext_gauss = Extended(gaussian) blh = BinnedLH(ext_gauss, data, extended=True)