Machine Learning Kurs im Rahmen der Studierendentage im SS 2023
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
5.6 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "Exercise 3: Least square fit with a 3rd order polynomial with iminuit"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": null,
  13. "metadata": {},
  14. "outputs": [],
  15. "source": [
  16. "import numpy as np\n",
  17. "import matplotlib.pyplot as plt\n",
  18. "import math"
  19. ]
  20. },
  21. {
  22. "cell_type": "code",
  23. "execution_count": null,
  24. "metadata": {},
  25. "outputs": [],
  26. "source": [
  27. "# Data x,y and dy\n",
  28. "x = np.array([0.2 , 0.4 , 0.6 , 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2., 2.2, 2.4, 2.6, 2.8 , 3., 3.2 ,3.4, 3.6, 3.8,4.],dtype='d')\n",
  29. "dy = np.array([0.04,0.021,0.035,0.03,0.029,0.019,0.024,0.018,0.019,0.022,0.02,0.025,0.018,0.024,0.019,0.021,0.03,0.019,0.03,0.024 ], dtype='d')\n",
  30. "y = np.array([1.792,1.695,1.541,1.514,1.427,1.399,1.388,1.270,1.262,1.228,1.189,1.182,1.121,1.129,1.124,1.089,1.092,1.084,1.058,1.057 ], dtype='d')"
  31. ]
  32. },
  33. {
  34. "cell_type": "code",
  35. "execution_count": null,
  36. "metadata": {},
  37. "outputs": [],
  38. "source": [
  39. "# Define fit functions - a 3rd order polynomial\n",
  40. "def pol3(a0, a1, a2, a3):\n",
  41. " return a0 + x*a1 + a2*x**2 + a3*x**3"
  42. ]
  43. },
  44. {
  45. "cell_type": "code",
  46. "execution_count": null,
  47. "metadata": {},
  48. "outputs": [],
  49. "source": [
  50. "# least-squares function = sum of data residuals squared\n",
  51. "def LSQ(a0, a1, a2, a3):\n",
  52. " return np.sum((y - pol3(a0, a1, a2, a3)) ** 2 / dy ** 2)"
  53. ]
  54. },
  55. {
  56. "cell_type": "code",
  57. "execution_count": null,
  58. "metadata": {},
  59. "outputs": [],
  60. "source": [
  61. "# import minuit2 fitting library\n",
  62. "from iminuit import Minuit"
  63. ]
  64. },
  65. {
  66. "cell_type": "code",
  67. "execution_count": null,
  68. "metadata": {},
  69. "outputs": [],
  70. "source": [
  71. "# create instance of Minuit and use LSQ function to minimize\n",
  72. "LSQ.errordef = Minuit.LEAST_SQUARES\n",
  73. "m = Minuit(LSQ,a0=0.01, a1=0.05 ,a2=0.01 ,a3=0.001)"
  74. ]
  75. },
  76. {
  77. "cell_type": "code",
  78. "execution_count": null,
  79. "metadata": {},
  80. "outputs": [],
  81. "source": [
  82. "m.params"
  83. ]
  84. },
  85. {
  86. "cell_type": "code",
  87. "execution_count": null,
  88. "metadata": {},
  89. "outputs": [],
  90. "source": [
  91. "# run migrad for minimization\n",
  92. "m.migrad()\n",
  93. "chi2 = m.fval / (len(y) - len(m.values))\n",
  94. "print (\"Chi2/ndof =\" , chi2)"
  95. ]
  96. },
  97. {
  98. "cell_type": "code",
  99. "execution_count": null,
  100. "metadata": {},
  101. "outputs": [],
  102. "source": [
  103. "# run covariance \n",
  104. "m.hesse()"
  105. ]
  106. },
  107. {
  108. "cell_type": "code",
  109. "execution_count": null,
  110. "metadata": {},
  111. "outputs": [],
  112. "source": [
  113. "#get correlation matrix\n",
  114. "cov = m.covariance\n",
  115. "print (cov)"
  116. ]
  117. },
  118. {
  119. "cell_type": "code",
  120. "execution_count": null,
  121. "metadata": {},
  122. "outputs": [],
  123. "source": [
  124. "# access elements of the numpy arrays\n",
  125. "print(cov[0, 1])\n",
  126. "print(cov[0, 2])"
  127. ]
  128. },
  129. {
  130. "cell_type": "code",
  131. "execution_count": null,
  132. "metadata": {},
  133. "outputs": [],
  134. "source": [
  135. "# run minos error analysis\n",
  136. "# The Minos algorithm uses the profile likelihood method to compute\n",
  137. "# (generally asymmetric) confidence intervals.\n",
  138. "m.minos()"
  139. ]
  140. },
  141. {
  142. "cell_type": "code",
  143. "execution_count": null,
  144. "metadata": {},
  145. "outputs": [],
  146. "source": [
  147. "# Get a 2D contour of the function around the minimum for 2 parameters\n",
  148. "# and draw a 2 D contours up to 4 sigma of a1 and a2 \n",
  149. "m.draw_mncontour(\"a1\", \"a2\", cl=[1, 2, 3, 4])\n"
  150. ]
  151. },
  152. {
  153. "cell_type": "code",
  154. "execution_count": null,
  155. "metadata": {},
  156. "outputs": [],
  157. "source": [
  158. "m.draw_profile(\"a2\",subtract_min=True)"
  159. ]
  160. },
  161. {
  162. "cell_type": "code",
  163. "execution_count": null,
  164. "metadata": {},
  165. "outputs": [],
  166. "source": [
  167. "# access fit results by parameter name and get minos asymetric errors\n",
  168. "print (m.merrors['a2'].lower)\n",
  169. "print (m.merrors['a2'].upper)"
  170. ]
  171. },
  172. {
  173. "cell_type": "code",
  174. "execution_count": null,
  175. "metadata": {},
  176. "outputs": [],
  177. "source": [
  178. "# more print out\n",
  179. "print (m.values,m.errors)\n",
  180. "print (m.errors)"
  181. ]
  182. },
  183. {
  184. "cell_type": "code",
  185. "execution_count": null,
  186. "metadata": {},
  187. "outputs": [],
  188. "source": [
  189. "# Access fit results\n",
  190. "a0_fit = m.values[\"a0\"]\n",
  191. "a1_fit = m.values[\"a1\"]\n",
  192. "a2_fit = m.values[\"a2\"]\n",
  193. "a3_fit = m.values[\"a3\"]"
  194. ]
  195. },
  196. {
  197. "cell_type": "code",
  198. "execution_count": null,
  199. "metadata": {},
  200. "outputs": [],
  201. "source": [
  202. "# display fitted function \n",
  203. "x_plot = np.linspace( 0.1, 4.1 , 100 )\n",
  204. "y_fit = a0_fit + a1_fit * x_plot + a2_fit * x_plot**2 + a3_fit * x_plot**3\n"
  205. ]
  206. },
  207. {
  208. "cell_type": "code",
  209. "execution_count": null,
  210. "metadata": {},
  211. "outputs": [],
  212. "source": [
  213. "plt.figure()\n",
  214. "\n",
  215. "plt.errorbar(x, y, dy , fmt=\"o\")\n",
  216. "plt.plot(x_plot,y_fit ) \n",
  217. "plt.xlabel('x')\n",
  218. "plt.ylabel('f(x)')\n",
  219. "plt.title('iminuit exponential Fit')\n",
  220. "#plt.axis([0,30,-1.2,1.2])\n",
  221. "\n",
  222. "# show the plot\n",
  223. "plt.show()"
  224. ]
  225. },
  226. {
  227. "cell_type": "code",
  228. "execution_count": null,
  229. "metadata": {},
  230. "outputs": [],
  231. "source": []
  232. }
  233. ],
  234. "metadata": {
  235. "kernelspec": {
  236. "display_name": "Python [conda env:ML]",
  237. "language": "python",
  238. "name": "conda-env-ML-py"
  239. },
  240. "language_info": {
  241. "codemirror_mode": {
  242. "name": "ipython",
  243. "version": 3
  244. },
  245. "file_extension": ".py",
  246. "mimetype": "text/x-python",
  247. "name": "python",
  248. "nbconvert_exporter": "python",
  249. "pygments_lexer": "ipython3",
  250. "version": "3.10.9"
  251. }
  252. },
  253. "nbformat": 4,
  254. "nbformat_minor": 4
  255. }