Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fitting hands-on #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 26 additions & 52 deletions smd_handson_fitting.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,6 @@
"<span style=\"font-size: 3rem; font-weight: bold;\">SMD Hands-On Estimators / Fitting</span>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%javascript\n",
"$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1 id=\"tocheading\">Table of Contents</h1>\n",
"<div id=\"toc\"></div>"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -133,28 +115,25 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"metadata": {},
"outputs": [],
"source": [
"# create some randomized example data points\n",
"rng = np.random.default_rng(1337)\n",
"\n",
"N = 100\n",
"true_parameters = np.array([2, 1, 0.5])\n",
"y_unc = rng.uniform(0.1, 0.4, N)\n",
"\n",
"x = np.linspace(0, 4 * np.pi, N)\n",
"y = linear_combination(x, funcs, true_parameters)\n",
"\n",
"# add some noise for to simulate measurement uncertainty\n",
"y_unc = rng.uniform(0.1, 0.4, N)\n",
"y += rng.normal(0, y_unc)\n",
"\n",
"plt.figure()\n",
"plt.errorbar(x, y, yerr=y_unc, ls='', marker='.', color='k')\n",
"plt.xlabel('x')\n",
"plt.ylabel('y')"
"plt.ylabel('y');"
]
},
{
Expand Down Expand Up @@ -213,7 +192,7 @@
"\n",
"fig, ax = plt.subplots()\n",
"plot = ax.matshow(W)\n",
"fig.colorbar(plot)"
"fig.colorbar(plot);"
]
},
{
Expand All @@ -239,7 +218,7 @@
"outputs": [],
"source": [
"def solve_linear_least_squares(A, W, y):\n",
" \"\"\"Solve the linear least sqaures problem\n",
" \"\"\"Solve the linear least squares problem\n",
" \n",
" Parameters\n",
" ----------\n",
Expand Down Expand Up @@ -378,7 +357,7 @@
")\n",
"\n",
"\n",
"ax.legend()"
"ax.legend();"
]
},
{
Expand All @@ -389,10 +368,9 @@
"source": [
"corr = cov_to_corr(cov)\n",
"\n",
"fig = plt.figure(constrained_layout=True)\n",
"ax = fig.add_subplot(1, 1, 1)\n",
"fig, ax = plt.subplots()\n",
"m = ax.matshow(corr, cmap='RdBu_r', vmin=-1, vmax=1)\n",
"fig.colorbar(m)"
"fig.colorbar(m);"
]
},
{
Expand Down Expand Up @@ -469,7 +447,7 @@
")\n",
"\n",
"\n",
"ax.legend()"
"ax.legend();"
]
},
{
Expand Down Expand Up @@ -512,9 +490,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('resources/spalt.csv')\n",
Expand Down Expand Up @@ -547,9 +523,7 @@
" xlabel=r'$\\varphi \\,\\, / \\,\\, \\mathrm{rad}$',\n",
" ylabel=r'$I \\,\\, / \\,\\, \\mathrm{A}$',\n",
")\n",
"ax.legend(loc='best')\n",
"\n",
"None # to not have mpl objects in the output"
"ax.legend(loc='best');"
]
},
{
Expand Down Expand Up @@ -583,10 +557,10 @@
"E_MAX = 175\n",
"\n",
"# normally distributed signal\n",
"higgs_signal = np.random.normal(126, 5, 500)\n",
"higgs_signal = rng.normal(126, 5, 500)\n",
"\n",
"# exponentially distributed background\n",
"background = np.random.exponential(50, size=20000)\n",
"background = rng.exponential(50, size=20000)\n",
"\n",
"# combine signal and background\n",
"measured = np.append(higgs_signal, background)\n",
Expand All @@ -598,7 +572,7 @@
"\n",
"fig, ax = plt.subplots()\n",
"ax.hist(measured, bins=100)\n",
"ax.set_xlabel('$m \\,/\\, \\mathrm{GeV}$')\n",
"ax.set_xlabel(r'$m \\,/\\, \\mathrm{GeV}$')\n",
"ax.margins(x=0)\n",
"None"
]
Expand Down Expand Up @@ -714,7 +688,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"from scipy.optimize import minimize\n",
Expand Down Expand Up @@ -768,7 +744,6 @@
"metadata": {},
"outputs": [],
"source": [
"\n",
"e = np.linspace(E_MIN, E_MAX, 1000)\n",
"\n",
"\n",
Expand All @@ -778,7 +753,7 @@
"ax.plot(e, pdf(e, *result.x))\n",
"ax.axvline(result.x[0], color='C2', lw=2)\n",
"\n",
"ax.set_xlabel('$m \\,/\\, \\mathrm{GeV}$')\n",
"ax.set_xlabel(r'$m \\,/\\, \\mathrm{GeV}$')\n",
"ax.margins(x=0)"
]
},
Expand Down Expand Up @@ -904,9 +879,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"metadata": {},
"outputs": [],
"source": [
"N = np.genfromtxt(\"resources/muon_data.txt\")\n",
Expand Down Expand Up @@ -1000,17 +973,18 @@
"source": [
"plt.figure()\n",
"\n",
"plt.axvline(m.values['tau'], color='C1')\n",
"plt.axvline(m.values['tau'], color='C1', label=\"Fit-Result\")\n",
"plt.plot(m.values['tau'], m.fval, color='C1', marker='o', zorder=3)\n",
"\n",
"plt.axvline(m.values['tau'] + m.merrors['tau'].lower, color='C2')\n",
"plt.axvline(m.values['tau'] + m.merrors['tau'].upper, color='C2')\n",
"plt.axhline(m.fval + 1, color='C3')\n",
"plt.axvline(m.values['tau'] + m.merrors['tau'].lower, color='C2', label=\"Lower Error\")\n",
"plt.axvline(m.values['tau'] + m.merrors['tau'].upper, color='C2', label=\"Upper Error\")\n",
"plt.axhline(m.fval + 1, color='C3', label=\"NLL + 1\")\n",
"plt.axvline(pdg_reference, color='k', label='PDG')\n",
"plt.plot(tau, ts)\n",
"\n",
"plt.xlabel('τ / µs')\n",
"plt.ylabel('NLL')\n",
"plt.legend()\n",
"None"
]
},
Expand All @@ -1021,7 +995,7 @@
"outputs": [],
"source": [
"plt.figure()\n",
"m.draw_mncontour('tau', 'p', size=250)"
"m.draw_mncontour('tau', 'p', size=250);"
]
},
{
Expand All @@ -1048,7 +1022,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down