-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
062757f
commit 0dad0a2
Showing
7 changed files
with
118 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright (c) 2020 zfit | ||
|
||
import boost_histogram as bh | ||
import numpy as np | ||
|
||
import zfit | ||
from zfit.core.binneddata import BinnedData | ||
from zfit.core.binning import RectBinning | ||
from zfit.models.binned_functor import BinnedSumPDF | ||
from zfit.models.template import BinnedTemplatePDF | ||
|
||
|
||
def test_binned_nll_simple(): | ||
counts = np.random.uniform(high=1, size=(10, 20)) # generate counts | ||
counts2 = np.random.normal(loc=5, size=(10, 20)) | ||
counts3 = np.linspace(0, 10, num=10)[:, None] * np.linspace(0, 5, num=20)[None, :] | ||
binnings = [bh.axis.Regular(10, 0, 10), bh.axis.Regular(20, -10, 30)] | ||
binning = RectBinning(binnings=binnings) | ||
obs = zfit.Space(obs=['obs1', 'obs2'], binning=binning) | ||
|
||
mc1 = BinnedData.from_numpy(obs=obs, counts=counts, w2error=10) | ||
mc2 = BinnedData.from_numpy(obs=obs, counts=counts2, w2error=10) | ||
mc3 = BinnedData.from_numpy(obs=obs, counts=counts3, w2error=10) | ||
|
||
observed_data = BinnedData.from_numpy(obs=obs, counts=counts + counts2 + counts3, w2error=10) | ||
|
||
pdf = BinnedTemplatePDF(data=mc1) | ||
pdf2 = BinnedTemplatePDF(data=mc2) | ||
pdf3 = BinnedTemplatePDF(data=mc3) | ||
pdf.set_yield(np.sum(counts)) | ||
pdf2.set_yield(np.sum(counts2)) | ||
pdf3.set_yield(np.sum(counts3)) | ||
# assert len(pdf.ext_pdf(None)) > 0 | ||
pdf_sum = BinnedSumPDF(pdfs=[pdf, pdf2, pdf3], obs=obs) | ||
|
||
nll = zfit.loss.ExtendedBinnedNLL(pdf_sum, data=observed_data) | ||
nll.value() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Copyright (c) 2020 zfit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright (c) 2020 zfit | ||
from typing import Iterable | ||
|
||
from .. import z | ||
from ..core.interfaces import ZfitBinnedPDF, ZfitBinnedData | ||
from ..core.loss import BaseLoss | ||
from ..util import ztyping | ||
|
||
import tensorflow as tf | ||
|
||
|
||
class ExtendedBinnedNLL(BaseLoss): | ||
|
||
def __init__(self, model: ztyping.ModelsInputType, data: ztyping.DataInputType, | ||
constraints: ztyping.ConstraintsTypeInput = None): | ||
super().__init__(model=model, data=data, constraints=constraints, fit_range=None) | ||
|
||
@z.function(wraps='loss') | ||
def _loss_func(self, model: Iterable[ZfitBinnedPDF], data: Iterable[ZfitBinnedData], | ||
fit_range, constraints): | ||
poisson_terms = [] | ||
for mod, dat in zip(model, data): | ||
poisson_terms.append(tf.nn.log_poisson_loss(dat.get_counts(obs=mod.obs), | ||
tf.math.log(mod.ext_pdf(None)))) # TODO: change None | ||
nll = tf.reduce_sum(poisson_terms, axis=0) | ||
|
||
if constraints: | ||
constraints = z.reduce_sum([c.value() for c in constraints]) | ||
nll += constraints | ||
|
||
return nll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters