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.

218 lines
4.8 KiB

2 years ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "Exercise 4: Least square fit to data"
  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": "code",
  23. "execution_count": null,
  24. "metadata": {},
  25. "outputs": [],
  26. "source": [
  27. "# data\n",
  28. "x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype='d')\n",
  29. "dx = np.array([0.1,0.1,0.5,0.1,0.5,0.1,0.5,0.1,0.5,0.1], dtype='d')\n",
  30. "y = np.array([1.1 ,2.3 ,2.7 ,3.2 ,3.1 ,2.4 ,1.7 ,1.5 ,1.5 ,1.7 ], dtype='d')\n",
  31. "dy = np.array([0.15,0.22,0.29,0.39,0.31,0.21,0.13,0.15,0.19,0.13], dtype='d')"
  32. ]
  33. },
  34. {
  35. "cell_type": "code",
  36. "execution_count": null,
  37. "metadata": {},
  38. "outputs": [],
  39. "source": [
  40. "# define fit function \n",
  41. "def pol3(a0, a1, a2, a3):\n",
  42. " return a0 + x*a1 + a2*x**2 + a3*x**3"
  43. ]
  44. },
  45. {
  46. "cell_type": "code",
  47. "execution_count": null,
  48. "metadata": {},
  49. "outputs": [],
  50. "source": [
  51. "# least-squares function = sum of data residuals squared\n",
  52. "def LSQ(a0, a1, a2, a3):\n",
  53. " return np.sum((y - pol3(a0, a1, a2, a3)) ** 2 / dy ** 2)"
  54. ]
  55. },
  56. {
  57. "cell_type": "code",
  58. "execution_count": null,
  59. "metadata": {},
  60. "outputs": [],
  61. "source": [
  62. "# import Minuit object\n",
  63. "from iminuit import Minuit"
  64. ]
  65. },
  66. {
  67. "cell_type": "code",
  68. "execution_count": null,
  69. "metadata": {},
  70. "outputs": [],
  71. "source": [
  72. "# create instance of Minuit and use LSQ function to minimize\n",
  73. "LSQ.errordef = Minuit.LEAST_SQUARES\n",
  74. "m = Minuit(LSQ,a0=-1.3, a1=2.6 ,a2=-0.24 ,a3=0.005)\n",
  75. "# run migrad \n",
  76. "m.migrad()"
  77. ]
  78. },
  79. {
  80. "cell_type": "code",
  81. "execution_count": null,
  82. "metadata": {},
  83. "outputs": [],
  84. "source": [
  85. "# get function value at the minimum, which is per definition a chi2\n",
  86. "# obtain chi2 / degree of freedom (dof)\n",
  87. "chi2 = m.fval / (len(y) - len(m.values))\n",
  88. "print (\"Chi2/ndof =\" , chi2)"
  89. ]
  90. },
  91. {
  92. "cell_type": "code",
  93. "execution_count": null,
  94. "metadata": {},
  95. "outputs": [],
  96. "source": [
  97. "# run covariance \n",
  98. "m.hesse()"
  99. ]
  100. },
  101. {
  102. "cell_type": "code",
  103. "execution_count": null,
  104. "metadata": {},
  105. "outputs": [],
  106. "source": [
  107. "#get covariance matrix\n",
  108. "m.covariance"
  109. ]
  110. },
  111. {
  112. "cell_type": "code",
  113. "execution_count": null,
  114. "metadata": {},
  115. "outputs": [],
  116. "source": [
  117. "#get correlation matrix in numpy array\n",
  118. "cov = m.covariance\n",
  119. "print (cov)"
  120. ]
  121. },
  122. {
  123. "cell_type": "code",
  124. "execution_count": null,
  125. "metadata": {},
  126. "outputs": [],
  127. "source": [
  128. "# run minos error analysis\n",
  129. "# The Minos algorithm uses the profile likelihood method to compute\n",
  130. "# (generally asymmetric) confidence intervals.\n",
  131. "m.minos()"
  132. ]
  133. },
  134. {
  135. "cell_type": "code",
  136. "execution_count": null,
  137. "metadata": {},
  138. "outputs": [],
  139. "source": [
  140. "# Get a 2D contour of the function around the minimum for 2 parameters\n",
  141. "# and draw a 2 D contours up to 4 sigma of a1 and a2 \n",
  142. "#m.draw_profile(\"a1\")\n",
  143. "m.draw_mncontour(\"a2\", \"a3\", cl=[1, 2, 3, 4])"
  144. ]
  145. },
  146. {
  147. "cell_type": "code",
  148. "execution_count": null,
  149. "metadata": {},
  150. "outputs": [],
  151. "source": [
  152. "print(m.values,m.errors)\n",
  153. "a0_fit = m.values[\"a0\"]\n",
  154. "a1_fit = m.values[\"a1\"]\n",
  155. "a2_fit = m.values[\"a2\"]\n",
  156. "a3_fit = m.values[\"a3\"]"
  157. ]
  158. },
  159. {
  160. "cell_type": "code",
  161. "execution_count": null,
  162. "metadata": {},
  163. "outputs": [],
  164. "source": [
  165. "# display fitted function \n",
  166. "x_plot = np.linspace( 0.1, 10.1 , 200 )\n",
  167. "y_fit = a0_fit + a1_fit * x_plot + a2_fit * x_plot**2 + a3_fit * x_plot**3"
  168. ]
  169. },
  170. {
  171. "cell_type": "code",
  172. "execution_count": null,
  173. "metadata": {},
  174. "outputs": [],
  175. "source": [
  176. "# plot data \n",
  177. "plt.figure()\n",
  178. "plt.errorbar(x, y, dy , dx, fmt=\"o\")\n",
  179. "plt.plot(x_plot,y_fit )\n",
  180. "plt.title(\"iminuit Fit Test\")\n",
  181. "plt.xlabel('x')\n",
  182. "plt.ylabel('f(x)')\n",
  183. "plt.xlim(-0.1, 10.1)\n",
  184. "\n",
  185. "# show the plot\n",
  186. "plt.show()"
  187. ]
  188. },
  189. {
  190. "cell_type": "code",
  191. "execution_count": null,
  192. "metadata": {},
  193. "outputs": [],
  194. "source": []
  195. }
  196. ],
  197. "metadata": {
  198. "kernelspec": {
  199. "display_name": "Python 3 (ipykernel)",
  200. "language": "python",
  201. "name": "python3"
  202. },
  203. "language_info": {
  204. "codemirror_mode": {
  205. "name": "ipython",
  206. "version": 3
  207. },
  208. "file_extension": ".py",
  209. "mimetype": "text/x-python",
  210. "name": "python",
  211. "nbconvert_exporter": "python",
  212. "pygments_lexer": "ipython3",
  213. "version": "3.8.16"
  214. }
  215. },
  216. "nbformat": 4,
  217. "nbformat_minor": 4
  218. }