Skip to content

Commit 38bec9a

Browse files
authored
Merge pull request #12 from tudo-astroparticlephysics/update_fitting
* Fix typos * harmonize matplotlib usage
2 parents 133f801 + efac228 commit 38bec9a

File tree

1 file changed

+26
-52
lines changed

1 file changed

+26
-52
lines changed

smd_handson_fitting.ipynb

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,6 @@
77
"<span style=\"font-size: 3rem; font-weight: bold;\">SMD Hands-On Estimators / Fitting</span>"
88
]
99
},
10-
{
11-
"cell_type": "code",
12-
"execution_count": null,
13-
"metadata": {},
14-
"outputs": [],
15-
"source": [
16-
"%%javascript\n",
17-
"$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')"
18-
]
19-
},
20-
{
21-
"cell_type": "markdown",
22-
"metadata": {},
23-
"source": [
24-
"<h1 id=\"tocheading\">Table of Contents</h1>\n",
25-
"<div id=\"toc\"></div>"
26-
]
27-
},
2810
{
2911
"cell_type": "code",
3012
"execution_count": null,
@@ -133,28 +115,25 @@
133115
{
134116
"cell_type": "code",
135117
"execution_count": null,
136-
"metadata": {
137-
"scrolled": false
138-
},
118+
"metadata": {},
139119
"outputs": [],
140120
"source": [
141121
"# create some randomized example data points\n",
142122
"rng = np.random.default_rng(1337)\n",
143123
"\n",
144124
"N = 100\n",
145125
"true_parameters = np.array([2, 1, 0.5])\n",
146-
"y_unc = rng.uniform(0.1, 0.4, N)\n",
147-
"\n",
148126
"x = np.linspace(0, 4 * np.pi, N)\n",
149127
"y = linear_combination(x, funcs, true_parameters)\n",
150128
"\n",
151129
"# add some noise for to simulate measurement uncertainty\n",
130+
"y_unc = rng.uniform(0.1, 0.4, N)\n",
152131
"y += rng.normal(0, y_unc)\n",
153132
"\n",
154133
"plt.figure()\n",
155134
"plt.errorbar(x, y, yerr=y_unc, ls='', marker='.', color='k')\n",
156135
"plt.xlabel('x')\n",
157-
"plt.ylabel('y')"
136+
"plt.ylabel('y');"
158137
]
159138
},
160139
{
@@ -213,7 +192,7 @@
213192
"\n",
214193
"fig, ax = plt.subplots()\n",
215194
"plot = ax.matshow(W)\n",
216-
"fig.colorbar(plot)"
195+
"fig.colorbar(plot);"
217196
]
218197
},
219198
{
@@ -239,7 +218,7 @@
239218
"outputs": [],
240219
"source": [
241220
"def solve_linear_least_squares(A, W, y):\n",
242-
" \"\"\"Solve the linear least sqaures problem\n",
221+
" \"\"\"Solve the linear least squares problem\n",
243222
" \n",
244223
" Parameters\n",
245224
" ----------\n",
@@ -378,7 +357,7 @@
378357
")\n",
379358
"\n",
380359
"\n",
381-
"ax.legend()"
360+
"ax.legend();"
382361
]
383362
},
384363
{
@@ -389,10 +368,9 @@
389368
"source": [
390369
"corr = cov_to_corr(cov)\n",
391370
"\n",
392-
"fig = plt.figure(constrained_layout=True)\n",
393-
"ax = fig.add_subplot(1, 1, 1)\n",
371+
"fig, ax = plt.subplots()\n",
394372
"m = ax.matshow(corr, cmap='RdBu_r', vmin=-1, vmax=1)\n",
395-
"fig.colorbar(m)"
373+
"fig.colorbar(m);"
396374
]
397375
},
398376
{
@@ -469,7 +447,7 @@
469447
")\n",
470448
"\n",
471449
"\n",
472-
"ax.legend()"
450+
"ax.legend();"
473451
]
474452
},
475453
{
@@ -512,9 +490,7 @@
512490
{
513491
"cell_type": "code",
514492
"execution_count": null,
515-
"metadata": {
516-
"scrolled": false
517-
},
493+
"metadata": {},
518494
"outputs": [],
519495
"source": [
520496
"df = pd.read_csv('resources/spalt.csv')\n",
@@ -547,9 +523,7 @@
547523
" xlabel=r'$\\varphi \\,\\, / \\,\\, \\mathrm{rad}$',\n",
548524
" ylabel=r'$I \\,\\, / \\,\\, \\mathrm{A}$',\n",
549525
")\n",
550-
"ax.legend(loc='best')\n",
551-
"\n",
552-
"None # to not have mpl objects in the output"
526+
"ax.legend(loc='best');"
553527
]
554528
},
555529
{
@@ -583,10 +557,10 @@
583557
"E_MAX = 175\n",
584558
"\n",
585559
"# normally distributed signal\n",
586-
"higgs_signal = np.random.normal(126, 5, 500)\n",
560+
"higgs_signal = rng.normal(126, 5, 500)\n",
587561
"\n",
588562
"# exponentially distributed background\n",
589-
"background = np.random.exponential(50, size=20000)\n",
563+
"background = rng.exponential(50, size=20000)\n",
590564
"\n",
591565
"# combine signal and background\n",
592566
"measured = np.append(higgs_signal, background)\n",
@@ -598,7 +572,7 @@
598572
"\n",
599573
"fig, ax = plt.subplots()\n",
600574
"ax.hist(measured, bins=100)\n",
601-
"ax.set_xlabel('$m \\,/\\, \\mathrm{GeV}$')\n",
575+
"ax.set_xlabel(r'$m \\,/\\, \\mathrm{GeV}$')\n",
602576
"ax.margins(x=0)\n",
603577
"None"
604578
]
@@ -714,7 +688,9 @@
714688
{
715689
"cell_type": "code",
716690
"execution_count": null,
717-
"metadata": {},
691+
"metadata": {
692+
"scrolled": true
693+
},
718694
"outputs": [],
719695
"source": [
720696
"from scipy.optimize import minimize\n",
@@ -768,7 +744,6 @@
768744
"metadata": {},
769745
"outputs": [],
770746
"source": [
771-
"\n",
772747
"e = np.linspace(E_MIN, E_MAX, 1000)\n",
773748
"\n",
774749
"\n",
@@ -778,7 +753,7 @@
778753
"ax.plot(e, pdf(e, *result.x))\n",
779754
"ax.axvline(result.x[0], color='C2', lw=2)\n",
780755
"\n",
781-
"ax.set_xlabel('$m \\,/\\, \\mathrm{GeV}$')\n",
756+
"ax.set_xlabel(r'$m \\,/\\, \\mathrm{GeV}$')\n",
782757
"ax.margins(x=0)"
783758
]
784759
},
@@ -904,9 +879,7 @@
904879
{
905880
"cell_type": "code",
906881
"execution_count": null,
907-
"metadata": {
908-
"scrolled": false
909-
},
882+
"metadata": {},
910883
"outputs": [],
911884
"source": [
912885
"N = np.genfromtxt(\"resources/muon_data.txt\")\n",
@@ -1000,17 +973,18 @@
1000973
"source": [
1001974
"plt.figure()\n",
1002975
"\n",
1003-
"plt.axvline(m.values['tau'], color='C1')\n",
976+
"plt.axvline(m.values['tau'], color='C1', label=\"Fit-Result\")\n",
1004977
"plt.plot(m.values['tau'], m.fval, color='C1', marker='o', zorder=3)\n",
1005978
"\n",
1006-
"plt.axvline(m.values['tau'] + m.merrors['tau'].lower, color='C2')\n",
1007-
"plt.axvline(m.values['tau'] + m.merrors['tau'].upper, color='C2')\n",
1008-
"plt.axhline(m.fval + 1, color='C3')\n",
979+
"plt.axvline(m.values['tau'] + m.merrors['tau'].lower, color='C2', label=\"Lower Error\")\n",
980+
"plt.axvline(m.values['tau'] + m.merrors['tau'].upper, color='C2', label=\"Upper Error\")\n",
981+
"plt.axhline(m.fval + 1, color='C3', label=\"NLL + 1\")\n",
1009982
"plt.axvline(pdg_reference, color='k', label='PDG')\n",
1010983
"plt.plot(tau, ts)\n",
1011984
"\n",
1012985
"plt.xlabel('τ / µs')\n",
1013986
"plt.ylabel('NLL')\n",
987+
"plt.legend()\n",
1014988
"None"
1015989
]
1016990
},
@@ -1021,7 +995,7 @@
1021995
"outputs": [],
1022996
"source": [
1023997
"plt.figure()\n",
1024-
"m.draw_mncontour('tau', 'p', size=250)"
998+
"m.draw_mncontour('tau', 'p', size=250);"
1025999
]
10261000
},
10271001
{
@@ -1048,7 +1022,7 @@
10481022
"name": "python",
10491023
"nbconvert_exporter": "python",
10501024
"pygments_lexer": "ipython3",
1051-
"version": "3.10.6"
1025+
"version": "3.12.7"
10521026
}
10531027
},
10541028
"nbformat": 4,

0 commit comments

Comments
 (0)