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.

274 lines
5.4 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
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. "Example fit for the usage of iminuit"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": null,
  13. "metadata": {},
  14. "outputs": [],
  15. "source": [
  16. "from matplotlib import pyplot as plt\n",
  17. "plt.rcParams[\"font.size\"] = 20\n",
  18. "import numpy as np"
  19. ]
  20. },
  21. {
  22. "cell_type": "markdown",
  23. "metadata": {},
  24. "source": [
  25. "Data "
  26. ]
  27. },
  28. {
  29. "cell_type": "code",
  30. "execution_count": null,
  31. "metadata": {},
  32. "outputs": [],
  33. "source": [
  34. "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",
  35. "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",
  36. "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')"
  37. ]
  38. },
  39. {
  40. "cell_type": "markdown",
  41. "metadata": {},
  42. "source": [
  43. "Define fit functions -an exponential"
  44. ]
  45. },
  46. {
  47. "cell_type": "code",
  48. "execution_count": null,
  49. "metadata": {},
  50. "outputs": [],
  51. "source": [
  52. "def xp(a, b , c):\n",
  53. " return a * np.exp(b*x) + c"
  54. ]
  55. },
  56. {
  57. "cell_type": "markdown",
  58. "metadata": {},
  59. "source": [
  60. "least-squares function: sum of data residuals squared"
  61. ]
  62. },
  63. {
  64. "cell_type": "code",
  65. "execution_count": null,
  66. "metadata": {},
  67. "outputs": [],
  68. "source": [
  69. "def LS(a,b,c):\n",
  70. " return np.sum((y - xp(a,b,c)) ** 2 / dy ** 2)"
  71. ]
  72. },
  73. {
  74. "cell_type": "markdown",
  75. "metadata": {},
  76. "source": [
  77. "import Minuit object"
  78. ]
  79. },
  80. {
  81. "cell_type": "code",
  82. "execution_count": null,
  83. "metadata": {},
  84. "outputs": [],
  85. "source": [
  86. "from iminuit import Minuit"
  87. ]
  88. },
  89. {
  90. "cell_type": "markdown",
  91. "metadata": {},
  92. "source": [
  93. "Minuit instance using LS function to minimize"
  94. ]
  95. },
  96. {
  97. "cell_type": "code",
  98. "execution_count": null,
  99. "metadata": {},
  100. "outputs": [],
  101. "source": [
  102. "LS.errordef = Minuit.LEAST_SQUARES\n",
  103. "m = Minuit(LS, a=0.9, b=-0.7 , c=0.95)"
  104. ]
  105. },
  106. {
  107. "cell_type": "markdown",
  108. "metadata": {},
  109. "source": [
  110. "Run migrad , parameter c is now fixed"
  111. ]
  112. },
  113. {
  114. "cell_type": "code",
  115. "execution_count": null,
  116. "metadata": {},
  117. "outputs": [],
  118. "source": [
  119. "m.fixed[\"c\"] = True\n",
  120. "m.migrad()"
  121. ]
  122. },
  123. {
  124. "cell_type": "markdown",
  125. "metadata": {},
  126. "source": [
  127. "release fix on \"c\" and minimize again"
  128. ]
  129. },
  130. {
  131. "cell_type": "code",
  132. "execution_count": null,
  133. "metadata": {},
  134. "outputs": [],
  135. "source": [
  136. "m.fixed[\"c\"] = False\n",
  137. "m.migrad()"
  138. ]
  139. },
  140. {
  141. "cell_type": "markdown",
  142. "metadata": {},
  143. "source": [
  144. "Get covariance information"
  145. ]
  146. },
  147. {
  148. "cell_type": "code",
  149. "execution_count": null,
  150. "metadata": {},
  151. "outputs": [],
  152. "source": [
  153. "m.hesse()\n",
  154. "m.params\n",
  155. "m.covariance"
  156. ]
  157. },
  158. {
  159. "cell_type": "markdown",
  160. "metadata": {},
  161. "source": [
  162. "Get a 2D contour of the function around the minimum for 2 parameters"
  163. ]
  164. },
  165. {
  166. "cell_type": "code",
  167. "execution_count": null,
  168. "metadata": {},
  169. "outputs": [],
  170. "source": [
  171. "m.minos()\n",
  172. "print (m.merrors['a']) # Print control information of parameter a\n",
  173. "m.draw_profile('b', subtract_min=True)"
  174. ]
  175. },
  176. {
  177. "cell_type": "markdown",
  178. "metadata": {},
  179. "source": [
  180. "The Minos algorithm uses the profile likelihood method to compute (generally asymmetric) confidence intervals. This can be plotted"
  181. ]
  182. },
  183. {
  184. "cell_type": "code",
  185. "execution_count": null,
  186. "metadata": {},
  187. "outputs": [],
  188. "source": [
  189. "m.draw_mncontour('a', 'b', cl=[1,2,3,4])"
  190. ]
  191. },
  192. {
  193. "cell_type": "markdown",
  194. "metadata": {},
  195. "source": [
  196. "Access fit results"
  197. ]
  198. },
  199. {
  200. "cell_type": "code",
  201. "execution_count": null,
  202. "metadata": {},
  203. "outputs": [],
  204. "source": [
  205. "print(m.values,m.errors)\n",
  206. "print (m.merrors['a'])\n",
  207. "print (m.merrors['a'].lower)\n",
  208. "print (m.merrors['a'].upper)\n",
  209. "a_fit = m.values[\"a\"]\n",
  210. "b_fit = m.values[\"b\"]\n",
  211. "c_fit = m.values[\"c\"]"
  212. ]
  213. },
  214. {
  215. "cell_type": "markdown",
  216. "metadata": {},
  217. "source": [
  218. "Prepare data to display fitted function "
  219. ]
  220. },
  221. {
  222. "cell_type": "code",
  223. "execution_count": null,
  224. "metadata": {},
  225. "outputs": [],
  226. "source": [
  227. "x_plot = np.linspace( 0.1, 4.5 , 100 )\n",
  228. "y_fit = a_fit * np.exp(b_fit*x_plot) + c_fit "
  229. ]
  230. },
  231. {
  232. "cell_type": "markdown",
  233. "metadata": {},
  234. "source": [
  235. "plot data and fit results with matplotlib"
  236. ]
  237. },
  238. {
  239. "cell_type": "code",
  240. "execution_count": null,
  241. "metadata": {},
  242. "outputs": [],
  243. "source": [
  244. "plt.figure()\n",
  245. "plt.errorbar(x, y, dy , fmt=\"o\")\n",
  246. "plt.plot(x_plot, y_fit)\n",
  247. "plt.title(\"iminuit exponential Fit\")\n",
  248. "plt.xlim(-0.1, 4.1)\n",
  249. "plt.show()"
  250. ]
  251. }
  252. ],
  253. "metadata": {
  254. "kernelspec": {
  255. "display_name": "Python 3 (ipykernel)",
  256. "language": "python",
  257. "name": "python3"
  258. },
  259. "language_info": {
  260. "codemirror_mode": {
  261. "name": "ipython",
  262. "version": 3
  263. },
  264. "file_extension": ".py",
  265. "mimetype": "text/x-python",
  266. "name": "python",
  267. "nbconvert_exporter": "python",
  268. "pygments_lexer": "ipython3",
  269. "version": "3.8.16"
  270. }
  271. },
  272. "nbformat": 4,
  273. "nbformat_minor": 4
  274. }