From 99a8a9d91e6e375c7824afefe6ca9e76c9f9a0f0 Mon Sep 17 00:00:00 2001 From: Jan Benda <jan.benda@uni-tuebingen.de> Date: Thu, 17 Dec 2020 22:38:07 +0100 Subject: [PATCH] [regression] improved cost function figure --- regression/lecture/cubiccost.py | 12 +++++++----- regression/lecture/regression.tex | 16 ++++++++-------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/regression/lecture/cubiccost.py b/regression/lecture/cubiccost.py index 0b8d2dc..30d9575 100644 --- a/regression/lecture/cubiccost.py +++ b/regression/lecture/cubiccost.py @@ -49,16 +49,18 @@ def plot_mse_min(ax, x, y, c): mses = np.zeros(len(ccs)) for i, cc in enumerate(ccs): mses[i] = np.mean((y-(cc*x**3.0))**2.0) - di = 20 - i0 = 14 - imin = np.argmin(mses[i0::di])*di + i0 + imin = np.argmin(mses) + di = 25 + i0 = 16 + dimin = np.argmin(mses[i0::di])*di + i0 ax.plot(c, 500.0, **psB) ax.plot(ccs, mses, **lsAm) ax.plot(ccs[i0::di], mses[i0::di], **psAm) - ax.plot(ccs[imin], mses[imin], **psC) + ax.plot(ccs[dimin], mses[dimin], **psD) + #ax.plot(ccs[imin], mses[imin], **psCm) ax.annotate('Estimated\nminimum of\ncost\nfunction', - xy=(ccs[imin], mses[imin]*1.2), xycoords='data', + xy=(ccs[dimin], mses[dimin]*1.2), xycoords='data', xytext=(4, 6700), textcoords='data', ha='left', arrowprops=dict(arrowstyle="->", relpos=(0.8,0.0), connectionstyle="angle3,angleA=0,angleB=85") ) diff --git a/regression/lecture/regression.tex b/regression/lecture/regression.tex index 8867744..2990aef 100644 --- a/regression/lecture/regression.tex +++ b/regression/lecture/regression.tex @@ -154,6 +154,14 @@ function $f_{cost}(c)$ that maps the parameter value $c$ to a scalar error value. For a given data set we thus can simply plot the cost function as a function of $c$ (\figref{cubiccostfig}). +\begin{exercise}{errorSurface.m}{}\label{errorsurfaceexercise} + Then calculate the mean squared error between the data and straight + lines for a range of slopes and intercepts using the + \varcode{meanSquaredErrorCubic()} function from the previous + exercise. Illustrate the error surface using the \code{surface()} + function. +\end{exercise} + \begin{figure}[t] \includegraphics{cubiccost} \titlecaption{Minimum of the cost function.}{For a given data set @@ -167,14 +175,6 @@ function as a function of $c$ (\figref{cubiccostfig}). the computed range.}\label{cubiccostfig} \end{figure} -\begin{exercise}{errorSurface.m}{}\label{errorsurfaceexercise} - Then calculate the mean squared error between the data and straight - lines for a range of slopes and intercepts using the - \varcode{meanSquaredErrorCubic()} function from the previous - exercise. Illustrate the error surface using the \code{surface()} - function. -\end{exercise} - By looking at the plot of the cost function we can visually identify the position of the minimum and thus estimate the optimal value for the parameter $c$. How can we use the error surface to guide an