[regression] improved cost function figure

This commit is contained in:
Jan Benda 2020-12-17 22:38:07 +01:00
parent 3541f30798
commit 99a8a9d91e
2 changed files with 15 additions and 13 deletions

View File

@ -49,16 +49,18 @@ def plot_mse_min(ax, x, y, c):
mses = np.zeros(len(ccs)) mses = np.zeros(len(ccs))
for i, cc in enumerate(ccs): for i, cc in enumerate(ccs):
mses[i] = np.mean((y-(cc*x**3.0))**2.0) mses[i] = np.mean((y-(cc*x**3.0))**2.0)
di = 20 imin = np.argmin(mses)
i0 = 14 di = 25
imin = np.argmin(mses[i0::di])*di + i0 i0 = 16
dimin = np.argmin(mses[i0::di])*di + i0
ax.plot(c, 500.0, **psB) ax.plot(c, 500.0, **psB)
ax.plot(ccs, mses, **lsAm) ax.plot(ccs, mses, **lsAm)
ax.plot(ccs[i0::di], mses[i0::di], **psAm) 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', 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', xytext=(4, 6700), textcoords='data', ha='left',
arrowprops=dict(arrowstyle="->", relpos=(0.8,0.0), arrowprops=dict(arrowstyle="->", relpos=(0.8,0.0),
connectionstyle="angle3,angleA=0,angleB=85") ) connectionstyle="angle3,angleA=0,angleB=85") )

View File

@ -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 error value. For a given data set we thus can simply plot the cost
function as a function of $c$ (\figref{cubiccostfig}). 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] \begin{figure}[t]
\includegraphics{cubiccost} \includegraphics{cubiccost}
\titlecaption{Minimum of the cost function.}{For a given data set \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} the computed range.}\label{cubiccostfig}
\end{figure} \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 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 position of the minimum and thus estimate the optimal value for
the parameter $c$. How can we use the error surface to guide an the parameter $c$. How can we use the error surface to guide an