yerr=u_period, marker="x", linestyle="None", color="black", label="experimental data") ax.set(xlabel='count', ylabel="Period (s)") 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,
__filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'quiz_data.csv') data = pandas.read_csv(__filepath).to_numpy() except: data = "Warning: failed to import data from csv file. Possibly the monashspa library is not installed correctly." 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())
# Fill these arrays with the relative resolution and the uncertainty on that estimate for each of the # particle energies rel_resolutions_withall = [] u_rel_resolutions_withall = [] from lmfit import fit_report from scipy import stats from monashspa.common.fitting import linear_fit, get_fit_parameters, make_lmfit_model, model_fit from monashspa.common.figures import savefig name = 'Energy resolution' model = make_lmfit_model("sqrt(a*a/x + b*b + c*c/(x*x))") params = model.make_params(a=0.09, b=0.03, c=3) fit_results = model_fit(model, params, particle_energies, rel_resolutions_withall, u_y=u_rel_resolutions_withall) # 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))
except FileNotFoundError: print( "ERROR: Failed to read CSV file {file} with data. It should be in the same folder {folder} as the Python file" .format(file=datafile, folder=folder)) if type(data) == type(None): sys.exit(1) volume = data[:, 0] u_volume = data[:, 1] temp = data[:, 2] # Create the model and run fit gas_model = make_lmfit_model(model) gas_params = gas_model.make_params(R=0.01) gas_fit_results = model_fit(gas_model, gas_params, temp, volume, u_y=u_volume) # Extract result and print nicely gas_fit = gas_fit_results.best_fit u_gas_fit = gas_fit_results.eval_uncertainty(sigma=1) gas_fit_parameters = get_fit_parameters(gas_fit_results) pvalue = 1.0 - stats.chi2.cdf(gas_fit_results.chisqr, gas_fit_results.ndata - gas_fit_results.nvarys) print(""" [[{name}]] ================= p-value = {pvalue:.2E} """.format(name=name, pvalue=pvalue)) print(gas_fit_results.fit_report())
xval = np.arange(start=-5.0, stop=5.0, step=0.8) u_yval = 0.8 + xval * xval / 15. yval = 1.5 * xval + np.random.normal(0.0, u_yval) ####################################################################### # 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())