def fit(self, function, minimizer='minuit', verbose=False): """ Fit the data with the provided function (an astromodels function) :param function: astromodels function :param minimizer: the minimizer to use :param verbose: print every step of the fit procedure :return: best fit results """ # This is a wrapper to give an easier way to fit simple data without having to go through the definition # of sources pts = PointSource("source", 0.0, 0.0, function) model = Model(pts) self.set_model(model) self._joint_like_obj = JointLikelihood(model, DataList(self), verbose=verbose) self._joint_like_obj.set_minimizer(minimizer) return self._joint_like_obj.fit()
def xy_fitted_joint_likelihood(xy_model_and_datalist): model, data = xy_model_and_datalist jl = JointLikelihood(model, data) res_frame, like_frame = jl.fit() return jl, res_frame, like_frame
def joint_likelihood_bn090217206_nai_multicomp(data_list_bn090217206_nai6): composite = Powerlaw() + Blackbody() model = get_grb_model(composite) jl = JointLikelihood(model, data_list_bn090217206_nai6, verbose=False) return jl
def joint_likelihood_bn090217206_nai(data_list_bn090217206_nai6): powerlaw = Powerlaw() model = get_grb_model(powerlaw) jl = JointLikelihood(model, data_list_bn090217206_nai6, verbose=False) return jl
def test_fit(): model, datalist = get_model_and_datalist() jl = JointLikelihood(model, datalist) jl.fit() _ = display_photometry_model_magnitudes(jl)
def test_fit(photometry_data_model): model, datalist = photometry_data_model jl = JointLikelihood(model, datalist) jl.fit() _ = display_photometry_model_magnitudes(jl) np.testing.assert_allclose([ model.grb.spectrum.main.Powerlaw.K.value, model.grb.spectrum.main.Powerlaw.index.value ], [0.00296, -1.505936], rtol=1e-3)
def restore_best_fit_model(self, interval): # Get sub-frame containing the results for the requested interval sub_frame = self._data_frame.loc[interval] # Get the model for this interval this_model = self._get_model(interval) # Get data for this interval this_data = self._get_data(interval) # Instance a useless joint likelihood object so that plugins have the chance to add nuisance parameters to the # model _ = JointLikelihood(this_model, this_data) # Restore best fit parameters for parameter in this_model.free_parameters: this_model[parameter].value = sub_frame["value"][parameter] return this_model, this_data
def worker(self, interval): # Get the dataset for this interval this_data = self._data_getter(interval) # type: DataList # Get the model for this interval this_models = self._model_getter(interval) # Apply preprocessor (if any) if self._preprocessor is not None: self._preprocessor(this_models, this_data) n_models = len(this_models) # Fit all models and collect the results parameters_frames = [] like_frames = [] analysis_results = [] for this_model in this_models: # Prepare a joint likelihood and fit it with warnings.catch_warnings(): warnings.simplefilter("ignore", RuntimeWarning) jl = JointLikelihood(this_model, this_data) this_parameter_frame, this_like_frame = self._fitter(jl) # Append results parameters_frames.append(this_parameter_frame) like_frames.append(this_like_frame) analysis_results.append(jl.results) # Now merge the results in one data frame for the parameters and one for the likelihood # values if n_models > 1: # Prepare the keys so that the first model will be indexed with model_0, the second model_1 and so on keys = ["model_%i" % x for x in range(n_models)] # Concatenate all results in one frame for parameters and one for likelihood frame_with_parameters = pd.concat(parameters_frames, keys=keys) frame_with_like = pd.concat(like_frames, keys=keys) else: frame_with_parameters = parameters_frames[0] frame_with_like = like_frames[0] return frame_with_parameters, frame_with_like, analysis_results
def test_energy_time_fit(): # Let's generate our dataset of 4 spectra with a normalization that follows # a powerlaw in time def generate_one(K): # Let's generate some data with y = Powerlaw(x) gen_function = Powerlaw() gen_function.K = K # Generate a dataset using the power law, and a # constant 30% error x = np.logspace(0, 2, 50) xyl_generator = XYLike.from_function("sim_data", function=gen_function, x=x, yerr=0.3 * gen_function(x)) y = xyl_generator.y y_err = xyl_generator.yerr # xyl = XYLike("data", x, y, y_err) # xyl.plot(x_scale='log', y_scale='log') return x, y, y_err time_tags = np.array([1.0, 2.0, 5.0, 10.0]) # This is the power law that defines the normalization as a function of time normalizations = 0.23 * time_tags**(-1.2) datasets = list(map(generate_one, normalizations)) # Now set up the fit and fit it time = IndependentVariable("time", 1.0, u.s) plugins = [] for i, dataset in enumerate(datasets): x, y, y_err = dataset xyl = XYLike("data%i" % i, x, y, y_err) xyl.tag = (time, time_tags[i]) assert xyl.tag == (time, time_tags[i], None) plugins.append(xyl) data = DataList(*plugins) spectrum = Powerlaw() spectrum.K.bounds = (0.01, 1000.0) src = PointSource("test", 0.0, 0.0, spectrum) model = Model(src) model.add_independent_variable(time) time_po = Powerlaw() time_po.K.bounds = (0.01, 1000) time_po.K.value = 2.0 time_po.index = -1.5 model.link(spectrum.K, time, time_po) jl = JointLikelihood(model, data) jl.set_minimizer("minuit") best_fit_parameters, likelihood_values = jl.fit() # Make sure we are within 10% of the expected result assert np.allclose( best_fit_parameters["value"].values, [0.25496115, -1.2282951, -2.01508341], rtol=0.1, )
def unbinned_polyfit(events: Iterable[float], grade: int, t_start: float, t_stop: float, exposure: float, bayes: bool) -> Tuple[Polynomial, float]: """ function to fit a polynomial to unbinned event data. not a member to allow parallel computation :param events: the events to fit :param grade: the polynomical order or grade :param t_start: the start time to fit over :param t_stop: the end time to fit over :param expousure: the exposure of the interval :param bayes: to do a bayesian fit or not """ log.debug(f"starting unbinned_polyfit with grade {grade}") log.debug(f"have {len(events)} events with {exposure} exposure") # create 3ML plugins and fit them with 3ML! # should eventuallly allow better config # select the model based on the grade if threeML_config.time_series.default_fit_method is not None: bayes = threeML_config.time_series.default_fit_method log.debug("using a default poly fit method") if len(events) == 0: log.debug("no events! returning zero") return Polynomial([0] * (grade + 1)), 0 shape = _grade_model_lookup[grade]() with silence_console_log(): ps = PointSource("dummy", 0, 0, spectral_shape=shape) model = Model(ps) observation = EventObservation(events, exposure, t_start, t_stop) xy = UnbinnedPoissonLike("series", observation=observation) if not bayes: # make sure the model is positive for i, (k, v) in enumerate(model.free_parameters.items()): if i == 0: v.bounds = (0, None) v.value = 10 else: v.value = 0.0 # we actually use a line here # because a constant is returns a # single number if grade == 0: shape.b = 0 shape.b.fix = True jl: JointLikelihood = JointLikelihood(model, DataList(xy)) grid_minimizer = GlobalMinimization("grid") local_minimizer = LocalMinimization("minuit") my_grid = { model.dummy.spectrum.main.shape.a: np.logspace(0, 3, 10)} grid_minimizer.setup( second_minimization=local_minimizer, grid=my_grid) jl.set_minimizer(grid_minimizer) # if the fit falis, retry and then just accept try: jl.fit(quiet=True) except(FitFailed, BadCovariance, AllFitFailed, CannotComputeCovariance): try: jl.fit(quiet=True) except(FitFailed, BadCovariance, AllFitFailed, CannotComputeCovariance): log.debug("all MLE fits failed, returning zero") return Polynomial([0]*(grade + 1)), 0 coeff = [v.value for _, v in model.free_parameters.items()] log.debug(f"got coeff: {coeff}") final_polynomial = Polynomial(coeff) final_polynomial.set_covariace_matrix(jl.results.covariance_matrix) min_log_likelihood = xy.get_log_like() else: # set smart priors for i, (k, v) in enumerate(model.free_parameters.items()): if i == 0: v.bounds = (0, None) v.prior = Log_normal(mu=np.log(5), sigma=np.log(5)) v.value = 1 else: v.prior = Gaussian(mu=0, sigma=.5) v.value = 0.1 # we actually use a line here # because a constant is returns a # single number if grade == 0: shape.b = 0 shape.b.fix = True ba: BayesianAnalysis = BayesianAnalysis(model, DataList(xy)) ba.set_sampler("emcee") ba.sampler.setup(n_iterations=500, n_burn_in=200, n_walkers=20) ba.sample(quiet=True) ba.restore_median_fit() coeff = [v.value for _, v in model.free_parameters.items()] log.debug(f"got coeff: {coeff}") final_polynomial = Polynomial(coeff) final_polynomial.set_covariace_matrix( ba.results.estimate_covariance_matrix()) min_log_likelihood = xy.get_log_like() log.debug(f"-min loglike: {-min_log_likelihood}") return final_polynomial, -min_log_likelihood
def polyfit(x: Iterable[float], y: Iterable[float], grade: int, exposure: Iterable[float], bayes: bool = False) -> Tuple[Polynomial, float]: """ function to fit a polynomial to data. not a member to allow parallel computation :param x: the x coord of the data :param y: teh y coord of the data :param grade: the polynomical order or grade :param expousure: the exposure of the interval :param bayes: to do a bayesian fit or not """ # Check that we have enough counts to perform the fit, otherwise # return a "zero polynomial" log.debug(f"starting polyfit with grade {grade} ") if threeML_config.time_series.default_fit_method is not None: bayes = threeML_config.time_series.default_fit_method log.debug("using a default poly fit method") nan_mask = np.isnan(y) y = y[~nan_mask] x = x[~nan_mask] exposure = exposure[~nan_mask] non_zero_mask = y > 0 n_non_zero = non_zero_mask.sum() if n_non_zero == 0: log.debug("no counts, return 0") # No data, nothing to do! return Polynomial([0.0]*(grade+1)), 0.0 # create 3ML plugins and fit them with 3ML! # should eventuallly allow better config # seelct the model based on the grade shape = _grade_model_lookup[grade]() ps = PointSource("_dummy", 0, 0, spectral_shape=shape) model = Model(ps) avg = np.mean(y/exposure) log.debug(f"starting polyfit with avg norm {avg}") with silence_console_log(): xy = XYLike("series", x=x, y=y, exposure=exposure, poisson_data=True, quiet=True) if not bayes: # make sure the model is positive for i, (k, v) in enumerate(model.free_parameters.items()): if i == 0: v.bounds = (0, None) v.value = avg else: v.value = 0.0 # we actually use a line here # because a constant is returns a # single number if grade == 0: shape.b = 0 shape.b.fix = True jl: JointLikelihood = JointLikelihood(model, DataList(xy)) jl.set_minimizer("minuit") # if the fit falis, retry and then just accept try: jl.fit(quiet=True) except(FitFailed, BadCovariance, AllFitFailed, CannotComputeCovariance): log.debug("1st fit failed") try: jl.fit(quiet=True) except(FitFailed, BadCovariance, AllFitFailed, CannotComputeCovariance): log.debug("all MLE fits failed") pass coeff = [v.value for _, v in model.free_parameters.items()] log.debug(f"got coeff: {coeff}") final_polynomial = Polynomial(coeff) try: final_polynomial.set_covariace_matrix( jl.results.covariance_matrix) except: log.exception(f"Fit failed in channel") raise FitFailed() min_log_likelihood = xy.get_log_like() else: # set smart priors for i, (k, v) in enumerate(model.free_parameters.items()): if i == 0: v.bounds = (0, None) v.prior = Log_normal( mu=np.log(avg), sigma=np.max([np.log(avg/2), 1])) v.value = 1 else: v.prior = Gaussian(mu=0, sigma=2) v.value = 1e-2 # we actually use a line here # because a constant is returns a # single number if grade == 0: shape.b = 0 shape.b.fix = True ba: BayesianAnalysis = BayesianAnalysis(model, DataList(xy)) ba.set_sampler("emcee") ba.sampler.setup(n_iterations=500, n_burn_in=200, n_walkers=20) ba.sample(quiet=True) ba.restore_median_fit() coeff = [v.value for _, v in model.free_parameters.items()] log.debug(f"got coeff: {coeff}") final_polynomial = Polynomial(coeff) final_polynomial.set_covariace_matrix( ba.results.estimate_covariance_matrix()) min_log_likelihood = xy.get_log_like() log.debug(f"-min loglike: {-min_log_likelihood}") return final_polynomial, -min_log_likelihood
def test_ubinned_poisson_full(event_observation_contiguous, event_observation_split): s = Line() ps = PointSource("s", 0, 0, spectral_shape=s) s.a.bounds = (0, None) s.a.value = .1 s.b.value = .1 s.a.prior = Log_normal(mu=np.log(10), sigma=1) s.b.prior = Gaussian(mu=0, sigma=1) m = Model(ps) ###### ###### ###### ub1 = UnbinnedPoissonLike("test", observation=event_observation_contiguous) jl = JointLikelihood(m, DataList(ub1)) jl.fit(quiet=True) np.testing.assert_allclose([s.a.value, s.b.value], [6.11, 1.45], rtol=.5) ba = BayesianAnalysis(m, DataList(ub1)) ba.set_sampler("emcee") ba.sampler.setup(n_burn_in=100, n_walkers=20, n_iterations=500) ba.sample(quiet=True) ba.restore_median_fit() np.testing.assert_allclose([s.a.value, s.b.value], [6.11, 1.45], rtol=.5) ###### ###### ###### ub2 = UnbinnedPoissonLike("test", observation=event_observation_split) jl = JointLikelihood(m, DataList(ub2)) jl.fit(quiet=True) np.testing.assert_allclose([s.a.value, s.b.value], [2., .2], rtol=.5) ba = BayesianAnalysis(m, DataList(ub2)) ba.set_sampler("emcee") ba.sampler.setup(n_burn_in=100, n_walkers=20, n_iterations=500) ba.sample(quiet=True) ba.restore_median_fit() np.testing.assert_allclose([s.a.value, s.b.value], [2., .2], rtol=.5)