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.

253 lines
5.5 KiB

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()"
  93. ]
  94. },
  95. {
  96. "cell_type": "code",
  97. "execution_count": null,
  98. "metadata": {},
  99. "outputs": [],
  100. "source": [
  101. "# run covariance \n",
  102. "m.hesse()"
  103. ]
  104. },
  105. {
  106. "cell_type": "code",
  107. "execution_count": null,
  108. "metadata": {},
  109. "outputs": [],
  110. "source": [
  111. "#get correlation matrix\n",
  112. "cov = m.covariance\n",
  113. "print (cov)"
  114. ]
  115. },
  116. {
  117. "cell_type": "code",
  118. "execution_count": null,
  119. "metadata": {},
  120. "outputs": [],
  121. "source": [
  122. "# access elements of the numpy arrays\n",
  123. "print(cov[0, 1])\n",
  124. "print(cov[0, 2])"
  125. ]
  126. },
  127. {
  128. "cell_type": "code",
  129. "execution_count": null,
  130. "metadata": {},
  131. "outputs": [],
  132. "source": [
  133. "# run minos error analysis\n",
  134. "# The Minos algorithm uses the profile likelihood method to compute\n",
  135. "# (generally asymmetric) confidence intervals.\n",
  136. "m.minos()"
  137. ]
  138. },
  139. {
  140. "cell_type": "code",
  141. "execution_count": null,
  142. "metadata": {},
  143. "outputs": [],
  144. "source": [
  145. "# Get a 2D contour of the function around the minimum for 2 parameters\n",
  146. "# and draw a 2 D contours up to 4 sigma of a1 and a2 \n",
  147. "m.draw_mncontour(\"a1\", \"a2\", cl=[1, 2, 3, 4])\n"
  148. ]
  149. },
  150. {
  151. "cell_type": "code",
  152. "execution_count": null,
  153. "metadata": {},
  154. "outputs": [],
  155. "source": [
  156. "m.draw_profile(\"a2\",subtract_min=True)"
  157. ]
  158. },
  159. {
  160. "cell_type": "code",
  161. "execution_count": null,
  162. "metadata": {},
  163. "outputs": [],
  164. "source": [
  165. "# access fit results by parameter name and get minos asymetric errors\n",
  166. "print (m.merrors['a2'].lower)\n",
  167. "print (m.merrors['a2'].upper)"
  168. ]
  169. },
  170. {
  171. "cell_type": "code",
  172. "execution_count": null,
  173. "metadata": {},
  174. "outputs": [],
  175. "source": [
  176. "# more print out\n",
  177. "print (m.values,m.errors)\n",
  178. "print (m.errors)"
  179. ]
  180. },
  181. {
  182. "cell_type": "code",
  183. "execution_count": null,
  184. "metadata": {},
  185. "outputs": [],
  186. "source": [
  187. "# Access fit results\n",
  188. "a0_fit = m.values[\"a0\"]\n",
  189. "a1_fit = m.values[\"a1\"]\n",
  190. "a2_fit = m.values[\"a2\"]\n",
  191. "a3_fit = m.values[\"a3\"]"
  192. ]
  193. },
  194. {
  195. "cell_type": "code",
  196. "execution_count": null,
  197. "metadata": {},
  198. "outputs": [],
  199. "source": [
  200. "# display fitted function \n",
  201. "x_plot = np.linspace( 0.1, 4.1 , 100 )\n",
  202. "y_fit = a0_fit + a1_fit * x_plot + a2_fit * x_plot**2 + a3_fit * x_plot**3\n"
  203. ]
  204. },
  205. {
  206. "cell_type": "code",
  207. "execution_count": null,
  208. "metadata": {},
  209. "outputs": [],
  210. "source": [
  211. "plt.figure()\n",
  212. "\n",
  213. "plt.errorbar(x, y, dy , fmt=\"o\")\n",
  214. "plt.plot(x_plot,y_fit ) \n",
  215. "plt.xlabel('x')\n",
  216. "plt.ylabel('f(x)')\n",
  217. "plt.title('iminuit exponential Fit')\n",
  218. "#plt.axis([0,30,-1.2,1.2])\n",
  219. "\n",
  220. "# show the plot\n",
  221. "plt.show()"
  222. ]
  223. },
  224. {
  225. "cell_type": "code",
  226. "execution_count": null,
  227. "metadata": {},
  228. "outputs": [],
  229. "source": []
  230. }
  231. ],
  232. "metadata": {
  233. "kernelspec": {
  234. "display_name": "Python 3 (ipykernel)",
  235. "language": "python",
  236. "name": "python3"
  237. },
  238. "language_info": {
  239. "codemirror_mode": {
  240. "name": "ipython",
  241. "version": 3
  242. },
  243. "file_extension": ".py",
  244. "mimetype": "text/x-python",
  245. "name": "python",
  246. "nbconvert_exporter": "python",
  247. "pygments_lexer": "ipython3",
  248. "version": "3.8.16"
  249. }
  250. },
  251. "nbformat": 4,
  252. "nbformat_minor": 4
  253. }