{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Hello World, `pyhf` style" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Two bin counting experiment with a background uncertainty**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pyhf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Returning the observed and expected** $\\mathrm{CL}_{s}$" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Observed: 0.052515541856109765, Expected: 0.06445521290832805\n" ] } ], "source": [ "model = pyhf.simplemodels.uncorrelated_background(\n", " signal=[12.0, 11.0], bkg=[50.0, 52.0], bkg_uncertainty=[3.0, 7.0]\n", ")\n", "data = [51, 48] + model.config.auxdata\n", "test_mu = 1.0\n", "CLs_obs, CLs_exp = pyhf.infer.hypotest(\n", " test_mu, data, model, test_stat=\"qtilde\", return_expected=True\n", ")\n", "print(f\"Observed: {CLs_obs}, Expected: {CLs_exp}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Returning the observed** $\\mathrm{CL}_{s}$, $\\mathrm{CL}_{s+b}$, **and** $\\mathrm{CL}_{b}$" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Observed CL_s: 0.052515541856109765, CL_sb: 0.023324961200974572, CL_b: 0.44415349012077077\n" ] } ], "source": [ "CLs_obs, p_values = pyhf.infer.hypotest(\n", " test_mu, data, model, test_stat=\"qtilde\", return_tail_probs=True\n", ")\n", "print(f\"Observed CL_s: {CLs_obs}, CL_sb: {p_values[0]}, CL_b: {p_values[1]}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A reminder that \n", "$$\n", "\\mathrm{CL}_{s} = \\frac{\\mathrm{CL}_{s+b}}{\\mathrm{CL}_{b}} = \\frac{p_{s+b}}{1-p_{b}}\n", "$$" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "assert CLs_obs == p_values[0] / p_values[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Returning the expected** $\\mathrm{CL}_{s}$ **band values**" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Observed CL_s: 0.052515541856109765\n", "\n", "Expected CL_s(-2 σ): 0.0026064088679947964\n", "Expected CL_s(-1 σ): 0.013820657528619273\n", "Expected CL_s : 0.06445521290832805\n", "Expected CL_s(1 σ): 0.23526103626937836\n", "Expected CL_s(2 σ): 0.5730418174887743\n" ] } ], "source": [ "CLs_obs, CLs_exp_band = pyhf.infer.hypotest(\n", " test_mu, data, model, test_stat=\"qtilde\", return_expected_set=True\n", ")\n", "print(f\"Observed CL_s: {CLs_obs}\\n\")\n", "for p_value, n_sigma in enumerate(np.arange(-2, 3)):\n", " print(\n", " \"Expected CL_s{}: {}\".format(\n", " \" \" if n_sigma == 0 else f\"({n_sigma} σ)\",\n", " CLs_exp_band[p_value],\n", " )\n", " )" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.5" } }, "nbformat": 4, "nbformat_minor": 4 }