Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Exception when using binned observable in fit with unbinned likelihood #634

@acampove

Description

@acampove

Current Behaviour

The following snippet:

import zfit

#---------------------------------------------
def get_model():
    obs          = zfit.Space('x', limits=(-10, 10), binning=100)
    mu           = zfit.Parameter("mu", 2.4, -1, 5)
    sg           = zfit.Parameter("sg", 1.3,  0, 5)
    gauss        = zfit.pdf.Gauss(obs=obs, mu=mu, sigma=sg)

    return gauss
#---------------------------------------------
def main():
    model = get_model()
    data  = model.create_sampler(n=1000)

    nll       = zfit.loss.UnbinnedNLL(model=model, data=data)
    minimizer = zfit.minimize.Minuit()
    result    = minimizer.minimize(nll)
    result.hesse(method='minuit_hesse')

    print(result)
#---------------------------------------------
if __name__ == '__main__':
    main()

shows me:

           ^^^^^^^^^^^^^^^^
  File "/home/acampove/micromamba/envs/rx/lib/python3.12/site-packages/zfit/z/zextension.py", line 269, in concrete_func
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/acampove/micromamba/envs/rx/lib/python3.12/site-packages/zfit/core/loss.py", line 1089, in _loss_func
    return self._loss_func_watched(
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/acampove/micromamba/envs/rx/lib/python3.12/site-packages/zfit/z/zextension.py", line 269, in concrete_func
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/acampove/micromamba/envs/rx/lib/python3.12/site-packages/zfit/core/loss.py", line 1105, in _loss_func_watched
    nll, nll_corr = _unbinned_nll_tf(
                    ^^^^^^^^^^^^^^^^^
  File "/home/acampove/micromamba/envs/rx/lib/python3.12/site-packages/zfit/z/zextension.py", line 269, in concrete_func
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/acampove/micromamba/envs/rx/lib/python3.12/site-packages/zfit/core/loss.py", line 103, in _unbinned_nll_tf
    _unbinned_nll_tf(model=p, data=d, fit_range=r, log_offset=log_offset, kahan=kahan)
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/acampove/micromamba/envs/rx/lib/python3.12/site-packages/zfit/z/zextension.py", line 269, in concrete_func
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/acampove/micromamba/envs/rx/lib/python3.12/site-packages/zfit/core/loss.py", line 122, in _unbinned_nll_tf
    weights=data.weights if data.weights is not None else None,
                            ^^^^^^^^^^^^
AttributeError: 'BinnedSamplerData' object has no attribute 'weights'

and this seems to be happening because the likelihood is unbinned, while a binning was specified for the observable.

Expected Behaviour

The user specified a binning for the observable, but they are not using a binned likelihood. That is likely to be an oversight of the user and maybe a warning is needed. Of course, it might be that we want to be strict and just raise the exception, because there is something that is contradictory in the user's code.

However, even in this case, the message in the exception is not clear and it's hard to deduce that this is happening because of the way the observable and likelihoods were created.

I would allow binning to be used in the observable, even if the likelihood is unbinned. Because even if the binning is not used for the likelihood itself, it can still be used for the plotting. I am trying to use the observable as a place to store this information.

Context (Environment)

  • zfit version: 0.26.0
  • Python version: 3.12.0
  • Are you using conda, pipenv, etc? : from pypi
  • Operating System: Alma9
  • Tensorflow version: 2.19.0

Possible Solution/Implementation

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions