|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Performing a Combination\n", |
| 8 | + "\n", |
| 9 | + "We'll demonstrate how a combination works by combining everything we've learned so far.\n", |
| 10 | + "\n", |
| 11 | + "## Loading the Workspace\n", |
| 12 | + "\n", |
| 13 | + "To do so, we'll use a simple workspace to demonstrate functionality of combinations." |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": null, |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "import json\n", |
| 23 | + "import pyhf" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "with open(\"data/2-bin_1-channel.json\") as serialized:\n", |
| 33 | + " spec = json.load(serialized)\n", |
| 34 | + "\n", |
| 35 | + "workspace = pyhf.Workspace(spec)" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "markdown", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "## Combine Workspaces\n", |
| 43 | + "\n", |
| 44 | + "Let's just try to combine naively right now." |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": null, |
| 50 | + "metadata": { |
| 51 | + "tags": [ |
| 52 | + "raises-exception" |
| 53 | + ] |
| 54 | + }, |
| 55 | + "outputs": [], |
| 56 | + "source": [ |
| 57 | + "pyhf.Workspace.combine(workspace, workspace)" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "markdown", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "As we can see, we can't just combine a workspace with itself if it has some channel names in common. We try very hard in `pyhf` to make sure a combination \"makes sense\".\n", |
| 65 | + "\n", |
| 66 | + "Let's go ahead and rename the channel (as well as the measurement). Then try to combine." |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": null, |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "other_workspace = workspace.rename(\n", |
| 76 | + " channels={\"singlechannel\": \"othersinglechannel\"},\n", |
| 77 | + " modifiers={\"uncorr_bkguncrt\": \"otheruncorr_bkguncrt\"},\n", |
| 78 | + " measurements={\"Measurement\": \"OtherMeasurement\"},\n", |
| 79 | + ")\n", |
| 80 | + "\n", |
| 81 | + "combined_workspace = pyhf.Workspace.combine(workspace, other_workspace)" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "markdown", |
| 86 | + "metadata": {}, |
| 87 | + "source": [ |
| 88 | + "And did we combine?" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": null, |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [], |
| 96 | + "source": [ |
| 97 | + "print(f\" channels: {combined_workspace.channels}\")\n", |
| 98 | + "print(f\" nbins: {combined_workspace.channel_nbins}\")\n", |
| 99 | + "print(f\" samples: {combined_workspace.samples}\")\n", |
| 100 | + "print(f\" modifiers: {combined_workspace.modifiers}\")\n", |
| 101 | + "print(f\" parameters: {combined_workspace.parameters}\")\n", |
| 102 | + "print(f\"measurements: {combined_workspace.measurement_names}\")" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "markdown", |
| 107 | + "metadata": {}, |
| 108 | + "source": [ |
| 109 | + "Indeed. And at this point, we can just use all the same functionality we expect of pyhf, such as performing a fit:" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "code", |
| 114 | + "execution_count": null, |
| 115 | + "metadata": {}, |
| 116 | + "outputs": [], |
| 117 | + "source": [ |
| 118 | + "model = workspace.model()\n", |
| 119 | + "data = workspace.data(model)\n", |
| 120 | + "test_poi = 1.0\n", |
| 121 | + "\n", |
| 122 | + "pyhf.infer.hypotest(test_poi, data, model, test_stat=\"qtilde\")" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "code", |
| 127 | + "execution_count": null, |
| 128 | + "metadata": {}, |
| 129 | + "outputs": [], |
| 130 | + "source": [ |
| 131 | + "other_model = other_workspace.model()\n", |
| 132 | + "other_data = other_workspace.data(other_model)\n", |
| 133 | + "\n", |
| 134 | + "pyhf.infer.hypotest(test_poi, other_data, other_model, test_stat=\"qtilde\")" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "code", |
| 139 | + "execution_count": null, |
| 140 | + "metadata": {}, |
| 141 | + "outputs": [], |
| 142 | + "source": [ |
| 143 | + "combined_model = combined_workspace.model()\n", |
| 144 | + "combined_data = combined_workspace.data(combined_model)\n", |
| 145 | + "\n", |
| 146 | + "pyhf.infer.hypotest(test_poi, combined_data, combined_model, test_stat=\"qtilde\")" |
| 147 | + ] |
| 148 | + } |
| 149 | + ], |
| 150 | + "metadata": { |
| 151 | + "kernelspec": { |
| 152 | + "display_name": "Python 3", |
| 153 | + "language": "python", |
| 154 | + "name": "python3" |
| 155 | + }, |
| 156 | + "language_info": { |
| 157 | + "codemirror_mode": { |
| 158 | + "name": "ipython", |
| 159 | + "version": 3 |
| 160 | + }, |
| 161 | + "file_extension": ".py", |
| 162 | + "mimetype": "text/x-python", |
| 163 | + "name": "python", |
| 164 | + "nbconvert_exporter": "python", |
| 165 | + "pygments_lexer": "ipython3", |
| 166 | + "version": "3.8.7" |
| 167 | + } |
| 168 | + }, |
| 169 | + "nbformat": 4, |
| 170 | + "nbformat_minor": 4 |
| 171 | +} |
0 commit comments