reference=1 * u.TeV) print(pwl) # In[ ]: livetime = 2 * u.h sim = SpectrumSimulation(aeff=aeff, edisp=edisp, source_model=pwl, livetime=livetime) sim.simulate_obs(seed=2309, obs_id=1) print(sim.obs) # In[ ]: fit = SpectrumFit(obs_list=sim.obs, model=pwl.copy(), stat="cash") fit.fit_range = [1, 10] * u.TeV fit.run() print(fit.result[0]) # ## Include background # # In this section we will include a background component. Furthermore, we will also simulate more than one observation and fit each one individuallt in order to get average fit results. # In[ ]: bkg_model = PowerLaw(index=2.5, amplitude=1e-11 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV) # In[ ]:
#print(sim2.result) sim2 = sim1 Indiv_best_fit_index = [] best_fit_index = [] i = 0 while i < n_obs: sim_result = (sim1.result[i], sim2.result[i]) #FIT SPECTRA: pwl.parameters['index'].parmax = 10 for obs in sim_result: fit = SpectrumFit(obs, pwl.copy(), stat='wstat') fit.model.parameters['index'].value = 2 fit.fit() fit.est_errors() Indiv_best_fit_index.append( fit.result[0].model.parameters['index'].value) #print(fit.result[0]) #print(' Indiv_best_fit_index = ', Indiv_best_fit_index) #i+=1 #STACK SPECTRA 2 by 2 # Add the two spectra obs_stacker = SpectrumObservationStacker(sim_result) #print('sim_result is the list = ',sim_result) obs_stacker.run()
# In[ ]: model = PowerLaw(index=2, amplitude=2e-11 * u.Unit("cm-2 s-1 TeV-1"), reference=1 * u.TeV) datasets_joint = extraction.spectrum_observations for dataset in datasets_joint: dataset.model = model fit_joint = Fit(datasets_joint) result_joint = fit_joint.run() # we make a copy here to compare it later model_best_joint = model.copy() model_best_joint.parameters.covariance = result_joint.parameters.covariance # In[ ]: print(result_joint) # In[ ]: plt.figure(figsize=(8, 6)) ax_spectrum, ax_residual = datasets_joint[0].plot_fit() ax_spectrum.set_ylim(0, 25) # ## Compute Flux Points # # To round up our analysis we can compute flux points by fitting the norm of the global model in energy bands. We'll use a fixed energy binning for now:
print(pwl) livetime = 2 * u.h # In[5]: sim = SpectrumSimulation(aeff=aeff, edisp=edisp, source_model=pwl, livetime=livetime) sim.simulate_obs(seed=2309, obs_id=1) print(sim.obs) # In[6]: fit = SpectrumFit(obs_list=sim.obs, model=pwl.copy(), stat='cash') fit.fit_range = [1, 10] * u.TeV fit.fit() fit.est_errors() print(fit.result[0]) # ## Include background # # In this section we will include a background component. Furthermore, we will also simulate more than one observation and fit each one individuallt in order to get average fit results. # In[7]: bkg_index = 2.5 * u.Unit('') bkg_amplitude = 1e-11 * u.Unit('cm-2 s-1 TeV-1') reference = 1 * u.TeV