Skip to content

Commit

Permalink
put m parameter first
Browse files Browse the repository at this point in the history
  • Loading branch information
ikrommyd committed Mar 15, 2024
1 parent 3430135 commit 4e2e271
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
15 changes: 8 additions & 7 deletions tests/test_pdf_cmsshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
import zfit_physics as zphys

# specify globals here. Do NOT add any TensorFlow but just pure python
m_true = 90.0
beta_true = 0.2
gamma_true = 0.3
m_true = 90.0


def create_cmsshape(gamma, beta, m, limits):
def create_cmsshape(m, beta, gamma, limits):
obs = zfit.Space("obs1", limits)
cmsshape = zphys.pdf.CMSShape(gamma=gamma, beta=beta, m=m, obs=obs)
cmsshape = zphys.pdf.CMSShape(m=m, beta=beta, gamma=gamma, obs=obs)
return cmsshape, obs


def test_cmsshape_pdf():
# Test PDF here
cmsshape, _ = create_cmsshape(gamma=gamma_true, beta=beta_true, m=m_true, limits=(50, 130))
cmsshape, _ = create_cmsshape(m=m_true, beta=beta_true, gamma=gamma_true, limits=(50, 130))
assert zfit.run(cmsshape.pdf(90.0)) == pytest.approx(
cmsshape_numba.pdf(90.0, beta=beta_true, gamma=gamma_true, loc=m_true).item(), rel=1e-5
)
Expand All @@ -43,7 +43,7 @@ def test_cmsshape_pdf():

def test_cmsshape_integral():
# Test CDF and integral here
cmsshape, obs = create_cmsshape(gamma=gamma_true, beta=beta_true, m=m_true, limits=(50, 130))
cmsshape, obs = create_cmsshape(m=m_true, beta=beta_true, gamma=gamma_true, limits=(50, 130))
full_interval_analytic = zfit.run(cmsshape.analytic_integrate(obs, norm_range=False))
full_interval_numeric = zfit.run(cmsshape.numeric_integrate(obs, norm_range=False))
true_integral = 0.99999
Expand All @@ -66,10 +66,11 @@ def test_cmsshape_integral():

# register the pdf here and provide sets of working parameter configurations
def cmsshape_params_factory():
m = zfit.Parameter("m", m_true)
beta = zfit.Parameter("beta", beta_true)
gamma = zfit.Parameter("gamma", gamma_true)
m = zfit.Parameter("m", m_true)
return {"beta": beta, "gamma": gamma, "m": m}

return {"m": m, "beta": beta, "gamma": gamma}


tester.register_pdf(pdf_class=zphys.pdf.CMSShape, params_factories=cmsshape_params_factory)
24 changes: 12 additions & 12 deletions zfit_physics/models/pdf_cmsshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@


@z.function(wraps="tensor")
def cmsshape_pdf_func(x, beta, gamma, m):
def cmsshape_pdf_func(x, m, beta, gamma):
"""Calculate the CMSShape PDF.
Args:
x: value(s) for which the PDF will be calculated.
m: approximate center of the disribution.
beta: steepness of the error function.
gamma: steepness of the exponential distribution.
m: approximate center of the distribution.
Returns:
`tf.Tensor`: The calculated PDF values.
Expand Down Expand Up @@ -44,14 +44,14 @@ def cmsshape_pdf_func(x, beta, gamma, m):


@z.function(wraps="tensor")
def cmsshape_cdf_func(x, beta, gamma, m):
def cmsshape_cdf_func(x, m, beta, gamma):
"""Analtical function for the CDF of the CMSShape distribution.
Args:
x: value(s) for which the CDF will be calculated.
m: approximate center of the distribution.
beta: steepness of the error function.
gamma: steepness of the exponential distribution.
m: approximate center of the distribution.
Returns:
`tf.Tensor`: The calculated CDF values.
Expand Down Expand Up @@ -81,11 +81,11 @@ def cmsshape_integral(limits: ztyping.SpaceType, params: dict, model) -> tf.Tens
The calculated integral.
"""
lower, upper = limits.limit1d
m = params["m"]
beta = params["beta"]
gamma = params["gamma"]
m = params["m"]
lower_cdf = cmsshape_cdf_func(x=lower, beta=beta, gamma=gamma, m=m)
upper_cdf = cmsshape_cdf_func(x=upper, beta=beta, gamma=gamma, m=m)
lower_cdf = cmsshape_cdf_func(x=lower, m=m, beta=beta, gamma=gamma)
upper_cdf = cmsshape_cdf_func(x=upper, m=m, beta=beta, gamma=gamma)
return upper_cdf - lower_cdf


Expand All @@ -94,9 +94,9 @@ class CMSShape(zfit.pdf.BasePDF):

def __init__(
self,
m: ztyping.ParamTypeInput,
beta: ztyping.ParamTypeInput,
gamma: ztyping.ParamTypeInput,
m: ztyping.ParamTypeInput,
obs: ztyping.ObsTypeInput,
*,
extended: Optional[ztyping.ExtendedInputType] = None,
Expand All @@ -116,9 +116,9 @@ def __init__(
and [numba-stats](https://github.com/HDembinski/numba-stats/blob/main/src/numba_stats/cmsshape.py)
Args:
m: Approximate center of the distribution.
beta: Steepness of the error function.
gamma: Steepness of the exponential distribution.
m: Approximate center of the distribution.
obs: |@doc:pdf.init.obs| Observables of the
model. This will be used as the default space of the PDF and,
if not given explicitly, as the normalization range.
Expand All @@ -140,14 +140,14 @@ def __init__(
the PDF for better identification.
Has no programmatical functional purpose as identification. |@docend:pdf.init.name|
"""
params = {"beta": beta, "gamma": gamma, "m": m}
params = {"m": m, "beta": beta, "gamma": gamma}
super().__init__(obs=obs, params=params, name=name, extended=extended, norm=norm)

def _unnormalized_pdf(self, x: tf.Tensor) -> tf.Tensor:
m = self.params["m"]
beta = self.params["beta"]
gamma = self.params["gamma"]
m = self.params["m"]
return cmsshape_pdf_func(x, beta, gamma, m)
return cmsshape_pdf_func(x=x, m=m, beta=beta, gamma=gamma)


cmsshape_integral_limits = Space(axes=(0,), limits=(((ANY_LOWER,),), ((ANY_UPPER,),)))
Expand Down

0 comments on commit 4e2e271

Please sign in to comment.