def test_mncontour(): m = Minuit(f1, 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) m.minos() m.draw_mncontour('x','y')
# <markdowncell> # ###Contour and $\chi^2$/Likelihood profile # $\chi^2$ and contour can be obtained easily # <codecell> #minos contour xminos, zminos, ctr = m.mncontour('x','z') fill(*zip(*ctr)) #looks kinda ugly, right? # <codecell> #drawing it nicely m.draw_mncontour('x','z', nsigma=4); # <codecell> #or you can get the gridded data x, y, g, r = m.mncontour_grid('x','z', nsigma=3) # r is the raw data pcolormesh(x,y,g) colorbar() # <codecell> #1D value Scan x,y = m.profile('x',subtract_min=True); plot(x,y) #if you have matplotlib # <codecell>
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")
# <codecell> m.minos(); #do m.minos('x') if you need just 1 of them # <codecell> m.merrors # <markdowncell> # ###Contour and Profile(Scan) # <codecell> m.draw_mncontour('x','y') # you can get the raw value using m.mncontour or m.mncontour_grid; # <codecell> m.draw_profile('x') # this is 1d evaluation not minos scan; # <markdowncell> # ####Initial value, Limit, initial error, fixing # <codecell> m = Minuit(f, x=2, y=4, fix_y=True, limit_z=(-10,10), error_z=0.1) # <codecell>
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')
from iminuit import Minuit def cost(x, y, z): return (x - 1)**2 + (y - x)**2 + (z - 2)**2 m = Minuit(cost, print_level=0, pedantic=False) m.migrad() m.draw_mncontour("x", "y", nsigma=4)
def Hubble_diagram(zcmb, zhel, mB, dmB, X1, dX1, C, dC, M_stell, IDJLA, exp, results, cov_path, sigma_mu_path, fixe, omgM=0.295, alpha=0.141, beta=3.101, Mb=-19.05, delta_M=-0.070): """ Function which make the hubble fit of the given compilation. inputs : -omgM: 1st free parameter to be fitted initialized to the 0.295 value if not precised -alpha: 2nd free parameter to be fitted initialized to the 0.141 value if not precised -beta: 3rd free parameter to be fitted initialized to the 3.101 value if not precised -Mb: 4th free parameter to be fitted initialized to the -19.05 value if not precised -delta_M: 5th free parameter to be fitted initialized to the -0.070 value if not precised -fixe: when fixe=0 omgK and w are fixe, fixe = 1 w is fixe, fixe =2 omgK is fixe else all parameters are fixe -zcmb: an array which contains the redshifts of the SNs -mB: B band peak magnitude of the SNs -dmB: uncertainty on mB -X1: SALT2 shape parameter -dX1: uncertainty on X1Cmu[N.diag_indices_from(Cmu)] -dC: uncertainty on C -C: colour parameter -M_stell: the log_10 host stellar mass in units of solar mass -IDJLA: index of the SNs from the 740 of jla used for the fit -results : a file where some results wiil be written - m : the iminuit object (see doc of iminuit for more information). -ecarts5 : the residuals of the fit """ #check : need to have at least 2 SNe '''f2,(test,P2,P3,P4,P5) = P.subplots(5, sharex=True, sharey=False, gridspec_kw=dict(height_ratios=[3,1,1,1,1]))''' if len(zcmb) == 1 or len(zcmb) == 0: results.write('Not enough data \n') return 0 #Definition of the Chi2 object chi2mini = Chi2(zcmb, zhel, mB, dmB, X1, dX1, C, dC, M_stell, IDJLA, cov_path, sigma_mu_path) #minimisation of the chi2 ''' the values of the free parameter can be initialized. ''' # TO BE CLEANED if fixe == 0: m = Minuit(chi2mini.chi2, omgM=0.295, omgK=0, w=-1, alpha=0.141, beta=3.101, Mb=-19.05, delta_M=-0.070, limit_omgM=(0.2, 0.4), limit_omgK=(-1, 1), limit_w=(-1.4, 0.4), limit_alpha=(0.1, 0.2), limit_beta=(2.0, 4.0), limit_Mb=(-20., -18.), limit_delta_M=(-0.1, -0.0), fix_omgM=False, fix_omgK=True, fix_w=True, fix_alpha=False, fix_beta=False, fix_Mb=False, fix_delta_M=False, print_level=1) elif fixe == 1: # last_iter_args={u'fix_Mb': True, u'limit_w': None, u'fix_delta_M': True, u'error_omgL': 0.1672842193144214, u'error_omgM': 0.11284796124765262, 'delta_M': -0.07040462309885408, u'error_Mb': 0.02559618467415318, u'error_w': 1.0, u'error_alpha': 0.006593439955397084, u'fix_alpha': True, u'limit_alpha': None, 'Mb': -19.038807526119403, u'limit_omgM': None, u'limit_omgL': None, u'limit_Mb': None, 'beta': 3.0999314004965535, u'limit_delta_M': None, u'limit_beta': None, 'alpha': 0.14098769528058613, 'omgM': 0.20020038670359183, 'omgL': 0.561065865545341, u'fix_w': True, u'fix_beta': True, u'fix_omgM': False, u'fix_omgL': False, u'error_delta_M': 0.023111327883836057, u'error_beta': 0.08066293365248206, 'w': -1.0} # m=Minuit(chi2mini.chi2,**last_iter_args) # m=Minuit(chi2mini.chi2,omgM=0.2,omgL=0.55,w=-1,alpha=0.141,beta=3.101,Mb=-19.05,delta_M=-0.070,limit_omgM=(0,0.4),limit_omgL=(-1,1),limit_w=(-1.4,0.4),limit_alpha=(0.1,0.2),limit_beta=(2.0,4.0),limit_Mb=(-20.,-18.),limit_delta_M=(-0.1,-0.0),fix_omgM=False,fix_omgL=False,fix_w=True, fix_alpha=False, fix_beta=False, fix_Mb=False, fix_delta_M=False, print_level=1) m = Minuit(chi2mini.chi2, omgM=0.2, omgL=0.55, w=-1, alpha=0.141, beta=3.101, Mb=-19.05, delta_M=-0.070, limit_omgM=(0, 0.4), limit_omgL=(-1, 1), limit_w=(-1.4, 0.4), limit_alpha=(0.1, 0.2), limit_beta=(2.0, 4.0), limit_Mb=(-20., -18.), limit_delta_M=(-0.1, -0.0), fix_omgM=False, fix_omgL=False, fix_w=True, fix_alpha=True, fix_beta=True, fix_Mb=True, fix_delta_M=True, print_level=1) #m=Minuit(chi2mini.chi2,omgM=0.2,omgL=0.55,w=-1,alpha=0.141,beta=3.101,Mb=-19.05,delta_M=-0.070,fix_omgM=False,fix_omgL=False,fix_w=True, fix_alpha=False, fix_beta=False, fix_Mb=False, fix_delta_M=False, print_level=1) elif fixe == 2: m = Minuit(chi2mini.chi2, omgM=0.2, omgK=0, w=-1, alpha=0.141, beta=3.101, Mb=-19.05, delta_M=-0.070, limit_omgM=(0, 0.4), limit_omgK=(-1, 1), limit_w=(-1.4, 0.4), limit_alpha=(0.1, 0.2), limit_beta=(2.0, 4.0), limit_Mb=(-20., -18.), limit_delta_M=(-0.1, -0.0), fix_omgM=False, fix_omgK=True, fix_w=False, fix_alpha=False, fix_beta=False, fix_Mb=False, fix_delta_M=False, print_level=1) else: m = Minuit(chi2mini.chi2, omgM=0.2, omgK=0, w=-1, alpha=0.141, beta=3.101, Mb=-19.05, delta_M=-0.070, limit_omgM=(0, 0.4), limit_omgK=(-1, 1), limit_w=(-1.4, 0.4), limit_alpha=(0.1, 0.2), limit_beta=(2.0, 4.0), limit_Mb=(-20., -18.), limit_delta_M=(-0.1, -0.0), fix_omgM=True, fix_omgK=True, fix_w=True, fix_alpha=True, fix_beta=True, fix_Mb=True, fix_delta_M=True, print_level=1) m.migrad() return m m.hesse() chi2miniminuit = chi2mini.chi2tot #minimun chi2 value obtain by iminuit #print(m.values) #print(m.errors) omgM = m.args[0] omgK = m.args[1] w = m.args[2] alpha = m.args[3] beta = m.args[4] Mb = m.args[5] delta_M = m.args[6] #errors obtain by minuit omgMerr = m.errors.get('omgM') omgKerr = m.errors.get('omgL') werr = m.errors.get('w') alphaerr = m.errors.get('alpha') betaerr = m.errors.get('beta') Mberr = m.errors.get('Mb') delta_Merr = m.errors.get('delta_M') # cov_omg=m.hesse() # cov_omg=m.matrix() if fixe == 1: # Pcov = P.plot() # bX,bY,contourXY = m.mncontour('omgM', 'omgL', numpoints=20, sigma=1.0) # bX,bY,contourXY = m.contour('omgM', 'omgL', bins=20, bound=2) # contourXY -= chi2miniminuit # Pcov=m.draw_contour('omgM', 'omgL', bins=20, bound=1) Pcov = m.draw_mncontour('omgM', 'omgL') # X,Y=N.meshgrid(N.linspace(0,0.3,1000),N.linspace(0,0.5,1000)) # Pcov = P.contour(bX,bY,contourXY,[1,2,9]) # P.show() elif fixe == 2: Pcov = P.plot() # bins_X,bins_Y,contour_X_Y = m.contour('omgM', 'w', bins=20, bound=1) m.draw_contour('omgM', 'w', bins=20, bound=1) P.show() #write some results in output results.write('\\begin{tabular}{|c|l|}' + '\n' + '\\hline' + '\n') results.write('chi2 / d.o.f& %.3f' % ((chi2mini.chi2tot) / (len(IDJLA) - 5)) + ' \\\\ \\hline \n') results.write('chi2& %.3f' % (chi2mini.chi2tot) + ' \\\\ \\hline \n') #Computation of the luminosity-distance modulus and its uncertainty for the minimized parameters mufinal = muexp(mB, X1, C, alpha, beta, Mb, delta_M, M_stell) dmufinal = dmuexp(dmB, dX1, dC, alpha, beta) #Computation of the difference between the measured luminosity-distance modulus and the theorical one (residuals) ecarts5 = ecarts(zcmb, zhel, mufinal, omgM, omgK, w) #computation of the RMS,mean and their error of the distribution #std5,std5err = comp_rms(N.array(ecarts5),(len(ecarts5))) std5 = N.std(ecarts5) std5err = RMSerr(ecarts5) mean5 = N.mean(ecarts5) mean5err = MEANerr(ecarts5) results.write('mean of the residuals& %.5f' % mean5 + ' \\\\ \\hline \n') sigma_mean5 = std5 / N.sqrt(len(ecarts5)) results.write('rms& %.4f' % (std5) + ' \\\\ \\hline \n') #mean = N.mean(ecarts5) #Computaion of the rms of the fit. rms, err_rms = comp_rms(N.array(ecarts5), len(mB), err=True, variance=None) results.write('omgM& %.4f' % (omgM) + ' +/- %.4f' % (omgMerr) + ' \\\\ \\hline \n') results.write('omgK& %.4f' % (omgK) + ' +/- %.4f' % (omgKerr) + ' \\\\ \\hline \n') results.write('w& %.4f' % (w) + ' +/- %.4f' % (werr) + ' \\\\ \\hline \n') results.write('alpha& %.4f' % (alpha) + ' +/- %.4f' % (alphaerr) + ' \\\\ \\hline \n') results.write('beta& %.4f' % (beta) + ' +/- %.4f' % (betaerr) + ' \\\\ \\hline \n') results.write('Mb& %.4f' % (Mb) + ' +/- %.4f' % (Mberr) + ' \\\\ \\hline \n') results.write('delta-M& %.4f' % (delta_M) + ' +/- %.4f' % (delta_Merr) + ' \\\\ \\hline \n') results.write('\\end{tabular}') return m
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')