From a63dd2f2fe468b143619bb27b0fd7dd168565060 Mon Sep 17 00:00:00 2001 From: Jan Benda Date: Wed, 23 Dec 2020 18:48:59 +0100 Subject: [PATCH] [regression] expandend exercise according to feedback from Alex --- regression/exercises/expdecaydescent.m | 9 ------ regression/exercises/expdecaygradient.m | 11 +++++++ regression/exercises/expdecaygradientplot.m | 15 +++++++++ regression/exercises/expdecaymse.m | 10 ++++++ regression/exercises/expdecaymseplot.m | 13 ++++++++ regression/exercises/expdecayplot.m | 2 +- regression/exercises/gradientdescent-1.tex | 35 +++++++++++++++++++-- 7 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 regression/exercises/expdecaygradient.m create mode 100644 regression/exercises/expdecaygradientplot.m create mode 100644 regression/exercises/expdecaymse.m create mode 100644 regression/exercises/expdecaymseplot.m diff --git a/regression/exercises/expdecaydescent.m b/regression/exercises/expdecaydescent.m index 02434ed..b75b63b 100644 --- a/regression/exercises/expdecaydescent.m +++ b/regression/exercises/expdecaydescent.m @@ -23,12 +23,3 @@ function [tau, taus, mses] = expdecaydescent(t, x, tau0, epsilon, threshold) count = count + 1; end end - -function mse = expdecaymse(t, x, tau) - mse = mean((x - expdecay(t, tau)).^2); -end - -function gradient = expdecaygradient(t, x, tau) - h = 1e-7; % stepsize for derivative - gradient = (expdecaymse(t, x, tau+h) - expdecaymse(t, x, tau))/h; -end diff --git a/regression/exercises/expdecaygradient.m b/regression/exercises/expdecaygradient.m new file mode 100644 index 0000000..1b10400 --- /dev/null +++ b/regression/exercises/expdecaygradient.m @@ -0,0 +1,11 @@ +function gradient = expdecaygradient(t, x, tau) +% Gradient of MSE for a decaying exponential. +% +% Arguments: t, vector of time points. +% x, vector of the corresponding measured data values. +% tau, value for the time constant. +% +% Returns: gradient: the derivative of the MSE with respect to tau. + h = 1e-7; % stepsize for derivative + gradient = (expdecaymse(t, x, tau+h) - expdecaymse(t, x, tau))/h; +end diff --git a/regression/exercises/expdecaygradientplot.m b/regression/exercises/expdecaygradientplot.m new file mode 100644 index 0000000..62baf42 --- /dev/null +++ b/regression/exercises/expdecaygradientplot.m @@ -0,0 +1,15 @@ +... + +gradients = zeros(length(taus), 1); +for i = 1:length(taus) + gradients(i) = expdecaygradient(time, voltage, taus(i)); +end + +subplot(2, 1, 2) +plot(taus, gradients); +xlabel('tau [ms]') +ylabel('gradient [mV^2/ms]') +savefigpdf(gcf, 'expdecaygradientplot.pdf', 10, 10); + + + diff --git a/regression/exercises/expdecaymse.m b/regression/exercises/expdecaymse.m new file mode 100644 index 0000000..4e16dd6 --- /dev/null +++ b/regression/exercises/expdecaymse.m @@ -0,0 +1,10 @@ +function mse = expdecaymse(t, x, tau) +% Mean squared error for a decaying exponential. +% +% Arguments: t, vector of time points. +% x, vector of the corresponding measured data values. +% tau, value for the time constant. +% +% Returns: mse: mean squared error + mse = mean((x - expdecay(t, tau)).^2); +end diff --git a/regression/exercises/expdecaymseplot.m b/regression/exercises/expdecaymseplot.m new file mode 100644 index 0000000..1e1ebd9 --- /dev/null +++ b/regression/exercises/expdecaymseplot.m @@ -0,0 +1,13 @@ +expdecaydata; % generate data +close all; + +taus = 0.0:0.1:20.0; +mses = zeros(length(taus), 1); +for i = 1:length(taus) + mses(i) = expdecaymse(time, voltage, taus(i)); +end + +subplot(2, 1, 1) +plot(taus, mses); +xlabel('tau [ms]') +ylabel('mean squared error [mV^2]') diff --git a/regression/exercises/expdecayplot.m b/regression/exercises/expdecayplot.m index 68ea89b..baa6d16 100644 --- a/regression/exercises/expdecayplot.m +++ b/regression/exercises/expdecayplot.m @@ -11,7 +11,7 @@ plot(taus, '-o'); plot([1, length(taus)], [tau, tau], 'k'); % line indicating true tau hold off; xlabel('Iteration'); -ylabel('tau'); +ylabel('tau [ms]'); subplot(2, 2, 3); % bottom left panel plot(mses, '-o'); xlabel('Iteration steps'); diff --git a/regression/exercises/gradientdescent-1.tex b/regression/exercises/gradientdescent-1.tex index b4df7b3..b940202 100644 --- a/regression/exercises/gradientdescent-1.tex +++ b/regression/exercises/gradientdescent-1.tex @@ -56,6 +56,29 @@ \lstinputlisting{expdecaydata.m} \end{solution} + \part Write a function that returns the mean squared error. The + function takes as arguments the measured data (time points and + corresponding voltage measurements) and a value for the membrane + time constant at which the mean squred error should be + evaluated. Plot the mean squared error as a function of $\tau$. + \begin{solution} + \lstinputlisting{expdecaymse.m} + \lstinputlisting{expdecaymseplot.m} + \end{solution} + + \part Write a function that returns the gradient of the mean + squared error, i.e. its derivative with respect to the $\tau$ + parameter. The function takes as arguments the measured data + (time points and corresponding voltage measurements) and a value + for the membrane time constant at which the gradient should be + evaluated. Plot the gradient as a function of $\tau$. + \begin{solution} + \lstinputlisting{expdecaygradient.m} + \newsolutionpage + \lstinputlisting{expdecaygradientplot.m} + \includegraphics{expdecaygradientplot} + \end{solution} + \part Implement the gradient descent algorithm for finding the least squares for the exponential function \eqref{expfunc}. The function takes as arguments the measured data, an initial value @@ -78,7 +101,6 @@ for each iteration step, (ii) the mean squared error for each iteration step, and (iii) the measured data and the fitted exponential function. - \newsolutionpage \begin{solution} \lstinputlisting{expdecayplot.m} \includegraphics{expdecayplot} @@ -140,9 +162,16 @@ \part Plot the path of the gradient descent algorithm into the error surface. - Why appears (potentially) the path of the gradient descent - algorithm not perpendicular to the contour lines of the error + Why does (potentially) the path of the gradient descent algorithm + appear to be not perpendicular to the contour lines of the error surface? + \begin{solution} + Because on the plot the $c$ and $\tau$ axes are (in general) not + equally scaled. That is a unit step in $c$ direction covers a + physical distance on paper or the screen than a unit stpe in + $\tau$ direction. Use \code{axis equal} to force the plot to + have equally scaled $c$ and $\tau$ axes. + \end{solution} \part Plot the data together with the fitted exponential function.