\documentclass[12pt,a4paper,pdftex]{exam}

\newcommand{\exercisetopic}{Gradient descent}
\newcommand{\exercisenum}{9}
\newcommand{\exercisedate}{December 22th, 2020}

\input{../../exercisesheader}

\firstpagefooter{Prof. Dr. Jan Benda}{}{jan.benda@uni-tuebingen.de}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\input{../../exercisestitle}

\begin{questions}

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  \question \qt{Read sections 8.1 -- 8.5 of chapter 8 on ``optimization
    and gradient descent!}\vspace{-3ex}

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  \question \qt{Fitting the time constant of an exponential function}
  Let's assume we record the membrane potential from a photoreceptor
  neuron.  We define the resting potential of the neuron to be at
  0\,mV. By means of a brief current injection we increase the
  membrane potential by exactly 1\,mV. We then record how the membrane
  potential decays exponentially down to the resting potential. We are
  interested in the membrane time constant and therefore want to fit
  an exponential function to the recorded time course of the membrane
  potential.

  \begin{parts}
    \part Implement (and document!) the exponential function 
    \begin{equation}
      \label{expfunc}
      x(t) = e^{-t/\tau} \quad , \qquad \tau \in \reZ
    \end{equation}
    with the membrane time constant $\tau$ as a matlab function
    \code{expdecay(t, tau)} that takes as arguments a vector of time
    points and the value of the membrane time constant. The function
    returns \eqnref{expfunc} computed for each time point as a vector.
    \begin{solution}
      \lstinputlisting{expdecay.m}
    \end{solution}

    \part Let's first generate the data. Set the membrane time
    constant to 10\,ms. Generate a time vector with sample times
    between zero and six times the membrane time constant and a
    sampling interval of 0.05\,ms. Then compute a vector containing
    the corresponding measurements of the membrane potential using the
    \code{expdecay()} function and adding some measurement noise with
    a standard deviation of 0.05\.mV (\code{randn()} function).
    Plot the data.
    \begin{solution}
      \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
    for the estimation of the membrane time constant, the $\epsilon$
    factor, and the threshold for the length of the gradient where to
    terminate the algorithm. The function should return the estimated
    membrane time constant at the minimum of the mean squared error,
    and a vector with the time constants as well as a vector with the
    mean squared errors for each step of the algorithm.
    \begin{solution}
      \lstinputlisting{expdecaydescent.m}
    \end{solution}

    \part Call the gradient descent function with the generated data.
    Watch the value of the gradient and of tau and adapt $\epsilon$
    and the threshold accordingly (they differ quite dramatically from
    the ones in the script for the cubic fit).

    \part Generate three plots: (i) the values of the time constant
    for each iteration step, (ii) the mean squared error for each
    iteration step, and (iii) the measured data and the fitted
    exponential function.
    \begin{solution}
      \lstinputlisting{expdecayplot.m}
      \includegraphics{expdecayplot}
    \end{solution}

  \end{parts}

  \continuepage
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  \question \qt{Read sections 8.6 -- 8.8 of chapter 8 on ``optimization
    and gradient descent!}\vspace{-3ex}

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  \question \qt{Fitting an exponential function with two parameters}
  Usually we do not know the exact value of the voltage step. So let's
  make this a paramter, too:
  \begin{equation}
    \label{exp2func}
    x(t) = c e^{-t/\tau} \quad , \qquad c, \tau \in \reZ
  \end{equation}
  
  \begin{parts}
    \part Implement the exponential function \eqref{exp2func} as a
    matlab function \code{exp2func(t, p)} that takes as arguments a
    vector of time points and a vector with the two parameter values
    $c$ and $\tau$. The function returns \eqnref{exp2func} computed for
    each time point as a vector.
    \begin{solution}
      \lstinputlisting{exp2func.m}
    \end{solution}

    \part Plot the error surface, the mean squared error, for a range
    of values for the two parameters $c$ and $\tau$. Try the
    \code{surface()}, the \code{contour()} as well as the
    \code{contourf()} functions to visualize the error surface for the
    data from the previous exercise. You may also want to try to plot
    the logarithm of the mean squared error.
    \begin{solution}
      \lstinputlisting{exp2errorsurface.m}
    \end{solution}

    \newsolutionpage
    \part Implement the gradient descent algorithm for finding the
    least squares of an arbitrary function. The function takes as
    arguments the measured data, a vector holding the initial values
    of the function parameters, the $\epsilon$ factor, and the
    threshold for the length of the gradient where to terminate the
    algorithm. The function should return the estimated parameter
    values in a vector, and a 2D vector with the parameter values as
    well as a vector with the mean squared errors for each step of the
    algorithm.
    \begin{solution}
      \lstinputlisting{gradientdescent.m}
    \end{solution}

    \part Call the gradient descent function with the generated data.
    Set $\epsilon=1.0$ and the threshold value initially to 0.001.

    \part Plot the path of the gradient descent algorithm into the
    error surface.

    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.

    \part Then adapt $\epsilon$ and the threshold value to improve the
    convergence of the algortihm to the minimum of the cost function.
    \begin{solution}
      \lstinputlisting{exp2plot.m}
      \includegraphics{exp2plot}
    \end{solution}

    \part Use the function \code{lsqcurvefit()} provided by matlab to
    find $c$ and $\tau$ for the exponential function describing the
    data.  Compare the resulting fit parameters of this function with
    the ones of your gradient descent algorithm.  Plot the data
    together with the fitted exponential function.
    \begin{solution}
      \lstinputlisting{exp2fit.m}
      \includegraphics{exp2fit}\\
      Resulting values for $c$ and $\tau$ are similar but not identical.
    \end{solution}

  \end{parts}

\end{questions}

\end{document}
