# Custom models#

All elements of zfit are built to be easily customized. Especially models offer many possibilities to be implemented by the user; in the end, regardless of how many models are provided by a library and of how many things are though, there is always a use-case that was not thought of. High flexibility is therefore a crucial aspect.

This has disadvantages: the more freedom a model takes for itself, the less optimizations are potentially available. But this is usually not noticeable.

## Creating a model#

Following the philosophy of zfit, there are different levels of customization. For the most simple use-case, all we need to do is to provide a function describing the shape and the name of the parameters. This can be done by overriding _unnormalized_pdf.

To implement a mathematical function in zfit, znp, a numpy-like interface or z, the backend, should be used. This ensures that the function can be traced and the gradient can be calculated. If a function is not available, tf can also be used. (There are other ways to use arbitrary Python functions, they will be discussed later on).

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import zfit
import zfit.z.numpy as znp
from zfit import z

/home/docs/checkouts/readthedocs.org/user_builds/zfit/checkouts/latest/.venv/lib/python3.12/site-packages/zfit/__init__.py:59: UserWarning: TensorFlow warnings are by default suppressed by zfit. In order to show them, set the environment variable ZFIT_DISABLE_TF_WARNINGS=0. In order to suppress the TensorFlow warnings AND this warning, set ZFIT_DISABLE_TF_WARNINGS=1.
warnings.warn(


We can start with a simple model and implement a custom second order polynomial. Therefore we need to inherit from the right base class, the simpler one is ZPDF.

For a minimal example, we need to override only _unnormalized_pdf and specify a list of parameters.

_unnormalized_pdf gets (currently) one argument, x. This is a zfit Data object and should first be unstacked. If it is one dimensional - such as here - it will return a single Tensor, otherwise a list of Tensors that can directly be unpacked.

class SecondOrderPoly(zfit.pdf.ZPDF):
"""Second order polynomial a + b * x + c * x^2"""
_PARAMS = ['b', 'c']  # specify which parameters to take

@zfit.supports()
def _unnormalized_pdf(self, x, params):  # implement function, unnormalized
data = x[0]  # axis 0
b = params['b']
c = params['c']

return 1 + b * data + c * data ** 2


Note that we omitted consciously any attempt to normalize the function, as this is usually done over a specific range. Also, no analytic sampling or integration has to be provided. The model handles all of this internally automatically and we have the full functionality available.

First, we can instantiate the model:

obs = zfit.Space("obs1", -4, 4)

b = zfit.Parameter('b', 0.2, 0.1, 10)
custom_poly = SecondOrderPoly(obs=obs, b=b, c=1.4)


which lets us now fully access all the main methods of a model:

integral = custom_poly.integrate(limits=(-1, 2))
sample = custom_poly.sample(n=1000)
prob = custom_poly.pdf(sample)
print(f"integral={integral}, sample={sample}, prob={prob[:10]}")

integral=[0.11072835], sample=zfit.Data: Data obs=('obs1',) array=[[ 3.42125145]
[-3.68708293]
[ 3.57348875]
[ 3.4040764 ]
[-2.73190593]
[-3.88243168]
[-2.13264483]
[-3.25889323]
[ 2.1953509 ]
[ 1.14730521]
[-1.37318919]
[-3.28014707]
[-2.31525084]
[ 3.63992008]
[-2.83233431]
[-2.95219404]
[-2.7601843 ]
[-3.32446917]
[-3.00205856]
[-2.24512967]
[ 2.89538931]
[-1.46642232]
[ 3.96295665]
[ 3.50561984]
[-3.09958086]
[ 3.73377688]
[-3.32718234]
[ 3.45757483]
[-3.19845861]
[ 3.99072398]
[ 1.23073775]
[ 2.42349167]
[ 3.63367523]
[-3.28325114]
[ 2.56288355]
[-1.99306758]
[-2.59169019]
[ 2.97258462]
[ 3.45324443]
[ 2.94824535]
[ 2.43690352]
[-2.27220185]
[ 1.664039  ]
[ 1.65617938]
[-3.99198402]
[ 3.07746095]
[-3.7781294 ]
[ 2.17194273]
[ 2.80048025]
[ 2.07207599]
[ 3.63637793]
[ 2.95560772]
[-2.61692044]
[ 3.23521247]
[-1.64241609]
[-2.104401  ]
[ 3.03081096]
[ 3.35542872]
[-3.64054362]
[ 3.17433038]
[-2.35710135]
[ 3.50929188]
[ 3.48148721]
[ 3.60619816]
[ 3.56651606]
[ 3.55582375]
[-3.70495563]
[ 1.75162092]
[-3.56964244]
[ 3.96244275]
[-3.53647907]
[-2.44279911]
[ 2.34500008]
[-2.96109221]
[ 1.38631686]
[ 3.44402663]
[-3.13978189]
[-1.92024689]
[-3.55521343]
[ 1.89843374]
[-3.63294592]
[ 3.56406076]
[ 2.61148195]
[ 3.82509643]
[-2.3288257 ]
[-3.58810477]
[ 2.32181753]
[ 1.05040619]
[ 3.7654397 ]
[ 3.25486106]
[-3.35135846]
[-1.52658059]
[ 3.17683659]
[-1.34113007]
[-3.3835411 ]
[ 3.07572323]
[ 3.38025642]
[-3.15119072]
[-3.97795196]
[-1.81828976]
[-3.59124735]
[-3.15222424]
[-3.51972882]
[ 3.9290087 ]
[-1.51164437]
[-2.53894598]
[ 3.85642792]
[-3.51568037]
[ 3.87659571]
[ 2.6008821 ]
[-3.25517001]
[ 0.59665184]
[ 2.47116376]
[-3.20601799]
[ 3.93186794]
[ 1.93440316]
[ 3.39015496]
[ 2.35139229]
[ 2.42085914]
[ 3.83786378]
[ 3.63880783]
[ 3.05082524]
[ 2.92142149]
[-3.78832304]
[ 2.57807842]
[ 3.98166478]
[ 2.63681072]
[-2.10632389]
[-3.50928686]
[-3.80036763]
[ 0.45517099]
[ 3.52896827]
[-3.7753408 ]
[-3.45694289]
[ 3.33069804]
[ 3.84012448]
[-3.65537567]
[ 3.7804101 ]
[-3.66048691]
[-3.88604051]
[-2.67155923]
[ 3.6834042 ]
[ 2.30709372]
[-3.63464617]
[-2.64854023]
[-3.13599764]
[-2.44265071]
[-2.89780738]
[-3.01205752]
[ 3.30280501]
[ 2.88123493]
[ 3.81265068]
[ 3.50445923]
[-2.58872978]
[-3.4242635 ]
[ 2.07934524]
[ 2.56735405]
[-3.0963243 ]
[ 3.67449155]
[ 1.56760791]
[ 2.33319822]
[-1.6506181 ]
[ 1.48226921]
[ 2.95005427]
[-1.89553733]
[ 3.2103175 ]
[ 2.9615855 ]
[ 2.8967673 ]
[ 3.29922274]
[-2.51725911]
[-3.09559639]
[ 2.78035173]
[ 3.68225028]
[-3.73430985]
[-3.68757681]
[ 3.10352787]
[ 3.6089488 ]
[-1.95742888]
[ 3.27607605]
[-3.75448551]
[-3.76669781]
[ 3.1909751 ]
[-3.37217425]
[-3.78673838]
[ 3.11575496]
[-3.6110395 ]
[ 2.89512823]
[-0.86329786]
[-3.03596688]
[ 3.93985679]
[-1.60218975]
[-3.50858552]
[ 1.00781308]
[ 3.79632754]
[-1.34198903]
[-1.47438093]
[ 0.51764032]
[ 3.32854996]
[ 3.70964354]
[-3.3009844 ]
[ 3.75817596]
[ 2.55638296]
[ 3.71309512]
[ 3.39006835]
[ 1.76123748]
[-3.66384376]
[ 3.50901068]
[ 3.92115164]
[ 2.63799758]
[-2.80539566]
[-1.000676  ]
[ 2.8850066 ]
[-0.30331267]
[ 3.73545664]
[-2.55182475]
[-3.61690303]
[-2.78471098]
[-1.96065106]
[ 3.3452502 ]
[-3.52356591]
[-3.42835143]
[-2.58019114]
[-3.09807409]
[ 3.65546727]
[-1.33891432]
[ 3.70942798]
[-3.19038624]
[ 3.31601604]
[ 2.8711986 ]
[ 3.25642452]
[ 2.1937211 ]
[-3.50696603]
[-3.87064291]
[-3.9914417 ]
[-3.87027545]
[-3.98460826]
[ 3.98536243]
[-3.34446375]
[ 3.02074855]
[-3.92203251]
[ 2.13439972]
[-2.41981435]
[ 0.55739059]
[ 2.61589101]
[-1.3553994 ]
[-3.23707296]
[-3.89261204]
[-3.15057588]
[-3.37403144]
[-2.70666203]
[ 3.47990543]
[ 3.91863248]
[-3.84261941]
[-2.73235824]
[ 3.40812704]
[-1.82108095]
[ 2.81497719]
[-3.66350227]
[ 3.62166612]
[ 3.82296595]
[-3.18690511]
[ 2.66185933]
[-1.92208032]
[ 3.31714689]
[ 2.9177708 ]
[-3.58735793]
[ 3.71729887]
[ 2.86352137]
[ 3.94124025]
[ 2.95779999]
[-3.22216694]
[ 2.92952766]
[ 3.23164659]
[ 3.28454395]
[-3.69225759]
[ 2.9142673 ]
[ 1.68471067]
[-1.1309718 ]
[-3.31100776]
[-1.99503704]
[ 3.01948178]
[ 3.71139422]
[-0.99815609]
[ 3.2468817 ]
[-2.61519951]
[-2.31961378]
[ 3.78974949]
[-2.11111685]
[-3.19302119]
[ 2.88791942]
[ 3.99233672]
[-1.16811341]
[ 1.78024283]
[-3.20886788]
[-3.555165  ]
[-3.66369021]
[-3.39467449]
[ 1.03059111]
[ 2.52075557]
[-2.33533099]
[-3.84998977]
[ 3.64486877]
[ 2.71311511]
[ 2.58837661]
[ 3.04160045]
[ 3.9725086 ]
[ 3.25256286]
[ 1.51777808]
[-3.84754408]
[-3.74443781]
[ 1.8764948 ]
[-3.83408395]
[-3.17857567]
[ 3.65737142]
[ 3.46232527]
[-3.09586506]
[-3.93822028]
[-0.64064273]
[-2.82242036]
[-3.70462795]
[-2.78955297]
[-3.16330868]
[ 3.26234686]
[ 3.26734824]
[-3.77871389]
[ 3.79143963]
[-3.07107665]
[ 3.61393244]
[-0.94198191]
[-3.97427192]
[ 3.90869563]
[-3.96935043]
[-2.61876088]
[ 1.64506197]
[-3.58541384]
[-2.91808815]
[ 3.7969766 ]
[-3.52078486]
[ 3.12346326]
[-3.01292773]
[ 3.60624258]
[-2.68646583]
[ 3.99416303]
[ 3.86181823]
[ 0.14564246]
[-3.03711291]
[-2.97995985]
[ 3.62273064]
[-2.67168091]
[-0.91047009]
[-3.7198804 ]
[ 3.07145352]
[ 2.77819732]
[-2.53248673]
[-3.07658774]
[ 3.43417819]
[ 3.34193875]
[-3.12424902]
[ 0.63282703]
[ 3.93049636]
[-0.2331471 ]
[-1.85110298]
[-1.05265436]
[-2.38807861]
[-3.07139174]
[-2.80849345]
[-3.67598287]
[-3.53430468]
[ 2.23717727]
[ 3.99270191]
[-3.85790428]
[ 2.82609258]
[ 3.61476998]
[ 2.06115593]
[-3.72821255]
[-2.12455499]
[ 3.92463555]
[-3.27942921]
[ 1.43248411]
[-3.23025437]
[-3.81102345]
[ 3.5969893 ]
[-3.74004828]
[-3.69814869]
[-3.83156053]
[-2.31744047]
[ 3.01151231]
[-3.1051199 ]
[-3.01433764]
[-3.62994076]
[ 2.64495927]
[-2.00515751]
[ 3.76970804]
[-3.90752971]
[ 3.43024879]
[-3.73672129]
[ 3.79260076]
[-3.90352721]
[ 1.5241585 ]
[ 3.84363858]
[ 1.55666867]
[-3.70880242]
[-3.9046248 ]
[-1.78481487]
[ 3.92921332]
[-3.883387  ]
[-2.82166074]
[ 2.82028061]
[ 3.75472016]
[ 2.89866109]
[ 3.49851006]
[-3.89370484]
[-3.72989737]
[-2.69166854]
[-1.59397794]
[-0.92324225]
[-3.98005103]
[-2.16124856]
[ 1.76966635]
[-1.83868861]
[-3.05657984]
[-3.18488696]
[-2.02333719]
[ 3.87459549]
[ 0.80982582]
[ 1.67172518]
[-3.67492566]
[ 1.67382564]
[-2.33996421]
[ 3.2094411 ]
[ 2.88934985]
[ 3.67186295]
[ 2.14382573]
[ 2.69406059]
[-3.52149042]
[ 3.81522914]
[-3.55794235]
[-3.85389241]
[ 2.79539453]
[-3.80280338]
[-3.50027769]
[ 1.24143351]
[ 3.85093438]
[-2.89495874]
[ 1.2004787 ]
[-2.76303732]
[ 2.8797553 ]
[-3.81783831]
[-3.63179094]
[-3.63636696]
[ 3.83825295]
[ 3.07383234]
[-3.71399365]
[-3.75655165]
[-3.34586873]
[-3.99162239]
[ 3.30679264]
[ 1.47902895]
[-3.96660299]
[ 3.95408971]
[-3.72422292]
[-1.87275542]
[ 3.20470647]
[ 2.00091379]
[-1.90705245]
[-3.94248039]
[ 2.74086721]
[ 3.9417421 ]
[-3.87124511]
[ 2.86662402]
[-3.516564  ]
[ 3.84538182]
[ 1.41192216]
[-1.77539173]
[ 2.27716232]
[ 3.00967632]
[ 3.86141784]
[ 3.70769956]
[ 3.04300997]
[-3.87501002]
[-0.08277464]
[-2.57842706]
[ 2.43239145]
[ 3.4807545 ]
[-1.73324339]
[-3.76246839]
[ 3.68579411]
[-1.11902028]
[ 3.08796673]
[ 2.92969576]
[-3.51002869]
[-3.55302455]
[ 2.60504961]
[-3.29257747]
[-3.24432773]
[ 3.3382061 ]
[ 3.35485537]
[-3.00380378]
[-2.67622421]
[-3.58166162]
[-2.27299601]
[ 2.55682905]
[ 3.83854893]
[ 3.77113406]
[-2.61103163]
[ 2.60866591]
[-0.27683247]
[ 3.73547823]
[-3.82012914]
[ 1.51409711]
[-3.19500182]
[-2.91077974]
[-0.87310958]
[ 2.82364604]
[ 3.35512836]
[-2.74505819]
[-2.69952236]
[ 0.93045549]
[ 1.10941412]
[-2.06732584]
[-3.46086027]
[ 3.92928886]
[-3.70347323]
[ 1.75930357]
[-3.9832941 ]
[-3.44929718]
[-3.1284727 ]
[-3.05429832]
[ 3.04010298]
[ 3.69754339]
[ 3.82042221]
[-2.99474165]
[ 2.52416303]
[-0.86828846]
[ 2.06806135]
[ 0.0182108 ]
[ 3.53375943]
[ 2.37631877]
[ 3.21589123]
[ 2.78088028]
[ 3.12926481]
[ 3.0567509 ]
[ 3.606847  ]
[ 3.97551431]
[-2.98288897]
[ 1.53447286]
[-2.4271485 ]
[ 2.67508938]
[ 2.15273097]
[ 2.15890756]
[ 3.57087636]
[-3.23707176]
[-3.7247544 ]
[-1.55676923]
[ 3.91426043]
[-0.76847889]
[ 3.00192749]
[ 3.77594657]
[ 3.71434601]
[-3.50776113]
[-3.38850745]
[ 3.26571008]
[ 3.32269324]
[-3.03375764]
[ 3.25254799]
[ 0.46359694]
[ 3.86893569]
[ 3.17021321]
[ 2.16257382]
[ 2.07165857]
[ 3.63293825]
[-2.89732007]
[ 0.21519669]
[ 3.87311279]
[-3.17528021]
[-0.10877796]
[-2.88899442]
[ 3.10703135]
[ 3.870581  ]
[ 1.19296787]
[ 1.5512569 ]
[-1.05824073]
[ 3.45112609]
[-3.00705785]
[-2.54232067]
[-3.71165427]
[ 3.97013664]
[-1.18090109]
[ 3.25577713]
[ 2.62794907]
[-3.65836468]
[ 2.89493326]
[ 1.43515955]
[-3.7089847 ]
[ 2.42989091]
[ 2.35060204]
[ 2.4154189 ]
[-3.78224589]
[-1.85505731]
[-2.78503846]
[-3.94294497]
[ 3.85894809]
[-3.81840544]
[ 1.46020373]
[ 2.19207734]
[-2.22103945]
[-2.0847777 ]
[-2.08646335]
[ 3.70682118]
[ 1.23879038]
[ 3.75289075]
[-2.80549216]
[ 1.53128729]
[-3.00851268]
[ 2.7347285 ]
[ 3.48049578]
[-2.1362175 ]
[ 1.72064192]
[-1.78152837]
[ 3.30561126]
[ 1.40785281]
[ 3.71841809]
[ 2.55891237]
[-2.99584288]
[ 3.54945474]
[-3.60280575]
[-3.57919332]
[-3.684433  ]
[-0.07883628]
[-1.22637366]
[ 3.38462817]
[-2.58150774]
[ 3.30638109]
[-2.94199587]
[ 3.01393039]
[-0.10669188]
[ 1.64652481]
[-1.58745748]
[-2.96065223]
[ 3.52310567]
[-2.95833992]
[ 2.74801592]
[ 3.10631552]
[-3.39396082]
[-1.35634328]
[ 3.81855028]
[-2.75837106]
[ 1.79984302]
[ 3.55909817]
[ 2.56226404]
[-2.92916147]
[ 1.1282712 ]
[-3.09329385]
[-3.45540132]
[-2.5290142 ]
[ 3.1503838 ]
[ 1.41028445]
[-3.25671113]
[ 1.00447212]
[ 3.86542822]
[-2.15558729]
[-3.94799046]
[-2.77777571]
[ 2.8848361 ]
[ 3.31904001]
[ 3.35075848]
[-2.74539411]
[ 3.96751497]
[-3.13442524]
[ 3.53669718]
[ 3.52131861]
[ 3.57179689]
[ 3.58471609]
[ 2.28315109]
[ 0.90966664]
[-1.58582535]
[ 2.06268615]
[-3.66949875]
[ 2.23149961]
[-3.85006489]
[-3.62765458]
[-2.63375445]
[ 3.73913689]
[ 0.7936757 ]
[ 3.93114985]
[-2.39937302]
[ 3.69941643]
[-1.23440584]
[-0.45375359]
[-0.91371948]
[ 3.44645827]
[ 3.93473925]
[ 3.03024388]
[ 3.83512334]
[-1.44914984]
[-2.76549409]
[ 3.53301914]
[ 3.78091291]
[-1.84511269]
[-3.17683684]
[-3.1323713 ]
[ 3.65743832]
[ 1.5991221 ]
[ 2.49812382]
[-1.99199118]
[ 3.74757942]
[ 2.46988978]
[ 2.66914033]
[-3.30852842]
[-2.58603418]
[-3.5185971 ]
[-3.65959781]
[-2.79088   ]
[-3.08774276]
[ 1.85830647]
[ 3.14942985]
[-2.41294283]
[ 3.74033977]
[-1.06737221]
[-2.70241168]
[ 2.81387914]
[-2.78294391]
[-3.79599348]
[ 2.17445655]
[-3.79251658]
[ 3.35981403]
[ 3.16083283]
[ 2.44743718]
[ 2.56065637]
[ 3.45239384]
[ 0.20843136]
[ 3.37244269]
[-3.79399164]
[ 3.35913554]
[ 2.44155477]
[-3.75393817]
[ 3.09798166]
[ 3.60204333]
[ 3.32711806]
[ 2.9298494 ]
[-2.70505377]
[-1.0864459 ]
[-1.76268981]
[ 3.41187216]
[ 1.67810667]
[ 3.39404883]
[ 3.27289076]
[-3.56421426]
[ 3.90227505]
[-3.24677401]
[ 3.30054062]
[ 2.70668666]
[ 3.30111368]
[-3.02911489]
[-2.78235217]
[ 3.99516834]
[-0.40319295]
[-1.79493997]
[-3.94923608]
[ 1.54063695]
[-2.6939461 ]
[ 1.63405   ]
[-3.64324944]
[-1.33497673]
[-3.08760856]
[ 2.86063558]
[-2.50212768]
[-3.55550913]
[-2.84946352]
[-2.80908547]
[ 3.8937808 ]
[-2.61488634]
[ 3.16378022]
[ 3.84508413]
[ 2.7308312 ]
[-2.97484269]
[ 2.51238432]
[ 3.8875995 ]
[-1.5888292 ]
[-2.99739506]
[ 2.00007201]
[-3.76914632]
[-3.19762768]
[ 3.52032353]
[-1.69389439]
[-3.90522961]
[-1.82694248]
[ 3.80592778]
[-3.6885093 ]
[ 2.41712185]
[-2.51287544]
[-1.59478105]
[-3.8019745 ]
[ 1.43632793]
[ 2.76084646]
[ 3.35407177]
[ 2.32373537]
[ 3.68812884]
[ 3.78994617]
[-2.84970686]
[-3.92595534]
[ 3.03050852]
[-3.16255912]
[ 3.67404662]
[ 3.9076191 ]
[-2.15745089]
[ 2.00259823]
[-3.73636526]
[ 3.07224686]
[-3.24524475]
[-3.0026725 ]
[ 3.47868632]
[-3.1617134 ]
[-1.99374717]
[-3.51246886]
[ 2.4912076 ]
[-3.70343924]
[ 1.04885213]
[ 3.46712164]
[-3.36910218]
[ 3.31168832]
[-3.26400474]
[-2.99611483]
[ 2.87082206]
[-3.90486208]
[-0.1814672 ]
[ 2.53103299]
[-0.25189839]
[ 3.52144798]
[-2.22215295]
[-3.9798842 ]
[ 3.23804782]
[-3.99899772]
[ 1.06121092]
[ 3.90427314]
[ 3.57448958]
[ 3.32302965]
[-3.71791004]
[-3.21039157]
[-2.56535163]
[ 3.25507149]
[-3.37989609]
[ 3.1418403 ]
[ 2.2713503 ]
[ 1.77789174]
[-3.91926072]
[ 3.66381415]
[ 2.83766137]
[-3.69570535]
[ 3.54725653]
[ 1.67349963]
[-2.40045099]
[-2.41684931]
[ 1.25108207]
[ 3.06287469]
[-1.40702323]
[ 1.78088456]
[-2.71924522]
[ 3.63446227]
[-1.2281985 ]
[ 2.95682658]
[-1.876736  ]
[ 2.4287591 ]
[-2.65114619]
[-3.49145742]
[-3.71078056]
[-2.98603984]
[-0.77476222]
[ 3.88193656]
[-3.97493362]
[-3.95081596]
[-3.97456663]
[ 2.87828881]
[-3.98207751]
[ 2.55529421]
[ 3.68380969]
[-3.21556582]
[ 1.8150299 ]
[ 3.91742456]
[ 2.59103322]
[ 1.72385422]
[ 3.62991528]
[-2.53388834]
[ 3.43269011]
[-3.85977379]
[ 3.26017263]
[ 3.0241337 ]
[ 2.178348  ]
[-2.53755933]
[-2.65459263]
[ 3.18363309]
[-3.51353199]
[-1.96069896]
[ 3.16749127]
[-2.8774178 ]
[-3.87820406]
[-2.72580792]
[-2.53363118]
[ 3.39508209]
[-1.54015074]
[-2.33863903]
[-2.3829792 ]
[-2.67570344]
[ 0.5545325 ]
[-1.68124514]
[ 3.75927416]
[-1.66026792]
[-2.54295854]
[ 3.753346  ]
[-1.96161988]
[ 1.97455957]
[ 1.53508704]
[-3.05223823]
[-2.92973113]
[-3.71333713]
[-3.5412358 ]
[ 3.39253009]
[-2.46449323]
[-3.60009842]
[-1.06300655]
[-2.75390616]
[-3.65952183]
[ 3.96267667]
[ 1.7925035 ]
[-3.80297771]
[ 2.91856931]
[-3.25284029]
[ 1.70252131]
[-2.2058361 ]
[-3.7462516 ]
[-3.13138922]
[-2.59220867]
[ 3.36278151]
[-3.48866435]
[-2.85333646]
[-1.22317212]
[ 0.52504387]
[-1.35320377]
[ 3.92945932]
[ 2.74578701]
[ 3.15264382]
[-3.20165712]
[ 3.06871546]
[ 2.89359959]
[ 1.99292156]
[-3.54116783]
[-3.72595929]
[ 2.04901689]
[ 2.63519661]
[-3.96610134]
[-3.58746587]
[ 1.09540519]
[-3.95650845]
[-3.50534394]
[ 3.36962009]
[ 3.59966935]
[-3.48050139]
[ 2.6031819 ]
[ 3.04551549]
[-3.72327831]
[-1.90251916]
[-3.38415065]
[-3.62153208]
[-2.50450487]
[-1.05952013]
[ 3.00193311]
[-2.69584636]
[ 2.56939951]
[-1.87034212]
[-3.24102196]
[-3.99775343]
[ 2.75848369]
[ 3.98253951]
[-1.88397387]
[ 1.86776321]
[ 2.12029706]
[-3.65887486]
[-3.93771701]
[-3.43703534]
[ 3.39331805]
[ 2.26489096]
[ 2.51151199]
[-2.70961249]
[ 3.00477606]
[-1.68463447]
[-3.0317509 ]
[ 3.99239869]
[ 3.64358414]
[ 3.59041054]
[-3.92897756]
[ 3.98644574]
[-2.21647856]
[-3.1062161 ]
[ 2.7345353 ]
[ 2.44526355]
[ 2.84304819]
[-2.87288443]
[ 3.68410043]
[-2.06515713]
[ 2.41971127]], prob=[0.26679916 0.28486707 0.28925859 0.26432548 0.16095846 0.31485383
0.10247413 0.2246569  0.12086312 0.04535867]




### What happened?#

The model tries to use analytical functions for integration and sampling if available, otherwise (as happened above), it falls back to the numerical methods. To improve our model, we can add an analytic integral, a common use case. This has to be the integral over the _unnormalized_pdf.

# define the integral function

def cdf_poly(limit, b, c):
return limit + 0.5 * b * limit ** 2 + 1 / 3 * c * limit ** 3

def integral_func(limits, norm_range, params, model):

b = params['b']
c = params['c']

lower, upper = limits.v1.limits

# calculate the integral
integral = cdf_poly(upper, b, c) - cdf_poly(lower, b, c)
print("Integral called")
return integral

# define the space over which it is defined. Here, we use the axes
integral_limits = zfit.Space(axes=(0,), limits=(zfit.Space.ANY, zfit.Space.ANY))

SecondOrderPoly.register_analytic_integral(func=integral_func, limits=integral_limits)

poly2 = SecondOrderPoly(obs=obs, b=b, c=1.2)

integral_analytic = custom_poly.integrate(limits=(-1, 2))
sample = custom_poly.sample(n=1000)
prob_analytic = custom_poly.pdf(sample)
print(f"integral={integral}, sample={sample}, prob={prob[:10]}")

Integral called



Integral called



Integral called



integral=[0.11072835], sample=zfit.Data: Data obs=('obs1',) array=[[-3.33442933]
[-1.08070903]
[-3.40286308]
[-3.5431224 ]
[ 3.16824701]
[ 1.6425478 ]
[ 2.41833329]
[ 2.69527588]
[-2.11941539]
[ 3.56692863]
[ 2.99364126]
[-2.24270819]
[-2.23782141]
[ 2.2321574 ]
[-3.38245596]
[ 2.93717713]
[ 2.33420646]
[-2.68805143]
[-3.36724936]
[-3.29697227]
[-3.78951787]
[-1.78356648]
[ 1.00208816]
[-2.58462777]
[ 3.83897247]
[ 3.69757823]
[ 3.94083733]
[ 1.41209427]
[-3.95246143]
[-2.48943891]
[-3.41258223]
[-3.77742303]
[ 0.21737679]
[ 3.68860959]
[ 2.80226488]
[ 2.9980766 ]
[ 2.35239764]
[-1.09468829]
[-3.86309992]
[ 2.4960417 ]
[ 3.73328121]
[-2.59082218]
[ 3.62469198]
[-3.31557813]
[ 3.80734825]
[ 3.67680402]
[-1.62171948]
[-1.12550234]
[-2.95209053]
[ 3.78723626]
[-2.28440065]
[ 2.69060137]
[-3.62456894]
[-0.7116482 ]
[-3.07990022]
[-3.0911219 ]
[ 3.4413677 ]
[-1.42646925]
[ 2.67838957]
[ 2.02442978]
[ 3.77390814]
[-3.62857891]
[ 1.63948129]
[-2.82080091]
[ 2.59991654]
[ 1.47722028]
[ 3.63110086]
[ 2.89521288]
[-3.50283711]
[-2.85794507]
[ 2.96955054]
[-2.33127852]
[ 3.09680218]
[-3.07712077]
[-3.53198151]
[ 1.53596148]
[-3.6810983 ]
[-3.36196452]
[-3.62779774]
[-1.74098399]
[ 3.82166142]
[ 1.53796264]
[-3.22959579]
[-1.5733647 ]
[ 3.06721676]
[-2.75339597]
[-3.78088421]
[ 3.95640657]
[ 1.57332999]
[ 2.6497506 ]
[ 2.11223589]
[-2.16423012]
[-3.56578018]
[-3.7239532 ]
[-1.63373631]
[ 1.70714893]
[ 3.0617766 ]
[-2.4896846 ]
[-3.79383328]
[ 3.05429743]
[ 2.34184041]
[-3.2289612 ]
[ 3.06040614]
[ 1.92014573]
[-2.70345229]
[-3.55259682]
[-2.19930155]
[-2.159821  ]
[-2.77037836]
[ 3.54805924]
[-2.77572739]
[ 1.92005973]
[ 2.10995401]
[-3.76645643]
[ 2.09228623]
[ 2.59299834]
[ 3.91817187]
[-2.85922149]
[ 1.24876995]
[ 2.69675431]
[-3.35667594]
[-2.97622827]
[ 3.43141919]
[ 2.07109125]
[ 2.61194469]
[ 3.84760928]
[ 3.43134445]
[ 2.57288516]
[ 2.22877501]
[ 3.69372722]
[-3.80104951]
[ 2.4662596 ]
[-3.57652709]
[-3.85075288]
[-1.38705905]
[-3.54200098]
[ 2.62173124]
[-0.84175114]
[ 2.21956625]
[-3.96242743]
[-1.77129741]
[-1.98985307]
[ 3.0629612 ]
[ 3.18397963]
[-0.9839095 ]
[ 1.93586334]
[ 3.66520773]
[ 3.31930013]
[ 2.67516751]
[-1.0722273 ]
[ 1.83923463]
[ 2.03174534]
[ 3.80589642]
[-2.71031212]
[-1.0781448 ]
[ 3.95289358]
[ 1.87859318]
[ 3.49848835]
[ 3.90085875]
[ 3.73151903]
[ 2.20580955]
[-3.96916545]
[ 3.31298873]
[-3.69832279]
[-3.40207813]
[-2.48895857]
[ 2.58255106]
[ 2.00005665]
[ 2.17491981]
[ 1.27235558]
[ 2.36597302]
[-3.63982723]
[-3.2981881 ]
[ 2.94849017]
[ 2.68685841]
[ 1.46781463]
[ 3.54596527]
[ 2.44413131]
[-3.04116393]
[ 2.25873922]
[-2.58297833]
[ 3.67963772]
[-3.13649364]
[ 2.38258967]
[ 3.92814862]
[-1.87131125]
[ 3.73369956]
[ 3.7469775 ]
[ 3.24670324]
[ 3.01580135]
[ 3.66523183]
[-3.54299424]
[ 3.70092824]
[-2.39978829]
[-2.08380575]
[ 3.62890864]
[-2.18979903]
[ 1.11707406]
[-3.0833978 ]
[-2.20579006]
[ 3.26612029]
[ 3.87669214]
[ 3.98409693]
[-2.1317549 ]
[-3.02124462]
[-1.70540613]
[-3.08107843]
[ 3.80042768]
[-3.80051954]
[ 3.47285319]
[ 3.5598611 ]
[ 3.50910894]
[-3.85060345]
[ 1.36446224]
[-1.75904365]
[ 3.203922  ]
[-3.85386828]
[ 3.79000838]
[ 1.82579138]
[-3.80049264]
[-3.63944432]
[-3.88107092]
[ 2.69469791]
[ 2.40110589]
[-3.6575911 ]
[-1.21110539]
[-3.89776089]
[-3.37978093]
[ 3.72805825]
[ 2.04993672]
[ 1.74662243]
[-3.513462  ]
[ 3.42564279]
[ 2.83847286]
[ 1.45968283]
[-3.92646609]
[-2.77206051]
[-3.8315873 ]
[ 2.56129646]
[-3.96861559]
[-1.9218058 ]
[-3.97797782]
[-3.1483242 ]
[ 3.22394018]
[-3.28639475]
[ 3.94329059]
[ 3.7600398 ]
[-3.16201249]
[ 3.26227069]
[ 2.7771039 ]
[-3.63012105]
[-3.82227546]
[-3.38837511]
[-3.97902688]
[ 1.47115335]
[ 0.75610834]
[ 2.89638605]
[-3.76174401]
[-3.03065225]
[-3.67984615]
[ 2.34607875]
[-3.76616754]
[ 1.50354022]
[ 3.90646935]
[-1.2752644 ]
[-3.15820522]
[-2.38658447]
[ 2.00554931]
[-3.93582507]
[ 0.78406941]
[-1.05568812]
[ 3.57420349]
[-3.56507889]
[ 3.6774681 ]
[ 3.33744323]
[ 3.54574516]
[-3.70453987]
[-3.90364649]
[ 2.48370394]
[ 0.22425027]
[-3.61708099]
[-3.77156977]
[ 1.67708907]
[ 3.25375281]
[-2.71728229]
[-2.85655562]
[ 2.75380551]
[-3.29929706]
[-1.78437825]
[ 3.50713127]
[ 1.75083398]
[-3.80055325]
[ 2.6381017 ]
[ 3.60320683]
[-2.84246892]
[-0.18148901]
[-3.13212066]
[ 3.50173556]
[-3.27208172]
[-2.79744818]
[-3.07134536]
[ 1.02463396]
[ 3.03690071]
[ 3.96783391]
[-1.83943673]
[ 2.09327056]
[ 3.563821  ]
[ 3.70477353]
[-2.05120013]
[-3.45559648]
[-3.00438617]
[-2.7044579 ]
[ 3.53267297]
[-2.16875451]
[ 3.34116755]
[ 3.15721315]
[-3.81792453]
[-2.7953653 ]
[ 2.99974665]
[-3.91871214]
[ 2.38127972]
[-0.56466879]
[-2.11271708]
[-2.40289252]
[-3.45909312]
[-1.73241005]
[ 3.90072207]
[ 2.26725116]
[-1.95171612]
[-3.55648265]
[-3.44331942]
[-3.19712528]
[ 1.00478986]
[ 3.41268268]
[-2.80500263]
[-3.97462791]
[ 1.42701879]
[-2.84576945]
[ 2.61000159]
[ 3.85518548]
[-3.96539538]
[ 3.94283872]
[-3.33902526]
[-3.33131444]
[-2.99946747]
[ 1.2228686 ]
[ 1.4812341 ]
[-2.96694744]
[-2.62673959]
[-2.44880367]
[ 3.93002882]
[-3.86447126]
[ 3.89787494]
[-3.04577995]
[-3.69432118]
[-2.67782647]
[ 3.55277144]
[ 2.9949517 ]
[ 3.72679915]
[ 2.57400848]
[ 3.4230879 ]
[ 2.39945813]
[ 3.80522361]
[ 2.27253697]
[ 2.63035861]
[-2.00551878]
[-3.96799242]
[-3.72247352]
[-3.76824041]
[ 1.69198376]
[ 3.46317921]
[-3.97876847]
[ 2.92551471]
[ 0.01278932]
[ 3.99273218]
[-1.62757305]
[ 2.68799177]
[-1.93480266]
[ 3.39540871]
[-3.2165954 ]
[-3.53420313]
[-3.43580018]
[-2.7797852 ]
[ 3.58931406]
[ 3.34523704]
[ 3.53275942]
[ 3.86784739]
[ 2.90417134]
[-3.53473027]
[ 2.74783691]
[-3.2135341 ]
[ 1.9370248 ]
[ 3.234876  ]
[ 3.77767824]
[ 3.69765446]
[-3.00220674]
[ 3.93041761]
[-2.51634226]
[ 0.06854905]
[-1.71571236]
[-3.20042835]
[-3.60658266]
[ 3.74163622]
[-3.95450618]
[-3.7260918 ]
[-3.61982635]
[-2.9925084 ]
[-3.04703719]
[-3.8260452 ]
[ 2.51770282]
[ 3.64874017]
[-3.49474761]
[ 3.55853915]
[ 2.89457022]
[-0.38790648]
[-3.18963067]
[-2.9817928 ]
[-3.93434278]
[ 3.94807608]
[ 3.10122427]
[-3.91658091]
[-1.06725128]
[-3.97610176]
[ 2.86264682]
[ 3.16914549]
[ 3.89531545]
[ 1.78789248]
[-2.61229156]
[-3.64566575]
[-3.09939722]
[-3.75232474]
[ 3.76949081]
[ 3.46244633]
[ 3.77205898]
[ 3.73447016]
[ 1.24994706]
[ 3.46885222]
[ 3.26188918]
[ 3.33969722]
[ 2.99469804]
[-2.80768086]
[ 3.56782614]
[-3.47356686]
[-3.1802983 ]
[ 2.6667764 ]
[ 2.80881615]
[-1.1896698 ]
[ 3.95844449]
[ 1.6492397 ]
[ 2.7092034 ]
[-2.1149109 ]
[ 3.93203235]
[-2.68835241]
[-3.18234903]
[-2.75028732]
[ 3.25451425]
[-2.86704394]
[ 3.22710081]
[ 3.62192877]
[-3.14074316]
[-3.89244254]
[ 2.14015582]
[-1.48621915]
[ 3.53790429]
[ 3.30667982]
[ 0.69244344]
[ 3.93823541]
[ 2.53209923]
[-3.87527307]
[-3.99658052]
[ 3.94023972]
[-3.12062238]
[-3.96474704]
[ 1.65226242]
[-3.67137016]
[ 2.06235456]
[ 3.97346777]
[ 3.25725146]
[ 3.54302583]
[-3.69586792]
[ 2.63639863]
[ 2.84303763]
[-1.28707768]
[-3.34316003]
[ 2.70244923]
[-2.24061607]
[-3.43547603]
[ 3.82345305]
[-3.99453585]
[ 3.39852694]
[ 1.91956683]
[-2.04716467]
[ 1.24882084]
[-3.51852725]
[ 2.87235622]
[-3.73542681]
[-3.59351528]
[ 3.82481226]
[ 3.15075847]
[ 3.44932419]
[ 3.02617201]
[-1.67161188]
[-3.12191886]
[ 3.65598488]
[-1.75577118]
[ 3.85877768]
[ 3.85942324]
[-2.78043714]
[ 1.32248272]
[ 3.92050828]
[ 2.95186907]
[-1.61836588]
[ 3.09968467]
[ 2.9890651 ]
[ 3.63488894]
[ 3.93122256]
[ 2.32923756]
[-3.34410157]
[-3.41558846]
[ 2.4691349 ]
[ 3.59807466]
[-3.78453896]
[-2.60197274]
[ 2.29646615]
[ 3.61136073]
[-3.80323503]
[ 2.85722649]
[ 3.06304354]
[ 2.45346339]
[ 2.23982174]
[ 3.8001185 ]
[-3.79721508]
[ 3.19361717]
[-0.3101943 ]
[ 3.43682941]
[ 3.89320574]
[-3.61127486]
[ 2.60719975]
[ 2.63308187]
[-1.67822714]
[ 2.84235101]
[-3.77903227]
[ 2.9512479 ]
[ 3.97393821]
[ 2.93371157]
[ 3.38039015]
[ 2.86325418]
[-3.6487146 ]
[ 2.2211634 ]
[-3.97505535]
[ 2.91794547]
[ 3.57644885]
[ 0.72658234]
[ 3.07473667]
[-1.37871072]
[-3.66974511]
[ 3.42370032]
[ 2.12206965]
[ 0.31809028]
[ 3.53487589]
[-1.25444763]
[-0.62036476]
[-3.73163283]
[-3.1762999 ]
[-3.94397722]
[-3.5716388 ]
[ 1.96475845]
[ 3.84982432]
[-3.85537922]
[ 3.8300016 ]
[ 3.83293906]
[-2.31174982]
[-3.66590872]
[-2.90574669]
[-3.92474289]
[-3.27440245]
[-2.9757075 ]
[-2.71373897]
[ 3.37717955]
[-3.1694861 ]
[ 1.78327197]
[ 3.70550017]
[ 3.25381434]
[ 2.64792095]
[ 3.13598822]
[-2.01658991]
[-2.56863632]
[-3.74225361]
[ 0.77437483]
[ 3.21232278]
[ 1.83872631]
[-3.7405752 ]
[-1.24684328]
[ 3.62001034]
[-3.50257042]
[ 3.53975276]
[ 0.7676876 ]
[-3.27174681]
[ 3.1619507 ]
[-3.41590299]
[ 1.10899825]
[ 3.240239  ]
[-3.88435503]
[-3.33361668]
[ 0.79628986]
[ 3.97017787]
[-3.11451137]
[ 2.37039412]
[ 3.86630772]
[-2.84851696]
[-2.86877154]
[ 2.03834577]
[-0.64460999]
[-3.63052215]
[-3.46799503]
[ 1.01815365]
[ 2.85642836]
[ 3.98717905]
[ 3.70819447]
[ 3.21179207]
[-3.17053384]
[ 3.64884808]
[ 2.001369  ]
[ 3.69406266]
[ 3.66036759]
[-3.78481108]
[-2.92472088]
[-2.19317202]
[ 2.93084331]
[ 1.10057354]
[-1.34137797]
[ 2.5032344 ]
[ 2.71585581]
[-3.6274152 ]
[ 1.99607629]
[ 2.81763985]
[ 1.63466589]
[ 3.92588869]
[ 3.65059388]
[ 3.48278938]
[ 2.6179916 ]
[-1.89566222]
[ 1.5447091 ]
[ 3.8507869 ]
[-3.59263775]
[ 1.19241493]
[-2.9947182 ]
[ 2.67561935]
[ 3.90816519]
[ 3.14338747]
[-3.45106815]
[ 2.24792841]
[ 3.76896889]
[-3.7897256 ]
[ 2.70698916]
[-1.91515486]
[ 0.74471395]
[ 3.54195912]
[ 1.65960295]
[-2.87109225]
[-3.65961656]
[ 3.1276309 ]
[-3.22322692]
[-3.26807189]
[-0.37378676]
[-2.33968209]
[-3.84813587]
[-3.20801295]
[-3.8781798 ]
[-2.9344273 ]
[-3.4067129 ]
[-2.52409976]
[-2.48653048]
[ 3.34609003]
[-3.62063818]
[ 1.79763752]
[ 3.38089438]
[ 2.25721247]
[-2.99172856]
[ 1.11711074]
[ 3.91920211]
[ 2.23254118]
[ 2.56744197]
[ 3.94945976]
[ 3.76459971]
[ 2.59093704]
[-3.86117226]
[-3.61813052]
[-2.43438487]
[ 3.39786246]
[-2.34762009]
[-1.47631462]
[-2.84577732]
[ 2.5045855 ]
[ 2.15233277]
[ 1.82378102]
[-3.82180735]
[-2.92223449]
[-3.08479182]
[ 2.18317685]
[-1.91355271]
[ 3.98312579]
[ 2.77828239]
[ 3.61157373]
[ 2.8712959 ]
[-2.8474496 ]
[-0.77221648]
[-1.50794234]
[-3.50629275]
[ 3.19672496]
[ 3.57822664]
[ 0.96061254]
[-0.97988568]
[-2.61998268]
[ 3.38806932]
[ 2.79588735]
[-2.29202206]
[ 3.15233668]
[ 2.53794075]
[-2.4760543 ]
[ 3.72796866]
[ 3.67303132]
[ 3.39635267]
[ 3.48058064]
[ 2.17357216]
[ 1.89682496]
[ 2.94780436]
[ 0.95640113]
[-2.45609041]
[-3.54030343]
[-3.53556182]
[-3.57768457]
[ 3.22438866]
[-3.57844358]
[ 3.61821676]
[-3.69543361]
[-3.22422292]
[ 2.43094034]
[-3.90736868]
[ 2.15675542]
[-0.05690722]
[ 3.07283909]
[ 1.85731931]
[ 2.58681842]
[ 1.8181943 ]
[ 3.37158659]
[-3.20048894]
[ 2.76793596]
[ 2.63361799]
[ 2.39181891]
[ 2.11281175]
[ 2.60139846]
[ 2.9645379 ]
[ 1.7304405 ]
[-0.61720598]
[ 3.64682049]
[-1.78150783]
[ 2.52208278]
[-3.2893872 ]
[-3.97253451]
[ 3.08700293]
[-1.53412685]
[ 3.2116434 ]
[-1.40375657]
[-3.88146055]
[ 0.37269111]
[ 2.6932597 ]
[-2.60132624]
[ 3.97668744]
[ 3.38523538]
[-2.76918844]
[ 3.57499053]
[ 3.16539928]
[ 3.62280465]
[ 3.9400916 ]
[ 2.83813221]
[-2.77188578]
[ 3.60917851]
[-3.9655528 ]
[ 1.34743262]
[-1.1505768 ]
[ 2.61486039]
[ 3.55464306]
[-3.16591622]
[ 2.91521834]
[ 2.03376858]
[ 2.71155031]
[ 3.16156843]
[-2.93765666]
[-3.44870721]
[-3.31655249]
[ 1.80635466]
[ 1.92688038]
[-2.85781494]
[-2.82181917]
[ 3.76610043]
[ 0.31923479]
[-3.70232706]
[ 3.0884451 ]
[-3.13666929]
[ 3.87699804]
[-3.04403584]
[-2.47688128]
[-3.40732458]
[-3.61291663]
[ 1.90329093]
[ 2.17848482]
[-3.78849949]
[-1.11898352]
[-0.84726392]
[-1.2554464 ]
[ 3.58230043]
[ 2.00014736]
[ 2.36390556]
[ 3.34304691]
[-3.70711748]
[ 3.85934303]
[-3.37619116]
[ 2.69233014]
[ 1.9443823 ]
[ 0.9794571 ]
[-2.58934439]
[ 3.4360311 ]
[ 2.94392135]
[ 3.76090845]
[ 0.20863891]
[-3.02360665]
[-3.05515209]
[-3.93418693]
[ 3.86566923]
[ 2.078694  ]
[ 0.47489498]
[ 3.43054531]
[ 1.93521631]
[ 2.64675405]
[-1.91104845]
[-3.5665927 ]
[-3.3407861 ]
[-3.32412659]
[ 3.3163942 ]
[ 3.11869903]
[-3.74548379]
[-2.92023313]
[ 2.37056786]
[ 3.36625481]
[-3.71781783]
[ 2.94238234]
[ 2.32960551]
[ 3.06695802]
[-3.81124331]
[ 2.24461642]
[ 3.76485442]
[ 3.923557  ]
[ 2.08341913]
[ 3.80379008]
[-3.68167268]
[ 3.88917449]
[ 2.77358235]
[-2.31437128]
[-3.86456183]
[-3.85072907]
[-2.9671684 ]
[-3.69297906]
[ 3.64194314]
[-3.87765978]
[-3.53425404]
[ 2.87173215]
[-2.94210331]
[ 3.20039219]
[ 3.67526469]
[ 3.95884722]
[-3.69202853]
[ 0.0061054 ]
[ 2.77007861]
[ 3.73913023]
[-3.69400648]
[ 3.16362757]
[-2.35197765]
[-3.31882629]
[-1.37774183]
[ 2.13713445]
[-3.8936278 ]
[ 3.11177315]
[-1.51990553]
[ 2.57405884]
[ 2.8091982 ]
[ 1.52568358]
[ 2.87167194]
[ 2.93236311]
[ 3.46186397]
[-3.11576955]
[-2.69131421]
[-1.10380874]
[ 3.71906064]
[ 1.84364725]
[-1.85820188]
[-3.91214133]
[-2.93061093]
[ 2.59615144]
[ 1.7162051 ]
[-3.88772808]
[ 3.13438758]
[ 3.83196058]
[-3.61693194]
[-3.85616147]
[-3.36438752]
[-3.04915537]
[ 2.93116008]
[ 3.2780756 ]
[ 3.08913096]
[ 3.78810015]
[-3.23401226]
[-2.92585363]
[-2.94520685]
[-2.62297438]
[ 3.09825061]
[ 1.64499339]
[ 1.7251677 ]
[-1.40981347]
[ 3.86721942]
[ 3.85945696]
[-2.95618694]
[-3.92353512]
[ 2.33013149]
[-2.36562876]
[ 1.03936305]
[ 2.6425662 ]
[-3.16024353]
[-1.7183392 ]
[ 0.92768993]
[ 3.10459408]
[ 3.75477687]
[ 3.62927258]
[ 3.42958044]
[-3.06920147]
[-3.35435337]
[ 3.80828726]
[ 3.852657  ]
[ 3.19663453]
[-3.89702222]
[ 2.22314996]
[ 3.72505934]
[-3.23614443]
[ 1.65932426]
[-2.24631839]
[ 1.09187613]
[-1.74721619]
[-2.18937903]
[-1.70509278]
[-3.53029445]
[-2.00840862]
[-3.90958935]
[-3.78780522]
[-2.58890335]
[-3.68736926]
[ 0.07379463]
[ 1.64964794]
[-3.96845923]
[-2.6146223 ]
[ 3.45685759]
[ 3.24896877]
[ 1.13032707]
[ 1.67038205]
[ 3.39248891]
[-3.04235269]
[-3.86411497]
[ 0.29539273]
[ 3.62499309]
[ 3.91018422]
[ 2.55466138]
[-3.31553328]
[-3.52168178]
[ 3.44151475]
[-2.9591257 ]
[ 3.42153871]
[-3.11638027]
[ 2.72517696]
[ 2.13722497]
[-3.59279868]
[ 0.62211659]
[ 2.98784993]
[-3.96075089]
[ 3.46188766]
[ 0.64106247]
[ 1.69064085]
[-2.13442248]
[-2.76896883]
[-2.64590391]
[-1.23195183]
[-3.40896818]
[-3.76500233]
[-3.44420519]
[-2.95770417]
[-3.40706587]
[ 3.86832223]
[ 1.80653071]
[ 2.61236603]
[-3.97072658]
[ 1.29735937]
[ 3.93197453]], prob=[0.26679916 0.28486707 0.28925859 0.26432548 0.16095846 0.31485383
0.10247413 0.2246569  0.12086312 0.04535867]




## Multiple dimensions and parameters with angular observables#

So far, we used rather simple examples and many basic shapes, such as polynomials, already have an efficient implementation within zfit. Therefore, we will now create a three dimensional PDF measuring the angular observables of a $$B^+ \rightarrow K^* l l$$ decay.

The implementation is not “special” or complicated at all, it rather shows how to deal with multiple dimensions and how to manage several parameters. It was created using the equation of the angular observables (taken from a paper).

Many thanks to Rafael Silva Coutinho for the implementation!

class AngularPDF(zfit.pdf.ZPDF):
"""Full d4Gamma/dq2dOmega for Bd -> Kst ll (l=e,mu)

Angular distribution obtained in the total PDF (using LHCb convention JHEP 02 (2016) 104)
i.e. the valid of the angles is given for
- phi: [-pi, pi]
- theta_K: [0, pi]
- theta_l: [0, pi]

The function is normalized over a finite range and therefore a PDF.

Args:

FL (zfit.Parameter): Fraction of longitudinal polarisation of the Kst
S3 (zfit.Parameter): A_perp^2 - A_para^2 / A_zero^2 + A_para^2 + A_perp^2 (L, R)
S4 (zfit.Parameter): RE(A_zero*^2 * A_para^2) / A_zero^2 + A_para^2 + A_perp^2 (L, R)
S5 (zfit.Parameter): RE(A_zero*^2 * A_perp^2) / A_zero^2 + A_para^2 + A_perp^2 (L, R)
AFB (zfit.Parameter): Forward-backward asymmetry of the di-lepton system (also i.e. 3/4 * S6s)
S7 (zfit.Parameter): IM(A_zero*^2 * A_para^2) / A_zero^2 + A_para^2 + A_perp^2 (L, R)
S8 (zfit.Parameter): IM(A_zero*^2 * A_perp^2) / A_zero^2 + A_para^2 + A_perp^2 (L, R)
S9 (zfit.Parameter): IM(A_perp*^2 * A_para^2) / A_zero^2 + A_para^2 + A_perp^2 (L, R)
obs (zfit.Space):
name (str):
dtype (tf.DType):
"""

_PARAMS = ['FL', 'S3', 'S4', 'S5', 'AFB', 'S7', 'S8', 'S9']
_N_OBS = 3

@zfit.supports()
def _unnormalized_pdf(self, x, params):
FL = params['FL']
S3 = params['S3']
S4 = params['S4']
S5 = params['S5']
AFB = params['AFB']
S7 = params['S7']
S8 = params['S8']
S9 = params['S9']

costheta_l = x[0]
costheta_k = x[1]
phi = x[2]

sintheta_k = tf.sqrt(1.0 - costheta_k * costheta_k)
sintheta_l = tf.sqrt(1.0 - costheta_l * costheta_l)

sintheta_2k = (1.0 - costheta_k * costheta_k)
sintheta_2l = (1.0 - costheta_l * costheta_l)

sin2theta_k = (2.0 * sintheta_k * costheta_k)
cos2theta_l = (2.0 * costheta_l * costheta_l - 1.0)
sin2theta_l = (2.0 * sintheta_l * costheta_l)

pdf = ((3.0 / 4.0) * (1.0 - FL) * sintheta_2k +
FL * costheta_k * costheta_k +
(1.0 / 4.0) * (1.0 - FL) * sintheta_2k * cos2theta_l +
-1.0 * FL * costheta_k * costheta_k * cos2theta_l +
S3 * sintheta_2k * sintheta_2l * znp.cos(2.0 * phi) +
S4 * sin2theta_k * sin2theta_l * znp.cos(phi) +
S5 * sin2theta_k * sintheta_l * znp.cos(phi) +
(4.0 / 3.0) * AFB * sintheta_2k * costheta_l +
S7 * sin2theta_k * sintheta_l * znp.sin(phi) +
S8 * sin2theta_k * sin2theta_l * znp.sin(phi) +
S9 * sintheta_2k * sintheta_2l * znp.sin(2.0 * phi))

return pdf


### Multidimensional Spaces#

This PDF now expects multidimensional data. Therefore, we need to provide a Space in multiple dimensions. The preferred way is to use the product operations to build this space from one dimensional Spaces

costhetha_k = zfit.Space('costheta_k', (-1, 1))
costhetha_l = zfit.Space('costheta_l', (-1, 1))
phi = zfit.Space('phi', (-np.pi, np.pi))
angular_obs = costhetha_k * costhetha_l * phi


### Managing parameters#

Luckily, we’re in Python, which provides many tools out-of-the-box. Handling parameters in a dict can make things very easy, even for several parameters as here.

params_init = {'FL':  0.43, 'S3': -0.1, 'S4': -0.2, 'S5': -0.4, 'AFB': 0.343, 'S7': 0.001, 'S8': 0.003, 'S9': 0.002}
params = {name: zfit.Parameter(name, val, -1, 1) for name, val in params_init.items()}
angular_pdf = AngularPDF(obs=angular_obs, **params)

integral_analytic = angular_pdf.integrate(limits=angular_obs)  # this should be one
sample = angular_pdf.sample(n=1000)
prob_analytic = angular_pdf.pdf(sample)
print(f"integral={integral}, sample={sample}, prob={prob[:10]}")

Estimated integral error ( 2.4615430566483435e-05 ) larger than tolerance ( 3e-06 ), which is maybe not enough (but maybe it's also fine). You can (best solution) implement an anatytical integral (see examples in repo) or manually set a higher number on the PDF with 'update_integration_options' and increase the 'max_draws' (or adjust 'tol'). If partial integration is chosen, this can lead to large memory consumption.This is a new warning checking the integral accuracy. It may warns too often as it is Work In Progress. If you have any observation on it, please tell us about it: https://github.com/zfit/zfit/issues/new/chooseTo suppress this warning, use zfit.settings.set_verbosity(-1).

Estimated integral error ( 2.4615430566483435e-05 ) larger than tolerance ( 3e-06 ), which is maybe not enough (but maybe it's also fine). You can (best solution) implement an anatytical integral (see examples in repo) or manually set a higher number on the PDF with 'update_integration_options' and increase the 'max_draws' (or adjust 'tol'). If partial integration is chosen, this can lead to large memory consumption.This is a new warning checking the integral accuracy. It may warns too often as it is Work In Progress. If you have any observation on it, please tell us about it: https://github.com/zfit/zfit/issues/new/chooseTo suppress this warning, use zfit.settings.set_verbosity(-1).

Estimated integral error ( 2.4615430566483435e-05 ) larger than tolerance ( 3e-06 ), which is maybe not enough (but maybe it's also fine). You can (best solution) implement an anatytical integral (see examples in repo) or manually set a higher number on the PDF with 'update_integration_options' and increase the 'max_draws' (or adjust 'tol'). If partial integration is chosen, this can lead to large memory consumption.This is a new warning checking the integral accuracy. It may warns too often as it is Work In Progress. If you have any observation on it, please tell us about it: https://github.com/zfit/zfit/issues/new/chooseTo suppress this warning, use zfit.settings.set_verbosity(-1).

integral=[0.11072835], sample=zfit.Data: Data obs=('costheta_k', 'costheta_l', 'phi') array=[[ 0.80811363 -0.77172246 -2.30893868]
[-0.83193635 -0.4766382   1.59708571]
[-0.50210578  0.90534143  2.79357558]
...
[-0.47080122  0.32750895 -2.39045202]
[ 0.91584436 -0.57738717  2.17138595]
[ 0.49159317  0.35894031 -1.09948168]], prob=[0.26679916 0.28486707 0.28925859 0.26432548 0.16095846 0.31485383
0.10247413 0.2246569  0.12086312 0.04535867]




### Including another observable#

We built our angular PDF successfully and can use this 3 dimensional PDF now. If we want, we could also include another observable. For example, the polynomial that we created above and make it 4 dimensional. Because it’s so simple, let’s do that!

full_pdf = angular_pdf * poly2

# equivalently
# full_pdf = zfit.pdf.ProductPDF([angular_pdf, poly2])


Done! This PDF is now 4 dimensional, which had to be, given that the observable of poly2 is different from the observable of angular_pdf. If they would coincide, e.g. if poly2 had the observable phi, this would now be a 3 dimensional PDF.

print(f"obs angular: {angular_pdf.obs} obs poly:{poly2.obs} obs product: {full_pdf.obs})")

obs angular: ('costheta_k', 'costheta_l', 'phi') obs poly:('obs1',) obs product: ('costheta_k', 'costheta_l', 'phi', 'obs1'))




## What happened exactly ?#

The model tries to be as smart as possible and calls the most explicit function. Then it starts falling back to alternatives and uses, whenever possible, the analytic version (if available), otherwise a numerical.

The rule simplified: public (sanitizes input and) calls […] private. So e.g. pdf calls _pdf and if this is not provided, it uses the fallback that may not be optimized, but general enough to work.

The rule extended (in its current implementation): public calls a series of well defined methods and hooks before it calls the private method. These intermediate can be used, they mostly automatically catch certain cases and handle them for us.

To remember: in order to have full control over a public function such as integrate, pdf, sample or normalization, the private method, e.g. _integrate can be overriden and is guaranteed to be called before other possibilities.

In the case above, pdf called first _pdf (which is not implemented), so it calls _unnormalized_pdf and divides this by the normalization. The latter also does not have an explicit implementation (_implementation), so it uses the fallback and calls integrate over the norm_range. Since _integrate is not provided, the fallback tries to perform an analytic integral, which is not available. Therefore, it integrates the _unnormalized_prob numerically. In all of this calls, we can hook in by overriding the mentioned, specified methods.

What we did not mention: ZPDF is just a wrapper around the actual BasePDF that should be preferred in general; it simply provides a convenient __init__. For the next example, we will implement a multidimensional PDF and use the custom __init__

### Overriding pdf#

Before, we used _unnormalized_pdf, which is the common use-case. Even if we want to add an analytic integral, we can register it. Or do more fancy stuff like overriding the _normalization. We can however also get the full control of what our model output by directly overriding _pdf. The signature does not contain only x but additionally norm_range. This can have no limits (norm_range.has_limits is False), in which case the “unnormalized pdf” is requested. Otherwise, norm_range can have different limits and we have to take care of the proper normalization.

This is usually not needed and inside zfit, all PDFs are implemented using the _unnormalized_pdf.

Therefore, it provides mostly a possibility to implement whatever is wanted, any unforeseen use-case, any kind of hack to “just try out something”.

class CustomPDF(zfit.pdf.BasePDF):
"""My custom pdf with three parameters.

"""

def __init__(self, param1, param2, param3, obs, name="CustomPDF", ):
# we can now do complicated stuff here if needed
# only thing: we have to specify explicitly here what is which parameter
params = {'super_param': param1,  # we can change/compose etc parameters
'param2': param2, 'param3': param3}
super().__init__(obs, params, name=name)

@zfit.supports(norm=True)
def _pdf(self, x, norm):
data = z.unstack_x(x)
param1 = self.params['super_param']
param2 = self.params['param2']
param3 = self.params['param3']

# just an arbitrary function
probs = 42 * param1 + (data * param3) ** param2
return probs


In a similar manner, other methods can be overriden as well. We won’t go into further details here, as this provides a quite advanced task. Furthermore, if stability is a large concern or such special cases need to be implemented, it is recommended to get in contact with the developers and share the idea.

### Composed PDFs#

So far, we only looked at creating a model that depends on parameters and data but did not include other models. This is crucial to create for example sums or products of PDFs. Instead of inheriting from BasePDF, we can use the BaseFunctor that contains a mixin which handles daughter PDFs correctly.

The main difference is that we can now provide a list of PDFs that our model depends on. There can still be parameters (as for example the fracs for the sum) that describe the behavior of the models but they can also be omitted (e.g. for the product). _Sidenote: technically, a normal BasePDF can of course also have no parameters, however, since this is a constant function without dependencies, this will rarely be used in practice.

class SquarePDF(zfit.pdf.BaseFunctor):
"""Example of a functor pdf that takes the log of a single PDF.

DEMONSTRATION PURPOSE ONLY, DO **NOT** USE IN REAL CASE.
"""

def __init__(self, pdf1, name="SumOf3"):
pdfs = [pdf1]  # we could have more of course, e.g. for sums
# no need for parameters here, so we can omit it
obs = pdf1.space
super().__init__(pdfs=pdfs, obs=obs, name=name)

def _unnormalized_pdf(self, x):
# we do not need to unstack x here as we want to feed it directly to the pdf1
pdf1 = self.pdfs[0]

return pdf1.pdf(x) ** 2

squarepdf = SquarePDF(pdf1=poly2)

squarepdf.integrate(limits=(-2, 3.2))

Integral called



Integral called



<tf.Tensor: shape=(1,), dtype=float64, numpy=array([0.22241748])>

sample_square = squarepdf.sample(n=1000)
sample_square

Integral called



<zfit.Data: Data obs=('obs1',) shape=(1000, 1)>

squarepdf.pdf(sample_square)[:10]

Integral called



Integral called



<tf.Tensor: shape=(10,), dtype=float64, numpy=
array([0.54233369, 0.57406307, 0.39931534, 0.59427233, 0.25196275,
0.46753243, 0.07864901, 0.20838655, 0.11738236, 0.29979387])>


## …and now?#

We’ve implemented a custom PDF. Maybe spent quite some time fine tuning it, debugging it. Adding an integral. And now? Time to make it available to others: zfit-physics. This repository is meant for community contributions. It has less requirements to contribute than to zfit core and has a low threshold. Core devs can provide you with help and you can provide the community with a PDF.

Make an issue or a PR, everything is welcome!

### Mixing with pure Python#

Whenever possible, it is preferrable to write anything in TensorFlow. But there is the possibility to mix with pure Python, however losing many of the benefits that TensorFlow provides. To do so:

• try to use z.py_function or tf.py_function to wrap pure Python code

• if you write something and want to make sure it is run in eager mode, use zfit.run.assert_executing_eagerly(). This way, your function won’t be compiled and an error would be raised.

• set the graph mode and numerical gradient accordingly

x_tf = z.constant(42.)

def sqrt(x):
return np.sqrt(x)

y = z.py_function(func=sqrt, inp=[x_tf], Tout=tf.float64)

/home/docs/checkouts/readthedocs.org/user_builds/zfit/checkouts/latest/.venv/lib/python3.12/site-packages/zfit/z/zextension.py:366: AdvancedFeatureWarning: Either you're using an advanced feature OR causing unwanted behavior. To turn this warning off, use zfit.settings.advanced_warnings['py_func_autograd'] = False  or 'all' (use with care) with zfit.settings.advanced_warnings['all'] = False
Using py_function without numerical gradients. If the Python code does not contain any parametrization by zfit.Parameter or similar, this can work out. Otherwise, in case it depends on those, you may want to set zfit.run.set_autograd_mode(=False).


This raises a warning: since we do not use pure TensorFlow anymore, it means that the automatic gradient (potentially) fails, as it cannot be traced through Python operations. Depending on the use-case, this is not a problem. That’s why the warning is an AdvancedFeatureWarning: it doesn’t say what we’re doing is wrong, it simply warns that we should know what we’re doing; it can also be switched off as explained in the warning.

It is technically not always required: if we e.g. use the internal, numerical gradient of a minimizer such as Minuit, the global setting does not really matter anyway.

This follows strongly the zfit philosophy that there must not be any bounds in terms of flexibility and even hackability of the framework, this should be an inherent part of it. However, the user should be made aware when leaving “the safe path”.

To do what the above warning told us to do, we can use zfit.run.set_autograd_mode(False).

This is needed whenever we want to use non-traceable Python calls in the dynamic calculations, be it by using py_function or be it by switching off the gradient mode as shown below.

#### Sidestep: What is ‘z’?#

This is a subset of TensorFlow, wrapped to improve dtype handling and sometimes even provide additional functionality, such as z.function decorator.

### Full Python compatibility#

To operate in a full Python compatible, yet (way) less efficient mode, we can switch off the automatic gradient, as discussed before, and the graph compilation, leaving us with a Numpy-like TensorFlow

zfit.run.set_graph_mode(False)

<zfit.util.temporary.TemporarilySet at 0x7fa926737e00>


We can now build a Gaussian purely based on Numpy. As we have seen when building graphs with TensorFlow: anything Python-like will be converted to a static value in the graph. So we have to make sure that our code is never run in graph mode but only executed eagerly.

This can be done by calling zfit.run.assert_executing_eagerly(), which raises an error if this code is run in graph mode.

Note that omitting the graph mode means to loose many optimizations: Not only do we loose the whole TensorFlow speedup from the graph, we also perform redundant tasks that are not cached, since zfit itself is optimized to be run in the graph mode. However, practially, this mode should anyway be used rather rarely and compares still in the same order of magnitude as alternatives.

class NumpyGauss(zfit.pdf.ZPDF):
_PARAMS = ['mu', 'sigma']

@zfit.supports()
def _unnormalized_pdf(self, x, params):
zfit.run.assert_executing_eagerly()  # make sure we're eager
data = x[0]
mu = params['mu']
sigma = params['sigma']
return np.exp( - 0.5 * (data - mu) ** 2 / sigma ** 2)  # note that we use numpy here


This can be tested and compared.

obs = zfit.Space('obs1', (-3, 3))
mu = zfit.Parameter('mu', 0., -1, 1)
sigma = zfit.Parameter('sigma', 1., 0.1, 10)

gauss_np = NumpyGauss(obs=obs, mu=mu, sigma=sigma)
gauss = zfit.pdf.Gauss(obs=obs, mu=mu, sigma=sigma)

integral_np = gauss_np.integrate((-1, 0))
integral = gauss.integrate((-1, 0))
print(integral_np, integral)

tf.Tensor([0.3422688], shape=(1,), dtype=float64)



tf.Tensor([0.3422688], shape=(1,), dtype=float64)


`