|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Creating your own pdf\n", |
| 8 | + "\n", |
| 9 | + "A core feature of zfit is the ability to create custom pdfs and functions in an simple and straightforward way.\n", |
| 10 | + "\n", |
| 11 | + "In this tutorial, we will show how to create a custom binned PDF.\n", |
| 12 | + "\n", |
| 13 | + "\n" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": null, |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "import numpy as np\n", |
| 23 | + "import zfit\n", |
| 24 | + "import zfit.z.numpy as znp\n", |
| 25 | + "from zfit import z" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "markdown", |
| 30 | + "metadata": {}, |
| 31 | + "source": [ |
| 32 | + "The first way is the most simple and should only be used for the trivial cases, i.e. if you're not familiar with Python classes (especially not with the `__init__` method)." |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": null, |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [], |
| 40 | + "source": [ |
| 41 | + "class MyGauss(zfit.pdf.BaseBinnedPDF): # TODO from here\n", |
| 42 | + " _N_OBS = 1 # dimension, can be omitted\n", |
| 43 | + " _PARAMS = ['mean', 'std'] # the name of the parameters\n", |
| 44 | + "\n", |
| 45 | + " @zfit.supports()\n", |
| 46 | + " def _unnormalized_pdf(self, x, params):\n", |
| 47 | + " x0 = x[0] # using the 0th axis\n", |
| 48 | + " mean = params['mean']\n", |
| 49 | + " std = params['std']\n", |
| 50 | + " return z.exp(- ((x0 - mean) / std) ** 2)" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "markdown", |
| 55 | + "metadata": {}, |
| 56 | + "source": [ |
| 57 | + "Done. Now we can use our pdf already!\n", |
| 58 | + "\n", |
| 59 | + "The slightly more general way involves overwritting the `__init__` and gives you all the possible flexibility: to use custom parameters, to preprocess them etc.\n", |
| 60 | + "\n", |
| 61 | + "Here we inherit from `BasePDF`" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": null, |
| 67 | + "metadata": {}, |
| 68 | + "outputs": [], |
| 69 | + "source": [ |
| 70 | + "class MyGauss(zfit.pdf.BasePDF):\n", |
| 71 | + "\n", |
| 72 | + " def __init__(self, mean, std, obs, extended=None, norm=None, name=None, label=None):\n", |
| 73 | + " params = {'mean': mean, # 'mean' is the name as it will be named in the PDF, mean is just the parameter to create the PDF\n", |
| 74 | + " 'std': std\n", |
| 75 | + " }\n", |
| 76 | + " super().__init__(obs=obs, params=params, extended=extended, norm=norm,\n", |
| 77 | + " name=name, label=label)\n", |
| 78 | + "\n", |
| 79 | + " @zfit.supports()\n", |
| 80 | + " def _unnormalized_pdf(self, x, params):\n", |
| 81 | + " x0 = x[0] # using the 0th axis\n", |
| 82 | + " mean = params['mean']\n", |
| 83 | + " std = params['std']\n", |
| 84 | + " return z.exp(- ((x0 - mean) / std) ** 2)" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "code", |
| 89 | + "execution_count": null, |
| 90 | + "metadata": {}, |
| 91 | + "outputs": [], |
| 92 | + "source": [ |
| 93 | + "obs = zfit.Space('obs1', -3, 6)\n", |
| 94 | + "\n", |
| 95 | + "data_np = np.random.random(size=1000)\n", |
| 96 | + "data = zfit.Data(data_np, obs=obs)" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "markdown", |
| 101 | + "metadata": {}, |
| 102 | + "source": [ |
| 103 | + "Create two parameters and an instance of your own pdf" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": null, |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [], |
| 111 | + "source": [ |
| 112 | + "mean = zfit.Parameter(\"mean\", 1.)\n", |
| 113 | + "std = zfit.Parameter(\"std\", 1.)\n", |
| 114 | + "my_gauss = MyGauss(obs=obs, mean=mean, std=std)" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": null, |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "probs = my_gauss.pdf(data)" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": null, |
| 129 | + "metadata": {}, |
| 130 | + "outputs": [], |
| 131 | + "source": [ |
| 132 | + "print(probs[:20])" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "metadata": {}, |
| 138 | + "source": [ |
| 139 | + "If we want to make sure it's a numpy array, we can use `zfit.run`" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "markdown", |
| 144 | + "metadata": {}, |
| 145 | + "source": [ |
| 146 | + "We could improve our PDF by registering an integral" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "code", |
| 151 | + "execution_count": null, |
| 152 | + "metadata": {}, |
| 153 | + "outputs": [], |
| 154 | + "source": [ |
| 155 | + "def gauss_integral_from_any_to_any(limits, params, model):\n", |
| 156 | + " lower, upper = limits.v1.limits\n", |
| 157 | + " mean = params['mean']\n", |
| 158 | + " std = params['std']\n", |
| 159 | + " # write your integral here\n", |
| 160 | + " return 42. # dummy integral, must be a scalar!" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": null, |
| 166 | + "metadata": {}, |
| 167 | + "outputs": [], |
| 168 | + "source": [ |
| 169 | + "limits = zfit.Space(axes=0, lower=zfit.Space.ANY_LOWER, upper=zfit.Space.ANY_UPPER)\n", |
| 170 | + "MyGauss.register_analytic_integral(func=gauss_integral_from_any_to_any, limits=limits)" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "markdown", |
| 175 | + "metadata": {}, |
| 176 | + "source": [ |
| 177 | + "More advanced custom PDFs are introduced in the guide on [custom PDFs](custom_pdfs.ipynb)." |
| 178 | + ] |
| 179 | + } |
| 180 | + ], |
| 181 | + "metadata": { |
| 182 | + "kernelspec": { |
| 183 | + "display_name": "Python 3 (ipykernel)", |
| 184 | + "language": "python", |
| 185 | + "name": "python3" |
| 186 | + }, |
| 187 | + "language_info": { |
| 188 | + "codemirror_mode": { |
| 189 | + "name": "ipython", |
| 190 | + "version": 3 |
| 191 | + }, |
| 192 | + "file_extension": ".py", |
| 193 | + "mimetype": "text/x-python", |
| 194 | + "name": "python", |
| 195 | + "nbconvert_exporter": "python", |
| 196 | + "pygments_lexer": "ipython3", |
| 197 | + "version": "3.10.4" |
| 198 | + } |
| 199 | + }, |
| 200 | + "nbformat": 4, |
| 201 | + "nbformat_minor": 4 |
| 202 | +} |
0 commit comments