-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reduce overhead for simple models #1174
Labels
research
experimental stuff
Comments
A few comparissons:
seems like with some not too much effort we can get m3, with some effort m2 import numpy as np
import scipy.stats
from jax.scipy.special import gammaln as jax_gammaln
from scipy.special import gammaln as gammaln
import jax
import pyhf
import jax.numpy as jnp
import numba
import pyhf.probability
from pyhf.tensor.common import _TensorViewer
pyhf.set_backend('numpy')
class Model(object):
def __init__(self,s,b,db):
self.s = s
self.b = b
self.db = db
self.tau = (b/db)**2
self.tv = _TensorViewer([np.array([0]),np.array([1])])
def logpdf(self,p,d):
mu,gamma = p
a = pyhf.probability.Poisson(np.array([mu*self.s + self.b*gamma]))
b = pyhf.probability.Poisson(np.array([self.tau*gamma]))
pdf = pyhf.probability.Simultaneous([a,b], self.tv)
return pdf.log_prob(d)
class PyhfBasedModel(object):
def __init__(self,nominal,config):
self.nominal = nominal
self.norm = pyhf.modifiers.normfactor_combined([('mu','normfactor')],m.config,mods)
self.shape = pyhf.modifiers.shapesys_combined([('uncorr_bkguncrt','shapesys')],m.config,mods)
self.pois = pyhf.constraints.poisson_constraint_combined(config)
def logpdf(self,p,d):
a = pyhf.probability.Poisson(np.sum(self.nominal * self.shape.apply(p) * self.norm.apply(p),axis=1))
b = self.pois.make_pdf(p)
return pyhf.probability.Simultaneous([a,b],m2.tv).log_prob(d)
def poisson_logpdf(n, lam):
return n * np.log(lam) - lam - gammaln(n + 1.0)
class PureModel(object):
def __init__(self,s,b,db):
self.s = s
self.b = b
self.db = db
self.tau = (b/db)**2
def logpdf(self,p,d):
mu,gamma = p
obs, aux = d
return poisson_logpdf(obs,mu*self.s + self.b*gamma) + poisson_logpdf(aux,self.tau*gamma)
m = pyhf.simplemodels.hepdata_like([5],[50],[6])
d = [50]+m.config.auxdata
m.config.auxdata
Np,Nd = (np.asarray([2.0,0.2]),np.asarray(d))
m2 = Model(5,50,6)
m3 = PyhfBasedModel(m.nominal_rates,m.config)
m4 = PureModel(5,50,6)
m2.logpdf(Np,Nd),m.logpdf(Np,Nd),m3.logpdf(Np,Nd),m4.logpdf(Np,Nd)
%%timeit
m.logpdf(Np,Nd)
%%timeit
m2.logpdf(Np,Nd)
%%timeit
m3.logpdf(Np,Nd)
%%timeit
m4.logpdf(Np,Nd)
|
these model like this
can be dropped in without changes into with |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
pyhf.simplemodels.hepdata_like
is used by a lot of people but for very simple models like this we have quite a bit of overhead over a manual calculationThis is alsoo important for toys (which often are used by single-bin analyses see #1161 )
The text was updated successfully, but these errors were encountered: