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

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exercise 4: Least square fit to data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from matplotlib import pyplot as plt\n",
"plt.rcParams[\"font.size\"] = 20\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# data\n",
"x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype='d')\n",
"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",
"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",
"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')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# define fit function \n",
"def pol3(a0, a1, a2, a3):\n",
" return a0 + x*a1 + a2*x**2 + a3*x**3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# least-squares function = sum of data residuals squared\n",
"def LSQ(a0, a1, a2, a3):\n",
" return np.sum((y - pol3(a0, a1, a2, a3)) ** 2 / dy ** 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# import Minuit object\n",
"from iminuit import Minuit"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# create instance of Minuit and use LSQ function to minimize\n",
"LSQ.errordef = Minuit.LEAST_SQUARES\n",
"m = Minuit(LSQ,a0=-1.3, a1=2.6 ,a2=-0.24 ,a3=0.005)\n",
"# run migrad \n",
"m.migrad()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# get function value at the minimum, which is per definition a chi2\n",
"# obtain chi2 / degree of freedom (dof)\n",
"chi2 = m.fval / (len(y) - len(m.values))\n",
"print (\"Chi2/ndof =\" , chi2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# run covariance \n",
"m.hesse()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#get covariance matrix\n",
"m.covariance"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#get correlation matrix in numpy array\n",
"cov = m.covariance\n",
"print (cov)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# run minos error analysis\n",
"# The Minos algorithm uses the profile likelihood method to compute\n",
"# (generally asymmetric) confidence intervals.\n",
"m.minos()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get a 2D contour of the function around the minimum for 2 parameters\n",
"# and draw a 2 D contours up to 4 sigma of a1 and a2 \n",
"#m.draw_profile(\"a1\")\n",
"m.draw_mncontour(\"a2\", \"a3\", cl=[1, 2, 3, 4])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(m.values,m.errors)\n",
"a0_fit = m.values[\"a0\"]\n",
"a1_fit = m.values[\"a1\"]\n",
"a2_fit = m.values[\"a2\"]\n",
"a3_fit = m.values[\"a3\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# display fitted function \n",
"x_plot = np.linspace( 0.1, 10.1 , 200 )\n",
"y_fit = a0_fit + a1_fit * x_plot + a2_fit * x_plot**2 + a3_fit * x_plot**3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# plot data \n",
"plt.figure()\n",
"plt.errorbar(x, y, dy , dx, fmt=\"o\")\n",
"plt.plot(x_plot,y_fit )\n",
"plt.title(\"iminuit Fit Test\")\n",
"plt.xlabel('x')\n",
"plt.ylabel('f(x)')\n",
"plt.xlim(-0.1, 10.1)\n",
"\n",
"# show the plot\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
}
},
"nbformat": 4,
"nbformat_minor": 4
}