예제 #1
0
def test_linear_fit_with_uncertainties():
    """Basic test of linear fit with uncertainties"""

    ### Get results ###
    from monashspa.common.fitting import linear_fit, get_fit_parameters

    data = [
        [1, 2.97958155926148, 0.435952175570949],
        [2, 4.84845692653319, 0.467234424802873],
        [3, 6.44561871392239, 0.740203944022575],
        [4, 8.72544337668039, 0.509084442407005],
        [5, 10.7326357174404, 0.557439432218125],
        [6, 12.3202409005262, 0.0236735417569119],
    ]
    data = np.array(data)

    fit_result = linear_fit(data[:, 0], data[:, 1], u_y=data[:, 2])
    results = get_fit_parameters(fit_result)

    ### Expected results (from WFIT) ###
    expected_results = {
        'slope': 1.866046353,
        'u_slope': 0.064841588,
        'intercept': 1.124423955,
        'u_intercept': 0.387570521,
    }
    precision = 2e-8

    ### Check results match within precision ###
    success = compare_dictionary(results, expected_results, precision)

    return success
예제 #2
0
fig.suptitle('Pendulum period')
savefig('pendulum_data.png')

# Let us assume that the period is constant

pendulum_model = make_lmfit_model("T", allow_constant_model=True)
pendulum_params = pendulum_model.make_params(T=2.0)
pendulum_fit_results = model_fit(pendulum_model,
                                 pendulum_params,
                                 count,
                                 period,
                                 u_y=u_period)

pendulum_fit = pendulum_fit_results.best_fit
u_pendulum_fit = pendulum_fit_results.eval_uncertainty(sigma=1)
pendulum_fit_parameters = get_fit_parameters(pendulum_fit_results)
pvalue = 1.0 - stats.chi2.cdf(
    pendulum_fit_results.chisqr,
    pendulum_fit_results.ndata - pendulum_fit_results.nvarys)

print("""
[[Simple pendulum]]
===================
  p-value       = {pvalue:.2E}
""".format(pars=pendulum_fit_parameters, f=pendulum_fit_results,
           pvalue=pvalue))
print(pendulum_fit_results.fit_report())

pendulum_fit = np.full(len(count), pendulum_fit)
u_pendulum_fit = np.full(len(count), u_pendulum_fit[0])
예제 #3
0
x = data[:,0]
u_x = data[:,1]
temp = data[:,2]

name = "linear model"

# Create the model and run fit
model = make_lmfit_model("a*x + b")
params = model.make_params(a=1.0, b=0.0)
fit_results = model_fit(model, params, temp, x, u_y=u_x)

# Extract result and print nicely
fit = fit_results.best_fit
u_fit = fit_results.eval_uncertainty(sigma=1)
fit_parameters = get_fit_parameters(fit_results)
pvalue = 1.0 - stats.chi2.cdf(fit_results.chisqr,
                              fit_results.ndata-fit_results.nvarys)

print("""
[[{name}]]
=================
  p-value       = {pvalue:.2E}
""".format(name=name, pvalue=pvalue))
print(fit_results.fit_report())


# Create some plots
fig, ax = plt.subplots(1)
ax.errorbar(temp, x, yerr=u_x, marker="x", linestyle="None", color="black",label="experimental data")
ax.plot(temp, fit, marker="None", linestyle="-", color="black",label="fit to {name}".format(name=name))
#######################################################################

# Name of the model that you fit
name = "linear model"

#######################################################################

# Create the model and run fit
model = make_lmfit_model("a*x + b")
params = model.make_params(a=1.0, b=0.0)
fit_results = model_fit(model, params, xval, yval, u_y=u_yval)

# Extract result and print nicely
fit = fit_results.best_fit
u_fit = fit_results.eval_uncertainty(sigma=1)
fit_parameters = get_fit_parameters(fit_results)
pvalue = 1.0 - stats.chi2.cdf(fit_results.chisqr,
                              fit_results.ndata - fit_results.nvarys)

print("""
[[{name}]]
=================
  p-value       = {pvalue:.2E}
""".format(name=name, pvalue=pvalue))
print(fit_results.fit_report())

# Create some plots
fig, ax = plt.subplots(1)
ax.errorbar(xval,
            yval,
            yerr=u_yval,