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.

284 lines
5.5 KiB

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.migrad()"
  120. ]
  121. },
  122. {
  123. "cell_type": "markdown",
  124. "metadata": {},
  125. "source": [
  126. "release fix on \"c\" and minimize again"
  127. ]
  128. },
  129. {
  130. "cell_type": "code",
  131. "execution_count": null,
  132. "metadata": {},
  133. "outputs": [],
  134. "source": [
  135. "m.fixed[\"c\"] = False\n",
  136. "m.migrad()"
  137. ]
  138. },
  139. {
  140. "cell_type": "markdown",
  141. "metadata": {},
  142. "source": [
  143. "Get covariance information"
  144. ]
  145. },
  146. {
  147. "cell_type": "code",
  148. "execution_count": null,
  149. "metadata": {},
  150. "outputs": [],
  151. "source": [
  152. "m.hesse()\n",
  153. "m.params\n",
  154. "m.covariance"
  155. ]
  156. },
  157. {
  158. "cell_type": "markdown",
  159. "metadata": {},
  160. "source": [
  161. "Copy covariance information to numpy arrays"
  162. ]
  163. },
  164. {
  165. "cell_type": "markdown",
  166. "metadata": {},
  167. "source": [
  168. "Get a 2D contour of the function around the minimum for 2 parameters"
  169. ]
  170. },
  171. {
  172. "cell_type": "code",
  173. "execution_count": null,
  174. "metadata": {},
  175. "outputs": [],
  176. "source": [
  177. "m.minos()\n",
  178. "print (m.merrors['a']) # Print control information of parameter a\n",
  179. "m.draw_profile('b', subtract_min=True)"
  180. ]
  181. },
  182. {
  183. "cell_type": "markdown",
  184. "metadata": {},
  185. "source": [
  186. "The Minos algorithm uses the profile likelihood method to compute (generally asymmetric) confidence intervals. This can be plotted"
  187. ]
  188. },
  189. {
  190. "cell_type": "code",
  191. "execution_count": null,
  192. "metadata": {},
  193. "outputs": [],
  194. "source": [
  195. "m.draw_mncontour('a', 'b')"
  196. ]
  197. },
  198. {
  199. "cell_type": "markdown",
  200. "metadata": {},
  201. "source": [
  202. "Access fit results"
  203. ]
  204. },
  205. {
  206. "cell_type": "code",
  207. "execution_count": null,
  208. "metadata": {},
  209. "outputs": [],
  210. "source": [
  211. "print(m.values,m.errors)\n",
  212. "a_fit = m.values[\"a\"]\n",
  213. "b_fit = m.values[\"b\"]\n",
  214. "c_fit = m.values[\"c\"]"
  215. ]
  216. },
  217. {
  218. "cell_type": "markdown",
  219. "metadata": {},
  220. "source": [
  221. "Prepare data to display fitted function "
  222. ]
  223. },
  224. {
  225. "cell_type": "code",
  226. "execution_count": null,
  227. "metadata": {},
  228. "outputs": [],
  229. "source": [
  230. "x_plot = np.linspace( 0.1, 4.5 , 100 )\n",
  231. "y_fit = a_fit * np.exp(b_fit*x_plot) + c_fit "
  232. ]
  233. },
  234. {
  235. "cell_type": "markdown",
  236. "metadata": {},
  237. "source": [
  238. "plot data and fit results with matplotlib"
  239. ]
  240. },
  241. {
  242. "cell_type": "code",
  243. "execution_count": null,
  244. "metadata": {},
  245. "outputs": [],
  246. "source": [
  247. "plt.figure()\n",
  248. "plt.errorbar(x, y, dy , fmt=\"o\")\n",
  249. "plt.plot(x_plot, y_fit)\n",
  250. "plt.title(\"iminuit exponential Fit\")\n",
  251. "plt.xlim(-0.1, 4.1)\n",
  252. "plt.show()"
  253. ]
  254. },
  255. {
  256. "cell_type": "code",
  257. "execution_count": null,
  258. "metadata": {},
  259. "outputs": [],
  260. "source": []
  261. }
  262. ],
  263. "metadata": {
  264. "kernelspec": {
  265. "display_name": "Python 3 (ipykernel)",
  266. "language": "python",
  267. "name": "python3"
  268. },
  269. "language_info": {
  270. "codemirror_mode": {
  271. "name": "ipython",
  272. "version": 3
  273. },
  274. "file_extension": ".py",
  275. "mimetype": "text/x-python",
  276. "name": "python",
  277. "nbconvert_exporter": "python",
  278. "pygments_lexer": "ipython3",
  279. "version": "3.8.16"
  280. }
  281. },
  282. "nbformat": 4,
  283. "nbformat_minor": 4
  284. }