Example #1
0
def run_minuit():
    from minuit import Minuit
    m = Minuit(chi2, b=5, a=10)
    m.migrad()
    m.hesse()
    popt = m.values.values()
    perr = np.array(m.errors.values())
    # In scipy.optimize.curve_fit, the following factor is applied
    #            cov = cov * sum_sqr / self.nfree
    factor = m.fval / (len(x) - 2)
    perr *= np.sqrt(factor)
    print ('minuit')
    print('popt: {0}'.format(popt))
    print('perr: {0}'.format(perr))
Example #2
0
# Load data
x, y, y_err = load_data()

# Define model
def power_law(x, norm, gamma):
    return norm * x ** -gamma

# Define fit statistic
def chi2(norm, gamma):
    model = power_law(x, norm, gamma)
    chi = (y - model) / y_err
    return (chi ** 2).sum()

# Perform fit
m = Minuit(chi2, norm=1e-12, gamma=2)
m.migrad()
m.hesse()
#m.minos()
#print 'matrix:\n', m.matrix()
#print 'errors: ', m.errors
#print 'merrors:', m.merrors

# Report results
package = 'minuit'
gamma, norm = m.values.values()
gamma_err, norm_err = m.errors.values()
chi2 = m.fval
cov = m.covariance[('norm', 'gamma')]
corr = m.matrix(correlation=True)[0][1]
report_results(package, norm, norm_err, gamma, gamma_err,
Example #3
0
phess = approx_hess(popt, chi2)
def approx_covar(hess, red_chi2):
    return red_chi2 * inv(phess / 2.)
red_chi2 = chi2(popt) / (len(x) - len(p0))
pcov = approx_covar(popt, red_chi2)
print 'fmin and approx_hess results:'
print 'values:', popt
print 'errors:', np.sqrt(pcov.diagonal())

"""Just to check, here is what Minuit has to say"""
from minuit import Minuit
def chi2(a, b, c):
    chi = yn - func(x, a, b, c)
    return (chi ** 2).sum()

m = Minuit(chi2, a=2.5, b=1.3, c=0.5)
m.migrad()
m.hesse()
pcov = red_chi2 * np.array(m.matrix())
popt = np.array(m.args)
print 'minuit results'
print 'values:', popt
print 'errors:', np.sqrt(pcov.diagonal())

try:
    raise
    import matplotlib.pyplot as plt
    plt.plot(x, yn, label='data')
    yfit = func(x, *popt)
    plt.plot(x, yfit, label='fit')
    plt.show()