def _set_indexed_fits(self, minimizer): _data = TestMultiFit._get_indexed_data() self._fit_indexed_all = TestMultiFit._get_indexed_fit( _data, TestMultiFit.quadratic_model_indexed_all, minimizer) _split_data_1 = _data[:50] _split_data_2 = _data[50:] self._fit_indexed_all_multi = MultiFit( TestMultiFit._get_indexed_fit( _data, TestMultiFit.quadratic_model_indexed_all, minimizer)) self._fit_indexed_split_multi = MultiFit(fit_list=[ TestMultiFit._get_indexed_fit( _split_data_1, TestMultiFit.quadratic_model_indexed_split_1, minimizer), TestMultiFit._get_indexed_fit( _split_data_2, TestMultiFit.quadratic_model_indexed_split_2, minimizer) ]) self._fit_indexed_split_1 = TestMultiFit._get_indexed_fit( _split_data_1, TestMultiFit.quadratic_model_indexed_split_1, minimizer) self._fit_indexed_split_1_multi = MultiFit( TestMultiFit._get_indexed_fit( _split_data_1, TestMultiFit.quadratic_model_indexed_split_1, minimizer)) self._fit_indexed_split_2 = TestMultiFit._get_indexed_fit( _split_data_2, TestMultiFit.quadratic_model_indexed_split_2, minimizer) self._fit_indexed_split_2_multi = MultiFit( TestMultiFit._get_indexed_fit( _split_data_2, TestMultiFit.quadratic_model_indexed_split_2, minimizer))
def _set_indexed_fits(self): _data = TestMultiFit._get_indexed_data() self._fit_indexed_all = self._get_indexed_fit( _data, TestMultiFit.quadratic_model_indexed_all) _split_data_1 = _data[:50] _split_data_2 = _data[50:] self._fit_indexed_all_multi = MultiFit([ self._get_indexed_fit(_data, TestMultiFit.quadratic_model_indexed_all) ]) self._fit_indexed_split_multi = MultiFit(fit_list=[ self._get_indexed_fit( _split_data_1, TestMultiFit.quadratic_model_indexed_split_1), self._get_indexed_fit(_split_data_2, TestMultiFit.quadratic_model_indexed_split_2) ]) self._fit_indexed_split_1 = self._get_indexed_fit( _split_data_1, TestMultiFit.quadratic_model_indexed_split_1) self._fit_indexed_split_1_multi = MultiFit([ self._get_indexed_fit(_split_data_1, TestMultiFit.quadratic_model_indexed_split_1) ]) self._fit_indexed_split_2 = self._get_indexed_fit( _split_data_2, TestMultiFit.quadratic_model_indexed_split_2) self._fit_indexed_split_2_multi = MultiFit([ self._get_indexed_fit(_split_data_2, TestMultiFit.quadratic_model_indexed_split_2) ])
def _set_hist_fits(self): _raw_data = TestMultiFit._get_hist_data() self._fit_hist_all = self._get_hist_fit(_raw_data) _split_data_1, _split_data_2 = self._split_data(_raw_data) self._fit_hist_all_multi = MultiFit([self._get_hist_fit(_raw_data)]) self._fit_hist_split_multi = MultiFit(fit_list=[ self._get_hist_fit(_split_data_1), self._get_hist_fit(_split_data_2), ]) self._fit_hist_split_1 = self._get_hist_fit(_split_data_1) self._fit_hist_split_1_multi = MultiFit( [self._get_hist_fit(_split_data_1)]) self._fit_hist_split_2 = self._get_hist_fit(_split_data_2) self._fit_hist_split_2_multi = MultiFit( [self._get_hist_fit(_split_data_2)])
def _set_xy_fits(self, minimizer): _xy_data = TestMultiFit._get_xy_data() self._fit_xy_all = TestMultiFit._get_xy_fit(_xy_data, minimizer) _split_data_1, _split_data_2 = TestMultiFit._split_data(_xy_data, axis=1) self._fit_xy_all_multi = MultiFit( TestMultiFit._get_xy_fit(_xy_data, minimizer)) self._fit_xy_split_multi = MultiFit(fit_list=[ TestMultiFit._get_xy_fit(_split_data_1, minimizer), TestMultiFit._get_xy_fit(_split_data_2, minimizer) ]) self._fit_xy_split_1 = TestMultiFit._get_xy_fit( _split_data_1, minimizer) self._fit_xy_split_1_multi = MultiFit( TestMultiFit._get_xy_fit(_split_data_1, minimizer)) self._fit_xy_split_2 = TestMultiFit._get_xy_fit( _split_data_2, minimizer) self._fit_xy_split_2_multi = MultiFit( TestMultiFit._get_xy_fit(_split_data_2, minimizer)) self._fit_xy_all_double = MultiFit([ TestMultiFit._get_xy_fit(_xy_data, minimizer), TestMultiFit._get_xy_fit(_xy_data, minimizer) ]) self._expected_parameters_all = calculate_expected_fit_parameters_xy( x_data=_xy_data[0], y_data=_xy_data[1], model_function=quadratic_model, y_error=0.01, initial_parameter_values=[1.0, 1.0, 1.0], x_error=0.01, model_function_derivative=quadratic_model_derivative) self._expected_parameters_split_1 = calculate_expected_fit_parameters_xy( x_data=_split_data_1[0], y_data=_split_data_1[1], model_function=quadratic_model, y_error=0.01, initial_parameter_values=[1.0, 1.0, 1.0], x_error=0.01, model_function_derivative=quadratic_model_derivative) self._expected_parameters_split_2 = calculate_expected_fit_parameters_xy( x_data=_split_data_2[0], y_data=_split_data_2[1], model_function=quadratic_model, y_error=0.01, initial_parameter_values=[1.0, 1.0, 1.0], x_error=0.01, model_function_derivative=quadratic_model_derivative)
def construct_multi_fit(shared_x_error): fit_1 = XYFit(xy_data=[U, T], model_function=empirical_T_U_model) fit_1.add_error(axis='y', err_val=sigT) # declare errors on T fit_2 = XYFit(xy_data=[U, I], model_function=I_U_model) fit_2.add_error(axis='y', err_val=sigI) # declare errors on I multi_fit = MultiFit(fit_list=[fit_1, fit_2], minimizer='iminuit') if shared_x_error: multi_fit.add_error(axis='x', err_val=sigU, fits='all') else: fit_1.add_error(axis='x', err_val=sigU) fit_2.add_error(axis='x', err_val=sigU) return multi_fit
def _set_hist_fits(self, minimizer): _raw_data = TestMultiFit._get_hist_data() self._fit_hist_all = TestMultiFit._get_hist_fit(_raw_data, minimizer) _split_data_1, _split_data_2 = TestMultiFit._split_data(_raw_data) self._fit_hist_all_multi = MultiFit( TestMultiFit._get_hist_fit(_raw_data, minimizer)) self._fit_hist_split_multi = MultiFit(fit_list=[ TestMultiFit._get_hist_fit(_split_data_1, minimizer), TestMultiFit._get_hist_fit(_split_data_2, minimizer), ]) self._fit_hist_split_1 = TestMultiFit._get_hist_fit( _split_data_1, minimizer) self._fit_hist_split_1_multi = MultiFit( TestMultiFit._get_hist_fit(_split_data_1, minimizer)) self._fit_hist_split_2 = TestMultiFit._get_hist_fit( _split_data_2, minimizer) self._fit_hist_split_2_multi = MultiFit( TestMultiFit._get_hist_fit(_split_data_2, minimizer))
def _set_xy_fits(self): self._xy_data = TestMultiFit._get_xy_data() self._fit_xy_all = self._get_xy_fit(self._xy_data) self._split_data_1, self._split_data_2 = TestMultiFit._split_data( self._xy_data, axis=1) self._fit_xy_all_multi = MultiFit([self._get_xy_fit(self._xy_data)]) self._fit_xy_split_multi = MultiFit(fit_list=[ self._get_xy_fit(self._split_data_1), self._get_xy_fit(self._split_data_2) ]) self._fit_xy_split_1 = self._get_xy_fit(self._split_data_1) self._fit_xy_split_1_multi = MultiFit( [self._get_xy_fit(self._split_data_1)]) self._fit_xy_split_2 = self._get_xy_fit(self._split_data_2) self._fit_xy_split_2_multi = MultiFit( [self._get_xy_fit(self._split_data_2)]) self._fit_xy_all_double = MultiFit( [self._get_xy_fit(self._xy_data), self._get_xy_fit(self._xy_data)])
def _get_multifit( self, hist_fit=True, xy_fit_1=True, indexed_fit=True, xy_fit_2=True, reverse=False): _fits = [] if hist_fit: _hist_container = HistContainer( n_bins=10, bin_range=(-5, 5), fill_data=self._hist_data) _hist_fit = HistFit(_hist_container) _fits.append(_hist_fit) if xy_fit_1: _xy_fit_1 = XYFit(xy_data=[self._x_data, self._y_data_1]) _xy_fit_1.add_error("y", 1.0) _fits.append(_xy_fit_1) if indexed_fit: _indexed_fit = IndexedFit( self._indexed_data, TestSharedErrorLogic.indexed_model_function) _indexed_fit.add_error(1.0) _fits.append(_indexed_fit) if xy_fit_2: _xy_fit_2 = XYFit(xy_data=[self._x_data, self._y_data_2]) _xy_fit_2.add_error("y", 1.0) _fits.append(_xy_fit_2) if reverse: _fits = reversed(_fits) return MultiFit(_fits)
xy_d1.axis_labels = ['x', 'y (1)'] xy_d2.label = 'Beispieldaten (2)' xy_d2.axis_labels = ['x', 'y(2) & f(x)'] # 3. create the Fit objects xyFit1 = Fit(xy_d1, model1) xyFit2 = Fit(xy_d2, model2) # set meaningful names for model xyFit1.model_label = 'Lineares Modell' xyFit2.model_label = 'Lineares Modell' # add the parameter constraints xyFit1.add_parameter_constraint(name='g1', value=c1, uncertainty=ec1) xyFit2.add_parameter_constraint(name='g2', value=c2, uncertainty=ec2) # combine the two fit objects to form a MultiFit multiFit = MultiFit(fit_list=[xyFit1, xyFit2]) # 4. perform the fit multiFit.do_fit() # 5. report fit results multiFit.report() # 6. create and draw plots multiPlot = Plot(multiFit) ##multiPlot = Plot(multiFit, separate_figures=True) multiPlot.plot(figsize=(13., 7.)) # 7. show or save plots # ##for i, fig in enumerate(multiPlot.figures): ## fig.savefig("MultiFit-"+str(i)+".pdf")
sigU, sigI, sigT = 0.1, 0.1, 0.1 # uncertainties T0 = 273.15 # 0 degrees C as absolute Temperature (in Kelvin) T -= T0 # Measurements are in Kelvin, convert to °C # -- Finally, go through the fitting procedure # Step 1: construct the singular fit objects fit_1 = XYFit(xy_data=[U, T], model_function=empirical_T_U_model) fit_1.add_error(axis='y', err_val=sigT) # declare errors on T fit_2 = XYFit(xy_data=[U, I], model_function=I_U_model) fit_2.add_error(axis='y', err_val=sigI) # declare errors on I # Step 2: construct a MultiFit object multi_fit = MultiFit(fit_list=[fit_1, fit_2], minimizer='iminuit') # Step 3: Add a shared error error for the x axis. multi_fit.add_error(axis='x', err_val=sigU, fits='all') # (Optional): assign names for models and parameters multi_fit.assign_parameter_latex_names(x='U', p2='p_2', p1='p_1', p0='p_0', R0='R_0', alph=r'\alpha_\mathrm{T}') multi_fit.assign_model_function_expression('{1}*{x}^2 + {2}*{x} + {3}', fit_index=0) multi_fit.assign_model_function_latex_expression(