|
7 | 7 | "<span style=\"font-size: 3rem; font-weight: bold;\">SMD Hands-On Estimators / Fitting</span>" |
8 | 8 | ] |
9 | 9 | }, |
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 | | - }, |
28 | 10 | { |
29 | 11 | "cell_type": "code", |
30 | 12 | "execution_count": null, |
|
133 | 115 | { |
134 | 116 | "cell_type": "code", |
135 | 117 | "execution_count": null, |
136 | | - "metadata": { |
137 | | - "scrolled": false |
138 | | - }, |
| 118 | + "metadata": {}, |
139 | 119 | "outputs": [], |
140 | 120 | "source": [ |
141 | 121 | "# create some randomized example data points\n", |
142 | 122 | "rng = np.random.default_rng(1337)\n", |
143 | 123 | "\n", |
144 | 124 | "N = 100\n", |
145 | 125 | "true_parameters = np.array([2, 1, 0.5])\n", |
146 | | - "y_unc = rng.uniform(0.1, 0.4, N)\n", |
147 | | - "\n", |
148 | 126 | "x = np.linspace(0, 4 * np.pi, N)\n", |
149 | 127 | "y = linear_combination(x, funcs, true_parameters)\n", |
150 | 128 | "\n", |
151 | 129 | "# add some noise for to simulate measurement uncertainty\n", |
| 130 | + "y_unc = rng.uniform(0.1, 0.4, N)\n", |
152 | 131 | "y += rng.normal(0, y_unc)\n", |
153 | 132 | "\n", |
154 | 133 | "plt.figure()\n", |
155 | 134 | "plt.errorbar(x, y, yerr=y_unc, ls='', marker='.', color='k')\n", |
156 | 135 | "plt.xlabel('x')\n", |
157 | | - "plt.ylabel('y')" |
| 136 | + "plt.ylabel('y');" |
158 | 137 | ] |
159 | 138 | }, |
160 | 139 | { |
|
213 | 192 | "\n", |
214 | 193 | "fig, ax = plt.subplots()\n", |
215 | 194 | "plot = ax.matshow(W)\n", |
216 | | - "fig.colorbar(plot)" |
| 195 | + "fig.colorbar(plot);" |
217 | 196 | ] |
218 | 197 | }, |
219 | 198 | { |
|
239 | 218 | "outputs": [], |
240 | 219 | "source": [ |
241 | 220 | "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", |
243 | 222 | " \n", |
244 | 223 | " Parameters\n", |
245 | 224 | " ----------\n", |
|
378 | 357 | ")\n", |
379 | 358 | "\n", |
380 | 359 | "\n", |
381 | | - "ax.legend()" |
| 360 | + "ax.legend();" |
382 | 361 | ] |
383 | 362 | }, |
384 | 363 | { |
|
389 | 368 | "source": [ |
390 | 369 | "corr = cov_to_corr(cov)\n", |
391 | 370 | "\n", |
392 | | - "fig = plt.figure(constrained_layout=True)\n", |
393 | | - "ax = fig.add_subplot(1, 1, 1)\n", |
| 371 | + "fig, ax = plt.subplots()\n", |
394 | 372 | "m = ax.matshow(corr, cmap='RdBu_r', vmin=-1, vmax=1)\n", |
395 | | - "fig.colorbar(m)" |
| 373 | + "fig.colorbar(m);" |
396 | 374 | ] |
397 | 375 | }, |
398 | 376 | { |
|
469 | 447 | ")\n", |
470 | 448 | "\n", |
471 | 449 | "\n", |
472 | | - "ax.legend()" |
| 450 | + "ax.legend();" |
473 | 451 | ] |
474 | 452 | }, |
475 | 453 | { |
|
512 | 490 | { |
513 | 491 | "cell_type": "code", |
514 | 492 | "execution_count": null, |
515 | | - "metadata": { |
516 | | - "scrolled": false |
517 | | - }, |
| 493 | + "metadata": {}, |
518 | 494 | "outputs": [], |
519 | 495 | "source": [ |
520 | 496 | "df = pd.read_csv('resources/spalt.csv')\n", |
|
547 | 523 | " xlabel=r'$\\varphi \\,\\, / \\,\\, \\mathrm{rad}$',\n", |
548 | 524 | " ylabel=r'$I \\,\\, / \\,\\, \\mathrm{A}$',\n", |
549 | 525 | ")\n", |
550 | | - "ax.legend(loc='best')\n", |
551 | | - "\n", |
552 | | - "None # to not have mpl objects in the output" |
| 526 | + "ax.legend(loc='best');" |
553 | 527 | ] |
554 | 528 | }, |
555 | 529 | { |
|
583 | 557 | "E_MAX = 175\n", |
584 | 558 | "\n", |
585 | 559 | "# normally distributed signal\n", |
586 | | - "higgs_signal = np.random.normal(126, 5, 500)\n", |
| 560 | + "higgs_signal = rng.normal(126, 5, 500)\n", |
587 | 561 | "\n", |
588 | 562 | "# exponentially distributed background\n", |
589 | | - "background = np.random.exponential(50, size=20000)\n", |
| 563 | + "background = rng.exponential(50, size=20000)\n", |
590 | 564 | "\n", |
591 | 565 | "# combine signal and background\n", |
592 | 566 | "measured = np.append(higgs_signal, background)\n", |
|
598 | 572 | "\n", |
599 | 573 | "fig, ax = plt.subplots()\n", |
600 | 574 | "ax.hist(measured, bins=100)\n", |
601 | | - "ax.set_xlabel('$m \\,/\\, \\mathrm{GeV}$')\n", |
| 575 | + "ax.set_xlabel(r'$m \\,/\\, \\mathrm{GeV}$')\n", |
602 | 576 | "ax.margins(x=0)\n", |
603 | 577 | "None" |
604 | 578 | ] |
|
714 | 688 | { |
715 | 689 | "cell_type": "code", |
716 | 690 | "execution_count": null, |
717 | | - "metadata": {}, |
| 691 | + "metadata": { |
| 692 | + "scrolled": true |
| 693 | + }, |
718 | 694 | "outputs": [], |
719 | 695 | "source": [ |
720 | 696 | "from scipy.optimize import minimize\n", |
|
768 | 744 | "metadata": {}, |
769 | 745 | "outputs": [], |
770 | 746 | "source": [ |
771 | | - "\n", |
772 | 747 | "e = np.linspace(E_MIN, E_MAX, 1000)\n", |
773 | 748 | "\n", |
774 | 749 | "\n", |
|
778 | 753 | "ax.plot(e, pdf(e, *result.x))\n", |
779 | 754 | "ax.axvline(result.x[0], color='C2', lw=2)\n", |
780 | 755 | "\n", |
781 | | - "ax.set_xlabel('$m \\,/\\, \\mathrm{GeV}$')\n", |
| 756 | + "ax.set_xlabel(r'$m \\,/\\, \\mathrm{GeV}$')\n", |
782 | 757 | "ax.margins(x=0)" |
783 | 758 | ] |
784 | 759 | }, |
|
904 | 879 | { |
905 | 880 | "cell_type": "code", |
906 | 881 | "execution_count": null, |
907 | | - "metadata": { |
908 | | - "scrolled": false |
909 | | - }, |
| 882 | + "metadata": {}, |
910 | 883 | "outputs": [], |
911 | 884 | "source": [ |
912 | 885 | "N = np.genfromtxt(\"resources/muon_data.txt\")\n", |
|
1000 | 973 | "source": [ |
1001 | 974 | "plt.figure()\n", |
1002 | 975 | "\n", |
1003 | | - "plt.axvline(m.values['tau'], color='C1')\n", |
| 976 | + "plt.axvline(m.values['tau'], color='C1', label=\"Fit-Result\")\n", |
1004 | 977 | "plt.plot(m.values['tau'], m.fval, color='C1', marker='o', zorder=3)\n", |
1005 | 978 | "\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", |
1009 | 982 | "plt.axvline(pdg_reference, color='k', label='PDG')\n", |
1010 | 983 | "plt.plot(tau, ts)\n", |
1011 | 984 | "\n", |
1012 | 985 | "plt.xlabel('τ / µs')\n", |
1013 | 986 | "plt.ylabel('NLL')\n", |
| 987 | + "plt.legend()\n", |
1014 | 988 | "None" |
1015 | 989 | ] |
1016 | 990 | }, |
|
1021 | 995 | "outputs": [], |
1022 | 996 | "source": [ |
1023 | 997 | "plt.figure()\n", |
1024 | | - "m.draw_mncontour('tau', 'p', size=250)" |
| 998 | + "m.draw_mncontour('tau', 'p', size=250);" |
1025 | 999 | ] |
1026 | 1000 | }, |
1027 | 1001 | { |
|
1048 | 1022 | "name": "python", |
1049 | 1023 | "nbconvert_exporter": "python", |
1050 | 1024 | "pygments_lexer": "ipython3", |
1051 | | - "version": "3.10.6" |
| 1025 | + "version": "3.12.7" |
1052 | 1026 | } |
1053 | 1027 | }, |
1054 | 1028 | "nbformat": 4, |
|
0 commit comments