Action #3625

Implement a more efficient Monte-Carlo sampler for exponentially and super-exponentially cut-off power laws

Added by Knödlseder Jürgen over 3 years ago. Updated over 3 years ago.

Status:ClosedStart date:04/27/2021
Priority:NormalDue date:
Assigned To:Knödlseder Jürgen% Done:

100%

Category:-
Target version:1.7.4
Tags: Performance
Duration:

Description

As reported in #3624, the super-exponentially cut-off power law Monte Carlo sampler is not very efficient when sampling deeply in the exponentially cut-off part of the power law. A more efficient sampler should be implemented to speed-up the computations.

mc-superexp-after-10000.png (45.3 KB) Knödlseder Jürgen, 04/27/2021 11:58 PM

mc-superexp-before-10000.png (45.6 KB) Knödlseder Jürgen, 04/27/2021 11:58 PM

mc-superexp-after-positive.png (41 KB) Knödlseder Jürgen, 04/28/2021 12:06 AM

mc-exp-after-10000.png (42.4 KB) Knödlseder Jürgen, 04/28/2021 12:28 AM

mc-exp-before-10000.png (42.8 KB) Knödlseder Jürgen, 04/28/2021 12:28 AM

mc-exp-positive-10000.png (42.1 KB) Knödlseder Jürgen, 04/28/2021 12:28 AM

mc-invexp-after-10000.png (42.4 KB) Knödlseder Jürgen, 04/28/2021 12:37 AM

mc-invexp-before-10000.png (42.8 KB) Knödlseder Jürgen, 04/28/2021 12:37 AM

mc-invexp-positive-10000.png (40.7 KB) Knödlseder Jürgen, 04/28/2021 12:37 AM

Mc-superexp-after-10000 Mc-superexp-before-10000 Mc-superexp-after-positive Mc-exp-after-10000 Mc-exp-before-10000 Mc-exp-positive-10000 Mc-invexp-after-10000 Mc-invexp-before-10000 Mc-invexp-positive-10000

Recurrence

No recurrence.


Related issues

Related to ctools - Bug #3624: bug on csspec with Composite model Closed 04/27/2021

History

#1 Updated by Knödlseder Jürgen over 3 years ago

  • Related to Bug #3624: bug on csspec with Composite model added

#2 Updated by Knödlseder Jürgen over 3 years ago

The Monte Carlo sampling in GModelSpectralSuperExpPlaw::mc is done using a rejection method that draws random energies for a power law and compares then the super-exponential cut-off model to the power law to decide whether to keep or to reject the energy. Since for the first energy bins the sampling is done deeply in the cut-off of the model, the energy is very often rejected before being accepted. Below some debugging of the Monte Carlo method, where samples indicates the number of random energies that are drawn before one is accepted. This number can go up to millions!

GModelSpectralSuperExpPlaw::mc(30 GeV,72.3259983091833 GeV,0 s (TT)): energy=35.7830766905839 GeV samples=786164
GModelSpectralSuperExpPlaw::mc(30 GeV,72.3259983091833 GeV,0 s (TT)): energy=40.3034500835591 GeV samples=345242
GModelSpectralSuperExpPlaw::mc(30 GeV,72.3259983091833 GeV,0 s (TT)): energy=46.6816418047913 GeV samples=116586
GModelSpectralSuperExpPlaw::mc(30 GeV,72.3259983091833 GeV,0 s (TT)): energy=36.3483683005087 GeV samples=284844
GModelSpectralSuperExpPlaw::mc(30 GeV,72.3259983091833 GeV,0 s (TT)): energy=37.2933148694958 GeV samples=170091
GModelSpectralSuperExpPlaw::mc(30 GeV,72.3259983091833 GeV,0 s (TT)): energy=31.566807167513 GeV samples=71186
GModelSpectralSuperExpPlaw::mc(30 GeV,72.3259983091833 GeV,0 s (TT)): energy=31.0769872610411 GeV samples=159230
GModelSpectralSuperExpPlaw::mc(30 GeV,72.3259983091833 GeV,0 s (TT)): energy=31.3044072005203 GeV samples=45231
GModelSpectralSuperExpPlaw::mc(30 GeV,72.3259983091833 GeV,0 s (TT)): energy=37.9303234539904 GeV samples=324109
GModelSpectralSuperExpPlaw::mc(30 GeV,72.3259983091833 GeV,0 s (TT)): energy=45.909979097982 GeV samples=6397

I implemented a speed-up by simply re-normalising the random number generator used for rejection:

double acceptance_fraction;
double inv_ecut  = 1.0 / m_ecut.value();
double norm_emin = std::exp(- std::pow(emin.MeV() * inv_ecut, m_index2.value()));
double norm_emax = std::exp(- std::pow(emax.MeV() * inv_ecut, m_index2.value()));
double norm      = (norm_emin > norm_emax) ? norm_emin : norm_emax;
do {
    ...
    acceptance_fraction = std::exp(- std::pow(eng * inv_ecut, m_index2.value()));
} while (ran.uniform() * norm > acceptance_fraction);

That this works is illustrated in the figures below, that show on the left the old results and on the rights the results after implementing the sampling with improved efficiency. The red line indicates the analytical model, the blue histogram the sampled energies.

#3 Updated by Knödlseder Jürgen over 3 years ago

That the code actually works also for rising power laws is illustrated in the figure below.

#4 Updated by Knödlseder Jürgen over 3 years ago

I did the same implementation of the exponentially cut-off power law GModelSpectralExpPlaw. Below the same comparison: left is before the code change, centre is after the code change, right is for a power-law that increases with energy. Speed-up is impressive and everything looks fine.

#5 Updated by Knödlseder Jürgen over 3 years ago

And the final verification for the GModelSpectralExpInvPlaw model, panels below are the same as before. Everything looks okay.

#6 Updated by Knödlseder Jürgen over 3 years ago

  • Target version set to 1.7.4
  • % Done changed from 60 to 70

#7 Updated by Knödlseder Jürgen over 3 years ago

  • Status changed from In Progress to Closed
  • % Done changed from 70 to 100

The code was merged into bugfix release 1.7.4 and devel. Close issue now.

Also available in: Atom PDF