Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PythonJupyter
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LabMeccanica
PythonJupyter
Commits
beb418ed
Commit
beb418ed
authored
1 year ago
by
Francesco Santanastasio
Browse files
Options
Downloads
Patches
Plain Diff
fit examples and counting experiment
parent
fe8e30c2
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
CountingExperiment.ipynb
+308
-0
308 additions, 0 deletions
CountingExperiment.ipynb
LinerFitExamples.ipynb
+680
-0
680 additions, 0 deletions
LinerFitExamples.ipynb
with
988 additions
and
0 deletions
CountingExperiment.ipynb
0 → 100644
+
308
−
0
View file @
beb418ed
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "c6c01141",
"metadata": {},
"outputs": [],
"source": [
"#plots will be shown inline \n",
"%matplotlib inline \n",
"\n",
"import numpy as np\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.mlab as mlab\n",
"\n",
"from scipy import stats\n",
"from scipy import special\n",
"from scipy import integrate\n",
"\n",
"import pandas as pd\n",
"\n",
"import math\n",
"\n",
"import emcee\n",
"import corner"
]
},
{
"cell_type": "markdown",
"id": "952245bd",
"metadata": {},
"source": [
"# Esperimento di conteggio in presenza di segnale e fondo con categorie"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "53316186",
"metadata": {},
"outputs": [],
"source": [
"# test\n",
"nobs = np.array([0])\n",
"nbkg = np.array([0])\n",
"eff = np.array([1])\n",
"\n",
"#0.7TeV\n",
"#inclusive\n",
"#nobs = np.array([14784,617])\n",
"#nbkg = np.array([14784,617])\n",
"#eff = np.array([0.266,0.124])\n",
"#tag+notag\n",
"#nobscat = np.array([3989,10795,39,578])\n",
"#nbkgcat = np.array([3989,10795,39,578])\n",
"#effcat = np.array([0.081,0.184,0.034,0.09])\n",
"\n",
"#1TeV\n",
"#inclusive\n",
"#nobs = np.array([3984,90])\n",
"#nbkg = np.array([3984,90])\n",
"#eff = np.array([0.285,0.153])\n",
"#tag+notag\n",
"#nobscat = np.array([912,3072,6,84])\n",
"#nbkgcat = np.array([912,3072,6,84])\n",
"#effcat = np.array([0.093,0.192,0.051,0.102])\n",
"\n",
"#2TeV\n",
"#inclusive\n",
"#nobs = np.array([67,0])\n",
"#nbkg = np.array([67,0])\n",
"#eff = np.array([0.316,0.2])\n",
"#tag+notag\n",
"#nobscat = np.array([25,42,0,0])\n",
"#nbkgcat = np.array([28,42,0,0])\n",
"#effcat = np.array([0.055,0.261,0.074,0.127])\n",
"\n",
"#3TeV\n",
"#inclusive\n",
"#nobs = np.array([2,0])\n",
"#nbkg = np.array([2,0])\n",
"#eff = np.array([0.32,0.217])\n",
"#tag+notag\n",
"#nobscat = np.array([0,2,0,0])\n",
"#nbkgcat = np.array([0,2,0,0])\n",
"#effcat = np.array([0.02,0.3,0.033,0.184])\n",
"\n",
"#4TeV\n",
"#inclusive\n",
"#nobs = np.array([0,0])\n",
"#nbkg = np.array([0.37,0])\n",
"#eff = np.array([0.309,0.2])\n",
"#tag+notag\n",
"#nobscat = np.array([0,0,0,0])\n",
"#nbkgcat = np.array([0.37,0,0,0])\n",
"#effcat = np.array([0.011,0.298,0.008,0.192])\n",
"\n",
"#5TeV\n",
"#inclusive\n",
"#nobs = np.array([0,0])\n",
"#nbkg = np.array([0.1,0])\n",
"#eff = np.array([0.293,0.182])\n",
"#tag+notag\n",
"#nobscat = np.array([0,0,0,0])\n",
"#nbkgcat = np.array([0.1,0,0,0])\n",
"#effcat = np.array([0.015,0.278,0.012,0.170])\n",
"\n",
"#lumi = 140 #fb-1\n",
"lumi =1 #fb-1"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "442b724f",
"metadata": {},
"outputs": [],
"source": [
"def log_likelihood(theta,nobs,nbkg,eff,lumi): \n",
" sigma = theta\n",
" nexp = eff * sigma * lumi + nbkg\n",
" #print (nexp)\n",
" return math.log(np.prod( stats.poisson.pmf(nobs,nexp) ))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "1f8fd349",
"metadata": {},
"outputs": [],
"source": [
"def log_prior(theta):\n",
" sigma = theta\n",
" if 0 < sigma < 10:\n",
" return 0.0\n",
" return -np.inf"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "1b76422e",
"metadata": {},
"outputs": [],
"source": [
"def log_probability(theta, nobs,nbkg,eff,lumi):\n",
" lp = log_prior(theta)\n",
" if not np.isfinite(lp):\n",
" return -np.inf\n",
" return lp + log_likelihood(theta, nobs,nbkg,eff,lumi)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "a1c463f3",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 500/500 [00:15<00:00, 32.65it/s]\n"
]
}
],
"source": [
"nwalkers = 500\n",
"niter = 500\n",
"initial = np.array([0.2])\n",
"ndim = len(initial)\n",
"p0 = [np.array(initial) + 1e-7 * np.random.randn(ndim) for i in range(nwalkers)]\n",
"#print (p0)\n",
"\n",
"#no PPS categories\n",
"sampler = emcee.EnsembleSampler(nwalkers, ndim, log_probability, args=(nobs,nbkg,eff,lumi))\n",
"#with PPS categories\n",
"#sampler = emcee.EnsembleSampler(nwalkers, ndim, log_probability, args=(nobscat,nbkgcat,effcat,lumi))\n",
"sampler.run_mcmc(p0, niter, progress=True);"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "6bb9b203",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(13000, 1)\n"
]
}
],
"source": [
"flat_samples = sampler.get_chain(discard=100, thin=15, flat=True)\n",
"print(flat_samples.shape)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "b3bed1a2",
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAKEAAADTCAYAAADpu3N8AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8/fFQqAAAACXBIWXMAAAsTAAALEwEAmpwYAAAH5ElEQVR4nO3dbYilZR3H8e9PLRUfStw1kHJXsJRMM91UpDBLo6IHojAqTKE0Q3ohRPRAZBCpFAURCGq2QVCQZCFRGGkRvahmMx03y2Jbk6RY0ZQ1czX/vThnYJnVM46eOf9zznw/MOzMfWbmunb3O9c958yZ605VIXXar3sCkhGqnRGqnRGqnRGqnRGq3QGreecNGzbU5s2b12gqmnfbtm17oKo2Lj++qgg3b97MwsLC+GaldSXJvU933NOx2hmh2hmh2hmh2q0YYZJLkiwkWdi1a9ck5qR1ZsUIq+raqtpSVVs2btzn3rX0vHk6VjsjVDsjVDsjVDsjVLtVRbi4uEiSp33xiQ16rlb1BIY9e/bwTL8YlWQsE9L64+lY7YxQ7YxQ7YxQ7YxQ7YxQ7YxQ7YxQ7YxQ7YxQ7YxQ7YxQ7YxQ7YxQ7YxQ7YxQ7YxQ7YxQ7YxQ7Vb8HZMklwCXTGAuWqeymis6JalRv+jk1aE0SpJtVbVl+XFPx2pnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGpnhGo3tgg3bdrkFUD1nKzqKp+j7Ny58xlv8wqgGsVtQNRubNuArPBxbhEitwHR9DJCtTNCtTNCtTNCtTNCtTNCtTNCtTNCtTNCtTNCtTNC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Y1NVdwGfAM4Ajh0e2wHsz2BVWuvxbwTOBX4F3D48ditwGINFYSym+XQMQFU9CexOcl+SK4E3AxdV1WMTGPv+JPcBnwMuq6qbk5wD/HWtx97LTxisflckuXd47DUMviDWXFU9lORW4Pwke4CDGHxB3DmuMab+JybDh0VeANw9/PNNVfWXCY7/MuCoqto2fHu/Cd85WZrHqcB7GXxrsrWqFic49ouBDwHvAf4LfLKq7hjb55/2CJckuQj4XVVtbxp/8tdUmzJJDmPQzCMrvvNqPu+s/LsawfyamQg1v6by3rHWFyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyNUOyN8FpJcn+SV3fOYV+7AoHauhMskOSTJj5PckeSuJO9L8oskW4a3fzjJPcNj1yX5xvD41iTXJLktyY4kZye5IcndSbbu9fmvSbKQZHuSLzT9NaeKEe7rLcD9VfXqqnoV8NOlG5IczWCvwjOB84ATln3sEcAbgcuBm4GvAScCJyU5Zfg+n63BRQpPBs5OcvIa/l1mghHuaxE4N8nVSV4/3DZ3yenAL6vqwap6Avj+so+9ebhz2CLwr6paHO5luB3YPHyf85P8nsHOpycC6/57zanfqXXSquqeJKcBbwOuTHLLXjevdB2TpU3dn9rr9aW3D0hyLIPtf1873AF1K4OdT9c1V8Jlhqfc/1TVdxhcxuLUvW7+LYNT6BFJDmCwc+lqHA48Cjyc5CXAW8cx51nnSrivk4AvJ3kKeAL4GIMYqap/JPkSg6sL3M/gQjsPP9MnWq6q7khyO4PT8w7g12Oe+0zyIZpVSnJoVe0eroQ3ATdU1U3d85plno5X74okfwDuAv4G/LB1NnPAlVDtXAnVzgjVzgjVzgjVzgjVzgjV7v/iOPdoaXtDuQAAAABJRU5ErkJggg==\n",
"text/plain": [
"<Figure size 244.8x244.8 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"labels = [\"sigma\"]\n",
"fig = corner.corner(\n",
" flat_samples, labels=labels\n",
");"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "d6babb25",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\mathrm{sigma} = 0.666_{-0.497}^{1.155}$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle \\mathrm{95\\% \\, CL \\, Upper \\, limit \\, on \\, sigma} = 3.017 \\mathrm{\\, fb}$"
],
"text/plain": [
"<IPython.core.display.Math object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import display, Math\n",
"\n",
"for i in range(ndim):\n",
" \n",
" mcmc = np.percentile(flat_samples[:, i], [16, 50, 84])\n",
" q = np.diff(mcmc)\n",
" txt = \"\\mathrm{{{3}}} = {0:.3f}_{{-{1:.3f}}}^{{{2:.3f}}}\"\n",
" txt = txt.format(mcmc[1], q[0], q[1], labels[i])\n",
" display(Math(txt))\n",
" \n",
" mcmc_up = np.percentile(flat_samples[:, i], [95])\n",
" txt_up = \"\\mathrm{{95\\% \\, CL \\, Upper \\, limit \\, on \\, {1}}} = {0:.3f} \\mathrm{{\\, fb}}\"\n",
" txt_up = txt_up.format(mcmc_up[0], labels[i])\n",
" display(Math(txt_up))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cfd9a67f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
%% Cell type:code id:c6c01141 tags:
```
python
#plots will be shown inline
%
matplotlib
inline
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
matplotlib.mlab
as
mlab
from
scipy
import
stats
from
scipy
import
special
from
scipy
import
integrate
import
pandas
as
pd
import
math
import
emcee
import
corner
```
%% Cell type:markdown id:952245bd tags:
# Esperimento di conteggio in presenza di segnale e fondo con categorie
%% Cell type:code id:53316186 tags:
```
python
# test
nobs
=
np
.
array
([
0
])
nbkg
=
np
.
array
([
0
])
eff
=
np
.
array
([
1
])
#0.7TeV
#inclusive
#nobs = np.array([14784,617])
#nbkg = np.array([14784,617])
#eff = np.array([0.266,0.124])
#tag+notag
#nobscat = np.array([3989,10795,39,578])
#nbkgcat = np.array([3989,10795,39,578])
#effcat = np.array([0.081,0.184,0.034,0.09])
#1TeV
#inclusive
#nobs = np.array([3984,90])
#nbkg = np.array([3984,90])
#eff = np.array([0.285,0.153])
#tag+notag
#nobscat = np.array([912,3072,6,84])
#nbkgcat = np.array([912,3072,6,84])
#effcat = np.array([0.093,0.192,0.051,0.102])
#2TeV
#inclusive
#nobs = np.array([67,0])
#nbkg = np.array([67,0])
#eff = np.array([0.316,0.2])
#tag+notag
#nobscat = np.array([25,42,0,0])
#nbkgcat = np.array([28,42,0,0])
#effcat = np.array([0.055,0.261,0.074,0.127])
#3TeV
#inclusive
#nobs = np.array([2,0])
#nbkg = np.array([2,0])
#eff = np.array([0.32,0.217])
#tag+notag
#nobscat = np.array([0,2,0,0])
#nbkgcat = np.array([0,2,0,0])
#effcat = np.array([0.02,0.3,0.033,0.184])
#4TeV
#inclusive
#nobs = np.array([0,0])
#nbkg = np.array([0.37,0])
#eff = np.array([0.309,0.2])
#tag+notag
#nobscat = np.array([0,0,0,0])
#nbkgcat = np.array([0.37,0,0,0])
#effcat = np.array([0.011,0.298,0.008,0.192])
#5TeV
#inclusive
#nobs = np.array([0,0])
#nbkg = np.array([0.1,0])
#eff = np.array([0.293,0.182])
#tag+notag
#nobscat = np.array([0,0,0,0])
#nbkgcat = np.array([0.1,0,0,0])
#effcat = np.array([0.015,0.278,0.012,0.170])
#lumi = 140 #fb-1
lumi
=
1
#fb-1
```
%% Cell type:code id:442b724f tags:
```
python
def
log_likelihood
(
theta
,
nobs
,
nbkg
,
eff
,
lumi
):
sigma
=
theta
nexp
=
eff
*
sigma
*
lumi
+
nbkg
#print (nexp)
return
math
.
log
(
np
.
prod
(
stats
.
poisson
.
pmf
(
nobs
,
nexp
)
))
```
%% Cell type:code id:1f8fd349 tags:
```
python
def
log_prior
(
theta
):
sigma
=
theta
if
0
<
sigma
<
10
:
return
0.0
return
-
np
.
inf
```
%% Cell type:code id:1b76422e tags:
```
python
def
log_probability
(
theta
,
nobs
,
nbkg
,
eff
,
lumi
):
lp
=
log_prior
(
theta
)
if
not
np
.
isfinite
(
lp
):
return
-
np
.
inf
return
lp
+
log_likelihood
(
theta
,
nobs
,
nbkg
,
eff
,
lumi
)
```
%% Cell type:code id:a1c463f3 tags:
```
python
nwalkers
=
500
niter
=
500
initial
=
np
.
array
([
0.2
])
ndim
=
len
(
initial
)
p0
=
[
np
.
array
(
initial
)
+
1e-7
*
np
.
random
.
randn
(
ndim
)
for
i
in
range
(
nwalkers
)]
#print (p0)
#no PPS categories
sampler
=
emcee
.
EnsembleSampler
(
nwalkers
,
ndim
,
log_probability
,
args
=
(
nobs
,
nbkg
,
eff
,
lumi
))
#with PPS categories
#sampler = emcee.EnsembleSampler(nwalkers, ndim, log_probability, args=(nobscat,nbkgcat,effcat,lumi))
sampler
.
run_mcmc
(
p0
,
niter
,
progress
=
True
);
```
%% Output
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 500/500 [00:15<00:00, 32.65it/s]
%% Cell type:code id:6bb9b203 tags:
```
python
flat_samples
=
sampler
.
get_chain
(
discard
=
100
,
thin
=
15
,
flat
=
True
)
print
(
flat_samples
.
shape
)
```
%% Output
(13000, 1)
%% Cell type:code id:b3bed1a2 tags:
```
python
labels
=
[
"
sigma
"
]
fig
=
corner
.
corner
(
flat_samples
,
labels
=
labels
);
```
%% Output
%% Cell type:code id:d6babb25 tags:
```
python
from
IPython.display
import
display
,
Math
for
i
in
range
(
ndim
):
mcmc
=
np
.
percentile
(
flat_samples
[:,
i
],
[
16
,
50
,
84
])
q
=
np
.
diff
(
mcmc
)
txt
=
"
\mathrm{{{3}}} = {0:.3f}_{{-{1:.3f}}}^{{{2:.3f}}}
"
txt
=
txt
.
format
(
mcmc
[
1
],
q
[
0
],
q
[
1
],
labels
[
i
])
display
(
Math
(
txt
))
mcmc_up
=
np
.
percentile
(
flat_samples
[:,
i
],
[
95
])
txt_up
=
"
\mathrm{{95\% \, CL \, Upper \, limit \, on \, {1}}} = {0:.3f} \mathrm{{\, fb}}
"
txt_up
=
txt_up
.
format
(
mcmc_up
[
0
],
labels
[
i
])
display
(
Math
(
txt_up
))
```
%% Output
$\displaystyle \mathrm{sigma} = 0.666_{-0.497}^{1.155}$
$\displaystyle \mathrm{95\% \, CL \, Upper \, limit \, on \, sigma} = 3.017 \mathrm{\, fb}$
%% Cell type:code id:cfd9a67f tags:
```
python
```
This diff is collapsed.
Click to expand it.
LinerFitExamples.ipynb
0 → 100644
+
680
−
0
View file @
beb418ed
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment