[regression] expandend exercise according to feedback from Alex
This commit is contained in:
parent
5a6cca59d3
commit
a63dd2f2fe
@ -23,12 +23,3 @@ function [tau, taus, mses] = expdecaydescent(t, x, tau0, epsilon, threshold)
|
|||||||
count = count + 1;
|
count = count + 1;
|
||||||
end
|
end
|
||||||
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
|
|
||||||
|
11
regression/exercises/expdecaygradient.m
Normal file
11
regression/exercises/expdecaygradient.m
Normal file
@ -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
|
15
regression/exercises/expdecaygradientplot.m
Normal file
15
regression/exercises/expdecaygradientplot.m
Normal file
@ -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);
|
||||||
|
|
||||||
|
|
||||||
|
|
10
regression/exercises/expdecaymse.m
Normal file
10
regression/exercises/expdecaymse.m
Normal file
@ -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
|
13
regression/exercises/expdecaymseplot.m
Normal file
13
regression/exercises/expdecaymseplot.m
Normal file
@ -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]')
|
@ -11,7 +11,7 @@ plot(taus, '-o');
|
|||||||
plot([1, length(taus)], [tau, tau], 'k'); % line indicating true tau
|
plot([1, length(taus)], [tau, tau], 'k'); % line indicating true tau
|
||||||
hold off;
|
hold off;
|
||||||
xlabel('Iteration');
|
xlabel('Iteration');
|
||||||
ylabel('tau');
|
ylabel('tau [ms]');
|
||||||
subplot(2, 2, 3); % bottom left panel
|
subplot(2, 2, 3); % bottom left panel
|
||||||
plot(mses, '-o');
|
plot(mses, '-o');
|
||||||
xlabel('Iteration steps');
|
xlabel('Iteration steps');
|
||||||
|
@ -56,6 +56,29 @@
|
|||||||
\lstinputlisting{expdecaydata.m}
|
\lstinputlisting{expdecaydata.m}
|
||||||
\end{solution}
|
\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
|
\part Implement the gradient descent algorithm for finding the
|
||||||
least squares for the exponential function \eqref{expfunc}. The
|
least squares for the exponential function \eqref{expfunc}. The
|
||||||
function takes as arguments the measured data, an initial value
|
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
|
for each iteration step, (ii) the mean squared error for each
|
||||||
iteration step, and (iii) the measured data and the fitted
|
iteration step, and (iii) the measured data and the fitted
|
||||||
exponential function.
|
exponential function.
|
||||||
\newsolutionpage
|
|
||||||
\begin{solution}
|
\begin{solution}
|
||||||
\lstinputlisting{expdecayplot.m}
|
\lstinputlisting{expdecayplot.m}
|
||||||
\includegraphics{expdecayplot}
|
\includegraphics{expdecayplot}
|
||||||
@ -140,9 +162,16 @@
|
|||||||
\part Plot the path of the gradient descent algorithm into the
|
\part Plot the path of the gradient descent algorithm into the
|
||||||
error surface.
|
error surface.
|
||||||
|
|
||||||
Why appears (potentially) the path of the gradient descent
|
Why does (potentially) the path of the gradient descent algorithm
|
||||||
algorithm not perpendicular to the contour lines of the error
|
appear to be not perpendicular to the contour lines of the error
|
||||||
surface?
|
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.
|
\part Plot the data together with the fitted exponential function.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user