def _compute(self): """ Wrap up calculating the flux upper limit for a powerlaw source. This function employes the pyLikelihood function IntegralUpperLimit to calculate a Bayesian upper limit. The primary benefit of this function is that it replaces the spectral model automatically with a PowerLaw spectral model and fixes the index to -2. It then picks a better scale for the powerlaw and gives the upper limit calculation a more reasonable starting value, which helps the convergence. """ if self.verbosity: print 'Calculating gtlike power-law upper limit' like = self.like name = self.name saved_state = SuperState(like) e = np.sqrt(self.emin*self.emax) """ I had tons of trouble getting a robust fitting algorithm. The problem with computing upper limits is (a) Getting an initial fit of the region (with the spectral index fixed) to converge (b) Getting the upper limit to integrate over a good range. This is what I found to be most robust way to compute upper limits: (a) Create a generic powerlaw model with the spectral index fixed at the desired value (typically set to -2). Note, don't set e0, use default. This ensures that the prefactor range really does convert to a physically reasonable range of parameters. (b) Give the spectral model the pointlike default spectral limits. This is important because it gives the source a big enough range such that the upper limit can find a proper integration range. (c) Set the flux of the current model to equal the flux of the input model. This starts the fitter at a reasonable value. Do this setting with the set_flux flag strict=False beacuse, in case the initial fit totally failed to converge (flux -> 0), you don't want to put the starting value fo the flux too far away from the true value. (d) Keep the lower and upper limit on the prefactor as the pointlike default limits, but set the scale of the source to be the new 'norm' found by preserving the flux. This ensures the fitter doesn't have too much trouble finding the true minimum. Using this procedure, you get a reasonable parameter limits which allows the preliminary fit to converge and the upper limits code to integrate over a reasonable parameter range. """ source = like.logLike.getSource(name) spectrum=source.spectrum() old_flux = like.flux(name, self.emin, self.emax) model = PowerLaw(index=self.powerlaw_index) model.set_flux(old_flux, emin=self.emin, emax=self.emax, strict=False) model.set_default_limits(oomp_limits=True) spectrum = build_gtlike_spectrum(model) like.setSpectrum(name,spectrum) like.syncSrcParams(name) results = super(GtlikePowerLawUpperLimit,self)._compute() saved_state.restore()
def _calculate(self): like = self.like name = self.name if self.verbosity: print 'Testing cutoff in gtlike' saved_state = SuperState(like) emin, emax = get_full_energy_range(like) self.results = d = dict( energy = energy_dict(emin=emin, emax=emax, energy_units=self.energy_units) ) try: def get_flux(): return like.flux(name, emin, emax) def spectrum(): source = like.logLike.getSource(name) s=source.spectrum() return spectrum_to_dict(s, errors=True) old_flux = get_flux() if spectrum()['name'] == 'PowerLaw': pass else: powerlaw_model=PowerLaw(norm=1e-11, index=2, e0=np.sqrt(emin*emax)) powerlaw_model.set_flux(old_flux,emin=emin,emax=emax) powerlaw_model.set_default_limits(oomp_limits=True) if self.verbosity: print 'powerlaw_model is',powerlaw_model powerlaw_spectrum=build_gtlike_spectrum(powerlaw_model) like.setSpectrum(name,powerlaw_spectrum) if self.verbosity: print 'About to fit powerlaw_spectrum' print summary(like) paranoid_gtlike_fit(like, verbosity=self.verbosity) if self.verbosity: print 'Done fitting powerlaw_spectrum' print summary(like) d['hypothesis_0'] = source_dict(like, name, emin=emin, emax=emax, flux_units=self.flux_units, energy_units=self.energy_units, verbosity=self.verbosity) if self.cutoff_model is None: self.cutoff_model=PLSuperExpCutoff(norm=1e-9, index=1, cutoff=1000, e0=1000, b=1) self.cutoff_model.set_free('b', False) self.cutoff_model.set_flux(old_flux,emin=emin,emax=emax) self.cutoff_model.set_default_limits(oomp_limits=True) if self.verbosity: print 'cutoff_model is',self.cutoff_model cutoff_spectrum=build_gtlike_spectrum(self.cutoff_model) like.setSpectrum(name,cutoff_spectrum) if self.verbosity: print 'About to fit cutoff_model' print summary(like) paranoid_gtlike_fit(like, verbosity=self.verbosity) ll = like.logLike.value() if ll < d['hypothesis_0']['logLikelihood']: # if fit is worse than PowerLaw fit, then # restart fit with parameters almost # equal to best fit powerlaw cutoff_plaw=PLSuperExpCutoff(b=1) cutoff_plaw.set_free('b', False) cutoff_plaw.setp_gtlike('norm', d['hypothesis_0']['spectrum']['Prefactor']) cutoff_plaw.setp_gtlike('index', d['hypothesis_0']['spectrum']['Index']) cutoff_plaw.setp_gtlike('e0', d['hypothesis_0']['spectrum']['Scale']) cutoff_plaw.setp_gtlike('cutoff', 1e6) cutoff_plaw.set_default_limits(oomp_limits=True) temp=build_gtlike_spectrum(cutoff_plaw) like.setSpectrum(name,temp) if self.verbosity: print 'Redoing fit with cutoff same as plaw' print summary(like) paranoid_gtlike_fit(like, verbosity=self.verbosity) if self.verbosity: print 'Done fitting cutoff_spectrum' print summary(like) d['hypothesis_1'] = source_dict(like, name, emin=emin, emax=emax, flux_units=self.flux_units, energy_units=self.energy_units, verbosity=self.verbosity) if self.cutoff_xml_name is not None: like.writeXml(self.cutoff_xml_name) d['TS_cutoff']=d['hypothesis_1']['TS']['reoptimize']-d['hypothesis_0']['TS']['reoptimize'] if self.verbosity: print 'For cutoff test, TS_cutoff = ', d['TS_cutoff'] except Exception, ex: print 'ERROR gtlike test cutoff: ', ex traceback.print_exc(file=sys.stdout) self.results = None
def _calculate(self): like = self.like name = self.name if self.verbosity: print 'Testing cutoff in gtlike' saved_state = SuperState(like) emin, emax = get_full_energy_range(like) self.results = d = dict(energy=energy_dict( emin=emin, emax=emax, energy_units=self.energy_units)) try: def get_flux(): return like.flux(name, emin, emax) def spectrum(): source = like.logLike.getSource(name) s = source.spectrum() return spectrum_to_dict(s, errors=True) old_flux = get_flux() if spectrum()['name'] == 'PowerLaw': pass else: powerlaw_model = PowerLaw(norm=1e-11, index=2, e0=np.sqrt(emin * emax)) powerlaw_model.set_flux(old_flux, emin=emin, emax=emax) powerlaw_model.set_default_limits(oomp_limits=True) if self.verbosity: print 'powerlaw_model is', powerlaw_model powerlaw_spectrum = build_gtlike_spectrum(powerlaw_model) like.setSpectrum(name, powerlaw_spectrum) if self.verbosity: print 'About to fit powerlaw_spectrum' print summary(like) paranoid_gtlike_fit(like, verbosity=self.verbosity) if self.verbosity: print 'Done fitting powerlaw_spectrum' print summary(like) d['hypothesis_0'] = source_dict(like, name, emin=emin, emax=emax, flux_units=self.flux_units, energy_units=self.energy_units, verbosity=self.verbosity) if self.cutoff_model is None: self.cutoff_model = PLSuperExpCutoff(norm=1e-9, index=1, cutoff=1000, e0=1000, b=1) self.cutoff_model.set_free('b', False) self.cutoff_model.set_flux(old_flux, emin=emin, emax=emax) self.cutoff_model.set_default_limits(oomp_limits=True) if self.verbosity: print 'cutoff_model is', self.cutoff_model cutoff_spectrum = build_gtlike_spectrum(self.cutoff_model) like.setSpectrum(name, cutoff_spectrum) if self.verbosity: print 'About to fit cutoff_model' print summary(like) paranoid_gtlike_fit(like, verbosity=self.verbosity) ll = like.logLike.value() if ll < d['hypothesis_0']['logLikelihood']: # if fit is worse than PowerLaw fit, then # restart fit with parameters almost # equal to best fit powerlaw cutoff_plaw = PLSuperExpCutoff(b=1) cutoff_plaw.set_free('b', False) cutoff_plaw.setp_gtlike( 'norm', d['hypothesis_0']['spectrum']['Prefactor']) cutoff_plaw.setp_gtlike('index', d['hypothesis_0']['spectrum']['Index']) cutoff_plaw.setp_gtlike('e0', d['hypothesis_0']['spectrum']['Scale']) cutoff_plaw.setp_gtlike('cutoff', 1e6) cutoff_plaw.set_default_limits(oomp_limits=True) temp = build_gtlike_spectrum(cutoff_plaw) like.setSpectrum(name, temp) if self.verbosity: print 'Redoing fit with cutoff same as plaw' print summary(like) paranoid_gtlike_fit(like, verbosity=self.verbosity) if self.verbosity: print 'Done fitting cutoff_spectrum' print summary(like) d['hypothesis_1'] = source_dict(like, name, emin=emin, emax=emax, flux_units=self.flux_units, energy_units=self.energy_units, verbosity=self.verbosity) if self.cutoff_xml_name is not None: like.writeXml(self.cutoff_xml_name) d['TS_cutoff'] = d['hypothesis_1']['TS']['reoptimize'] - d[ 'hypothesis_0']['TS']['reoptimize'] if self.verbosity: print 'For cutoff test, TS_cutoff = ', d['TS_cutoff'] except Exception, ex: print 'ERROR gtlike test cutoff: ', ex traceback.print_exc(file=sys.stdout) self.results = None